Refactor preferences dashboard

This commit is contained in:
Suphon Thanakornpakapong
2021-06-11 20:17:26 +07:00
parent bae4efbbee
commit e8563b34ea
4 changed files with 71 additions and 114 deletions

View File

@@ -16,8 +16,6 @@
package app.lawnchair.ui.preferences
import android.content.Context
import androidx.annotation.DrawableRes
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.MaterialTheme
@@ -31,9 +29,8 @@ import androidx.navigation.NavController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import app.lawnchair.preferences.getMajorVersion
import app.lawnchair.ui.preferences.about.aboutGraph
import app.lawnchair.ui.preferences.components.PreferenceCategoryList
import app.lawnchair.ui.preferences.components.PreferenceLayout
import app.lawnchair.ui.preferences.components.SystemUi
import app.lawnchair.ui.preferences.components.TopBar
import app.lawnchair.ui.util.portal.ProvidePortalNode
@@ -52,71 +49,6 @@ object Routes {
const val QUICKSTEP: String = "quickstep"
}
sealed class PreferenceCategory(
val label: String,
val description: String? = null,
@DrawableRes val iconResource: Int,
val route: String
) {
class General(context: Context) : PreferenceCategory(
label = context.getString(R.string.general_label),
description = context.getString(R.string.general_description),
iconResource = R.drawable.ic_general,
route = Routes.GENERAL
)
class HomeScreen(context: Context) : PreferenceCategory(
label = context.getString(R.string.home_screen_label),
description = context.getString(R.string.home_screen_description),
iconResource = R.drawable.ic_home_screen,
route = Routes.HOME_SCREEN
)
class Dock(context: Context) : PreferenceCategory(
label = context.getString(R.string.dock_label),
description = context.getString(R.string.dock_description),
iconResource = R.drawable.ic_dock,
route = Routes.DOCK
)
class AppDrawer(context: Context) : PreferenceCategory(
label = context.getString(R.string.app_drawer_label),
description = context.getString(R.string.app_drawer_description),
iconResource = R.drawable.ic_app_drawer,
route = Routes.APP_DRAWER
)
class Folders(context: Context) : PreferenceCategory(
label = context.getString(R.string.folders_label),
description = context.getString(R.string.folders_description),
iconResource = R.drawable.ic_folder,
route = Routes.FOLDERS
)
class Quickstep(context: Context) : PreferenceCategory(
label = context.getString(R.string.quickstep_label),
iconResource = R.drawable.ic_quickstep,
route = Routes.QUICKSTEP
)
class About(context: Context) : PreferenceCategory(
label = context.getString(R.string.about_label),
description = "${context.getString(R.string.derived_app_name)} ${getMajorVersion(context)}",
iconResource = R.drawable.ic_about,
route = Routes.ABOUT
)
}
fun getPreferenceCategories(context: Context) = listOf(
PreferenceCategory.General(context),
PreferenceCategory.HomeScreen(context),
PreferenceCategory.Dock(context),
PreferenceCategory.AppDrawer(context),
PreferenceCategory.Folders(context),
PreferenceCategory.Quickstep(context),
PreferenceCategory.About(context)
)
val LocalNavController = compositionLocalOf<NavController> {
error("CompositionLocal LocalNavController not present")
}
@@ -141,7 +73,9 @@ fun Preferences(interactor: PreferenceInteractor = viewModel<PreferenceViewModel
NavHost(navController = navController, startDestination = "preferences") {
composable(route = Routes.PREFERENCES) {
pageMeta.provide(Meta(title = stringResource(id = R.string.settings)))
PreferenceCategoryList(navController)
PreferenceLayout {
PreferencesDashboard()
}
}
generalGraph(route = Routes.GENERAL)
homeScreenGraph(route = Routes.HOME_SCREEN)

View File

@@ -0,0 +1,61 @@
package app.lawnchair.ui.preferences
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import app.lawnchair.preferences.getMajorVersion
import app.lawnchair.ui.preferences.components.PreferenceCategory
import com.android.launcher3.R
@Composable
fun PreferencesDashboard() {
PreferenceCategory(
label = stringResource(R.string.general_label),
description = stringResource(R.string.general_description),
iconResource = R.drawable.ic_general,
route = Routes.GENERAL
)
PreferenceCategory(
label = stringResource(R.string.home_screen_label),
description = stringResource(R.string.home_screen_description),
iconResource = R.drawable.ic_home_screen,
route = Routes.HOME_SCREEN
)
PreferenceCategory(
label = stringResource(R.string.dock_label),
description = stringResource(R.string.dock_description),
iconResource = R.drawable.ic_dock,
route = Routes.DOCK
)
PreferenceCategory(
label = stringResource(R.string.app_drawer_label),
description = stringResource(R.string.app_drawer_description),
iconResource = R.drawable.ic_app_drawer,
route = Routes.APP_DRAWER
)
PreferenceCategory(
label = stringResource(R.string.folders_label),
description = stringResource(R.string.folders_description),
iconResource = R.drawable.ic_folder,
route = Routes.FOLDERS
)
PreferenceCategory(
label = stringResource(R.string.quickstep_label),
iconResource = R.drawable.ic_quickstep,
route = Routes.QUICKSTEP
)
val context = LocalContext.current
PreferenceCategory(
label = stringResource(R.string.about_label),
description = "${context.getString(R.string.derived_app_name)} ${getMajorVersion(context)}",
iconResource = R.drawable.ic_about,
route = Routes.ABOUT
)
}

View File

@@ -27,17 +27,19 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import app.lawnchair.ui.preferences.LocalNavController
@Composable
fun PreferenceCategoryListItem(
fun PreferenceCategory(
label: String,
description: String?,
description: String? = null,
@DrawableRes iconResource: Int,
onClick: () -> Unit
route: String
) {
val navController = LocalNavController.current
Row(
modifier = Modifier
.clickable(onClick = onClick)
.clickable(onClick = { navController.navigate(route) })
.height(72.dp)
.padding(start = 16.dp, end = 16.dp)
.fillMaxWidth(),

View File

@@ -1,40 +0,0 @@
/*
* Copyright 2021, Lawnchair
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.lawnchair.ui.preferences.components
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavController
import app.lawnchair.ui.preferences.getPreferenceCategories
@Composable
fun PreferenceCategoryList(navController: NavController) {
val context = LocalContext.current
val categories = remember { getPreferenceCategories(context) }
PreferenceLayoutLazyColumn {
items(categories) { item ->
PreferenceCategoryListItem(
label = item.label,
description = item.description,
iconResource = item.iconResource,
onClick = { navController.navigate(item.route) })
}
}
}