Skip to content

Commit

Permalink
chore: save/load default time format #63
Browse files Browse the repository at this point in the history
  • Loading branch information
vishal2376 committed Apr 14, 2024
1 parent f5d1eb8 commit f999e0d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import java.time.LocalTime
sealed class MainEvent {
data class UpdateAppTheme(val theme: AppTheme, val context: Context) : MainEvent()
data class UpdateTimePicker(val isWheelTimePicker: Boolean, val context: Context) : MainEvent()
data class UpdateTimeFormat(val isTimeFormat: Boolean, val context: Context) : MainEvent()
data class UpdateSleepTime(val sleepTime: LocalTime, val context: Context) : MainEvent()
data class UpdateLanguage(val language: String, val context: Context) : MainEvent()
data class UpdateSortByTask(val sortTask: SortTask, val context: Context) : MainEvent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ data class MainState(
val sleepTime: LocalTime = LocalTime.of(23, 59),
val language: String = Locale.ENGLISH.language,
val isWheelTimePicker: Boolean = true,
val is24hourTimeFormat: Boolean = true,
val is24hourTimeFormat: Boolean = false,
val calenderView: CalenderView = CalenderView.MONTHLY,
val calenderDate: LocalDate? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ class TaskViewModel @Inject constructor(private val repository: TaskRepository)
}
}

is MainEvent.UpdateTimeFormat -> {
viewModelScope.launch {
appState = appState.copy(is24hourTimeFormat = event.isTimeFormat)
SettingsStore(event.context).setTimeFormat(event.isTimeFormat)
}
}

is MainEvent.UpdateSleepTime -> {
viewModelScope.launch {
appState = appState.copy(sleepTime = event.sleepTime)
Expand Down Expand Up @@ -354,12 +361,19 @@ class TaskViewModel @Inject constructor(private val repository: TaskRepository)
appState = appState.copy(calenderView = CalenderView.entries[it])
}
}

viewModelScope.launch {
settingsStore.timeFormatKey.collect {
appState = appState.copy(is24hourTimeFormat = it)
}
}

viewModelScope.launch {
settingsStore.sortTaskKey.collect {
appState = appState.copy(sortBy = SortTask.entries[it])
}
}

viewModelScope.launch {
settingsStore.lastOpenedKey.collect { lastDateString ->
if (lastDateString == "") {
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/vishal2376/snaptick/util/SettingsStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SettingsStore(val context: Context) {

private val THEME_KEY = intPreferencesKey("theme_key")
private val TIME_PICKER_KEY = booleanPreferencesKey("time_picker_key")
private val TIME_FORMAT_KEY = booleanPreferencesKey("time_format_key")
private val LANGUAGE_KEY = stringPreferencesKey("language_key")
private val SORT_TASK_KEY = intPreferencesKey("sort_task_key")
private val CALENDER_VIEW_KEY = intPreferencesKey("calender_view_key")
Expand All @@ -34,6 +35,7 @@ class SettingsStore(val context: Context) {
private const val DEFAULT_LANGUAGE = "en"
private val DEFAULT_THEME = AppTheme.Dark.ordinal
private const val DEFAULT_TIME_PICKER_KEY = true
private const val DEFAULT_TIME_FORMAT = false
private val DEFAULT_CALENDER_VIEW = CalenderView.MONTHLY.ordinal
private val DEFAULT_SORT_TASK = SortTask.BY_CREATE_TIME_DESCENDING.ordinal
private val DEFAULT_LAST_OPENED = LocalDate.now().toString()
Expand All @@ -50,6 +52,10 @@ class SettingsStore(val context: Context) {
preferences[TIME_PICKER_KEY] ?: DEFAULT_TIME_PICKER_KEY
}

val timeFormatKey: Flow<Boolean> = context.dataStore.data.map { preferences ->
preferences[TIME_FORMAT_KEY] ?: DEFAULT_TIME_FORMAT
}

val languageKey: Flow<String> = context.dataStore.data.map { preferences ->
preferences[LANGUAGE_KEY] ?: DEFAULT_LANGUAGE
}
Expand Down Expand Up @@ -86,6 +92,12 @@ class SettingsStore(val context: Context) {
}
}

suspend fun setTimeFormat(is24hourFormat: Boolean) {
context.dataStore.edit { preferences ->
preferences[TIME_FORMAT_KEY] = is24hourFormat
}
}

suspend fun setLanguage(language: String) {
context.dataStore.edit { preferences ->
preferences[LANGUAGE_KEY] = language
Expand Down

0 comments on commit f999e0d

Please sign in to comment.