diff options
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoViewer.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoViewer.kt | 78 |
1 files changed, 54 insertions, 24 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoViewer.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoViewer.kt index 0f7d49e8..7ba49d3c 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoViewer.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoViewer.kt @@ -2,13 +2,14 @@ package com.pitchedapps.frost.views import android.content.Context import android.graphics.Color +import android.graphics.PointF import android.net.Uri -import android.support.constraint.ConstraintLayout import android.support.v7.widget.Toolbar import android.util.AttributeSet import android.view.MotionEvent import android.view.View import android.view.ViewGroup +import android.widget.FrameLayout import android.widget.ImageView import ca.allanwang.kau.utils.* import com.mikepenz.google_material_typeface_library.GoogleMaterial @@ -23,24 +24,7 @@ import com.pitchedapps.frost.utils.frostDownload */ class FrostVideoViewer @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 -) : ConstraintLayout(context, attrs, defStyleAttr), FrostVideoViewerContract { - - override fun onFade(alpha: Float, duration: Long) { - toolbar.animate().alpha(alpha).setDuration(duration) - } - - override fun onSingleTapConfirmed(event: MotionEvent): Boolean { - if (restarter.isVisible) { - restarter.performClick() - return true - } - return false - } - - override fun onVideoComplete() { - video.jumpToStart() - restarter.fadeIn() - } +) : FrameLayout(context, attrs, defStyleAttr), FrostVideoViewerContract { val container: ViewGroup by bindView(R.id.video_container) val toolbar: Toolbar by bindView(R.id.video_toolbar) @@ -48,20 +32,21 @@ class FrostVideoViewer @JvmOverloads constructor( val video: FrostVideoView by bindView(R.id.video) val restarter: ImageView by bindView(R.id.video_restart) - companion object { /** * Simplified binding to add video to layout, and remove it when finished * This is under the assumption that the container allows for overlays, * such as a FrameLayout */ - inline fun showVideo(container: ViewGroup, url: String, crossinline onFinish: () -> Unit): FrostVideoViewer { + fun showVideo(url: String, contract: FrostVideoContainerContract): FrostVideoViewer { + val container = contract.videoContainer val videoViewer = FrostVideoViewer(container.context) container.addView(videoViewer) videoViewer.bringToFront() L.d("Create video view", url) videoViewer.setVideo(url) - videoViewer.video.onFinishedListener = { container.removeView(videoViewer); onFinish() } + videoViewer.video.containerContract = contract + videoViewer.video.onFinishedListener = { container.removeView(videoViewer); contract.onVideoFinished() } return videoViewer } } @@ -91,6 +76,7 @@ class FrostVideoViewer @JvmOverloads constructor( video.restart() restarter.fadeOut { restarter.gone() } } +// toolbar.setOnTouchListener { _, event -> video.shouldParentAcceptTouch(event) } } fun setVideo(url: String) { @@ -103,14 +89,40 @@ class FrostVideoViewer @JvmOverloads constructor( * returns true if consumed, false otherwise */ fun onBackPressed(): Boolean { - if (video.isExpanded) { + parent ?: return false + if (video.isExpanded) video.isExpanded = false + else + video.destroy() + return true + } + + fun pause() = video.pause() + + /* + * ------------------------------------------------------------- + * FrostVideoViewerContract + * ------------------------------------------------------------- + */ + + override fun onFade(alpha: Float, duration: Long) { + toolbar.visible().animate().alpha(alpha).setDuration(duration).withEndAction { + if (alpha == 0f) toolbar.gone() + } + } + + override fun onSingleTapConfirmed(event: MotionEvent): Boolean { + if (restarter.isVisible) { + restarter.performClick() return true } return false } - fun pause() = video.pause() + override fun onVideoComplete() { + video.jumpToStart() + restarter.fadeIn() + } } @@ -118,4 +130,22 @@ interface FrostVideoViewerContract { fun onSingleTapConfirmed(event: MotionEvent): Boolean fun onFade(alpha: Float, duration: Long) fun onVideoComplete() +} + +interface FrostVideoContainerContract { + /** + * Returns extra padding to be added + * from the right and from the bottom respectively + */ + val lowerVideoPadding: PointF + + /** + * Get the container which will hold the video viewer + */ + val videoContainer: FrameLayout + + /** + * Called once the video has stopped & should be removed + */ + fun onVideoFinished() }
\ No newline at end of file |