Platform independent UUID generator

This commit is contained in:
Olof Hedman
2025-09-29 11:36:27 +02:00
parent 22df5b26c5
commit db00c93771
3 changed files with 16 additions and 11 deletions

View File

@@ -84,6 +84,7 @@ import app.klottr.util.KLog
import app.klottr.util.NoteDetailCache
import app.klottr.util.OutboxStore
import app.klottr.util.SettingsStore
import app.klottr.util.UUID
import app.klottr.util.formatCoords
import app.klottr.util.formatDistance
import coil3.ImageLoader
@@ -95,7 +96,6 @@ import kotlinx.coroutines.IO
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.util.UUID
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.collections.iterator
@@ -680,7 +680,7 @@ fun KlottrAppShell(
return@WriteDocDialog
}
val created = Clock.System.now().toEpochMilliseconds()
val requestId = UUID.randomUUID().toString()
val requestId = UUID.randomUUID()
if (isSavingDoc) return@WriteDocDialog
isSavingDoc = true

View File

@@ -0,0 +1,13 @@
package app.klottr.util
import kotlin.random.Random
object UUID {
fun randomUUID(): String {
val b = Random.nextBytes(16)
b[6] = ((b[6].toInt() and 0x0f) or 0x40).toByte() // version 4
b[8] = ((b[8].toInt() and 0x3f) or 0x80).toByte() // variant 10
val h = b.joinToString("") { (it.toInt() and 0xff).toString(16).padStart(2, '0') }
return "${h.substring(0, 8)}-${h.substring(8, 12)}-${h.substring(12, 16)}-${h.substring(16, 20)}-${h.substring(20)}"
}
}

View File

@@ -11,16 +11,8 @@ object UserId {
if (KvPrefs.contains(KEY)) {
KvPrefs.getString(KEY, null)?.let { return it }
}
val fresh = randomUuidV4()
val fresh = UUID.randomUUID()
KvPrefs.putString(KEY, fresh)
return fresh
}
private fun randomUuidV4(): String {
val b = Random.nextBytes(16)
b[6] = ((b[6].toInt() and 0x0f) or 0x40).toByte() // version 4
b[8] = ((b[8].toInt() and 0x3f) or 0x80).toByte() // variant 10
val h = b.joinToString("") { (it.toInt() and 0xff).toString(16).padStart(2, '0') }
return "${h.substring(0,8)}-${h.substring(8,12)}-${h.substring(12,16)}-${h.substring(16,20)}-${h.substring(20)}"
}
}