Skip to content

Commit

Permalink
Use mrmans0n Compose rules to check styles (LawnchairLauncher#3788)
Browse files Browse the repository at this point in the history
* Use customRuleSets

* Disable ktlint_compose_modifier-missing-check

* Fix compose:vm-forwarding-check

https://mrmans0n.github.io/compose-rules/rules/#hoist-all-the-things

* Fix compose:param-order-check

https://mrmans0n.github.io/compose-rules/rules/#ordering-composable-parameters-properly

* Intro kotlinx-collections-immutable

* Fix compose:unstable-collections

https://mrmans0n.github.io/compose-rules/rules/#avoid-using-unstable-collections

* Fix compose:modifier-not-used-at-root

https://mrmans0n.github.io/compose-rules/rules/#modifiers-should-be-used-at-the-top-most-layout-of-the-component

* Fix compose:multiple-emitters-check

https://mrmans0n.github.io/compose-rules/rules/#do-not-emit-multiple-pieces-of-content

* Fix compose:compositionlocal-allowlist

https://mrmans0n.github.io/compose-rules/rules/#compositionlocals

* Enable modifier-missing-check

https://mrmans0n.github.io/compose-rules/rules/#when-should-i-expose-modifier-parameters

* Cleanups

* io.nlopez.compose.rules:ktlint:0.3.5

* Rearrange params

* Revert "Fix compose:modifier-not-used-at-root"

This reverts commit 7dee424.

* Disable modifier-not-used-at-root for PreferenceTemplate

* Cleanups

* Remove unused modifiers

* Disable ktlint_compose_modifier-missing-check

* Disable ktlint_compose_compositionlocal-allowlist
  • Loading branch information
Goooler authored Dec 9, 2023
1 parent 5931c9a commit d9af42b
Show file tree
Hide file tree
Showing 49 changed files with 240 additions and 167 deletions.
10 changes: 8 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ dependencies {

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3'
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.2'
implementation "org.jetbrains.kotlinx:kotlinx-collections-immutable:0.3.6"
implementation 'com.github.ChickenHook:RestrictionBypass:2.2'
implementation 'dev.rikka.tools.refine:runtime:4.4.0'

Expand Down Expand Up @@ -438,8 +439,13 @@ ksp {

spotless {
kotlin {
ktlint()
target("lawnchair/src/**/*.kt", "baseline-profile/src/**/*.kt")
target("lawnchair/src/**/*.kt")
ktlint().customRuleSets([
"io.nlopez.compose.rules:ktlint:0.3.5",
]).editorConfigOverride([
"ktlint_compose_modifier-missing-check": "disabled",
"ktlint_compose_compositionlocal-allowlist": "disabled",
])
}
}

Expand Down
4 changes: 3 additions & 1 deletion lawnchair/src/app/lawnchair/backup/ui/CreateBackupScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ fun NavGraphBuilder.createBackupGraph(route: String) {

@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun CreateBackupScreen(viewModel: CreateBackupViewModel) {
fun CreateBackupScreen(
viewModel: CreateBackupViewModel,
) {
val contents by viewModel.backupContents.collectAsState()
val screenshot by viewModel.screenshot.collectAsState()
val screenshotDone by viewModel.screenshotDone.collectAsState()
Expand Down
10 changes: 6 additions & 4 deletions lawnchair/src/app/lawnchair/backup/ui/RestoreBackupScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ fun NavGraphBuilder.restoreBackupGraph(route: String) {
viewModel.init(backupUri)
onDispose { }
}
RestoreBackupScreen(viewModel)
RestoreBackupScreen()
}
}
}

@Composable
fun RestoreBackupScreen(viewModel: RestoreBackupViewModel) {
fun RestoreBackupScreen(
viewModel: RestoreBackupViewModel = viewModel(),
) {
val isPortrait = LocalConfiguration.current.orientation == Configuration.ORIENTATION_PORTRAIT
val scrollState = rememberScrollState()
val uiState = viewModel.uiState.collectAsState().value
Expand All @@ -92,7 +94,7 @@ fun RestoreBackupScreen(viewModel: RestoreBackupViewModel) {
scrollState = if (isPortrait) null else scrollState,
) {
when (uiState) {
is RestoreBackupUiState.Success -> RestoreBackupOptions(isPortrait, uiState.backup, viewModel = viewModel)
is RestoreBackupUiState.Success -> RestoreBackupOptions(isPortrait, uiState.backup)
is RestoreBackupUiState.Loading -> {
Box(modifier = Modifier.fillMaxSize()) {
CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
Expand All @@ -115,7 +117,7 @@ fun RestoreBackupScreen(viewModel: RestoreBackupViewModel) {
fun ColumnScope.RestoreBackupOptions(
isPortrait: Boolean,
backup: LawnchairBackup,
viewModel: RestoreBackupViewModel,
viewModel: RestoreBackupViewModel = viewModel(),
) {
val backupContents = backup.info.contents
val contents by viewModel.backupContents.collectAsState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ class SleepMethodPieAccessibility(context: Context) : SleepGestureHandler.SleepM
title = R.string.dt2s_a11y_hint_title,
description = R.string.dt2s_a11y_hint,
settingsIntent = intent,
handleClose = { close(true) },
)
) { close(true) }
}
return
}
Expand Down Expand Up @@ -114,8 +113,7 @@ class SleepMethodDeviceAdmin(context: Context) : SleepGestureHandler.SleepMethod
title = R.string.dt2s_admin_hint_title,
description = R.string.dt2s_admin_hint,
settingsIntent = intent,
handleClose = { close(true) },
)
) { close(true) }
}
return
}
Expand Down
4 changes: 3 additions & 1 deletion lawnchair/src/app/lawnchair/ui/OverflowMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import androidx.compose.ui.unit.dp
import app.lawnchair.ui.preferences.components.ClickableIcon

@Composable
fun OverflowMenu(block: @Composable OverflowMenuScope.() -> Unit) {
fun OverflowMenu(
block: @Composable OverflowMenuScope.() -> Unit,
) {
val showMenu = remember { mutableStateOf(false) }
val overflowMenuScope = remember { OverflowMenuScopeImpl(showMenu) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,15 @@ private fun IconShapeClipboardPreferenceGroup(

@Composable
private fun ClipboardButton(
modifier: Modifier = Modifier,
label: String,
imageVector: ImageVector,
modifier: Modifier = Modifier,
description: String? = null,
enabled: Boolean = true,
imageVector: ImageVector,
onClick: () -> Unit,
) {
PreferenceTemplate(
modifier = modifier.clickable(enabled = enabled, onClick = onClick),
contentModifier = Modifier,
title = { Text(text = label) },
description = { description?.let { Text(text = it) } },
startWidget = {
Expand All @@ -247,12 +246,12 @@ private fun ClipboardButton(

@Composable
private fun IconShapeCornerPreference(
modifier: Modifier = Modifier,
title: String,
scale: Float,
onScaleChange: (Float) -> Unit,
cornerShape: IconCornerShape,
onScaleChange: (Float) -> Unit,
onCornerShapeChange: (IconCornerShape) -> Unit,
modifier: Modifier = Modifier,
) {
CornerSlider(
modifier = modifier,
Expand All @@ -268,12 +267,12 @@ private fun IconShapeCornerPreference(

@Composable
private fun CornerSlider(
modifier: Modifier = Modifier,
label: String,
value: Float,
onValueChange: (Float) -> Unit,
cornerShape: IconCornerShape,
onCornerShapeChange: (IconCornerShape) -> Unit,
modifier: Modifier = Modifier,
) {
val bottomSheetHandler = LocalBottomSheetHandler.current
val options = listOf<IconCornerShape>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import app.lawnchair.ui.preferences.components.PreferenceLayout
import app.lawnchair.ui.preferences.components.SliderPreference
import app.lawnchair.ui.preferences.components.SwitchPreference
import com.android.launcher3.R
import kotlinx.collections.immutable.toPersistentList

object DockRoutes {
const val SEARCH_PROVIDER = "searchProvider"
Expand Down Expand Up @@ -116,7 +117,7 @@ private fun HotseatModePreference(

ListPreference(
adapter = adapter,
entries = entries,
entries = entries.toPersistentList(),
label = stringResource(id = R.string.hotseat_mode_label),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ private enum class ContentType {
}

@Composable
fun FontSelection(fontPref: BasePreferenceManager.FontPref) {
fun FontSelection(
fontPref: BasePreferenceManager.FontPref,
) {
val context = LocalContext.current
val customFonts by remember { FontCache.INSTANCE.get(context).customFonts }.collectAsState(initial = emptyList())
val items by produceState(initialValue = emptyList<FontCache.Family>()) {
Expand Down Expand Up @@ -254,15 +256,7 @@ private fun FontSelectionItem(
endWidget = when {
selected && family.variants.size > 1 -> {
{
Row(
horizontalArrangement = Arrangement.End,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.wrapContentWidth()
.padding(end = 16.dp),
) {
VariantDropdown(adapter = adapter, family = family)
}
VariantDropdown(adapter = adapter, family = family)
}
}
onDelete != null -> {
Expand Down Expand Up @@ -298,46 +292,54 @@ private fun VariantDropdown(
adapter: PreferenceAdapter<FontCache.Font>,
family: FontCache.Family,
) {
val selectedFont = adapter.state.value
var showVariants by remember { mutableStateOf(false) }
Row(
horizontalArrangement = Arrangement.End,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.wrapContentWidth()
.padding(end = 16.dp),
) {
val selectedFont = adapter.state.value
var showVariants by remember { mutableStateOf(false) }

val context = LocalContext.current
DisposableEffect(family) {
val fontCache = FontCache.INSTANCE.get(context)
family.variants.forEach { fontCache.preloadFont(it.value) }
onDispose { }
}
val context = LocalContext.current
DisposableEffect(family) {
val fontCache = FontCache.INSTANCE.get(context)
family.variants.forEach { fontCache.preloadFont(it.value) }
onDispose { }
}

TextButton(
onClick = { showVariants = true },
colors = ButtonDefaults.textButtonColors(contentColor = MaterialTheme.colorScheme.onBackground),
contentPadding = VariantButtonContentPadding,
) {
AndroidText(
modifier = Modifier.wrapContentWidth(),
update = {
it.text = selectedFont.displayName
it.setFont(selectedFont)
},
)
Icon(
imageVector = Icons.Rounded.ArrowDropDown,
contentDescription = null,
)
}
DropdownMenu(
expanded = showVariants,
onDismissRequest = { showVariants = false },
) {
family.sortedVariants.forEach { font ->
DropdownMenuItem(onClick = {
adapter.onChange(font)
showVariants = false
}) {
Text(
text = font.displayName,
fontFamily = font.composeFontFamily,
)
TextButton(
onClick = { showVariants = true },
colors = ButtonDefaults.textButtonColors(contentColor = MaterialTheme.colorScheme.onBackground),
contentPadding = VariantButtonContentPadding,
) {
AndroidText(
modifier = Modifier.wrapContentWidth(),
update = {
it.text = selectedFont.displayName
it.setFont(selectedFont)
},
)
Icon(
imageVector = Icons.Rounded.ArrowDropDown,
contentDescription = null,
)
}
DropdownMenu(
expanded = showVariants,
onDismissRequest = { showVariants = false },
) {
family.sortedVariants.forEach { font ->
DropdownMenuItem(onClick = {
adapter.onChange(font)
showVariants = false
}) {
Text(
text = font.displayName,
fontFamily = font.composeFontFamily,
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import app.lawnchair.util.appComparator
import app.lawnchair.util.appsState
import com.android.launcher3.R
import java.util.Comparator.comparing
import kotlinx.collections.immutable.ImmutableSet
import kotlinx.collections.immutable.toPersistentSet

fun NavGraphBuilder.hiddenAppsGraph(route: String) {
preferenceGraph(route, { HiddenAppsPreferences() })
Expand All @@ -55,7 +57,7 @@ fun HiddenAppsPreferences() {
} else {
stringResource(id = R.string.hidden_apps_label_with_count, hiddenApps.size)
}
val apps by appsState(comparator = hiddenAppsComparator(hiddenApps))
val apps by appsState(comparator = hiddenAppsComparator(hiddenApps.toPersistentSet()))
val state = rememberLazyListState()
PreferenceScaffold(
label = pageTitle,
Expand Down Expand Up @@ -107,7 +109,7 @@ fun HiddenAppsPreferences() {
}

@Composable
fun hiddenAppsComparator(hiddenApps: Set<String>): Comparator<App> = remember {
fun hiddenAppsComparator(hiddenApps: ImmutableSet<String>): Comparator<App> = remember {
comparing<App, Int> {
if (hiddenApps.contains(it.key.toString())) 0 else 1
}.then(appComparator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import app.lawnchair.ui.preferences.components.SwitchPreference
import app.lawnchair.util.collectAsStateBlocking
import com.android.launcher3.R
import com.android.launcher3.Utilities
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.launch

object HomeScreenRoutes {
Expand Down Expand Up @@ -187,7 +188,7 @@ fun HomeScreenPreferences() {
fun HomeScreenTextColorPreference() {
ListPreference(
adapter = preferenceManager2().workspaceTextColor.getAdapter(),
entries = ColorMode.entries(),
entries = ColorMode.entries().toPersistentList(),
label = stringResource(id = R.string.home_screen_text_color),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import app.lawnchair.util.getThemedIconPacksInstalled
import app.lawnchair.util.isPackageInstalled
import com.android.launcher3.R
import com.google.accompanist.drawablepainter.rememberDrawablePainter
import kotlinx.collections.immutable.toPersistentList

data class IconPackInfo(
val name: String,
Expand Down Expand Up @@ -190,7 +191,7 @@ fun IconPackPreferences() {
value = it,
label = { stringResource(id = it.labelResourceId) },
)
},
}.toPersistentList(),
value = ThemedIconsState.getForSettings(
themedIcons = themedIconsAdapter.state.value,
drawerThemedIcons = drawerThemedIconsEnabled,
Expand Down Expand Up @@ -228,7 +229,6 @@ fun IconPackGrid(
val padding = 12.dp
var iconPacksLocal = iconPacks
val themedIconPacksName = themedIconPacks.map { it.name }
val modifier = Modifier.padding(bottom = 6.dp, top = 6.dp)

if (isThemedIconPack) {
iconPacksLocal = if (drawerThemedIcons) {
Expand Down Expand Up @@ -259,7 +259,7 @@ fun IconPackGrid(
state = lazyListState,
horizontalArrangement = Arrangement.spacedBy(space = padding),
contentPadding = PaddingValues(horizontal = padding),
modifier = modifier.fillMaxWidth(),
modifier = Modifier.padding(bottom = 6.dp, top = 6.dp).fillMaxWidth(),
) {
itemsIndexed(iconPacksLocal, { _, item -> item.packageName }) { index, item ->
IconPackItem(
Expand Down Expand Up @@ -300,7 +300,7 @@ private fun getIconPackItemWidth(
fun IconPackItem(
item: IconPackInfo,
selected: Boolean,
modifier: Modifier,
modifier: Modifier = Modifier,
onClick: () -> Unit,
) {
Surface(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ fun NavGraphBuilder.iconPickerGraph(route: String) {
}

@Composable
fun IconPickerPreference(packageName: String) {
fun IconPickerPreference(
packageName: String,
) {
val context = LocalContext.current
val iconPack = remember {
IconPackProvider.INSTANCE.get(context).getIconPackOrSystem(packageName)
Expand Down
4 changes: 3 additions & 1 deletion lawnchair/src/app/lawnchair/ui/preferences/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ val LocalPreferenceInteractor = staticCompositionLocalOf<PreferenceInteractor> {
}

@Composable
fun Preferences(interactor: PreferenceInteractor = viewModel<PreferenceViewModel>()) {
fun Preferences(
interactor: PreferenceInteractor = viewModel<PreferenceViewModel>(),
) {
val navController = rememberNavController()
val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl
val slideDistance = rememberSlideDistance()
Expand Down
Loading

0 comments on commit d9af42b

Please sign in to comment.