@@ -3,12 +3,13 @@ package de.tutao.calendar.widget.model
33import android.content.Context
44import android.content.Intent
55import android.util.Log
6+ import androidx.datastore.core.DataStore
67import androidx.datastore.core.IOException
8+ import androidx.datastore.preferences.core.Preferences
79import androidx.glance.action.Action
810import androidx.glance.appwidget.action.actionStartActivity
911import androidx.lifecycle.ViewModel
1012import de.tutao.calendar.MainActivity
11- import de.tutao.calendar.R
1213import de.tutao.calendar.widget.WidgetUpdateTrigger
1314import de.tutao.calendar.widget.data.BirthdayEventDao
1415import de.tutao.calendar.widget.data.LastSyncDao
@@ -18,7 +19,6 @@ import de.tutao.calendar.widget.data.WidgetRepository
1819import de.tutao.calendar.widget.data.WidgetUIData
1920import de.tutao.calendar.widget.error.WidgetError
2021import de.tutao.calendar.widget.error.WidgetErrorType
21- import de.tutao.calendar.widget.widgetCacheDataStore
2222import de.tutao.calendar.widget.widgetDataStore
2323import de.tutao.tutasdk.LoginException
2424import de.tutao.tutasdk.Sdk
@@ -50,7 +50,8 @@ class WidgetUIViewModel(
5050 private val credentialsFacade : NativeCredentialsFacade ,
5151 private val cryptoFacade : AndroidNativeCryptoFacade ,
5252 private val sdk : Sdk ? ,
53- private val calendar : Calendar
53+ private val calendar : Calendar ,
54+ private val birthdayStrings : BirthdayStrings
5455) : ViewModel() {
5556 private val _uiState = MutableStateFlow <WidgetUIData ?>(null )
5657 val uiState: StateFlow <WidgetUIData ?> = _uiState .asStateFlow()
@@ -62,7 +63,11 @@ class WidgetUIViewModel(
6263 private const val TAG = " WidgetUIViewModel"
6364 }
6465
65- suspend fun loadUIState (context : Context , now : LocalDateTime ): WidgetUIData ? {
66+ suspend fun loadUIState (
67+ widgetDataStore : DataStore <Preferences >,
68+ widgetCacheDataStore : DataStore <Preferences >,
69+ now : LocalDateTime
70+ ): WidgetUIData ? {
6671 Log .i(TAG , " Init loadUIState" )
6772 val allDayEvents: HashMap <Long , List <UIEvent >> = HashMap ()
6873 val normalEvents: HashMap <Long , List <UIEvent >> = HashMap ()
@@ -80,18 +85,18 @@ class WidgetUIViewModel(
8085 val calendars: List <String >
8186
8287 try {
83- settings = repository.loadSettings(context. widgetDataStore, widgetId) ? : return WidgetUIData (
88+ settings = repository.loadSettings(widgetDataStore, widgetId) ? : return WidgetUIData (
8489 normalEvents,
8590 allDayEvents
8691 )
8792 Log .i(TAG , " Widget settings has ${settings.calendars.values.size} calendars" )
8893 settings.calendars.entries.forEach { (calendarId, calendar) ->
8994 Log .d(TAG , " $calendarId - ${calendar.name} " )
9095 }
91- lastSync = repository.loadLastSync(context. widgetDataStore, widgetId)
96+ lastSync = repository.loadLastSync(widgetDataStore, widgetId)
9297 Log .i(TAG , " Widget last sync at $lastSync " )
9398
94- sdk?.let { sdk -> loadCalendars(context , sdk, settings) }
99+ sdk?.let { sdk -> loadCalendars(widgetDataStore , sdk, settings) }
95100 calendars = settings.calendars.keys.toList()
96101 } catch (e: Exception ) {
97102 // We couldn't load widget settings, so we must show an error to User
@@ -128,7 +133,7 @@ class WidgetUIViewModel(
128133 val loggedInSdk = this .sdk.login(credentials.toSdkCredentials())
129134
130135 repository.loadEvents(
131- context. widgetCacheDataStore,
136+ widgetCacheDataStore,
132137 widgetId,
133138 settings.userId,
134139 calendars,
@@ -144,7 +149,7 @@ class WidgetUIViewModel(
144149 " Missing credentials for user ${settings.userId} when trying to load widget content}" , e
145150 )
146151 repository.loadEventsFromCache(
147- context. widgetCacheDataStore,
152+ widgetCacheDataStore,
148153 widgetId,
149154 calendars,
150155 credentials,
@@ -153,7 +158,7 @@ class WidgetUIViewModel(
153158 } catch (e: Exception ) {
154159 Log .e(TAG , " Unknown exception occurred" , e)
155160 repository.loadEventsFromCache(
156- context. widgetCacheDataStore,
161+ widgetCacheDataStore,
157162 widgetId,
158163 calendars,
159164 credentials,
@@ -162,7 +167,7 @@ class WidgetUIViewModel(
162167 }
163168 } else {
164169 repository.loadEventsFromCache(
165- context. widgetCacheDataStore,
170+ widgetCacheDataStore,
166171 widgetId,
167172 calendars,
168173 credentials,
@@ -244,7 +249,7 @@ class WidgetUIViewModel(
244249 calendarId,
245250 it.eventDao.id,
246251 calendarColor = settings.calendars[calendarId]?.color ? : " 2196f3" ,
247- summary = buildBirthdayEventTitle(it, context ),
252+ summary = buildBirthdayEventTitle(it),
248253 start.format(formatter),
249254 end.format(formatter),
250255 isAllDay = true ,
@@ -288,15 +293,15 @@ class WidgetUIViewModel(
288293 return uiState.value
289294 }
290295
291- private suspend fun loadCalendars (context : Context , sdk : Sdk , settings : SettingsDao ) {
296+ private suspend fun loadCalendars (widgetDataStore : DataStore < Preferences > , sdk : Sdk , settings : SettingsDao ) {
292297 try {
293298 Log .i(TAG , " Fetching new calendar data from server" )
294299 val loadedCalendars = repository.loadCalendars(settings.userId, credentialsFacade, sdk)
295300 Log .i(TAG , " Successfully fetched ${loadedCalendars.size} calendars" )
296301 for (key in loadedCalendars.keys) {
297302 settings.calendars[key]?.color = loadedCalendars[key]?.color ? : continue
298303 }
299- repository.storeSettings(context , widgetId, settings)
304+ repository.storeSettings(widgetDataStore , widgetId, settings)
300305 Log .i(TAG , " Cached calendar data updated successfully!" )
301306 } catch (e: LoginException .ApiCall ) {
302307 // Failed to login into SDK, probably because of connection issues
@@ -310,12 +315,12 @@ class WidgetUIViewModel(
310315 }
311316 }
312317
313- private fun buildBirthdayEventTitle (event : BirthdayEventDao , context : Context ): String {
318+ private fun buildBirthdayEventTitle (event : BirthdayEventDao ): String {
314319 if (event.contact.age == null ) {
315- return context.getString( R .string.birthdayEvent_title) .replace(" {name}" , event.contact.name)
320+ return birthdayStrings.birthdayTitleTemplate .replace(" {name}" , event.contact.name)
316321 }
317322
318- val age = context.getString( R .string.birthdayEventAge_title) .replace(
323+ val age = birthdayStrings.birthdayAgeTemplate .replace(
319324 " {age}" ,
320325 event.contact.age.toString()
321326 )
@@ -371,4 +376,9 @@ fun openCalendarAgenda(
371376 }
372377
373378 return actionStartActivity(openCalendarAgenda)
374- }
379+ }
380+
381+ data class BirthdayStrings (
382+ val birthdayTitleTemplate : String ,
383+ val birthdayAgeTemplate : String
384+ )
0 commit comments