Got it working, but it's not very pretty. Code underneath dateStore
was not public, So I had to copy/paste/adapt a bit of code :
object SingleWidgetDataStateDefinition : GlanceStateDefinition<SingleWidgetData> {
private const val DATA_STORE_FILENAME = "singleWidget"
//private val Context.datastore by dataStore(DATA_STORE_FILENAME, SingleWidgetDataSerializer)
override suspend fun getDataStore(
context: Context,
fileKey: String
): DataStore<SingleWidgetData> {
return DataStoreSingletonDelegate(
serializer = SingleWidgetDataSerializer,
fileName = DATA_STORE_FILENAME + fileKey,
corruptionHandler = null,
produceMigrations = { listOf() },
scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
).getValue(context)
}
}
and
class DataStoreSingletonDelegate<T> internal constructor(
private val fileName: String,
private val serializer: Serializer<T>,
private val corruptionHandler: ReplaceFileCorruptionHandler<T>?,
private val produceMigrations: (Context) -> List<DataMigration<T>>,
private val scope: CoroutineScope
) {
private val lock = Any()
@GuardedBy("lock")
@Volatile
private var INSTANCE: DataStore<T>? = null
fun getValue(thisRef: Context): DataStore<T> {
return INSTANCE ?: synchronized(lock) {
if (INSTANCE == null) {
val applicationContext = thisRef.applicationContext
INSTANCE = DataStoreFactory.create(
serializer = serializer,
produceFile = { applicationContext.dataStoreFile(fileName) },
corruptionHandler = corruptionHandler,
migrations = produceMigrations(applicationContext),
scope = scope
)
}
INSTANCE!!
}
}
}