Skip to content

Commit

Permalink
haptics setting added
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Dec 31, 2023
1 parent 9ee0c31 commit 7d04222
Show file tree
Hide file tree
Showing 16 changed files with 288 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ object Keys {
val DRAW_SLIDER_SHADOWS = booleanPreferencesKey("DRAW_SLIDER_SHADOWS")
val DRAW_APPBAR_SHADOWS = booleanPreferencesKey("DRAW_APPBAR_SHADOWS")
val COPY_TO_CLIPBOARD = booleanPreferencesKey("COPY_TO_CLIPBOARD")
val VIBRATION_STRENGTH = intPreferencesKey("VIBRATION_STRENGTH")
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import ru.tech.imageresizershrinker.data.keys.Keys.SELECTED_FONT_INDEX
import ru.tech.imageresizershrinker.data.keys.Keys.SHOW_UPDATE_DIALOG
import ru.tech.imageresizershrinker.data.keys.Keys.THEME_CONTRAST_LEVEL
import ru.tech.imageresizershrinker.data.keys.Keys.THEME_STYLE
import ru.tech.imageresizershrinker.data.keys.Keys.VIBRATION_STRENGTH
import ru.tech.imageresizershrinker.domain.model.FontFam
import ru.tech.imageresizershrinker.domain.model.NightMode
import ru.tech.imageresizershrinker.domain.model.Preset
Expand All @@ -76,58 +77,7 @@ class SettingsRepositoryImpl @Inject constructor(

private val default = SettingsState.Default()

override suspend fun getSettingsState(): SettingsState {
val prefs = dataStore.data.first()
return SettingsState(
nightMode = NightMode.fromOrdinal(prefs[NIGHT_MODE]),
isDynamicColors = prefs[DYNAMIC_COLORS] ?: default.isDynamicColors,
isAmoledMode = prefs[AMOLED_MODE] ?: default.isAmoledMode,
appColorTuple = prefs[APP_COLOR_TUPLE] ?: default.appColorTuple,
borderWidth = prefs[BORDER_WIDTH] ?: default.borderWidth,
showDialogOnStartup = prefs[SHOW_UPDATE_DIALOG] ?: default.showDialogOnStartup,
selectedEmoji = prefs[SELECTED_EMOJI_INDEX] ?: default.selectedEmoji,
screenList = default.screenList,
emojisCount = prefs[EMOJI_COUNT] ?: default.emojisCount,
clearCacheOnLaunch = prefs[AUTO_CACHE_CLEAR] ?: default.clearCacheOnLaunch,
groupOptionsByTypes = prefs[GROUP_OPTIONS_BY_TYPE] ?: default.groupOptionsByTypes,
allowChangeColorByImage = default.allowChangeColorByImage,
presets = default.presets,
fabAlignment = default.fabAlignment,
imagePickerModeInt = default.imagePickerModeInt,
colorTupleList = default.colorTupleList,
addSequenceNumber = default.addSequenceNumber,
saveFolderUri = default.saveFolderUri,
filenamePrefix = default.filenamePrefix,
addSizeInFilename = default.addSizeInFilename,
addOriginalFilename = default.addOriginalFilename,
randomizeFilename = default.randomizeFilename,
font = FontFam.fromOrdinal(prefs[SELECTED_FONT_INDEX]),
fontScale = (prefs[FONT_SCALE] ?: 1f).takeIf { it > 0f },
allowCollectCrashlytics = prefs[ALLOW_CRASHLYTICS] ?: default.allowCollectCrashlytics,
allowCollectAnalytics = prefs[ALLOW_ANALYTICS] ?: default.allowCollectAnalytics,
allowBetas = prefs[ALLOW_BETAS] ?: default.allowBetas,
drawContainerShadows = prefs[DRAW_CONTAINER_SHADOWS]
?: default.drawContainerShadows,
drawFabShadows = prefs[DRAW_FAB_SHADOWS]
?: default.drawFabShadows,
drawSwitchShadows = prefs[DRAW_SWITCH_SHADOWS]
?: default.drawSwitchShadows,
drawSliderShadows = prefs[DRAW_SLIDER_SHADOWS]
?: default.drawSliderShadows,
drawButtonShadows = prefs[DRAW_BUTTON_SHADOWS]
?: default.drawButtonShadows,
drawAppBarShadows = prefs[DRAW_APPBAR_SHADOWS]
?: default.drawAppBarShadows,
appOpenCount = prefs[APP_OPEN_COUNT] ?: default.appOpenCount,
aspectRatios = default.aspectRatios,
lockDrawOrientation = prefs[LOCK_DRAW_ORIENTATION] ?: default.lockDrawOrientation,
themeContrastLevel = prefs[THEME_CONTRAST_LEVEL] ?: default.themeContrastLevel,
themeStyle = prefs[THEME_STYLE] ?: default.themeStyle,
isInvertThemeColors = prefs[INVERT_THEME] ?: default.isInvertThemeColors,
screensSearchEnabled = prefs[SCREEN_SEARCH_ENABLED] ?: default.screensSearchEnabled,
autoCopyToClipBoard = prefs[COPY_TO_CLIPBOARD] ?: default.autoCopyToClipBoard
)
}
override suspend fun getSettingsState(): SettingsState = getSettingsStateFlow().first()

override fun getSettingsStateFlow(): Flow<SettingsState> = dataStore.data.map { prefs ->
SettingsState(
Expand Down Expand Up @@ -180,7 +130,8 @@ class SettingsRepositoryImpl @Inject constructor(
themeStyle = prefs[THEME_STYLE] ?: default.themeStyle,
isInvertThemeColors = prefs[INVERT_THEME] ?: default.isInvertThemeColors,
screensSearchEnabled = prefs[SCREEN_SEARCH_ENABLED] ?: default.screensSearchEnabled,
autoCopyToClipBoard = prefs[COPY_TO_CLIPBOARD] ?: default.autoCopyToClipBoard
autoCopyToClipBoard = prefs[COPY_TO_CLIPBOARD] ?: default.autoCopyToClipBoard,
hapticsStrength = prefs[VIBRATION_STRENGTH] ?: default.hapticsStrength
)
}

Expand Down Expand Up @@ -510,6 +461,12 @@ class SettingsRepositoryImpl @Inject constructor(
}
}

override suspend fun setVibrationStrength(strength: Int) {
dataStore.edit {
it[VIBRATION_STRENGTH] = strength
}
}

private fun InputStream.toByteArray(): ByteArray {
val bytes = ByteArray(this.available())
val dis = DataInputStream(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ data class SettingsState(
val themeStyle: Int,
val isInvertThemeColors: Boolean,
val screensSearchEnabled: Boolean,
val autoCopyToClipBoard: Boolean
val autoCopyToClipBoard: Boolean,
val hapticsStrength: Int
) : Domain {

companion object {
Expand Down Expand Up @@ -88,7 +89,8 @@ data class SettingsState(
themeStyle = 0,
isInvertThemeColors = false,
screensSearchEnabled = false,
autoCopyToClipBoard = false
autoCopyToClipBoard = false,
hapticsStrength = 1
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,6 @@ interface SettingsRepository {
suspend fun toggleDrawAppBarShadows()

suspend fun toggleAutoPinClipboard()

suspend fun setVibrationStrength(strength: Int)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.tech.imageresizershrinker.domain.use_case.edit_settings

import ru.tech.imageresizershrinker.domain.repository.SettingsRepository
import javax.inject.Inject

class SetVibrationStrengthUseCase @Inject constructor(
private val settingsRepository: SettingsRepository
) {
suspend operator fun invoke(strength: Int) = settingsRepository.setVibrationStrength(strength)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ru.tech.imageresizershrinker.presentation.main_screen

import android.annotation.SuppressLint
import android.content.Intent
import android.content.res.Configuration
import android.os.Build
Expand All @@ -23,6 +22,7 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.core.view.WindowInsetsControllerCompat
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -45,6 +45,7 @@ import ru.tech.imageresizershrinker.presentation.root.utils.helper.ContextUtils.
import ru.tech.imageresizershrinker.presentation.root.utils.navigation.LocalNavController
import ru.tech.imageresizershrinker.presentation.root.widget.activity.M3Activity
import ru.tech.imageresizershrinker.presentation.root.widget.controls.EnhancedSliderInit
import ru.tech.imageresizershrinker.presentation.root.widget.haptics.customHapticFeedback
import ru.tech.imageresizershrinker.presentation.root.widget.other.LocalToastHost
import ru.tech.imageresizershrinker.presentation.root.widget.other.ToastHost
import ru.tech.imageresizershrinker.presentation.root.widget.other.rememberToastHostState
Expand Down Expand Up @@ -76,7 +77,8 @@ class MainActivity : M3Activity() {
LocalNavController provides viewModel.navController,
LocalEditPresetsState provides editPresetsState,
LocalConfettiController provides rememberToastHostState(),
LocalImageLoader provides viewModel.imageLoader
LocalImageLoader provides viewModel.imageLoader,
LocalHapticFeedback provides customHapticFeedback(viewModel.settingsState.hapticsStrength)
) {
val showSelectSheet = rememberSaveable(viewModel.showSelectDialog) {
mutableStateOf(viewModel.showSelectDialog)
Expand Down Expand Up @@ -183,7 +185,6 @@ class MainActivity : M3Activity() {
}
}

@SuppressLint("MissingSuperCall")
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
parseImage(intent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.sizeIn
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.systemBars
Expand Down Expand Up @@ -283,23 +282,22 @@ fun MainScreen(
.background(MaterialTheme.colorScheme.surfaceColorAtElevation(1.dp))
.drawHorizontalStroke(),
actions = {
EnhancedIconButton(
containerColor = Color.Transparent,
contentColor = LocalContentColor.current,
enableAutoShadowAndBorder = false,
onClick = {
if (!showSettingsSearch) {
showSettingsSearch = true
} else {
settingsSearchKeyword = ""
AnimatedContent(
targetState = showSettingsSearch to settingsSearchKeyword.isNotEmpty(),
transitionSpec = { fadeIn() + scaleIn() togetherWith fadeOut() + scaleOut() }
) { (searching, hasSearchKey) ->
EnhancedIconButton(
containerColor = Color.Transparent,
contentColor = LocalContentColor.current,
enableAutoShadowAndBorder = false,
onClick = {
if (!showSettingsSearch) {
showSettingsSearch = true
} else {
settingsSearchKeyword = ""
}
}
},
modifier = Modifier.size(48.dp)
) {
AnimatedContent(
targetState = showSettingsSearch to settingsSearchKeyword.isNotEmpty(),
transitionSpec = { fadeIn() + scaleIn() togetherWith fadeOut() + scaleOut() }
) { (searching, hasSearchKey) ->
) {
if (searching && hasSearchKey) {
Icon(Icons.Rounded.Close, null)
} else if (!searching) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,9 @@ sealed class Setting(
title = R.string.auto_pin,
subtitle = R.string.auto_pin_sub
)

data object VibrationStrength : Setting(
title = R.string.vibration_strength,
subtitle = null
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -389,5 +389,9 @@ fun SettingItem(
Setting.AutoPinClipboard -> {
AutoPinClipboardSettingItem(onClick = { viewModel.toggleAutoPinClipboard() })
}

Setting.VibrationStrength -> {
VibrationStrengthSettingItem(onValueChange = viewModel::setVibrationStrength)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.material.icons.rounded.ShieldMoon
import androidx.compose.material.icons.rounded.SystemSecurityUpdate
import androidx.compose.material.icons.rounded.TableRows
import androidx.compose.material.icons.rounded.TextFormat
import androidx.compose.material.icons.rounded.Vibration
import androidx.compose.material.icons.twotone.Palette
import androidx.compose.ui.graphics.vector.ImageVector
import ru.tech.imageresizershrinker.R
Expand Down Expand Up @@ -220,6 +221,15 @@ sealed class SettingsGroup(
initialState = false
)

data object Haptics : SettingsGroup(
icon = Icons.Rounded.Vibration,
titleId = R.string.vibration,
settingsList = listOf(
Setting.VibrationStrength
),
initialState = false
)

companion object {
val entries: List<SettingsGroup> by lazy {
listOf(
Expand All @@ -228,6 +238,7 @@ sealed class SettingsGroup(
SecondaryCustomization,
NightMode,
Shadows,
Haptics,
Font,
OptionsArrangement,
Presets,
Expand Down
Loading

0 comments on commit 7d04222

Please sign in to comment.