Skip to content

Commit

Permalink
Refactor haptic preference
Browse files Browse the repository at this point in the history
  • Loading branch information
sadellie committed Jan 21, 2024
1 parent 8b526e1 commit bddd4c0
Show file tree
Hide file tree
Showing 23 changed files with 266 additions and 280 deletions.
100 changes: 50 additions & 50 deletions app/src/main/java/com/sadellie/unitto/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,61 +67,61 @@ internal fun App(prefs: AppPreferences?) {
}
}

if (prefs != null) {
val themmoController = remember(prefs) {
ThemmoController(
lightColorScheme = LightThemeColors,
darkColorScheme = DarkThemeColors,
themingMode = prefs.themingMode,
dynamicThemeEnabled = prefs.enableDynamicTheme,
amoledThemeEnabled = prefs.enableAmoledTheme,
customColor = prefs.customColor.toColor(),
monetMode = prefs.monetMode
)
}
if (prefs == null) return

Themmo(
themmoController = themmoController,
typography = TypographySystem,
animationSpec = tween(250)
) {
val backgroundColor = MaterialTheme.colorScheme.background
val useDarkIcons = remember(backgroundColor) { backgroundColor.luminance() > 0.5f }

NavigationDrawer(
modifier = Modifier,
state = drawerState,
gesturesEnabled = gesturesEnabled,
tabs = DrawerItem.main,
currentDestination = navBackStackEntry?.destination?.route,
onItemClick = { destination ->
drawerScope.launch { drawerState.close() }

navController.navigate(destination.graph) {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
val themmoController = remember(prefs) {
ThemmoController(
lightColorScheme = LightThemeColors,
darkColorScheme = DarkThemeColors,
themingMode = prefs.themingMode,
dynamicThemeEnabled = prefs.enableDynamicTheme,
amoledThemeEnabled = prefs.enableAmoledTheme,
customColor = prefs.customColor.toColor(),
monetMode = prefs.monetMode
)
}

Themmo(
themmoController = themmoController,
typography = TypographySystem,
animationSpec = tween(250)
) {
val backgroundColor = MaterialTheme.colorScheme.background
val useDarkIcons = remember(backgroundColor) { backgroundColor.luminance() > 0.5f }

shortcutsScope.launch { mContext.pushDynamicShortcut(destination) }
},
content = {
UnittoNavigation(
navController = navController,
themmoController = it,
startDestination = prefs.startingScreen,
rpnMode = prefs.rpnMode,
openDrawer = { drawerScope.launch { drawerState.open() } }
)
NavigationDrawer(
modifier = Modifier,
state = drawerState,
gesturesEnabled = gesturesEnabled,
tabs = DrawerItem.main,
currentDestination = navBackStackEntry?.destination?.route,
onItemClick = { destination ->
drawerScope.launch { drawerState.close() }

navController.navigate(destination.graph) {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
)

LaunchedEffect(useDarkIcons) {
sysUiController.setNavigationBarColor(Color.Transparent, useDarkIcons)
sysUiController.setStatusBarColor(Color.Transparent, useDarkIcons)
shortcutsScope.launch { mContext.pushDynamicShortcut(destination) }
},
content = {
UnittoNavigation(
navController = navController,
themmoController = it,
startDestination = prefs.startingScreen,
rpnMode = prefs.rpnMode,
openDrawer = { drawerScope.launch { drawerState.open() } }
)
}
)

LaunchedEffect(useDarkIcons) {
sysUiController.setNavigationBarColor(Color.Transparent, useDarkIcons)
sysUiController.setStatusBarColor(Color.Transparent, useDarkIcons)
}
}

Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/sadellie/unitto/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.compose.ui.platform.LocalConfiguration
import androidx.core.os.ConfigurationCompat
import androidx.core.view.WindowCompat
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.sadellie.unitto.core.ui.LocalHapticPreference
import com.sadellie.unitto.core.ui.LocalLocale
import com.sadellie.unitto.core.ui.LocalWindowSize
import com.sadellie.unitto.core.ui.calculateWindowSizeClass
Expand Down Expand Up @@ -69,7 +70,8 @@ internal class MainActivity : AppCompatActivity() {
CompositionLocalProvider(
LocalLocale provides locale,
LocalWindowSize provides calculateWindowSizeClass(this@MainActivity),
LocalNumberTypography provides numbersTypography
LocalNumberTypography provides numbersTypography,
LocalHapticPreference provides (prefs?.enableVibrations ?: true)
) {
App(prefs)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2024 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.sadellie.unitto.core.ui

import androidx.compose.runtime.compositionLocalOf

val LocalHapticPreference = compositionLocalOf {
true
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalView
import com.sadellie.unitto.core.ui.LocalHapticPreference
import kotlinx.coroutines.launch

@Composable
Expand All @@ -48,10 +49,10 @@ fun BasicKeyboardButton(
icon: ImageVector,
contentDescription: String?,
iconColor: Color,
allowVibration: Boolean,
) {
val view = LocalView.current
val coroutineScope = rememberCoroutineScope()
val allowVibration = LocalHapticPreference.current
fun vibrate() {
if (allowVibration) {
coroutineScope.launch {
Expand Down Expand Up @@ -93,7 +94,6 @@ fun KeyboardButtonLight(
modifier: Modifier,
icon: ImageVector,
contentDescription: String?,
allowVibration: Boolean,
contentHeight: Float,
onLongClick: (() -> Unit)? = null,
onClick: () -> Unit,
Expand All @@ -107,7 +107,6 @@ fun KeyboardButtonLight(
icon = icon,
contentDescription = contentDescription,
iconColor = MaterialTheme.colorScheme.onSurfaceVariant,
allowVibration = allowVibration,
)
}

Expand All @@ -116,7 +115,6 @@ fun KeyboardButtonFilled(
modifier: Modifier,
icon: ImageVector,
contentDescription: String?,
allowVibration: Boolean,
contentHeight: Float,
onLongClick: (() -> Unit)? = null,
onClick: () -> Unit,
Expand All @@ -130,7 +128,6 @@ fun KeyboardButtonFilled(
icon = icon,
contentDescription = contentDescription,
iconColor = MaterialTheme.colorScheme.onPrimaryContainer,
allowVibration = allowVibration,
)
}

Expand All @@ -139,7 +136,6 @@ fun KeyboardButtonAdditional(
modifier: Modifier,
icon: ImageVector,
contentDescription: String?,
allowVibration: Boolean,
contentHeight: Float,
onLongClick: (() -> Unit)? = null,
onClick: () -> Unit,
Expand All @@ -153,7 +149,6 @@ fun KeyboardButtonAdditional(
icon = icon,
contentDescription = contentDescription,
iconColor = MaterialTheme.colorScheme.onSurfaceVariant,
allowVibration = allowVibration,
)
}

Expand All @@ -162,7 +157,6 @@ fun KeyboardButtonTertiary(
modifier: Modifier,
icon: ImageVector,
contentDescription: String?,
allowVibration: Boolean,
contentHeight: Float,
onLongClick: (() -> Unit)? = null,
onClick: () -> Unit,
Expand All @@ -176,7 +170,6 @@ fun KeyboardButtonTertiary(
icon = icon,
contentDescription = contentDescription,
iconColor = MaterialTheme.colorScheme.onTertiaryContainer,
allowVibration = allowVibration,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2023 Elshan Agaev
* Copyright (c) 2023-2024 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -20,5 +20,4 @@ package com.sadellie.unitto.data.model.userprefs

interface AddSubtractPreferences{
val separator: Int
val enableVibrations: Boolean
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2023 Elshan Agaev
* Copyright (c) 2023-2024 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -31,4 +31,5 @@ interface AppPreferences {
val enableToolsExperiment: Boolean
val systemFont: Boolean
val rpnMode: Boolean
val enableVibrations: Boolean
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2023 Elshan Agaev
* Copyright (c) 2023-2024 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -20,5 +20,4 @@ package com.sadellie.unitto.data.model.userprefs

interface BodyMassPreferences{
val separator: Int
val enableVibrations: Boolean
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2023 Elshan Agaev
* Copyright (c) 2023-2024 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -20,7 +20,6 @@ package com.sadellie.unitto.data.model.userprefs

interface CalculatorPreferences {
val radianMode: Boolean
val enableVibrations: Boolean
val separator: Int
val middleZero: Boolean
val acButton: Boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2023 Elshan Agaev
* Copyright (c) 2023-2024 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -22,7 +22,6 @@ import com.sadellie.unitto.data.model.UnitGroup
import com.sadellie.unitto.data.model.UnitsListSorting

interface ConverterPreferences {
val enableVibrations: Boolean
val separator: Int
val middleZero: Boolean
val acButton: Boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2023 Elshan Agaev
* Copyright (c) 2023-2024 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -45,6 +45,7 @@ data class AppPreferencesImpl(
override val enableToolsExperiment: Boolean,
override val systemFont: Boolean,
override val rpnMode: Boolean,
override val enableVibrations: Boolean,
) : AppPreferences

data class GeneralPreferencesImpl(
Expand All @@ -54,7 +55,6 @@ data class GeneralPreferencesImpl(

data class CalculatorPreferencesImpl(
override val radianMode: Boolean,
override val enableVibrations: Boolean,
override val separator: Int,
override val middleZero: Boolean,
override val acButton: Boolean,
Expand All @@ -64,7 +64,6 @@ data class CalculatorPreferencesImpl(
) : CalculatorPreferences

data class ConverterPreferencesImpl(
override val enableVibrations: Boolean,
override val separator: Int,
override val middleZero: Boolean,
override val acButton: Boolean,
Expand Down Expand Up @@ -97,12 +96,10 @@ data class UnitGroupsPreferencesImpl(

data class AddSubtractPreferencesImpl(
override val separator: Int,
override val enableVibrations: Boolean,
) : AddSubtractPreferences

data class BodyMassPreferencesImpl(
override val separator: Int,
override val enableVibrations: Boolean,
) : BodyMassPreferences

data class AboutPreferencesImpl(
Expand Down
Loading

0 comments on commit bddd4c0

Please sign in to comment.