added SignerDialog in the hashtagscreen

This commit is contained in:
greenart7c3
2023-08-09 10:42:00 -03:00
parent b761aaf970
commit 7a46376bc1
3 changed files with 110 additions and 30 deletions

View File

@@ -503,8 +503,8 @@ class Account(
followCommunities = emptyList(), followCommunities = emptyList(),
followEvents = DefaultChannels.toList(), followEvents = DefaultChannels.toList(),
relayUse = Constants.defaultRelays.associate { it.url to ContactListEvent.ReadWrite(it.read, it.write) }, relayUse = Constants.defaultRelays.associate { it.url to ContactListEvent.ReadWrite(it.read, it.write) },
privateKey = keyPair.privKey!!, privateKey = keyPair.privKey,
publicKey = keyPair.pubKey publicKey = if (signEvent) null else keyPair.pubKey
) )
} }
@@ -564,17 +564,25 @@ class Account(
LocalCache.consume(event) LocalCache.consume(event)
} }
fun followHashtag(tag: String) { fun followHashtag(tag: String, signEvent: Boolean = true): ContactListEvent? {
if (!isWriteable()) return if (!isWriteable() && signEvent) return null
val contactList = migrateCommunitiesAndChannelsIfNeeded(userProfile().latestContactList) val contactList = migrateCommunitiesAndChannelsIfNeeded(userProfile().latestContactList)
val event = if (contactList != null) { val event = if (contactList != null) {
ContactListEvent.followHashtag( if (signEvent) {
contactList, ContactListEvent.followHashtag(
tag, contactList,
keyPair.privKey!! tag,
) keyPair.privKey!!
)
} else {
ContactListEvent.followHashtag(
contactList,
tag,
keyPair.pubKey.toHexKey()
)
}
} else { } else {
ContactListEvent.createFromScratch( ContactListEvent.createFromScratch(
followUsers = emptyList(), followUsers = emptyList(),
@@ -583,12 +591,18 @@ class Account(
followCommunities = emptyList(), followCommunities = emptyList(),
followEvents = DefaultChannels.toList(), followEvents = DefaultChannels.toList(),
relayUse = Constants.defaultRelays.associate { it.url to ContactListEvent.ReadWrite(it.read, it.write) }, relayUse = Constants.defaultRelays.associate { it.url to ContactListEvent.ReadWrite(it.read, it.write) },
privateKey = keyPair.privKey!! privateKey = keyPair.privKey,
publicKey = if (signEvent) null else keyPair.pubKey
) )
} }
if (!signEvent) {
return event
}
Client.send(event) Client.send(event)
LocalCache.consume(event) LocalCache.consume(event)
return null
} }
fun followGeohash(geohash: String) { fun followGeohash(geohash: String) {
@@ -645,12 +659,20 @@ class Account(
return null return null
} }
fun unfollowHashtag(tag: String) { fun unfollowHashtag(tag: String, signEvent: Boolean = true): ContactListEvent? {
if (!isWriteable()) return if (!isWriteable() && signEvent) return null
val contactList = migrateCommunitiesAndChannelsIfNeeded(userProfile().latestContactList) val contactList = migrateCommunitiesAndChannelsIfNeeded(userProfile().latestContactList)
if (contactList != null && contactList.tags.isNotEmpty()) { if (contactList != null && contactList.tags.isNotEmpty()) {
if (!signEvent) {
return ContactListEvent.unfollowHashtag(
contactList,
tag,
keyPair.pubKey.toHexKey()
)
}
val event = ContactListEvent.unfollowHashtag( val event = ContactListEvent.unfollowHashtag(
contactList, contactList,
tag, tag,
@@ -659,7 +681,11 @@ class Account(
Client.send(event) Client.send(event)
LocalCache.consume(event) LocalCache.consume(event)
return null
} }
return null
} }
fun unfollowGeohash(geohash: String) { fun unfollowGeohash(geohash: String) {

View File

@@ -97,7 +97,7 @@ class ContactListEvent(
followCommunities: List<ATag>, followCommunities: List<ATag>,
followEvents: List<String>, followEvents: List<String>,
relayUse: Map<String, ReadWrite>?, relayUse: Map<String, ReadWrite>?,
privateKey: ByteArray, privateKey: ByteArray?,
createdAt: Long = TimeUtils.now(), createdAt: Long = TimeUtils.now(),
publicKey: ByteArray? = null publicKey: ByteArray? = null
): ContactListEvent { ): ContactListEvent {
@@ -131,7 +131,7 @@ class ContactListEvent(
return create( return create(
content = content, content = content,
tags = tags, tags = tags,
privateKey = privateKey, privateKey = privateKey!!,
createdAt = createdAt createdAt = createdAt
) )
} }
@@ -198,6 +198,17 @@ class ContactListEvent(
) )
} }
fun followHashtag(earlierVersion: ContactListEvent, hashtag: String, pubKey: HexKey, createdAt: Long = TimeUtils.now()): ContactListEvent {
if (earlierVersion.isTaggedHash(hashtag)) return earlierVersion
return create(
content = earlierVersion.content,
tags = earlierVersion.tags.plus(element = listOf("t", hashtag)),
pubKey = pubKey,
createdAt = createdAt
)
}
fun unfollowHashtag(earlierVersion: ContactListEvent, hashtag: String, privateKey: ByteArray, createdAt: Long = TimeUtils.now()): ContactListEvent { fun unfollowHashtag(earlierVersion: ContactListEvent, hashtag: String, privateKey: ByteArray, createdAt: Long = TimeUtils.now()): ContactListEvent {
if (!earlierVersion.isTaggedHash(hashtag)) return earlierVersion if (!earlierVersion.isTaggedHash(hashtag)) return earlierVersion
@@ -209,6 +220,17 @@ class ContactListEvent(
) )
} }
fun unfollowHashtag(earlierVersion: ContactListEvent, hashtag: String, pubKey: HexKey, createdAt: Long = TimeUtils.now()): ContactListEvent {
if (!earlierVersion.isTaggedHash(hashtag)) return earlierVersion
return create(
content = earlierVersion.content,
tags = earlierVersion.tags.filter { it.size > 1 && it[1] != hashtag },
pubKey = pubKey,
createdAt = createdAt
)
}
fun followGeohash(earlierVersion: ContactListEvent, hashtag: String, privateKey: ByteArray, createdAt: Long = TimeUtils.now()): ContactListEvent { fun followGeohash(earlierVersion: ContactListEvent, hashtag: String, privateKey: ByteArray, createdAt: Long = TimeUtils.now()): ContactListEvent {
if (earlierVersion.isTaggedGeoHash(hashtag)) return earlierVersion if (earlierVersion.isTaggedGeoHash(hashtag)) return earlierVersion

View File

@@ -16,8 +16,10 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
@@ -28,7 +30,12 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.service.NostrHashtagDataSource import com.vitorpamplona.amethyst.service.NostrHashtagDataSource
import com.vitorpamplona.amethyst.service.PackageUtils
import com.vitorpamplona.amethyst.service.model.Event
import com.vitorpamplona.amethyst.service.relays.Client
import com.vitorpamplona.amethyst.ui.actions.SignerDialog
import com.vitorpamplona.amethyst.ui.screen.NostrHashtagFeedViewModel import com.vitorpamplona.amethyst.ui.screen.NostrHashtagFeedViewModel
import com.vitorpamplona.amethyst.ui.screen.RefresheableFeedView import com.vitorpamplona.amethyst.ui.screen.RefresheableFeedView
import com.vitorpamplona.amethyst.ui.theme.StdPadding import com.vitorpamplona.amethyst.ui.theme.StdPadding
@@ -143,18 +150,39 @@ private fun HashtagActionOptions(
userState?.user?.isFollowingHashtagCached(tag) ?: false userState?.user?.isFollowingHashtagCached(tag) ?: false
} }
} }
var event by remember { mutableStateOf<Event?>(null) }
if (event != null) {
SignerDialog(
onClose = {
event = null
},
onPost = {
scope.launch(Dispatchers.IO) {
Client.send(it)
LocalCache.verifyAndConsume(it, null)
event = null
}
},
event = event!!
)
}
if (isFollowingTag) { if (isFollowingTag) {
UnfollowButton { UnfollowButton {
if (!accountViewModel.isWriteable()) { if (!accountViewModel.isWriteable()) {
scope.launch { if (PackageUtils.isAmberInstalled(context)) {
Toast event = accountViewModel.account.unfollowHashtag(tag, false)
.makeText( } else {
context, scope.launch {
context.getString(R.string.login_with_a_private_key_to_be_able_to_unfollow), Toast
Toast.LENGTH_SHORT .makeText(
) context,
.show() context.getString(R.string.login_with_a_private_key_to_be_able_to_unfollow),
Toast.LENGTH_SHORT
)
.show()
}
} }
} else { } else {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
@@ -165,14 +193,18 @@ private fun HashtagActionOptions(
} else { } else {
FollowButton { FollowButton {
if (!accountViewModel.isWriteable()) { if (!accountViewModel.isWriteable()) {
scope.launch { if (PackageUtils.isAmberInstalled(context)) {
Toast event = accountViewModel.account.followHashtag(tag, false)
.makeText( } else {
context, scope.launch {
context.getString(R.string.login_with_a_private_key_to_be_able_to_follow), Toast
Toast.LENGTH_SHORT .makeText(
) context,
.show() context.getString(R.string.login_with_a_private_key_to_be_able_to_follow),
Toast.LENGTH_SHORT
)
.show()
}
} }
} else { } else {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {