Skip to content

Commit

Permalink
update: save episode progress before activity back
Browse files Browse the repository at this point in the history
  • Loading branch information
muedsa committed Nov 15, 2023
1 parent 4670968 commit 54e751f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/src/main/kotlin/com/muedsa/agetv/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.muedsa.agetv.model.LazyType
import com.muedsa.agetv.ui.navigation.AppNavigation
import com.muedsa.agetv.viewmodel.HomePageViewModel
import com.muedsa.compose.tv.theme.TvTheme
import com.muedsa.compose.tv.widget.AppCloseHandler
import com.muedsa.compose.tv.widget.AppBackHandler
import com.muedsa.compose.tv.widget.ErrorMessageBox
import com.muedsa.compose.tv.widget.ErrorMessageBoxState
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -41,7 +41,7 @@ class MainActivity : ComponentActivity() {
color = MaterialTheme.colorScheme.background
) {
val errorMsgBoxState = remember { ErrorMessageBoxState() }
AppCloseHandler {
AppBackHandler {
errorMsgBoxState.error("再次点击返回键退出")
}
ErrorMessageBox(state = errorMsgBoxState) {
Expand Down
21 changes: 13 additions & 8 deletions app/src/main/kotlin/com/muedsa/agetv/PlaybackActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Surface
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.tv.material3.ExperimentalTvMaterial3Api
import androidx.tv.material3.MaterialTheme
import androidx.compose.ui.graphics.Color
import com.muedsa.agetv.ui.features.playback.PlaybackScreen
import com.muedsa.compose.tv.theme.TvTheme
import com.muedsa.compose.tv.widget.AppCloseHandler
import com.muedsa.compose.tv.widget.AppBackHandler
import com.muedsa.compose.tv.widget.ErrorMessageBox
import com.muedsa.compose.tv.widget.ErrorMessageBoxState
import com.muedsa.compose.tv.widget.FillTextScreen
Expand All @@ -20,7 +20,6 @@ import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint
class PlaybackActivity : ComponentActivity() {

@OptIn(ExperimentalTvMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -30,16 +29,21 @@ class PlaybackActivity : ComponentActivity() {
val episodeId = intent.getLongExtra(DAN_EPISODE_ID_KEY, 0)
setContent {
TvTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
color = Color.Black
) {
if (aid <= 0 || episodeTitle.isNullOrEmpty() || mediaUrl.isNullOrEmpty()) {
FillTextScreen(context = "视频地址错误")
} else {
val errorMsgBoxState = remember { ErrorMessageBoxState() }
AppCloseHandler {
val backListeners = remember {
mutableStateListOf<() -> Unit>()
}
AppBackHandler {
backListeners.forEach {
it()
}
errorMsgBoxState.error("再次点击返回键退出")
}
ErrorMessageBox(state = errorMsgBoxState) {
Expand All @@ -48,7 +52,8 @@ class PlaybackActivity : ComponentActivity() {
episodeTitle = episodeTitle,
mediaUrl = mediaUrl,
danEpisodeId = episodeId,
errorMsgBoxState = errorMsgBoxState
errorMsgBoxState = errorMsgBoxState,
backListeners = backListeners
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import android.app.Activity
import androidx.annotation.OptIn
import androidx.compose.material3.SnackbarDuration
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.platform.LocalContext
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.media3.common.MediaItem
Expand All @@ -36,7 +38,8 @@ fun PlaybackScreen(
mediaUrl: String,
danEpisodeId: Long = 0,
playbackViewModel: PlaybackViewModel = hiltViewModel(),
errorMsgBoxState: ErrorMessageBoxState
errorMsgBoxState: ErrorMessageBoxState,
backListeners: SnapshotStateList<() -> Unit>
) {
val activity = LocalContext.current as? Activity

Expand Down Expand Up @@ -93,6 +96,28 @@ fun PlaybackScreen(
}
}

DisposableEffect(key1 = exoplayerHolder) {
val listener = {
if (exoplayerHolder != null) {
val exoPlayer = exoplayerHolder!!
if (episodeProgress.aid == aid) {
val currentPosition = exoPlayer.currentPosition
if (currentPosition > 10_000) {
// 观看超过10s才保存进度
episodeProgress.progress = currentPosition
episodeProgress.duration = exoPlayer.duration
episodeProgress.updateAt = System.currentTimeMillis()
playbackViewModel.saveEpisodeProgress(episodeProgress)
}
}
}
}
backListeners.add(listener)
onDispose {
backListeners.remove(listener)
}
}

if (danmakuListLD.type == LazyType.SUCCESS && danmakuSettingLD.type == LazyType.SUCCESS && episodeProgress.aid == aid) {
val danmakuSetting = danmakuSettingLD.data!!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import androidx.compose.runtime.setValue
import kotlinx.coroutines.delay

@Composable
fun AppCloseHandler(
fun AppBackHandler(
onAllowBack: () -> Unit = {}
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
Expand Down Expand Up @@ -85,7 +86,7 @@ class ErrorMessageBoxState(
) {
var visible by mutableStateOf(initVisible)
var message by mutableStateOf(initMessage)
var duration by mutableStateOf(initDuration)
var duration by mutableIntStateOf(initDuration)

// private val mutex = Mutex()
//
Expand Down

0 comments on commit 54e751f

Please sign in to comment.