mirror of
https://github.com/PranshulGG/WeatherMaster.git
synced 2026-10-22 02:52:37 -05:00
refactor WeatherWidgetStateDefinition to use DataStoreFactory and dynamic fileKey
This commit is contained in:
1 parent
664419b770
commit
fdfaa74e17
1 file changed
+6
-10
+6
-10
@@ -2,6 +2,7 @@ package com.pranshulgg.weather_master_app.widgets
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.core.DataStoreFactory
|
||||
import androidx.datastore.core.Serializer
|
||||
import androidx.datastore.dataStore
|
||||
import androidx.datastore.dataStoreFile
|
||||
@@ -16,20 +17,15 @@ import java.io.OutputStream
|
||||
object WeatherWidgetStateDefinition :
|
||||
GlanceStateDefinition<WeatherWidgetStateJson> {
|
||||
|
||||
private const val FILE_NAME =
|
||||
"weather_widget_state"
|
||||
|
||||
private val Context.datastore by dataStore(
|
||||
fileName = FILE_NAME,
|
||||
serializer = WeatherWidgetSerializer
|
||||
)
|
||||
|
||||
override suspend fun getDataStore(
|
||||
context: Context,
|
||||
fileKey: String
|
||||
): DataStore<WeatherWidgetStateJson> {
|
||||
|
||||
return context.datastore
|
||||
return DataStoreFactory.create(
|
||||
serializer = WeatherWidgetSerializer,
|
||||
produceFile = { context.dataStoreFile(fileKey) }
|
||||
)
|
||||
}
|
||||
|
||||
override fun getLocation(
|
||||
@@ -37,7 +33,7 @@ object WeatherWidgetStateDefinition :
|
||||
fileKey: String
|
||||
): File {
|
||||
|
||||
return context.dataStoreFile(FILE_NAME)
|
||||
return context.dataStoreFile(fileKey)
|
||||
}
|
||||
|
||||
object WeatherWidgetSerializer :
|
||||
|
||||
Reference in new issue
Block a user