Platform independent UUID generator
This commit is contained in:
@@ -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
|
||||
|
||||
13
shared/src/commonMain/kotlin/app/klottr/util/UUID.kt
Normal file
13
shared/src/commonMain/kotlin/app/klottr/util/UUID.kt
Normal 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)}"
|
||||
}
|
||||
}
|
||||
@@ -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)}"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user