Skip to content

Commit

Permalink
Improve the demo and document
Browse files Browse the repository at this point in the history
  • Loading branch information
SungjunApp committed Nov 12, 2020
1 parent b8dd8ee commit 1dc37db
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import com.pixlee.pixleesdk.ui.widgets.ImageScaleType
import com.pixlee.pixleesdk.ui.widgets.PXLPhotoView
import com.pixlee.pixleesdk.ui.widgets.TextViewStyle
import com.pixlee.pixleesdk.util.px
import kotlinx.android.synthetic.main.fragment_ktx_gallery_grid.*
import kotlinx.android.synthetic.main.fragment_ktx_gallery_list.*
import kotlinx.android.synthetic.main.fragment_ktx_gallery_list.drawerLayout
import kotlinx.android.synthetic.main.fragment_ktx_gallery_list.fabFilter
Expand Down Expand Up @@ -105,7 +104,7 @@ class KtxGalleryListFragment : BaseFragment(), LifecycleObserver {
is BaseViewModel.Command.Data -> {
if (it.isFirstPage) {
pxlPhotoRecyclerView.replaceList(it.list)
pxlPhotoRecyclerView.playVideo()
pxlPhotoRecyclerView.playVideoOnResume()
if (it.list.isNotEmpty()) {
it.list.firstOrNull()?.pxlPhoto?.also {
viewModel.getPhotoFromRegion(it, readRegionIdFromUI()) // add your own region id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.pixlee.pixleeandroidsdk.ui.widgets
import android.graphics.Color
import android.graphics.Rect
import android.os.Bundle
import android.util.Log
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -136,98 +137,64 @@ class PXLPhotoViewFragment : BaseFragment(), LifecycleObserver {
}

var currentView: Int = 0
fun playRelevantVideo(scrollBounds: Rect) {
if (pxlPhotoViewFitWrapLandscape.getLocalVisibleRect(scrollBounds)) {
currentView = 1
pxlPhotoViewFitWrapLandscape.playVideo()
return
private fun playRelevantVideo(scrollBounds: Rect) {
fun stopExistingAndPlayNew(pxlPhotoView: PXLPhotoView, position:Int){
if(currentView!=position){
stopVideo()
currentView = position
pxlPhotoView.playVideo()
}
}

if (pxlPhotoViewFitWrapLandscape.getLocalVisibleRect(scrollBounds)) {
stopExistingAndPlayNew(pxlPhotoViewFitWrapLandscape, 1)

if (pxlPhotoViewCrop.getLocalVisibleRect(scrollBounds)) {
currentView = 2
pxlPhotoViewCrop.playVideo()
return
}
} else if (pxlPhotoViewCrop.getLocalVisibleRect(scrollBounds)) {
stopExistingAndPlayNew(pxlPhotoViewCrop, 2)

}else if (pxlPhotoViewFitPortrait.getLocalVisibleRect(scrollBounds)) {
stopExistingAndPlayNew(pxlPhotoViewFitPortrait, 3)

if (pxlPhotoViewFitPortrait.getLocalVisibleRect(scrollBounds)) {
currentView = 3
pxlPhotoViewFitPortrait.playVideo()
return
}
}

@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun playVideoOnStart() {
playVideo()
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun playVideo() {
if (currentView > 0) {
val scrollBounds = Rect()
scrollView.getHitRect(scrollBounds)
playRelevantVideo(scrollBounds)
}
fun playVideoOnResume() {
playVideo()
}

@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun stopVideo() {
PXLPhotoView.releaseAllVideos()
fun stopVideoOnPause() {
stopVideo()
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun stopVideoOnStop() {
stopVideo()
}

fun playVideo(){
val scrollBounds = Rect()
scrollView.getHitRect(scrollBounds)
playRelevantVideo(scrollBounds)
}

fun stopVideo() {
pxlPhotoViewFitWrapLandscape.pauseVideo()
pxlPhotoViewCrop.pauseVideo()
pxlPhotoViewFitPortrait.pauseVideo()
}

override fun onStop() {
super.onStop()

}

// var currentView: Int = 0
// fun playRelevantVideo(scrollBounds: Rect) {
// if (pxlPhotoViewFitWrapLandscape.getLocalVisibleRect(scrollBounds)) {
// currentView = 1
// stopVideo(2)
// stopVideo(3)
// pxlPhotoViewFitWrapLandscape.playVideo()
// return
// }
//
// if (pxlPhotoViewCrop.getLocalVisibleRect(scrollBounds)) {
// currentView = 2
// stopVideo(1)
// stopVideo(3)
// pxlPhotoViewCrop.playVideo()
// return
// }
//
// if (pxlPhotoViewFitPortrait.getLocalVisibleRect(scrollBounds)) {
// currentView = 3
// stopVideo(1)
// stopVideo(2)
// pxlPhotoViewFitPortrait.playVideo()
// return
// }
// }
//
// @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
// fun playVideo() {
// if (currentView > 0) {
// val scrollBounds = Rect()
// scrollView.getHitRect(scrollBounds)
// playRelevantVideo(scrollBounds)
// }
// }
//
// @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
// fun stopVideoOnResume() {
// when(currentView){
// 1 -> pxlPhotoViewFitWrapLandscape.pauseVideo()
// 2 -> pxlPhotoViewCrop.pauseVideo()
// 3 -> pxlPhotoViewFitPortrait.pauseVideo()
// }
// currentView = 0
// }
//
// fun stopVideo(index:Int){
// when(index){
// 1 -> pxlPhotoViewFitWrapLandscape.pauseVideo()
// 2 -> pxlPhotoViewCrop.pauseVideo()
// 3 -> pxlPhotoViewFitPortrait.pauseVideo()
// }
// }

companion object {
fun getInstance(pxlPhoto: PhotoWithImageScaleType): Fragment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ class PXLPhotoViewInRecyclerViewFragment : BaseFragment() {

override fun onResume() {
super.onResume()
pxlPhotoRecyclerView.playVideo()
pxlPhotoRecyclerView.playVideoOnResume()
}

override fun onStop() {
super.onStop()
pxlPhotoRecyclerView.stopVideo()
pxlPhotoRecyclerView.stopVideoOnPause()
}

companion object {
Expand Down
56 changes: 38 additions & 18 deletions doc/kotlin/UI.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,21 +241,25 @@ pxlPhotoProductView.loadContent(...
```kotlin
#!kotlin
class YourActivity : AppCompatActivity() {
// play video

override fun onStart() {
super.onStart()
pxlPhotoProductView.playVideoOnStart()
}

override fun onResume() {
super.onResume()
pxlPhotoProductView.playVideo()
pxlPhotoProductView.playVideoOnResume()
}

// stop video
override fun onPause() {
super.onPause()
pxlPhotoProductView.stopVideo()
pxlPhotoProductView.stopVideoOnPause()
}

override fun onDestroy() {
super.onDestroy()
pxlPhotoProductView.releaseVideo()
override fun onStop() {
super.onStart()
pxlPhotoProductView.stopVideoOnStop()
}
}
```
Expand Down Expand Up @@ -505,17 +509,26 @@ override fun onCreate(savedInstanceState: Bundle?) {
```kotlin
#!kotlin
class YourActivity : AppCompatActivity() {
// play video
override fun onStart() {
super.onStart()
pxlPhotoRecyclerView.playVideoOnStart()
}

override fun onResume() {
super.onResume()
pxlPhotoRecyclerView.playVideo()
pxlPhotoRecyclerView.playVideoOnResume()
}

// stop video
override fun onPause() {
super.onPause()
pxlPhotoRecyclerView.stopVideo()
pxlPhotoRecyclerView.stopVideoOnPause()
}

override fun onStop() {
super.onStart()
pxlPhotoRecyclerView.stopVideoOnStop()
}

}
```

Expand Down Expand Up @@ -831,18 +844,25 @@ class YourActivity: AppCompatActivity, LifecycleObserver {
val item: PhotoWithImageScaleType? = arguments?.getParcelable("photoWithImageScaleType") // read PhotoWithImageScaleType
pxlPhotoView.setContent(item, PXLPhotoView.ImageScaleType.CENTER_CROP)
}

@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun playVideoOnStart() {
pxlPhotoView.playVideo()
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun playVideo() {
// play video, you can also use this code onCreate or when getting data from the API
pxlPhotoView.playVideo() // play the video
fun playVideoOnResume() {
pxlPhotoView.playVideo()
}


@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun stopVideo() {
// stop any video
PXLPhotoView.releaseAllVideos()
fun stopVideoOnPause() {
pxlPhotoView.pauseVideo()
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun stopVideoOnStop() {
pxlPhotoView.pauseVideo()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class PXLPhotoProductView : FrameLayout, LifecycleObserver {
var onCheckedListener: ((isChecked: Boolean) -> Unit)? = null
)

private var adapter: ProductAdapter? = null
private var photoInfo: PhotoWithVideoInfo? = null
var bookmarkMap: HashMap<String, Boolean>? = null
var onBookmarkClicked: ((productId: String, isBookmarkChecked: Boolean) -> Unit)? = null
Expand Down Expand Up @@ -201,34 +202,22 @@ class PXLPhotoProductView : FrameLayout, LifecycleObserver {
}

@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStart() {
playVideo()
fun playVideoOnStart() {
playVideoOnResume()
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun playVideo() {
fun playVideoOnResume() {
pxlPhotoView.playVideo()
}

@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun stopVideo() {
fun stopVideoOnPause() {
pxlPhotoView.pauseVideo()
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onStop() {
stopVideo()
}

private var adapter: ProductAdapter? = null

override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
// Stop media player
}

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun releaseVideo() {
PXLPhotoView.releaseAllVideos()
fun stopVideoOnStop() {
stopVideoOnPause()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,6 @@ import java.util.*
* This view is to show a photo of PXLPhoto inside a RecyclerView or a ViewGroup
*/
class PXLPhotoView : RelativeLayout {
companion object {
/**
* this is to release video player
*/
fun releaseAllVideos() {
//Jzvd.releaseAllVideos()
}
}

@Parcelize
data class Configuration(
var pxlPhotoSize: PXLPhotoSize = PXLPhotoSize.ORIGINAL, // PXLPhotoSize [THUMBNAIL, MEDIUM, BIG, ORIGINAL]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout
//import com.master.exoplayer.MasterExoPlayerHelper
import com.pixlee.pixleesdk.R
import com.pixlee.pixleesdk.ui.adapter.PXLPhotoAdapter
import com.pixlee.pixleesdk.ui.viewholder.PhotoWithImageScaleType
import com.pixlee.pixleesdk.ui.widgets.PXLPhotoView
import com.pixlee.pixleesdk.util.AutoPlayUtils
import com.pixlee.pixleesdk.video.TransparentStyledPlayerView
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext

Expand Down Expand Up @@ -140,11 +138,11 @@ class PXLPhotoRecyclerView : BaseRecyclerView, LifecycleObserver, CoroutineScope

@OnLifecycleEvent(Lifecycle.Event.ON_START)
private fun playVideoOnStart() {
playVideo()
playVideoOnResume()
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun playVideo() {
fun playVideoOnResume() {
// The reason why we need post {} is to give list time to get the item loaded completely.
// linearLayoutManager.findFirstVisibleItemPosition() and linearLayoutManager.findLastVisibleItemPosition() return -1 without post {}.
post {
Expand All @@ -155,7 +153,7 @@ class PXLPhotoRecyclerView : BaseRecyclerView, LifecycleObserver, CoroutineScope
}

@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun stopVideo() {
fun stopVideoOnPause() {
changingSoundJob?.cancel()
playingVideo = false

Expand All @@ -164,7 +162,7 @@ class PXLPhotoRecyclerView : BaseRecyclerView, LifecycleObserver, CoroutineScope

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
private fun stopVideoOnStop() {
stopVideo()
stopVideoOnPause()
}

/**
Expand Down

0 comments on commit 1dc37db

Please sign in to comment.