Code clean up

This commit is contained in:
Vitor Pamplona
2023-09-19 16:43:57 -04:00
parent c2beaf5f80
commit 15e7540b26
13 changed files with 102 additions and 72 deletions
@@ -15,16 +15,28 @@ class AppDefinitionEvent(
content: String,
sig: HexKey
) : BaseAddressableEvent(id, pubKey, createdAt, kind, tags, content, sig) {
fun appMetaData() = try {
mapper.readValue(
ByteArrayInputStream(content.toByteArray(Charsets.UTF_8)),
UserMetadata::class.java
)
} catch (e: Exception) {
e.printStackTrace()
Log.w("MT", "Content Parse Error ${e.localizedMessage} $content")
null
}
@Transient
private var cachedMetadata: UserMetadata? = null
fun appMetaData() =
if (cachedMetadata != null) {
cachedMetadata
} else {
try {
val newMetadata = mapper.readValue(
ByteArrayInputStream(content.toByteArray(Charsets.UTF_8)),
UserMetadata::class.java
)
cachedMetadata = newMetadata
newMetadata
} catch (e: Exception) {
e.printStackTrace()
Log.w("MT", "Content Parse Error ${e.localizedMessage} $content")
null
}
}
fun supportedKinds() = tags.filter { it.size > 1 && it[0] == "k" }.mapNotNull {
runCatching { it[1].toInt() }.getOrNull()
@@ -350,6 +350,8 @@ class UserMetadata {
@Stable
data class ImmutableListOfLists<T>(val lists: List<List<T>> = emptyList())
val EmptyTagList = ImmutableListOfLists<String>(emptyList())
fun List<List<String>>.toImmutableListOfLists(): ImmutableListOfLists<String> {
return ImmutableListOfLists(this)
}