-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initialize dictionary with data in multiplatform compose resources #27
Comments
I see. But you can just use the regular constructor of the FeatureDictionary. Adding a function like fun createFeatureDictionary(
featuresAccess: ResourceAccessAdapter,
brandFeaturesAccess: ResourceAccessAdapter?,
) = FeatureDictionary(
featureCollection = IDLocalizedFeatureCollection(featuresAccess),
brandFeatureCollection = brandFeaturesAccess?.let { IDBrandPresetsFeatureCollection(it) }
) Then, you just need to implement a |
It took way too long to find this out, but you can use val buffer = Buffer()
UnsafeBufferOperations.moveToTail(buffer, byteArray) to wrap a |
Is there a method like |
Not exactly, but it can be checked with |
(I am curious, though, what do you need osmfeatures for? I've noticed you also posted an issue in sargunv/maplibre-compose, so you must be working on some Compose Multiplatform app that works with OSM data) Anyway, it sounds like you can build something to initialize the data with compose resources in a few lines of code. It's such few lines of code, that I don't think it would be worth it to create a library for that ( |
Yes, I'm working on a Compose Multiplatform app that works with OSM data. I decided not to use osmfeatures and instead just read the file, parse it to json and read the values I need using kotlinx.serialization library. const val DEFAULT_PRESETS_LANGUAGE = "en"
data class IdTags(
val presets: JsonObject,
val fields: JsonObject
)
suspend fun getIdTagsFromFile(language: String?, logException: Boolean = false): IdTags? {
language?.let { lang ->
try {
val json = getJsonFromFile("id-presets", "$lang.json", logException)
if (json != null) {
val basePresets = json[lang]!!.jsonObject["presets"]!!.jsonObject
val presets = basePresets.jsonObject["presets"]!!.jsonObject
val fields = basePresets.jsonObject["fields"]!!.jsonObject
return IdTags(presets, fields)
} else if (lang != DEFAULT_PRESETS_LANGUAGE) {
return getIdTagsFromFile(DEFAULT_PRESETS_LANGUAGE, true)
}
} catch (e: Exception) {
if (lang != DEFAULT_PRESETS_LANGUAGE)
return getIdTagsFromFile(DEFAULT_PRESETS_LANGUAGE, true)
else
Firebase.crashlytics.recordException(e)
}
}
return null
}
@OptIn(ExperimentalResourceApi::class)
private suspend fun getJsonFromFile(basepath: String, filename: String, logException: Boolean = true): JsonObject? {
try {
val path = Path("files/$basepath", filename)
val string = Res.readBytes(path.toString()).decodeToString()
return Json.parseToJsonElement(string).jsonObject
} catch (e: Exception) {
if (logException)
Firebase.crashlytics.recordException(e)
return null
}
} |
okidoki, thanks |
Currently, there are 2 options to initialize the dictionary, either by providing
FileSystem
orAssetManager
, but onlyFileSystem
is available in the common source set of the Kotlin Multiplatform project. In case when we keep our presets data in compose resources, they are not available fromFileSystem
, they are bundled into apk, thus right now it's not possible to get them with osmfeatures library in the common source set.To get a raw file from compose resources we can use
Res.readBytes("files/presets/preset.json")
(reference).To solve this issue, I propose to make
FeatureDictionary.create()
accept aRes
object as a parameter and read presets from compose resources byRes.readBytes()
The text was updated successfully, but these errors were encountered: