Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Add setting as homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed Jun 29, 2024
1 parent 07ad14c commit 6405c65
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.sanmer.mrepo.datastore.model

enum class Homepage {
Modules,
Repository
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
21 changes: 17 additions & 4 deletions app/src/main/kotlin/dev/sanmer/mrepo/ui/activity/MainScreen.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) }
Expand All @@ -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
)
}
}
Expand All @@ -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),
Expand Down Expand Up @@ -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)) }
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ fun ModulesScreen(
onOpenSearch = viewModel::openSearch,
onCloseSearch = viewModel::closeSearch,
setMenu = viewModel::setModulesMenu,
setHomepage = viewModel::setHomepage,
scrollBehavior = scrollBehavior
)
},
Expand Down Expand Up @@ -137,6 +138,7 @@ private fun TopBar(
onOpenSearch: () -> Unit,
onCloseSearch: () -> Unit,
setMenu: (ModulesMenu) -> Unit,
setHomepage: () -> Unit,
scrollBehavior: TopAppBarScrollBehavior,
) {
var query by remember { mutableStateOf("") }
Expand Down Expand Up @@ -167,7 +169,8 @@ private fun TopBar(
}

ModulesMenu(
setMenu = setMenu
setMenu = setMenu,
setHomepage = setHomepage
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) }
Expand All @@ -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
)
}
}
Expand All @@ -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),
Expand Down Expand Up @@ -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)) }
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fun RepositoryScreen(
onOpenSearch = viewModel::openSearch,
onCloseSearch = viewModel::closeSearch,
setMenu = viewModel::setRepositoryMenu,
setHomepage = viewModel::setHomepage,
scrollBehavior = scrollBehavior
)
},
Expand Down Expand Up @@ -95,6 +96,7 @@ private fun TopBar(
onOpenSearch: () -> Unit,
onCloseSearch: () -> Unit,
setMenu: (RepositoryMenu) -> Unit,
setHomepage: () -> Unit,
scrollBehavior: TopAppBarScrollBehavior,
) {
var query by remember { mutableStateOf("") }
Expand Down Expand Up @@ -125,7 +127,8 @@ private fun TopBar(
}

RepositoryMenu(
setMenu = setMenu
setMenu = setMenu,
setHomepage = setHomepage
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<LocalModule, Boolean>>
): Pair<OnlineState, OnlineModule> {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
<string name="menu_descending">降序</string>
<string name="menu_pin_enabled">置顶已启用</string>
<string name="menu_show_updated">显示更新时间</string>
<string name="menu_set_homepage">设置为主页</string>

<!-- Message -->
<string name="message_download_success">下载成功</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
<string name="menu_descending">Descending</string>
<string name="menu_pin_enabled">Pin Enabled</string>
<string name="menu_show_updated">Show Updated Time</string>
<string name="menu_set_homepage">Set as Homepage</string>

<!-- Message -->
<string name="message_download_success">Download successful</string>
Expand Down

0 comments on commit 6405c65

Please sign in to comment.