Skip to content

Commit

Permalink
refactor: Use Android viewmodel for application context
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Apr 20, 2024
1 parent 19bc6fd commit 8714544
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/clock/navigation/NavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fun AppNavHost(
val alarmModel: AlarmModel = viewModel()
val timerModel: TimerModel = viewModel()
val stopwatchModel: StopwatchModel = viewModel()
val clockModel: ClockModel = viewModel(factory = ClockModel.Factory)
val clockModel: ClockModel = viewModel()

NavHost(navController, startDestination = NavRoutes.Home.route, modifier = modifier) {
composable(NavRoutes.Home.route,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.bnyro.clock.presentation.screens.clock.model

import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.bnyro.clock.App
import com.bnyro.clock.data.database.DatabaseHolder
import com.bnyro.clock.domain.model.SortOrder
import com.bnyro.clock.domain.model.TimeZone
Expand All @@ -21,12 +17,12 @@ import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

class ClockModel(app: Application) : ViewModel() {
class ClockModel(application: Application) : AndroidViewModel(application) {
private val sortOrderPref =
Preferences.instance.getString(Preferences.clockSortOrder, "").orEmpty()
val sortOrder =
MutableStateFlow(if (sortOrderPref.isNotEmpty()) SortOrder.valueOf(sortOrderPref) else SortOrder.ALPHABETIC)
private val countryTimezones = getCountryTimezones(app.applicationContext)
private val countryTimezones = getCountryTimezones(application.applicationContext)
val timeZones = TimeHelper.getTimezonesForCountries(countryTimezones)
var selectedTimeZones = combine(
DatabaseHolder.instance.timeZonesDao().getAllStream(),
Expand Down Expand Up @@ -62,13 +58,4 @@ class ClockModel(app: Application) : ViewModel() {
val time = TimeHelper.getTimeByZone(timeZone)
return TimeHelper.formatDateTime(time, false)
}

companion object {
val Factory = viewModelFactory {
initializer {
val application = this[APPLICATION_KEY] as App
ClockModel(application)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fun DigitalClockWidgetSettings(
onComplete: (DigitalClockWidgetOptions) -> Unit
) {

val clockModel: ClockModel = viewModel(factory = ClockModel.Factory)
val clockModel: ClockModel = viewModel()
var showTimeZoneDialog by remember { mutableStateOf(false) }

var customTimeZone by remember { mutableStateOf(options.timeZone) }
Expand Down

0 comments on commit 8714544

Please sign in to comment.