Skip to content

Commit

Permalink
Save vibration setting to data store and fix bug that vibration confi…
Browse files Browse the repository at this point in the history
…g was unused
  • Loading branch information
NiroDeveloper committed May 31, 2024
1 parent bcaadbb commit 3db0142
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ dependencies {
implementation(libs.kotlinx.coroutines.android)
implementation(libs.fragment)
implementation(libs.wear)
implementation(libs.datastore.preferences)

androidTestImplementation(platform(libs.compose.bom))
androidTestImplementation(libs.ui.test.junit4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
Expand Down Expand Up @@ -94,7 +95,7 @@ fun MainActivityLayout(permissionLauncher: ActivityResultLauncher<String>? = nul
HorizontalPager(state = pagerState) {page ->
when(page) {
0 -> RemoteLayout(permissionLauncher)
1 -> Text(text = "Devices List", modifier = Modifier.fillMaxSize())
1 -> Text(text = "Devices List", modifier = Modifier.fillMaxSize(), textAlign = TextAlign.Center)
2 -> SettingsLayout()
}
}
Expand Down
50 changes: 40 additions & 10 deletions app/src/main/java/dev/niro/cameraremote/ui/pages/SettingsPage.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.niro.cameraremote.ui.pages

import android.content.Context
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
Expand All @@ -9,25 +10,54 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.preferencesDataStore
import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
import androidx.wear.compose.foundation.lazy.rememberScalingLazyListState
import androidx.wear.compose.material.PositionIndicator
import androidx.wear.compose.material.Switch
import androidx.wear.compose.material.Text
import androidx.wear.compose.material.ToggleChip
import androidx.wear.tooling.preview.devices.WearDevices
import dev.niro.cameraremote.BuildConfig
import dev.niro.cameraremote.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch

object SettingsPage {

private val VIBRATION_ENABLED_KEY = booleanPreferencesKey("vibration_enabled")
private val Context.settingsPreferencesDataStore: DataStore<Preferences> by preferencesDataStore("settings")

var vibrationEnabled = mutableStateOf(true)

fun loadSettings(context: Context) {
CoroutineScope(Dispatchers.Default).launch {
val dataStore = context.settingsPreferencesDataStore.data.first()

dataStore[VIBRATION_ENABLED_KEY]?.let {
vibrationEnabled.value = it
}
}
}

fun writeSettings(context: Context) {
CoroutineScope(Dispatchers.Default).launch {
context.settingsPreferencesDataStore.edit { preferences ->
preferences[VIBRATION_ENABLED_KEY] = vibrationEnabled.value
}
}
}

}

@Preview(device = WearDevices.RECT, showSystemUi = true)
Expand All @@ -36,13 +66,11 @@ object SettingsPage {
@Preview(device = WearDevices.LARGE_ROUND, showSystemUi = true)
@Composable
fun SettingsLayout() {
val scalingState = rememberScalingLazyListState()

PositionIndicator(scalingLazyListState = scalingState)
val context = LocalContext.current
SettingsPage.loadSettings(context)

ScalingLazyColumn(
modifier = Modifier.fillMaxSize(),
state = scalingState
modifier = Modifier.fillMaxSize()
) {

item {
Expand All @@ -57,11 +85,14 @@ fun SettingsLayout() {
}

item {
var vibrationEnabled by remember { mutableStateOf(true) }
var vibrationEnabled by remember { SettingsPage.vibrationEnabled }

ToggleChip(
checked = vibrationEnabled,
onCheckedChange = { vibrationEnabled = it },
onCheckedChange = {
vibrationEnabled = it
SettingsPage.writeSettings(context)
},
label = { Text(stringResource(id = R.string.vibration)) },
toggleControl = { Switch(checked = vibrationEnabled) },
modifier = Modifier.fillMaxWidth()
Expand All @@ -77,6 +108,5 @@ fun SettingsLayout() {
.padding(8.dp)
)
}

}
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ kotlinxCoroutinesAndroid = "1.8.1"
activityCompose = "1.9.0"
fragment = "1.7.1" # Required for some other libs, but we have to declare a newer version ... otherwise there is a lint error.
wear = "1.3.0"
datastorePreferences = "1.1.1"

[libraries]
play-services-wearable = { group = "com.google.android.gms", name = "play-services-wearable", version.ref = "playServicesWearable" }
Expand All @@ -28,6 +29,7 @@ wear-tooling-preview = { group = "androidx.wear", name = "wear-tooling-preview",
kotlinx-coroutines-android = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-android", version.ref = "kotlinxCoroutinesAndroid" }
fragment = { group = "androidx.fragment", name = "fragment", version.ref = "fragment" }
wear = { group = "androidx.wear", name = "wear", version.ref = "wear" }
datastore-preferences = { group = "androidx.datastore", name = "datastore-preferences", version.ref = "datastorePreferences" }

[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
Expand Down

0 comments on commit 3db0142

Please sign in to comment.