How to Make Unique DataStore for each glance widge...
# glance
j
Hello everyone, been really enjoying making my first Android app and using Glance. My only issue is that I realized after following the Glance Widget WeatherApp demo that I didn't understand how to make each widget have it's own datastore. I see the comments that it can be done with fileKey, but most examples on GitHub from other projects seem to use datastore-preferences instead. Is it possible to slightly modify the WeatherApp demo, keeping it's serializer and existing setup and get a unique datastore per widget? I tried:
private val Context.datastore by dataStore(fileKey, WeatherInfoSerializer)
(replaceing the default FILENAME parameter with fileKey, but as it was not yet initialized, it does not work. I'm a bit lost as to how this should be done, though the comments in WeatherApp allude to it being close. Could someone share some help? I also asked my question here in Stack Overflow: https://stackoverflow.com/questions/77008645/how-to-make-unique-datastore-for-each-glance-widget
s
You can use
DataStoreFactory
to create a new DataStore for each `fileKey`:
Copy code
override suspend fun getDataStore(context: Context, fileKey: String) = DataStoreFactory.create(
        serializer = serializer,
        produceFile = { getLocation(context, fileKey) }
    )

    override fun getLocation(context: Context, fileKey: String) =
        context.dataStoreFile(DATA_STORE_FILENAME_PREFIX + fileKey.lowercase())
😍 1
j
Ah, I think I see, so infact the original
private val Context.datastore by dataStore(DATA_STORE_FILENAME, WeatherInfoSerializer)
is no longer needed to create the datastore, and instead getDataStore creates the unique files. I implemented the above as you shared it, and by one small change to get the random weather info to be made for each GlanceId, I was able to see different numbers for each widget, as well as confirm that inside /datastore/ there were two files! Thank you so much!
👍 1
Is it possible for me to submit a PR on the GitHub examples that includes your example above in the comment. File: https://github.com/android/user-interface-samples/blob/main/AppWidget/app/src/main/java/com/example/android/appwidget/glance/weather/WeatherInfoStateDefinition.kt Comment I'd like to edit: /** * Use the same file name regardless of the widget instance to share data between them * * If you need different state/data for each instance, create a store using the provided fileKey */ Reason being it seems others may be confused about this and the workarounds I saw on github and StackOverflow seemed much more complex than what you shared. For example, this one: https://stackoverflow.com/questions/76563756/android-datastore-how-to-create-it-with-a-dynamic-filename Not sure if that is acceptable, given that it is official examples, but would love to contribute.
Actually, following up on this. It seems that I had, via the Google Android Docs, ended up at the wrong samples that were from several months ago. After finding the newer repo I saw that last month the updating to RC1 ended up removing GlanceStateDefinition for the Weather Widget. Does anyone know why that was removed from the samples?
s
Oh no! There shouldn't be any links in the Android Docs to GlanceStateDefinition in platform-samples. Could you send me the page you found the link on so I can update it? As to your second question, our current advice is to avoid using GlanceStateDefinition and prefer using more compose style patterns like a repository with state flows.
Rereading your message, it seems like you found a link to "user-interface-samples" instead of "platform-samples". Do you remember the page you found it on?
j
@Summers Pittman Sorry for the late reply, you were correct in what you said, but upon going back to look I didn't see any more links to the incorrect repo anymore. Previously I thought I found it in this link: https://developer.android.com/jetpack/compose/glance But I do not see it there now, so I think it's OK.