diff --git a/app/src/main/kotlin/com/muedsa/agetv/MainActivity.kt b/app/src/main/kotlin/com/muedsa/agetv/MainActivity.kt index ac015c8..c49bab9 100644 --- a/app/src/main/kotlin/com/muedsa/agetv/MainActivity.kt +++ b/app/src/main/kotlin/com/muedsa/agetv/MainActivity.kt @@ -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 @@ -41,7 +41,7 @@ class MainActivity : ComponentActivity() { color = MaterialTheme.colorScheme.background ) { val errorMsgBoxState = remember { ErrorMessageBoxState() } - AppCloseHandler { + AppBackHandler { errorMsgBoxState.error("再次点击返回键退出") } ErrorMessageBox(state = errorMsgBoxState) { diff --git a/app/src/main/kotlin/com/muedsa/agetv/PlaybackActivity.kt b/app/src/main/kotlin/com/muedsa/agetv/PlaybackActivity.kt index 2005512..47bd036 100644 --- a/app/src/main/kotlin/com/muedsa/agetv/PlaybackActivity.kt +++ b/app/src/main/kotlin/com/muedsa/agetv/PlaybackActivity.kt @@ -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 @@ -20,7 +20,6 @@ import dagger.hilt.android.AndroidEntryPoint @AndroidEntryPoint class PlaybackActivity : ComponentActivity() { - @OptIn(ExperimentalTvMaterial3Api::class) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -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) { @@ -48,7 +52,8 @@ class PlaybackActivity : ComponentActivity() { episodeTitle = episodeTitle, mediaUrl = mediaUrl, danEpisodeId = episodeId, - errorMsgBoxState = errorMsgBoxState + errorMsgBoxState = errorMsgBoxState, + backListeners = backListeners ) } } diff --git a/app/src/main/kotlin/com/muedsa/agetv/ui/features/playback/PlaybackScreen.kt b/app/src/main/kotlin/com/muedsa/agetv/ui/features/playback/PlaybackScreen.kt index 2cae076..e40f258 100644 --- a/app/src/main/kotlin/com/muedsa/agetv/ui/features/playback/PlaybackScreen.kt +++ b/app/src/main/kotlin/com/muedsa/agetv/ui/features/playback/PlaybackScreen.kt @@ -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 @@ -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 @@ -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!! diff --git a/app/src/main/kotlin/com/muedsa/compose/tv/widget/AppCloseHandler.kt b/app/src/main/kotlin/com/muedsa/compose/tv/widget/AppBackHandler.kt similarity index 97% rename from app/src/main/kotlin/com/muedsa/compose/tv/widget/AppCloseHandler.kt rename to app/src/main/kotlin/com/muedsa/compose/tv/widget/AppBackHandler.kt index 42204a1..c3ed94d 100644 --- a/app/src/main/kotlin/com/muedsa/compose/tv/widget/AppCloseHandler.kt +++ b/app/src/main/kotlin/com/muedsa/compose/tv/widget/AppBackHandler.kt @@ -11,7 +11,7 @@ import androidx.compose.runtime.setValue import kotlinx.coroutines.delay @Composable -fun AppCloseHandler( +fun AppBackHandler( onAllowBack: () -> Unit = {} ) { diff --git a/app/src/main/kotlin/com/muedsa/compose/tv/widget/ErrorMessageBox.kt b/app/src/main/kotlin/com/muedsa/compose/tv/widget/ErrorMessageBox.kt index 8d3ba13..70816f6 100644 --- a/app/src/main/kotlin/com/muedsa/compose/tv/widget/ErrorMessageBox.kt +++ b/app/src/main/kotlin/com/muedsa/compose/tv/widget/ErrorMessageBox.kt @@ -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 @@ -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() //