enhancement: back to home; closes #345 (#358)

This commit is contained in:
Diego Beraldin 2023-12-23 12:26:38 +01:00 committed by GitHub
parent b3a29d2cf4
commit c9bd899c1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 9 deletions

View File

@ -68,6 +68,8 @@ dependencies {
implementation(libs.koin.android)
implementation(libs.coil)
implementation(libs.coil.gif)
implementation(libs.voyager.navigator)
implementation(libs.voyager.tab)
implementation(projects.shared)
implementation(projects.core.utils)

View File

@ -8,7 +8,9 @@ import androidx.activity.compose.setContent
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.lifecycleScope
import com.github.diegoberaldin.raccoonforlemmy.MainView
import com.github.diegoberaldin.raccoonforlemmy.core.navigation.TabNavigationSection
import com.github.diegoberaldin.raccoonforlemmy.core.navigation.di.getNavigationCoordinator
import com.github.diegoberaldin.raccoonforlemmy.feature.home.ui.HomeTab
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
@ -27,8 +29,17 @@ class MainActivity : ComponentActivity() {
val navigationCoordinator = getNavigationCoordinator()
val backPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (!navigationCoordinator.exitMessageVisible.value) {
navigationCoordinator.setExitMessageVisible(true)
if (navigationCoordinator.currentSection.value == TabNavigationSection.Home) {
// asks for confirmation
if (!navigationCoordinator.exitMessageVisible.value) {
navigationCoordinator.setExitMessageVisible(true)
}
} else {
// goes back to home
with(navigationCoordinator) {
changeTab(HomeTab)
setCurrentSection(TabNavigationSection.Home)
}
}
}
}

View File

@ -4,6 +4,8 @@ import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.navigator.bottomSheet.BottomSheetNavigator
import cafe.adriel.voyager.navigator.tab.Tab
import cafe.adriel.voyager.navigator.tab.TabNavigator
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.SupervisorJob
@ -13,6 +15,7 @@ import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.getAndUpdate
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.receiveAsFlow
@ -28,6 +31,7 @@ private sealed interface NavigationEvent {
@OptIn(FlowPreview::class)
internal class DefaultNavigationCoordinator : NavigationCoordinator {
override val currentSection = MutableStateFlow<TabNavigationSection?>(null)
override val onDoubleTabSelection = MutableSharedFlow<TabNavigationSection>()
override val deepLinkUrl = MutableSharedFlow<String>()
override val inboxUnread = MutableStateFlow(0)
@ -38,7 +42,7 @@ internal class DefaultNavigationCoordinator : NavigationCoordinator {
private var connection: NestedScrollConnection? = null
private var navigator: Navigator? = null
private var bottomNavigator: BottomSheetNavigator? = null
private var currentTab: TabNavigationSection? = null
private var tabNavigator: TabNavigator? = null
private val scope = CoroutineScope(SupervisorJob())
private var canGoBackCallback: (() -> Boolean)? = null
private val bottomSheetChannel = Channel<NavigationEvent>()
@ -93,12 +97,13 @@ internal class DefaultNavigationCoordinator : NavigationCoordinator {
override fun getBottomBarScrollConnection() = connection
override fun setCurrentSection(section: TabNavigationSection) {
val oldTab = currentTab
currentTab = section
if (section == oldTab) {
scope.launch {
onDoubleTabSelection.emit(section)
currentSection.getAndUpdate { oldValue ->
if (section == oldValue) {
scope.launch {
onDoubleTabSelection.emit(section)
}
}
section
}
}
@ -157,4 +162,12 @@ internal class DefaultNavigationCoordinator : NavigationCoordinator {
override fun setBottomSheetGesturesEnabled(value: Boolean) {
bottomSheetGesturesEnabled.value = value
}
override fun setTabNavigator(value: TabNavigator) {
tabNavigator = value
}
override fun changeTab(value: Tab) {
tabNavigator?.current = value
}
}

View File

@ -5,6 +5,8 @@ import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.navigator.bottomSheet.BottomSheetNavigator
import cafe.adriel.voyager.navigator.tab.Tab
import cafe.adriel.voyager.navigator.tab.TabNavigator
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
@ -19,6 +21,7 @@ sealed interface TabNavigationSection {
@Stable
interface NavigationCoordinator {
val currentSection: StateFlow<TabNavigationSection?>
val onDoubleTabSelection: Flow<TabNavigationSection>
val inboxUnread: StateFlow<Int>
val deepLinkUrl: Flow<String?>
@ -41,4 +44,6 @@ interface NavigationCoordinator {
fun popScreen()
fun setExitMessageVisible(value: Boolean)
fun setBottomSheetGesturesEnabled(value: Boolean)
fun setTabNavigator(value: TabNavigator)
fun changeTab(value: Tab)
}

View File

@ -110,10 +110,13 @@ internal object MainScreen : Screen {
}
TabNavigator(HomeTab) { tabNavigator ->
navigationCoordinator.setTabNavigator(tabNavigator)
LaunchedEffect(tabNavigator.current) {
// when the current tab chanes, reset the bottom bar offset to the default value
model.reduce(MainScreenMviModel.Intent.SetBottomBarOffsetHeightPx(0f))
}
LaunchedEffect(drawerCoordinator) {
drawerCoordinator.events.onEach { evt ->
when (evt) {
@ -121,7 +124,10 @@ internal object MainScreen : Screen {
if (tabNavigator.current == HomeTab) {
notificationCenter.send(NotificationCenterEvent.ChangeFeedType(evt.value))
} else {
tabNavigator.current = HomeTab
with(navigationCoordinator) {
changeTab(HomeTab)
setCurrentSection(TabNavigationSection.Home)
}
launch {
// wait for transition to finish
delay(750)