-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Gaëtan Muller <[email protected]>
- Loading branch information
Showing
19 changed files
with
820 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...rbox-demo-shared/src/main/java/ch/srgssr/pillarbox/demo/shared/ui/settings/AppSettings.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) SRG SSR. All rights reserved. | ||
* License information is available from the LICENSE file. | ||
*/ | ||
package ch.srgssr.pillarbox.demo.shared.ui.settings | ||
|
||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.unit.TextUnit | ||
import androidx.compose.ui.unit.sp | ||
|
||
/** | ||
* App settings | ||
* | ||
* @property metricsOverlayEnabled | ||
* @property metricsOverlayTextSize | ||
* @property metricsOverlayTextColor | ||
*/ | ||
class AppSettings( | ||
val metricsOverlayEnabled: Boolean = false, | ||
val metricsOverlayTextSize: TextSize = TextSize.Medium, | ||
val metricsOverlayTextColor: TextColor = TextColor.Yellow, | ||
) { | ||
|
||
/** | ||
* Text size | ||
* | ||
* @property size the [TextUnit]. | ||
*/ | ||
enum class TextSize(val size: TextUnit) { | ||
Small(8.sp), | ||
Medium(12.sp), | ||
Large(18.sp), | ||
} | ||
|
||
/** | ||
* Text color | ||
* | ||
* @property color the [Color]. | ||
*/ | ||
enum class TextColor(val color: Color) { | ||
Yellow(Color.Yellow), | ||
Red(Color.Red), | ||
Green(Color.Green), | ||
Blue(Color.Blue), | ||
White(Color.White) | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
...shared/src/main/java/ch/srgssr/pillarbox/demo/shared/ui/settings/AppSettingsRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright (c) SRG SSR. All rights reserved. | ||
* License information is available from the LICENSE file. | ||
*/ | ||
package ch.srgssr.pillarbox.demo.shared.ui.settings | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.booleanPreferencesKey | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.emptyPreferences | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import androidx.datastore.preferences.preferencesDataStore | ||
import ch.srgssr.pillarbox.demo.shared.ui.settings.AppSettings.TextColor | ||
import ch.srgssr.pillarbox.demo.shared.ui.settings.AppSettings.TextSize | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.map | ||
import java.io.IOException | ||
|
||
private val Context.dataStore by preferencesDataStore(name = "settings") | ||
|
||
/** | ||
* App settings repository | ||
* @param context The context. | ||
*/ | ||
class AppSettingsRepository(context: Context) { | ||
private val dataStore = context.dataStore | ||
|
||
/** | ||
* Get app settings | ||
* | ||
* @return | ||
*/ | ||
fun getAppSettings(): Flow<AppSettings> { | ||
return dataStore.data | ||
.catch { | ||
if (it is IOException) { | ||
emit(emptyPreferences()) | ||
} else { | ||
throw it | ||
} | ||
} | ||
.map { preferences -> | ||
AppSettings( | ||
metricsOverlayTextSize = preferences.getEnum(PreferencesKeys.METRICS_OVERLAY_TEXT_SIZE, TextSize.Medium), | ||
metricsOverlayTextColor = preferences.getEnum(PreferencesKeys.METRICS_OVERLAY_TEXT_COLOR, TextColor.Yellow), | ||
metricsOverlayEnabled = preferences[PreferencesKeys.METRICS_OVERLAY_ENABLED] ?: false, | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* Set metrics overlay enabled | ||
* | ||
* @param enabled | ||
*/ | ||
suspend fun setMetricsOverlayEnabled(enabled: Boolean) { | ||
dataStore.edit { | ||
it[PreferencesKeys.METRICS_OVERLAY_ENABLED] = enabled | ||
} | ||
} | ||
|
||
/** | ||
* Set metrics overlay text color | ||
* | ||
* @param textColor | ||
*/ | ||
suspend fun setMetricsOverlayTextColor(textColor: TextColor) { | ||
dataStore.edit { | ||
it[PreferencesKeys.METRICS_OVERLAY_TEXT_COLOR] = textColor.name | ||
} | ||
} | ||
|
||
/** | ||
* Set metrics overlay text size | ||
* | ||
* @param textSize | ||
*/ | ||
suspend fun setMetricsOverlayTextSize(textSize: TextSize) { | ||
dataStore.edit { | ||
it[PreferencesKeys.METRICS_OVERLAY_TEXT_SIZE] = textSize.name | ||
} | ||
} | ||
|
||
private object PreferencesKeys { | ||
val METRICS_OVERLAY_ENABLED = booleanPreferencesKey("metrics_overlay_enabled") | ||
val METRICS_OVERLAY_TEXT_COLOR = stringPreferencesKey("metrics_overlay_text_color") | ||
val METRICS_OVERLAY_TEXT_SIZE = stringPreferencesKey("metrics_overlay_text_size") | ||
} | ||
|
||
private companion object { | ||
private const val TAG = "AppSettingsRepository" | ||
|
||
private inline fun <reified T : Enum<T>> Preferences.getEnum( | ||
key: Preferences.Key<String>, | ||
defaultValue: T, | ||
): T { | ||
return try { | ||
get(key)?.let { enumValueOf<T>(it) } ?: defaultValue | ||
} catch (e: IllegalArgumentException) { | ||
Log.w(TAG, "Can't parse enum value", e) | ||
defaultValue | ||
} | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...-shared/src/main/java/ch/srgssr/pillarbox/demo/shared/ui/settings/AppSettingsViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) SRG SSR. All rights reserved. | ||
* License information is available from the LICENSE file. | ||
*/ | ||
package ch.srgssr.pillarbox.demo.shared.ui.settings | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.launch | ||
|
||
/** | ||
* App settings view model | ||
* | ||
* @param appSettingsRepository | ||
*/ | ||
class AppSettingsViewModel(private val appSettingsRepository: AppSettingsRepository) : ViewModel() { | ||
|
||
/** | ||
* Current app settings | ||
*/ | ||
val currentAppSettings = appSettingsRepository.getAppSettings() | ||
|
||
/** | ||
* Set metrics overlay enabled | ||
* | ||
* @param enabled | ||
*/ | ||
fun setMetricsOverlayEnabled(enabled: Boolean) { | ||
viewModelScope.launch { | ||
appSettingsRepository.setMetricsOverlayEnabled(enabled) | ||
} | ||
} | ||
|
||
/** | ||
* Set metrics overlay text color | ||
* | ||
* @param textColor | ||
*/ | ||
fun setMetricsOverlayTextColor(textColor: AppSettings.TextColor) { | ||
viewModelScope.launch { | ||
appSettingsRepository.setMetricsOverlayTextColor(textColor) | ||
} | ||
} | ||
|
||
/** | ||
* Set metrics overlay text size | ||
* | ||
* @param textSize | ||
*/ | ||
fun setMetricsOverlayTextSize(textSize: AppSettings.TextSize) { | ||
viewModelScope.launch { | ||
appSettingsRepository.setMetricsOverlayTextSize(textSize) | ||
} | ||
} | ||
|
||
/** | ||
* Factory | ||
* | ||
* @param appSettingsRepository | ||
*/ | ||
class Factory(private val appSettingsRepository: AppSettingsRepository) : ViewModelProvider.Factory { | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
return AppSettingsViewModel(appSettingsRepository) as T | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...shared/src/main/java/ch/srgssr/pillarbox/demo/shared/ui/settings/MetricsOverlayOptions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright (c) SRG SSR. All rights reserved. | ||
* License information is available from the LICENSE file. | ||
*/ | ||
package ch.srgssr.pillarbox.demo.shared.ui.settings | ||
|
||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.unit.TextUnit | ||
|
||
/** | ||
* Metrics overlay options | ||
* | ||
* @property textColor The [Color] for the text overlay. | ||
* @property textSize The [TextUnit] for the text overlay. | ||
*/ | ||
|
||
data class MetricsOverlayOptions( | ||
val textColor: Color = Color.Yellow, | ||
val textSize: TextUnit = TextUnit.Unspecified, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.