Skip to content

Commit

Permalink
feat: Integrate What's new dialog with viewmodel and state
Browse files Browse the repository at this point in the history
  • Loading branch information
vishal2376 committed Nov 1, 2024
1 parent 28f2474 commit 839fdb4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import com.vishal2376.snaptick.presentation.home_screen.components.InfoComponent
import com.vishal2376.snaptick.presentation.home_screen.components.NavigationDrawerComponent
import com.vishal2376.snaptick.presentation.home_screen.components.SortTaskDialogComponent
import com.vishal2376.snaptick.presentation.home_screen.components.TaskComponent
import com.vishal2376.snaptick.presentation.home_screen.components.WhatsNewDialogComponent
import com.vishal2376.snaptick.presentation.main.MainEvent
import com.vishal2376.snaptick.presentation.main.MainState
import com.vishal2376.snaptick.presentation.navigation.Routes
Expand Down Expand Up @@ -103,6 +104,7 @@ fun HomeScreen(
// streak
val appStreakText = if (appState.streak > 0) appState.streak.toString() else "0"
val context = LocalContext.current
val packageInfo = context.packageManager.getPackageInfo(LocalContext.current.packageName, 0)

LaunchedEffect(inCompletedTasks) {
appState.totalTaskDuration = totalTaskTime
Expand Down Expand Up @@ -253,6 +255,14 @@ fun HomeScreen(
}
)

if ((appState.showWhatsNew && appState.firstTimeOpened) || appState.buildVersionCode != packageInfo.versionCode) {
WhatsNewDialogComponent(appState = appState) {
onMainEvent(MainEvent.UpdateFirstTimeOpened(false))
onMainEvent(MainEvent.UpdateShowWhatsNew(it, context))
onMainEvent(MainEvent.UpdateBuildVersionCode(context, packageInfo.versionCode))
}
}

Column(modifier = Modifier.padding(innerPadding)) {
Row(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ sealed class MainEvent {
data class UpdateAppTheme(val theme: AppTheme, val context: Context) : MainEvent()
data class UpdateDynamicTheme(val isEnabled: Boolean, val context: Context) : MainEvent()
data class UpdateTimePicker(val isWheelTimePicker: Boolean, val context: Context) : MainEvent()
data class UpdateFirstTimeOpened(val isFirstTimeOpened: Boolean) : 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()
data class UpdateCalenderView(val calenderView: CalenderView, val context: Context) :
MainEvent()

data class UpdateCalenderDate(val date: LocalDate?) : MainEvent()
data class UpdateShowWhatsNew(val showWhatsNew: Boolean, val context: Context) : MainEvent()
data class OnClickNavDrawerItem(val context: Context, val item: NavDrawerItem) : MainEvent()
data class UpdateBuildVersionCode(val context: Context, val versionCode: Int) : MainEvent()
data class UpdateCalenderView(val calenderView: CalenderView, val context: Context) :
MainEvent()
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import java.util.Locale

data class MainState(
val buildVersion: String = "0.0",
val buildVersionCode: Int = 1,
val firstTimeOpened: Boolean = true,
val showWhatsNew: Boolean = true,
val theme: AppTheme = AppTheme.Dark,
val dynamicTheme: Boolean = false,
val sortBy: SortTask = SortTask.BY_START_TIME_ASCENDING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,26 @@ class TaskViewModel @Inject constructor(
}
}
}

is MainEvent.UpdateShowWhatsNew -> {
viewModelScope.launch {
appState = appState.copy(showWhatsNew = event.showWhatsNew)
SettingsStore(event.context).setShowWhatsNew(event.showWhatsNew)
}
}

is MainEvent.UpdateFirstTimeOpened -> {
viewModelScope.launch {
appState = appState.copy(firstTimeOpened = event.isFirstTimeOpened)
}
}

is MainEvent.UpdateBuildVersionCode -> {
viewModelScope.launch {
appState = appState.copy(buildVersionCode = event.versionCode)
SettingsStore(event.context).setBuildVersionCode(event.versionCode)
}
}
}
}

Expand Down Expand Up @@ -418,6 +438,12 @@ class TaskViewModel @Inject constructor(
}
}

viewModelScope.launch {
settingsStore.showWhatsNewKey.collect {
appState = appState.copy(showWhatsNew = it)
}
}

viewModelScope.launch {
settingsStore.lastOpenedKey.collect { lastDateString ->
if (lastDateString == "") {
Expand All @@ -440,6 +466,12 @@ class TaskViewModel @Inject constructor(
val buildVersionCode =
context.packageManager.getPackageInfo(context.packageName, 0).versionName
appState = appState.copy(buildVersion = buildVersionCode)

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

}
24 changes: 24 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 @@ -32,6 +32,8 @@ class SettingsStore(val context: Context) {
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 val SHOW_WHATS_NEW_KEY = booleanPreferencesKey("show_whats_new_key")
private val BUILD_VERSION_CODE = intPreferencesKey("build_version_code")

private const val DEFAULT_LANGUAGE = "en"
private val DEFAULT_THEME = AppTheme.Dark.ordinal
Expand All @@ -43,6 +45,8 @@ class SettingsStore(val context: Context) {
private val DEFAULT_LAST_OPENED = LocalDate.now().toString()
private const val DEFAULT_STREAK = 0
private val DEFAULT_SLEEP_TIME_KEY = LocalTime.of(23, 59).toString()
private const val DEFAULT_SHOW_WHATS_NEW = true
private const val DEFAULT_BUILD_VERSION_CODE = 1
}


Expand Down Expand Up @@ -86,6 +90,14 @@ class SettingsStore(val context: Context) {
preferences[STREAK_KEY] ?: DEFAULT_STREAK
}

val showWhatsNewKey: Flow<Boolean> = context.dataStore.data.map { preferences ->
preferences[SHOW_WHATS_NEW_KEY] ?: DEFAULT_SHOW_WHATS_NEW
}

val buildVersionCode: Flow<Int> = context.dataStore.data.map { preferences ->
preferences[BUILD_VERSION_CODE] ?: DEFAULT_BUILD_VERSION_CODE
}

suspend fun setTheme(theme: Int) {
context.dataStore.edit { preferences ->
preferences[THEME_KEY] = theme
Expand Down Expand Up @@ -146,5 +158,17 @@ class SettingsStore(val context: Context) {
}
}

suspend fun setShowWhatsNew(showWhatsNew: Boolean) {
context.dataStore.edit { preferences ->
preferences[SHOW_WHATS_NEW_KEY] = showWhatsNew
}
}

suspend fun setBuildVersionCode(versionCode: Int) {
context.dataStore.edit { preferences ->
preferences[BUILD_VERSION_CODE] = versionCode
}
}

}

0 comments on commit 839fdb4

Please sign in to comment.