package com.vitorpamplona.amethyst.model import androidx.lifecycle.LiveData import com.vitorpamplona.amethyst.service.NostrSingleEventDataSource import com.vitorpamplona.amethyst.ui.note.toDisplayHex import fr.acinq.secp256k1.Hex import java.util.Collections import nostr.postr.events.Event class Note(val idHex: String) { val id = Hex.decode(idHex) val idDisplayHex = id.toDisplayHex() var event: Event? = null var author: User? = null var mentions: List? = null var replyTo: MutableList? = null val replies = Collections.synchronizedSet(mutableSetOf()) val reactions = Collections.synchronizedSet(mutableSetOf()) val boosts = Collections.synchronizedSet(mutableSetOf()) fun loadEvent(event: Event, author: User, mentions: List, replyTo: MutableList) { this.event = event this.author = author this.mentions = mentions this.replyTo = replyTo refreshObservers() } fun addReply(note: Note) { if (replies.add(note)) refreshObservers() } fun addBoost(note: Note) { if (boosts.add(note)) refreshObservers() } fun addReaction(note: Note) { if (reactions.add(note)) refreshObservers() } fun isReactedBy(user: User): Boolean { return reactions.any { it.author == user } } fun isBoostedBy(user: User): Boolean { return boosts.any { it.author == user } } // Observers line up here. val live: NoteLiveData = NoteLiveData(this) private fun refreshObservers() { live.refresh() } } class NoteLiveData(val note: Note): LiveData(NoteState(note)) { fun refresh() { postValue(NoteState(note)) } override fun onActive() { super.onActive() NostrSingleEventDataSource.add(note.idHex) } override fun onInactive() { super.onInactive() NostrSingleEventDataSource.remove(note.idHex) } } class NoteState(val note: Note)