From 451dc80d67632b5a4d55f6a4c231fbdd0bedab09 Mon Sep 17 00:00:00 2001 From: Sanmer Date: Sat, 29 Jun 2024 16:52:22 +0800 Subject: [PATCH] Add setting as homepage (#253) --- .../datastore/UserPreferencesDataSource.kt | 9 ++++++++ .../sanmer/mrepo/datastore/model/Homepage.kt | 6 ++++++ .../mrepo/datastore/model/UserPreferences.kt | 9 ++++++++ .../repository/UserPreferencesRepository.kt | 3 +++ .../sanmer/mrepo/ui/activity/MainScreen.kt | 21 +++++++++++++++---- .../mrepo/ui/screens/modules/ModulesMenu.kt | 18 +++++++++++++--- .../mrepo/ui/screens/modules/ModulesScreen.kt | 5 ++++- .../ui/screens/repository/RepositoryMenu.kt | 18 +++++++++++++--- .../ui/screens/repository/RepositoryScreen.kt | 5 ++++- .../mrepo/viewmodel/ModulesViewModel.kt | 7 +++++++ .../mrepo/viewmodel/RepositoryViewModel.kt | 9 +++++++- app/src/main/res/values-zh-rCN/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 13 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 app/src/main/kotlin/dev/sanmer/mrepo/datastore/model/Homepage.kt diff --git a/app/src/main/kotlin/dev/sanmer/mrepo/datastore/UserPreferencesDataSource.kt b/app/src/main/kotlin/dev/sanmer/mrepo/datastore/UserPreferencesDataSource.kt index ea36ef9c..031a9f33 100644 --- a/app/src/main/kotlin/dev/sanmer/mrepo/datastore/UserPreferencesDataSource.kt +++ b/app/src/main/kotlin/dev/sanmer/mrepo/datastore/UserPreferencesDataSource.kt @@ -2,6 +2,7 @@ package dev.sanmer.mrepo.datastore import androidx.datastore.core.DataStore import dev.sanmer.mrepo.datastore.model.DarkMode +import dev.sanmer.mrepo.datastore.model.Homepage import dev.sanmer.mrepo.datastore.model.ModulesMenu import dev.sanmer.mrepo.datastore.model.RepositoryMenu import dev.sanmer.mrepo.datastore.model.UserPreferences @@ -55,6 +56,14 @@ class UserPreferencesDataSource @Inject constructor( } } + suspend fun setHomepage(value: Homepage) = withContext(Dispatchers.IO) { + userPreferences.updateData { + it.copy( + homepage = value + ) + } + } + suspend fun setRepositoryMenu(value: RepositoryMenu) = withContext(Dispatchers.IO) { userPreferences.updateData { it.copy( diff --git a/app/src/main/kotlin/dev/sanmer/mrepo/datastore/model/Homepage.kt b/app/src/main/kotlin/dev/sanmer/mrepo/datastore/model/Homepage.kt new file mode 100644 index 00000000..7733effc --- /dev/null +++ b/app/src/main/kotlin/dev/sanmer/mrepo/datastore/model/Homepage.kt @@ -0,0 +1,6 @@ +package dev.sanmer.mrepo.datastore.model + +enum class Homepage { + Modules, + Repository +} \ No newline at end of file diff --git a/app/src/main/kotlin/dev/sanmer/mrepo/datastore/model/UserPreferences.kt b/app/src/main/kotlin/dev/sanmer/mrepo/datastore/model/UserPreferences.kt index 8c6aad93..659fd23c 100644 --- a/app/src/main/kotlin/dev/sanmer/mrepo/datastore/model/UserPreferences.kt +++ b/app/src/main/kotlin/dev/sanmer/mrepo/datastore/model/UserPreferences.kt @@ -4,6 +4,7 @@ import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.runtime.Composable import dev.sanmer.mrepo.app.Const import dev.sanmer.mrepo.compat.BuildCompat +import dev.sanmer.mrepo.datastore.model.WorkingMode.Companion.isNonRoot import dev.sanmer.mrepo.ui.theme.Colors import kotlinx.serialization.Serializable import kotlinx.serialization.decodeFromByteArray @@ -20,11 +21,19 @@ data class UserPreferences( val themeColor: Int = if (BuildCompat.atLeastS) Colors.Dynamic.id else Colors.Pourville.id, val deleteZipFile: Boolean = false, val downloadPath: String = Const.PUBLIC_DOWNLOADS.path, + val homepage: Homepage = Homepage.Repository, @ProtoNumber(20) val repositoryMenu: RepositoryMenu = RepositoryMenu(), @ProtoNumber(30) val modulesMenu: ModulesMenu = ModulesMenu() ) { + val currentHomepage by lazy { + when { + workingMode.isNonRoot -> Homepage.Repository + else -> homepage + } + } + @Composable fun isDarkMode() = when (darkMode) { DarkMode.AlwaysOff -> false diff --git a/app/src/main/kotlin/dev/sanmer/mrepo/repository/UserPreferencesRepository.kt b/app/src/main/kotlin/dev/sanmer/mrepo/repository/UserPreferencesRepository.kt index bf9fab89..199e1e07 100644 --- a/app/src/main/kotlin/dev/sanmer/mrepo/repository/UserPreferencesRepository.kt +++ b/app/src/main/kotlin/dev/sanmer/mrepo/repository/UserPreferencesRepository.kt @@ -2,6 +2,7 @@ package dev.sanmer.mrepo.repository import dev.sanmer.mrepo.datastore.UserPreferencesDataSource import dev.sanmer.mrepo.datastore.model.DarkMode +import dev.sanmer.mrepo.datastore.model.Homepage import dev.sanmer.mrepo.datastore.model.ModulesMenu import dev.sanmer.mrepo.datastore.model.RepositoryMenu import dev.sanmer.mrepo.datastore.model.WorkingMode @@ -24,6 +25,8 @@ class UserPreferencesRepository @Inject constructor( suspend fun setDownloadPath(value: String) = userPreferencesDataSource.setDownloadPath(value) + suspend fun setHomepage(value: Homepage) = userPreferencesDataSource.setHomepage(value) + suspend fun setRepositoryMenu(value: RepositoryMenu) = userPreferencesDataSource.setRepositoryMenu(value) suspend fun setModulesMenu(value: ModulesMenu) = userPreferencesDataSource.setModulesMenu(value) diff --git a/app/src/main/kotlin/dev/sanmer/mrepo/ui/activity/MainScreen.kt b/app/src/main/kotlin/dev/sanmer/mrepo/ui/activity/MainScreen.kt index 85e32126..2b07b295 100644 --- a/app/src/main/kotlin/dev/sanmer/mrepo/ui/activity/MainScreen.kt +++ b/app/src/main/kotlin/dev/sanmer/mrepo/ui/activity/MainScreen.kt @@ -1,6 +1,8 @@ package dev.sanmer.mrepo.ui.activity +import androidx.compose.foundation.layout.WindowInsets import androidx.compose.foundation.layout.imePadding +import androidx.compose.foundation.layout.navigationBars import androidx.compose.foundation.layout.padding import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme @@ -20,6 +22,7 @@ import androidx.navigation.NavDestination.Companion.hierarchy import androidx.navigation.compose.NavHost import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.rememberNavController +import dev.sanmer.mrepo.datastore.model.Homepage import dev.sanmer.mrepo.datastore.model.WorkingMode.Companion.isRoot import dev.sanmer.mrepo.ui.navigation.MainScreen import dev.sanmer.mrepo.ui.navigation.graphs.modulesScreen @@ -30,21 +33,31 @@ import dev.sanmer.mrepo.ui.utils.navigatePopUpTo @Composable fun MainScreen() { - val userPreferences = LocalUserPreferences.current val navController = rememberNavController() + val userPreferences = LocalUserPreferences.current + val startDestination by remember { + derivedStateOf { + when (userPreferences.currentHomepage) { + Homepage.Repository -> MainScreen.Repository.route + Homepage.Modules -> MainScreen.Modules.route + } + } + } + Scaffold( bottomBar = { BottomNav( navController = navController, isRoot = userPreferences.workingMode.isRoot ) - } + }, + contentWindowInsets = WindowInsets.navigationBars ) { NavHost( - modifier = Modifier.padding(bottom = it.calculateBottomPadding()), + modifier = Modifier.padding(it), navController = navController, - startDestination = MainScreen.Repository.route + startDestination = startDestination ) { repositoryScreen( navController = navController diff --git a/app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/modules/ModulesMenu.kt b/app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/modules/ModulesMenu.kt index 07b903ac..d9ffa491 100644 --- a/app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/modules/ModulesMenu.kt +++ b/app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/modules/ModulesMenu.kt @@ -26,6 +26,7 @@ import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import dev.sanmer.mrepo.R +import dev.sanmer.mrepo.datastore.model.Homepage import dev.sanmer.mrepo.datastore.model.ModulesMenu import dev.sanmer.mrepo.datastore.model.Option import dev.sanmer.mrepo.ui.component.MenuChip @@ -42,7 +43,8 @@ private val options = listOf( @Composable internal fun ModulesMenu( - setMenu: (ModulesMenu) -> Unit + setMenu: (ModulesMenu) -> Unit, + setHomepage: () -> Unit, ) { val userPreferences = LocalUserPreferences.current var open by rememberSaveable { mutableStateOf(false) } @@ -59,7 +61,9 @@ internal fun ModulesMenu( BottomSheet( onClose = { open = false }, menu = userPreferences.modulesMenu, - setMenu = setMenu + setMenu = setMenu, + isHomepage = userPreferences.homepage == Homepage.Modules, + setHomepage = setHomepage ) } } @@ -69,7 +73,9 @@ internal fun ModulesMenu( private fun BottomSheet( onClose: () -> Unit, menu: ModulesMenu, - setMenu: (ModulesMenu) -> Unit + setMenu: (ModulesMenu) -> Unit, + isHomepage: Boolean, + setHomepage: () -> Unit ) = ModalBottomSheet( onDismissRequest = onClose, sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true), @@ -135,6 +141,12 @@ private fun BottomSheet( onClick = { setMenu(menu.copy(showUpdatedTime = !menu.showUpdatedTime)) }, label = { Text(text = stringResource(id = R.string.menu_show_updated)) } ) + + MenuChip( + selected = isHomepage, + onClick = setHomepage, + label = { Text(text = stringResource(id = R.string.menu_set_homepage)) } + ) } } } \ No newline at end of file diff --git a/app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/modules/ModulesScreen.kt b/app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/modules/ModulesScreen.kt index bb3d4d8c..dcc8aa51 100644 --- a/app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/modules/ModulesScreen.kt +++ b/app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/modules/ModulesScreen.kt @@ -83,6 +83,7 @@ fun ModulesScreen( onOpenSearch = viewModel::openSearch, onCloseSearch = viewModel::closeSearch, setMenu = viewModel::setModulesMenu, + setHomepage = viewModel::setHomepage, scrollBehavior = scrollBehavior ) }, @@ -137,6 +138,7 @@ private fun TopBar( onOpenSearch: () -> Unit, onCloseSearch: () -> Unit, setMenu: (ModulesMenu) -> Unit, + setHomepage: () -> Unit, scrollBehavior: TopAppBarScrollBehavior, ) { var query by remember { mutableStateOf("") } @@ -167,7 +169,8 @@ private fun TopBar( } ModulesMenu( - setMenu = setMenu + setMenu = setMenu, + setHomepage = setHomepage ) } ) diff --git a/app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/repository/RepositoryMenu.kt b/app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/repository/RepositoryMenu.kt index 3bd7996d..7e10840e 100644 --- a/app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/repository/RepositoryMenu.kt +++ b/app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/repository/RepositoryMenu.kt @@ -29,6 +29,7 @@ import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import dev.sanmer.mrepo.R +import dev.sanmer.mrepo.datastore.model.Homepage import dev.sanmer.mrepo.datastore.model.Option import dev.sanmer.mrepo.datastore.model.RepositoryMenu import dev.sanmer.mrepo.model.online.OnlineModule @@ -47,7 +48,8 @@ private val options = listOf( @Composable internal fun RepositoryMenu( - setMenu: (RepositoryMenu) -> Unit + setMenu: (RepositoryMenu) -> Unit, + setHomepage: () -> Unit, ) { val userPreferences = LocalUserPreferences.current var open by rememberSaveable { mutableStateOf(false) } @@ -64,7 +66,9 @@ internal fun RepositoryMenu( BottomSheet( onClose = { open = false }, menu = userPreferences.repositoryMenu, - setMenu = setMenu + setMenu = setMenu, + isHomepage = userPreferences.homepage == Homepage.Repository, + setHomepage = setHomepage ) } } @@ -74,7 +78,9 @@ internal fun RepositoryMenu( private fun BottomSheet( onClose: () -> Unit, menu: RepositoryMenu, - setMenu: (RepositoryMenu) -> Unit + setMenu: (RepositoryMenu) -> Unit, + isHomepage: Boolean, + setHomepage: () -> Unit ) = ModalBottomSheet( onDismissRequest = onClose, sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true), @@ -171,6 +177,12 @@ private fun BottomSheet( onClick = { setMenu(menu.copy(showUpdatedTime = !menu.showUpdatedTime)) }, label = { Text(text = stringResource(id = R.string.menu_show_updated)) } ) + + MenuChip( + selected = isHomepage, + onClick = setHomepage, + label = { Text(text = stringResource(id = R.string.menu_set_homepage)) } + ) } } } \ No newline at end of file diff --git a/app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/repository/RepositoryScreen.kt b/app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/repository/RepositoryScreen.kt index 92d9929b..eba0b754 100644 --- a/app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/repository/RepositoryScreen.kt +++ b/app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/repository/RepositoryScreen.kt @@ -60,6 +60,7 @@ fun RepositoryScreen( onOpenSearch = viewModel::openSearch, onCloseSearch = viewModel::closeSearch, setMenu = viewModel::setRepositoryMenu, + setHomepage = viewModel::setHomepage, scrollBehavior = scrollBehavior ) }, @@ -95,6 +96,7 @@ private fun TopBar( onOpenSearch: () -> Unit, onCloseSearch: () -> Unit, setMenu: (RepositoryMenu) -> Unit, + setHomepage: () -> Unit, scrollBehavior: TopAppBarScrollBehavior, ) { var query by remember { mutableStateOf("") } @@ -125,7 +127,8 @@ private fun TopBar( } RepositoryMenu( - setMenu = setMenu + setMenu = setMenu, + setHomepage = setHomepage ) } ) diff --git a/app/src/main/kotlin/dev/sanmer/mrepo/viewmodel/ModulesViewModel.kt b/app/src/main/kotlin/dev/sanmer/mrepo/viewmodel/ModulesViewModel.kt index 1188106d..c528b251 100644 --- a/app/src/main/kotlin/dev/sanmer/mrepo/viewmodel/ModulesViewModel.kt +++ b/app/src/main/kotlin/dev/sanmer/mrepo/viewmodel/ModulesViewModel.kt @@ -10,6 +10,7 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel import dev.sanmer.mrepo.Compat +import dev.sanmer.mrepo.datastore.model.Homepage import dev.sanmer.mrepo.datastore.model.ModulesMenu import dev.sanmer.mrepo.datastore.model.Option import dev.sanmer.mrepo.model.json.UpdateJson @@ -170,6 +171,12 @@ class ModulesViewModel @Inject constructor( } } + fun setHomepage() { + viewModelScope.launch { + userPreferencesRepository.setHomepage(Homepage.Modules) + } + } + fun createModuleOps(module: LocalModule) = when (module.state) { State.Enable -> ModuleOps( isOpsRunning = opsTasks.contains(module.id), diff --git a/app/src/main/kotlin/dev/sanmer/mrepo/viewmodel/RepositoryViewModel.kt b/app/src/main/kotlin/dev/sanmer/mrepo/viewmodel/RepositoryViewModel.kt index f928d273..3d51b8f8 100644 --- a/app/src/main/kotlin/dev/sanmer/mrepo/viewmodel/RepositoryViewModel.kt +++ b/app/src/main/kotlin/dev/sanmer/mrepo/viewmodel/RepositoryViewModel.kt @@ -7,6 +7,7 @@ import androidx.compose.runtime.setValue import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel +import dev.sanmer.mrepo.datastore.model.Homepage import dev.sanmer.mrepo.datastore.model.Option import dev.sanmer.mrepo.datastore.model.RepositoryMenu import dev.sanmer.mrepo.model.local.LocalModule @@ -139,7 +140,13 @@ class RepositoryViewModel @Inject constructor( } } - private suspend fun createOnlineState( + fun setHomepage() { + viewModelScope.launch { + userPreferencesRepository.setHomepage(Homepage.Repository) + } + } + + private fun createOnlineState( online: OnlineModule, locals: List> ): Pair { diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 94889f70..d23850ed 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -111,6 +111,7 @@ 降序 置顶已启用 显示更新时间 + 设置为主页 下载成功 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9c1faf79..c4ddcbda 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -112,6 +112,7 @@ Descending Pin Enabled Show Updated Time + Set as Homepage Download successful