Skip to content

Commit

Permalink
feat: back arrow for more screens.
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyroid committed Jan 15, 2024
1 parent 9079c2a commit 8804570
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 52 deletions.
12 changes: 10 additions & 2 deletions androidApp/src/main/java/com/m3u/androidApp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class MainActivity : AppCompatActivity() {
helper(
title = viewModel.title::value,
actions = viewModel.actions::value,
fob = viewModel.fob::value
fob = viewModel.fob::value,
deep = viewModel.deep::value
)
}

Expand Down Expand Up @@ -116,7 +117,8 @@ class MainActivity : AppCompatActivity() {
private fun helper(
title: Method<String>,
actions: Method<ImmutableList<Action>>,
fob: Method<Fob?>
fob: Method<Fob?>,
deep: Method<Int>
): Helper = object : Helper {
init {
addOnPictureInPictureModeChangedListener { info ->
Expand Down Expand Up @@ -149,6 +151,12 @@ class MainActivity : AppCompatActivity() {
applyConfiguration()
}

override var deep: Int
get() = deep.get()
set(value) {
deep.set(value.coerceAtLeast(0))
}

override val message: StateFlow<Message> = viewModel.message

override var darkMode: UBoolean = UBoolean.Unspecified
Expand Down
9 changes: 3 additions & 6 deletions androidApp/src/main/java/com/m3u/androidApp/ui/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,22 @@ fun App(
) {
val helper = LocalHelper.current

val title: String by viewModel.title.collectAsStateWithLifecycle()
val actions by viewModel.actions.collectAsStateWithLifecycle()
val message by helper.message.collectAsStateWithLifecycle()
val fob by viewModel.fob.collectAsStateWithLifecycle()
val deep by viewModel.deep.collectAsStateWithLifecycle()

val navDestination = state.navDestination
val rootDestination = state.rootDestination

val isBackPressedVisible = AppDefaults.isBackPressedVisible(navDestination)

val title: String by viewModel.title.collectAsStateWithLifecycle()

AppRootGraph(
title = title,
message = message,
actions = actions,
currentRootDestination = rootDestination,
rootDestinations = state.rootDestinations,
fob = fob,
onBackPressed = state::onBackClick.takeIf { isBackPressedVisible },
onBackPressed = state::onBackClick.takeIf { deep > 0 },
navigateToRoot = state::navigateToRoot,
modifier = Modifier.fillMaxSize(),
) { contentPadding ->
Expand Down
13 changes: 0 additions & 13 deletions androidApp/src/main/java/com/m3u/androidApp/ui/AppDefaults.kt

This file was deleted.

15 changes: 0 additions & 15 deletions androidApp/src/main/java/com/m3u/androidApp/ui/AppNavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ import com.m3u.features.playlist.navigation.navigateToPlaylist
import com.m3u.features.playlist.navigation.playlistScreen
import com.m3u.features.playlist.navigation.playlistTvScreen
import com.m3u.features.stream.PlayerActivity
import com.m3u.i18n.R.string
import com.m3u.material.ktx.isTelevision
import com.m3u.ui.Destination
import com.m3u.ui.Destination.Root.Setting.SettingFragment
import com.m3u.ui.LocalHelper
import kotlinx.collections.immutable.ImmutableList

@Composable
Expand All @@ -40,7 +38,6 @@ fun AppNavHost(
navController: NavHostController = rememberNavController(),
startDestination: String = ROOT_ROUTE
) {
val helper = LocalHelper.current
val context = LocalContext.current
val pref = LocalPref.current

Expand Down Expand Up @@ -72,12 +69,6 @@ fun AppNavHost(
roots = roots,
contentPadding = contentPadding,
navigateToPlaylist = { playlist ->
if (!tv) {
helper.title = playlist.title.ifEmpty {
if (playlist.local) context.getString(string.feat_foryou_imported_playlist_title)
else ""
}
}
navController.navigateToPlaylist(playlist.url, null, tv)
},
navigateToStream = {
Expand All @@ -99,12 +90,6 @@ fun AppNavHost(
navController.navigateToAbout()
},
navigateToRecommendPlaylist = { playlist, recommend ->
if (!tv) {
helper.title = playlist.title.ifEmpty {
if (playlist.local) context.getString(string.feat_foryou_imported_playlist_title)
else ""
}
}
navController.navigateToPlaylist(playlist.url, recommend, tv)
},
navigateToSettingPlaylistManagement = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ class AppViewModel @Inject constructor(
val title: MutableStateFlow<String> = MutableStateFlow("")
val actions: MutableStateFlow<ImmutableList<Action>> = MutableStateFlow(persistentListOf())
val fob: MutableStateFlow<Fob?> = MutableStateFlow(null)
val deep: MutableStateFlow<Int> = MutableStateFlow(0)
}

Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private fun RootGraph(
var key by rememberSaveable { mutableStateOf(true) }
LifecycleResumeEffect(Unit) {
key = !key
onPauseOrDispose { }
onPauseOrDispose { }
}

HorizontalPager(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.res.stringResource
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.LifecycleResumeEffect
import androidx.lifecycle.compose.LifecycleStartEffect
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.m3u.core.util.basic.title
import com.m3u.features.about.components.ContributorItem
Expand All @@ -36,9 +36,12 @@ internal fun AboutRoute(
) {
val helper = LocalHelper.current
val title = stringResource(string.feat_about_title)
LifecycleResumeEffect(Unit) {
LifecycleStartEffect(Unit) {
helper.title = title.title()
onPauseOrDispose { }
helper.deep += 1
onStopOrDispose {
helper.deep -= 1
}
}

val state by viewModel.s.collectAsStateWithLifecycle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.LifecycleResumeEffect
import androidx.lifecycle.compose.LifecycleStartEffect
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.m3u.core.util.basic.title
import com.m3u.i18n.R.string
Expand All @@ -43,9 +43,12 @@ internal fun ConsoleRoute(
) {
val helper = LocalHelper.current
val title = stringResource(string.feat_console_title)
LifecycleResumeEffect(Unit) {
LifecycleStartEffect(Unit) {
helper.title = title.title()
onPauseOrDispose { }
helper.deep += 1
onStopOrDispose {
helper.deep -= 1
}
}
val state by viewModel.state.collectAsStateWithLifecycle()
ConsoleScreen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.ui.res.stringResource
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.m3u.core.architecture.pref.LocalPref
import com.m3u.core.util.basic.title
import com.m3u.data.database.model.Stream
import com.m3u.features.favorite.components.DialogStatus
import com.m3u.features.favorite.components.FavoriteDialog
Expand Down Expand Up @@ -59,7 +60,7 @@ fun FavouriteRoute(
var dialogStatus: DialogStatus by remember { mutableStateOf(DialogStatus.Idle) }

EventHandler(resume, title) {
helper.title = title
helper.title = title.title()
helper.actions = persistentListOf(
Action(
icon = Icons.AutoMirrored.Rounded.Sort,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.compose.ui.res.stringResource
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.m3u.core.architecture.pref.LocalPref
import com.m3u.core.util.basic.title
import com.m3u.data.database.model.Playlist
import com.m3u.features.foryou.components.ForyouDialog
import com.m3u.features.foryou.components.OnRename
Expand Down Expand Up @@ -69,7 +70,7 @@ fun ForyouRoute(
val recommend by viewModel.recommend.collectAsStateWithLifecycle()

EventHandler(resume, title) {
helper.title = title
helper.title = title.title()
helper.actions = persistentListOf(
Action(
icon = Icons.Rounded.Add,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.LifecycleStartEffect
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.google.accompanist.permissions.PermissionStatus
import com.google.accompanist.permissions.rememberPermissionState
import com.m3u.core.architecture.pref.LocalPref
import com.m3u.core.util.basic.title
import com.m3u.core.wrapper.Event
import com.m3u.core.wrapper.Message
import com.m3u.data.database.model.Stream
Expand Down Expand Up @@ -64,6 +66,7 @@ internal fun PlaylistRoute(
) {
val context = LocalContext.current
val pref = LocalPref.current
val helper = LocalHelper.current

val state by viewModel.state.collectAsStateWithLifecycle()
val zapping by viewModel.zapping.collectAsStateWithLifecycle()
Expand All @@ -87,6 +90,14 @@ internal fun PlaylistRoute(
Manifest.permission.WRITE_EXTERNAL_STORAGE
)

LifecycleStartEffect(playlist) {
helper.title = playlist?.title?.title().orEmpty()
helper.deep += 1
onStopOrDispose {
helper.deep -= 1
}
}

LaunchedEffect(pref.autoRefresh, playlistUrl) {
if (playlistUrl.isNotEmpty() && pref.autoRefresh) {
viewModel.onEvent(PlaylistEvent.Refresh)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ class TvPlaylistActivity : AppCompatActivity() {

override val message: StateFlow<Message> = messageService.message

override var deep: Int = 0

override val windowSizeClass: WindowSizeClass
@Composable get() = calculateWindowSizeClass(activity = this@TvPlaylistActivity)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.LifecycleResumeEffect
import androidx.lifecycle.compose.LifecycleStartEffect
import com.m3u.core.wrapper.Event
import com.m3u.data.database.model.Stream
import com.m3u.features.playlist.Channel
Expand Down Expand Up @@ -114,7 +114,7 @@ internal fun PlaylistScreenImpl(
var dialogStatus: DialogStatus by remember { mutableStateOf(DialogStatus.Idle) }
var isSortSheetVisible by rememberSaveable { mutableStateOf(false) }

LifecycleResumeEffect(Unit) {
LifecycleStartEffect(Unit) {
helper.actions = persistentListOf(
Action(
icon = Icons.AutoMirrored.Rounded.Sort,
Expand All @@ -127,7 +127,7 @@ internal fun PlaylistScreenImpl(
onClick = onRefresh
)
)
onPauseOrDispose { }
onStopOrDispose { }
}

Background {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fun SettingRoute(
val helper = LocalHelper.current

EventHandler(resume, title) {
helper.title = title
helper.title = title.title()
helper.actions = persistentListOf()
}

Expand Down Expand Up @@ -146,6 +146,10 @@ private fun SettingScreen(
SettingFragment.Scripts -> scriptTitle
SettingFragment.Appearance -> appearanceTitle
}.title()
helper.deep += when (it) {
SettingFragment.Root -> -1
else -> 1
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ class PlayerActivity : ComponentActivity() {

override val message: StateFlow<Message> = messageService.message


override var deep: Int = 0
override var darkMode: UBoolean = UBoolean.Unspecified
set(value) {
field = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.testTag
import androidx.core.content.ContextCompat
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.LifecycleResumeEffect
import androidx.lifecycle.compose.LifecycleStartEffect
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.m3u.core.architecture.pref.LocalPref
Expand Down Expand Up @@ -77,7 +76,7 @@ fun StreamRoute(
}
}

LifecycleResumeEffect(Unit) {
LifecycleStartEffect(Unit) {
with(helper) {
darkMode = true.unspecifiable
statusBarVisibility = false.unspecifiable
Expand All @@ -90,7 +89,7 @@ fun StreamRoute(
}
}
}
onPauseOrDispose { }
onStopOrDispose {}
}

LaunchedEffect(pref.zappingMode, playerState.videoSize) {
Expand Down
7 changes: 7 additions & 0 deletions ui/src/main/java/com/m3u/ui/Helper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface Helper {
val isInPipMode: Boolean
var screenOrientation: Int
val message: StateFlow<Message>
var deep: Int

@get:Composable
val windowSizeClass: WindowSizeClass
Expand Down Expand Up @@ -96,6 +97,12 @@ val EmptyHelper = object : Helper {
error("Cannot set navigationBarsVisibility")
}

override var deep: Int
get() = error("Cannot get deep")
set(_) {
error("Cannot set deep")
}

override var darkMode: UBoolean
get() = error("Cannot get darkMode")
set(_) {
Expand Down

0 comments on commit 8804570

Please sign in to comment.