mattinger
06/02/2025, 1:43 PMdata class StringTableKey(
val name: String,
val languageCode: String,
val countryCode: String
)
interface StringTable {
fun asMap(): Map<StringTableKey, String>
}
I want to define a DataStore for StringTable. However, it seems that the "Serializer" interface exists only in the android artifact. If that's the case, how am i supposed to define serialization for the iOS side of things?Vidmantas Kerbelis
06/02/2025, 1:53 PMandroidx.datastore.core.Serializer
does not exist in common code. If you really want to do it this way - you should expose your store as an expect / actual implementation.
The actual implementation for Android would use the Serializer,
On iOS - use a file solution (`NSDocumentDirectory`and so on),
On JS - use localStorage.
Or, you just use DataStore<Preferences>
and save the Map<StringTableKey, String>
with a forEach
, with no serailization. Though deserializing will be a bit more problematic.
EDIT: You could just serialize the key using JSON and save it as the preference keymattinger
06/02/2025, 4:48 PMfun createDataStore(): DataStore<Preferences> = createDataStore(
producePath = {
val documentDirectory: NSURL? = NSFileManager.defaultManager.URLForDirectory(
directory = NSDocumentDirectory,
inDomain = NSUserDomainMask,
appropriateForURL = null,
create = false,
error = null,
)
requireNotNull(documentDirectory).path + "/$dataStoreFileName"
}
)
mattinger
06/02/2025, 4:49 PMmattinger
06/02/2025, 5:35 PMmattinger
06/02/2025, 5:43 PMfun Preferences.asStringTable(): StringTable =
PreferencesStringTable(this)
fun Flow<Preferences>.toStringTableFlow(): Flow<StringTable> =
map {
it.asStringTable()
}
I think this will work well for me as 99% of the app only wants the immutable version and won't be pushing strings.