Skip to content

Commit

Permalink
chore: save/load calender view #62
Browse files Browse the repository at this point in the history
  • Loading branch information
vishal2376 committed Apr 11, 2024
1 parent c139bf5 commit 48f1595
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.vishal2376.snaptick.presentation.common

enum class CalenderView {
WEEKLY,
MONTHLY
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.vishal2376.snaptick.presentation.main

import android.content.Context
import com.vishal2376.snaptick.presentation.common.AppTheme
import com.vishal2376.snaptick.presentation.common.CalenderView
import com.vishal2376.snaptick.presentation.common.NavDrawerItem
import com.vishal2376.snaptick.presentation.common.SortTask
import java.time.LocalDate
Expand All @@ -13,6 +14,9 @@ sealed class 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()
data class UpdateCalenderView(val calenderView: CalenderView, val context: Context) :
MainEvent()

data class UpdateCalenderDate(val date: LocalDate?) : MainEvent()
data class OnClickNavDrawerItem(val context: Context, val item: NavDrawerItem) : MainEvent()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.vishal2376.snaptick.presentation.main

import com.vishal2376.snaptick.presentation.common.AppTheme
import com.vishal2376.snaptick.presentation.common.CalenderView
import com.vishal2376.snaptick.presentation.common.SortTask
import java.time.LocalDate
import java.time.LocalTime
Expand All @@ -16,5 +17,6 @@ data class MainState(
val sleepTime: LocalTime = LocalTime.of(23, 59),
val language: String = Locale.ENGLISH.language,
val isWheelTimePicker: Boolean = true,
val calenderView: CalenderView = CalenderView.MONTHLY,
val calenderDate: LocalDate? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.vishal2376.snaptick.data.repositories.TaskRepository
import com.vishal2376.snaptick.domain.model.Task
import com.vishal2376.snaptick.presentation.add_edit_screen.AddEditScreenEvent
import com.vishal2376.snaptick.presentation.common.AppTheme
import com.vishal2376.snaptick.presentation.common.CalenderView
import com.vishal2376.snaptick.presentation.common.NavDrawerItem
import com.vishal2376.snaptick.presentation.common.SortTask
import com.vishal2376.snaptick.presentation.home_screen.HomeScreenEvent
Expand Down Expand Up @@ -102,6 +103,13 @@ class TaskViewModel @Inject constructor(private val repository: TaskRepository)
}
}

is MainEvent.UpdateCalenderView -> {
viewModelScope.launch {
appState = appState.copy(calenderView = event.calenderView)
SettingsStore(event.context).setCalenderView(event.calenderView.ordinal)
}
}

is MainEvent.UpdateCalenderDate -> {
appState = appState.copy(calenderDate = event.date)
}
Expand Down Expand Up @@ -341,6 +349,12 @@ class TaskViewModel @Inject constructor(private val repository: TaskRepository)
}
}

viewModelScope.launch {
settingsStore.calenderViewKey.collect {
appState = appState.copy(calenderView = CalenderView.entries[it])
}
}

viewModelScope.launch {
settingsStore.lastOpenedKey.collect { lastDateString ->
if (lastDateString == "") {
Expand Down
13 changes: 13 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 @@ -9,6 +9,7 @@ import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.vishal2376.snaptick.presentation.common.AppTheme
import com.vishal2376.snaptick.presentation.common.CalenderView
import com.vishal2376.snaptick.presentation.common.SortTask
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
Expand All @@ -25,13 +26,15 @@ class SettingsStore(val context: Context) {
private val TIME_PICKER_KEY = booleanPreferencesKey("time_picker_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")
private val LAST_OPENED_KEY = stringPreferencesKey("last_opened_key")
private val STREAK_KEY = intPreferencesKey("streak_key")
private val SLEEP_TIME_KEY = stringPreferencesKey("sleep_time_key")

private const val DEFAULT_LANGUAGE = "en"
private val DEFAULT_THEME = AppTheme.Dark.ordinal
private const val DEFAULT_TIME_PICKER_KEY = true
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()
private const val DEFAULT_STREAK = 0
Expand Down Expand Up @@ -59,6 +62,10 @@ class SettingsStore(val context: Context) {
preferences[SORT_TASK_KEY] ?: DEFAULT_SORT_TASK
}

val calenderViewKey: Flow<Int> = context.dataStore.data.map { preferences ->
preferences[CALENDER_VIEW_KEY] ?: DEFAULT_CALENDER_VIEW
}

val lastOpenedKey: Flow<String> = context.dataStore.data.map { preferences ->
preferences[LAST_OPENED_KEY] ?: DEFAULT_LAST_OPENED
}
Expand Down Expand Up @@ -97,6 +104,12 @@ class SettingsStore(val context: Context) {
}
}

suspend fun setCalenderView(calenderView: Int) {
context.dataStore.edit { preferences ->
preferences[CALENDER_VIEW_KEY] = calenderView
}
}

suspend fun setLastOpened(lastOpened: String) {
context.dataStore.edit { preferences ->
preferences[LAST_OPENED_KEY] = lastOpened
Expand Down

0 comments on commit 48f1595

Please sign in to comment.