Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
[bug] Fix crash in Android Navigator: ARK-Builders/ARK-Navigator#412
Browse files Browse the repository at this point in the history
  • Loading branch information
tuancoltech committed Dec 14, 2023
1 parent a14440c commit 02f1134
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal class MetadataStorage(
) : FolderStorage<Metadata>(
"metadata", scope, path, MonoidIsNotUsed()
) {

override fun isNeutral(value: Metadata): Boolean = false

override suspend fun valueToBinary(value: Metadata): ByteArray {
Expand All @@ -32,9 +33,10 @@ internal class MetadataStorage(

override suspend fun valueFromBinary(raw: ByteArray): Metadata {
val text = String(raw, Charsets.UTF_8)
val json = Json.parseToJsonElement(text)
if (text.isEmpty()) return Metadata.Unknown()

val kind = json.jsonObject[KIND]!!.jsonPrimitive.content
val json = Json.parseToJsonElement(text)
val kind = json.jsonObject[KIND]?.jsonPrimitive?.content ?: return Metadata.Unknown()

val metadata = when (Kind.valueOf(kind)) {
Kind.IMAGE ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.arkbuilders.arklib.data.storage

import android.os.Build
import android.util.Log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -12,10 +13,10 @@ import java.nio.file.Path
import java.nio.file.attribute.FileTime
import java.time.Instant
import java.util.concurrent.ConcurrentHashMap
import java.util.stream.Collectors
import kotlin.io.path.deleteIfExists
import kotlin.io.path.isDirectory
import kotlin.io.path.relativeTo
import kotlin.streams.toList

abstract class FolderStorage<V>(
private val label: String,
Expand Down Expand Up @@ -78,7 +79,7 @@ abstract class FolderStorage<V>(
val newTimestamps: ConcurrentHashMap<ResourceId, FileTime> =
ConcurrentHashMap()

val jobs = Files.list(storageFolder)
val stream = Files.list(storageFolder)
.filter { !it.isDirectory() }
.map { path ->
Log.v(logPrefix, "reading value from $path")
Expand All @@ -101,7 +102,13 @@ abstract class FolderStorage<V>(
newTimestamps[id] = newTimestamp
}
}
}.toList()
}

val jobs = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
stream.toList()
} else {
stream.collect(Collectors.toList())
}

jobs.joinAll()

Expand Down

0 comments on commit 02f1134

Please sign in to comment.