From 02f11346468b9a02ac57ad40a09efe0a1a38a6be Mon Sep 17 00:00:00 2001 From: tuancoltech Date: Tue, 5 Dec 2023 07:32:16 +0700 Subject: [PATCH] [bug] Fix crash in Android Navigator: https://github.com/ARK-Builders/ARK-Navigator/issues/412 --- .../arkbuilders/arklib/data/meta/MetadataStorage.kt | 6 ++++-- .../arklib/data/storage/FolderStorage.kt | 13 ++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/src/main/java/dev/arkbuilders/arklib/data/meta/MetadataStorage.kt b/lib/src/main/java/dev/arkbuilders/arklib/data/meta/MetadataStorage.kt index db4e486d..9a7177ea 100644 --- a/lib/src/main/java/dev/arkbuilders/arklib/data/meta/MetadataStorage.kt +++ b/lib/src/main/java/dev/arkbuilders/arklib/data/meta/MetadataStorage.kt @@ -14,6 +14,7 @@ internal class MetadataStorage( ) : FolderStorage( "metadata", scope, path, MonoidIsNotUsed() ) { + override fun isNeutral(value: Metadata): Boolean = false override suspend fun valueToBinary(value: Metadata): ByteArray { @@ -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 -> diff --git a/lib/src/main/java/dev/arkbuilders/arklib/data/storage/FolderStorage.kt b/lib/src/main/java/dev/arkbuilders/arklib/data/storage/FolderStorage.kt index 6e5e4068..1b455520 100644 --- a/lib/src/main/java/dev/arkbuilders/arklib/data/storage/FolderStorage.kt +++ b/lib/src/main/java/dev/arkbuilders/arklib/data/storage/FolderStorage.kt @@ -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 @@ -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( private val label: String, @@ -78,7 +79,7 @@ abstract class FolderStorage( val newTimestamps: ConcurrentHashMap = ConcurrentHashMap() - val jobs = Files.list(storageFolder) + val stream = Files.list(storageFolder) .filter { !it.isDirectory() } .map { path -> Log.v(logPrefix, "reading value from $path") @@ -101,7 +102,13 @@ abstract class FolderStorage( 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()