Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement follow approvals #237

Merged
merged 7 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ import net.primal.android.core.compose.foundation.rememberLazyListStatePagingWor
import net.primal.android.core.compose.heightAdjustableLoadingLazyListPlaceholder
import net.primal.android.core.compose.isEmpty
import net.primal.android.core.compose.isNotEmpty
import net.primal.android.core.compose.profile.approvals.ApproveBookmarkAlertDialog
import net.primal.android.core.compose.pulltorefresh.PrimalPullToRefreshBox
import net.primal.android.core.errors.UiError
import net.primal.android.notes.feed.note.ui.ConfirmFirstBookmarkAlertDialog
import net.primal.android.theme.AppTheme
import net.primal.android.thread.articles.ArticleContract
import net.primal.android.thread.articles.ArticleViewModel
Expand Down Expand Up @@ -239,7 +239,7 @@ private fun ArticleFeedLazyColumn(
when {
item != null -> Column {
if (articleState.shouldApproveBookmark) {
ConfirmFirstBookmarkAlertDialog(
ApproveBookmarkAlertDialog(
onBookmarkConfirmed = {
articleEventPublisher(
ArticleContract.UiEvent.BookmarkAction(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.primal.android.notes.feed.note.ui
package net.primal.android.core.compose.profile.approvals

import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
Expand All @@ -9,7 +9,7 @@ import net.primal.android.R
import net.primal.android.theme.AppTheme

@Composable
fun ConfirmFirstBookmarkAlertDialog(onClose: () -> Unit, onBookmarkConfirmed: () -> Unit) {
fun ApproveBookmarkAlertDialog(onClose: () -> Unit, onBookmarkConfirmed: () -> Unit) {
AlertDialog(
containerColor = AppTheme.colorScheme.surfaceVariant,
onDismissRequest = onClose,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package net.primal.android.core.compose.profile.approvals

import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import net.primal.android.R
import net.primal.android.theme.AppTheme

@Composable
fun ApproveFollowUnfollowProfileAlertDialog(
profileApproval: ProfileApproval,
onFollowApproved: () -> Unit,
onUnfollowApproved: () -> Unit,
onClose: () -> Unit,
) {
val messages = when (profileApproval) {
is ProfileApproval.Follow -> {
ApprovalMessages(
title = stringResource(id = R.string.context_confirm_follow_title),
text = stringResource(id = R.string.context_confirm_follow_text),
positive = stringResource(id = R.string.context_confirm_follow_positive),
negative = stringResource(id = R.string.context_confirm_follow_negative),
)
}

is ProfileApproval.Unfollow -> {
ApprovalMessages(
title = stringResource(id = R.string.context_confirm_unfollow_title),
text = stringResource(id = R.string.context_confirm_unfollow_text),
positive = stringResource(id = R.string.context_confirm_unfollow_positive),
negative = stringResource(id = R.string.context_confirm_unfollow_negative),
)
}
}
AlertDialog(
containerColor = AppTheme.colorScheme.surfaceVariant,
onDismissRequest = onClose,
title = {
Text(
text = messages.title,
style = AppTheme.typography.titleLarge,
)
},
text = {
Text(
text = messages.text,
style = AppTheme.typography.bodyLarge,
)
},
dismissButton = {
TextButton(onClick = onClose) {
Text(text = messages.negative)
}
},
confirmButton = {
TextButton(
onClick = when (profileApproval) {
is ProfileApproval.Follow -> onFollowApproved
is ProfileApproval.Unfollow -> onUnfollowApproved
},
) {
Text(
text = messages.positive,
)
}
},
)
}

private data class ApprovalMessages(
val title: String,
val text: String,
val positive: String,
val negative: String,
)

sealed class ProfileApproval(open val profileId: String) {
data class Follow(override val profileId: String) : ProfileApproval(profileId)
data class Unfollow(override val profileId: String) : ProfileApproval(profileId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import net.primal.android.core.compose.ListNoContent
import net.primal.android.core.compose.NostrUserText
import net.primal.android.core.compose.button.FollowUnfollowButton
import net.primal.android.core.compose.preview.PrimalPreview
import net.primal.android.core.compose.profile.approvals.ApproveFollowUnfollowProfileAlertDialog
import net.primal.android.core.compose.profile.model.ProfileDetailsUi
import net.primal.android.core.errors.UiError
import net.primal.android.core.utils.shortened
Expand Down Expand Up @@ -81,6 +82,29 @@ fun ExplorePeople(
eventPublisher: (ExplorePeopleContract.UiEvent) -> Unit,
onProfileClick: (String) -> Unit,
) {
if (state.shouldApproveProfileAction != null) {
ApproveFollowUnfollowProfileAlertDialog(
profileApproval = state.shouldApproveProfileAction,
onFollowApproved = {
eventPublisher(
ExplorePeopleContract.UiEvent.FollowUser(
userId = state.shouldApproveProfileAction.profileId,
forceUpdate = true,
),
)
},
onUnfollowApproved = {
eventPublisher(
ExplorePeopleContract.UiEvent.UnfollowUser(
userId = state.shouldApproveProfileAction.profileId,
forceUpdate = true,
),
)
},
onClose = { eventPublisher(ExplorePeopleContract.UiEvent.DismissConfirmFollowUnfollowAlertDialog) },
)
}

if (state.loading && state.people.isEmpty()) {
HeightAdjustableLoadingLazyListPlaceholder(
modifier = modifier.fillMaxSize(),
Expand Down Expand Up @@ -117,12 +141,18 @@ fun ExplorePeople(
onItemClick = { onProfileClick(item.profile.pubkey) },
onFollowClick = {
eventPublisher(
ExplorePeopleContract.UiEvent.FollowUser(item.profile.pubkey),
ExplorePeopleContract.UiEvent.FollowUser(
userId = item.profile.pubkey,
forceUpdate = false,
),
)
},
onUnfollowClick = {
eventPublisher(
ExplorePeopleContract.UiEvent.UnfollowUser(item.profile.pubkey),
ExplorePeopleContract.UiEvent.UnfollowUser(
userId = item.profile.pubkey,
forceUpdate = false,
),
)
},
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.primal.android.explore.home.people

import net.primal.android.core.compose.profile.approvals.ProfileApproval
import net.primal.android.core.errors.UiError
import net.primal.android.explore.api.model.ExplorePeopleData

Expand All @@ -10,12 +11,13 @@ interface ExplorePeopleContract {
val people: List<ExplorePeopleData> = emptyList(),
val userFollowing: Set<String> = emptySet(),
val error: UiError? = null,
val shouldApproveProfileAction: ProfileApproval? = null,
)

sealed class UiEvent {
data class FollowUser(val userId: String) : UiEvent()
data class UnfollowUser(val userId: String) : UiEvent()

data class FollowUser(val userId: String, val forceUpdate: Boolean) : UiEvent()
data class UnfollowUser(val userId: String, val forceUpdate: Boolean) : UiEvent()
data object DismissConfirmFollowUnfollowAlertDialog : UiEvent()
data object RefreshPeople : UiEvent()
data object DismissError : UiEvent()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.getAndUpdate
import kotlinx.coroutines.launch
import net.primal.android.core.compose.profile.approvals.ProfileApproval
import net.primal.android.core.errors.UiError
import net.primal.android.explore.home.people.ExplorePeopleContract.UiEvent
import net.primal.android.explore.home.people.ExplorePeopleContract.UiState
Expand Down Expand Up @@ -76,79 +77,95 @@ class ExplorePeopleViewModel @Inject constructor(
viewModelScope.launch {
events.collect {
when (it) {
is UiEvent.FollowUser -> follow(profileId = it.userId)
is UiEvent.UnfollowUser -> unfollow(profileId = it.userId)
is UiEvent.FollowUser -> follow(profileId = it.userId, forceUpdate = it.forceUpdate)
is UiEvent.UnfollowUser -> unfollow(profileId = it.userId, forceUpdate = it.forceUpdate)
UiEvent.RefreshPeople -> fetchExplorePeople()
UiEvent.DismissError -> setState { copy(error = null) }
UiEvent.DismissConfirmFollowUnfollowAlertDialog ->
setState { copy(shouldApproveProfileAction = null) }
}
}
}

private fun follow(profileId: String) =
private fun follow(profileId: String, forceUpdate: Boolean) =
viewModelScope.launch {
updateStateProfileFollow(profileId)
updateStateProfileFollowAndClearApprovalFlag(profileId)

val followResult = runCatching {
profileRepository.follow(
userId = activeAccountStore.activeUserId(),
followedUserId = profileId,
forceUpdate = forceUpdate,
)
}

if (followResult.isFailure) {
followResult.exceptionOrNull()?.let { error ->
Timber.w(error)
updateStateProfileUnfollowAndClearApprovalFlag(profileId)
when (error) {
is WssException, is NostrPublishException, is ProfileRepository.FollowListNotFound ->
is WssException, is NostrPublishException ->
setState { copy(error = UiError.FailedToFollowUser(error)) }

is MissingRelaysException -> setState {
copy(
error = UiError.MissingRelaysConfiguration(error),
)
is ProfileRepository.FollowListNotFound -> setState {
copy(shouldApproveProfileAction = ProfileApproval.Follow(profileId = profileId))
}

is MissingRelaysException ->
setState { copy(error = UiError.MissingRelaysConfiguration(error)) }

else -> setState { copy(error = UiError.GenericError()) }
}
updateStateProfileUnfollow(profileId)
}
}
}

private fun unfollow(profileId: String) =
private fun unfollow(profileId: String, forceUpdate: Boolean) =
viewModelScope.launch {
updateStateProfileUnfollow(profileId)
updateStateProfileUnfollowAndClearApprovalFlag(profileId)

val unfollowResult = runCatching {
profileRepository.unfollow(
userId = activeAccountStore.activeUserId(),
unfollowedUserId = profileId,
forceUpdate = forceUpdate,
)
}

if (unfollowResult.isFailure) {
updateStateProfileFollowAndClearApprovalFlag(profileId)
unfollowResult.exceptionOrNull()?.let { error ->
Timber.w(error)
when (error) {
is WssException, is NostrPublishException, is ProfileRepository.FollowListNotFound ->
is WssException, is NostrPublishException ->
setState { copy(error = UiError.FailedToUnfollowUser(error)) }

is MissingRelaysException -> setState {
copy(
error = UiError.MissingRelaysConfiguration(error),
)
is ProfileRepository.FollowListNotFound -> setState {
copy(shouldApproveProfileAction = ProfileApproval.Unfollow(profileId = profileId))
}

is MissingRelaysException ->
setState { copy(error = UiError.MissingRelaysConfiguration(error)) }

else -> setState { copy(error = UiError.GenericError()) }
}
updateStateProfileFollow(profileId)
}
}
}

private fun updateStateProfileUnfollow(profileId: String) =
setState { copy(userFollowing = userFollowing - profileId) }
private fun updateStateProfileUnfollowAndClearApprovalFlag(profileId: String) =
setState {
copy(
userFollowing = userFollowing - profileId,
shouldApproveProfileAction = null,
)
}

private fun updateStateProfileFollow(profileId: String) =
setState { copy(userFollowing = userFollowing + profileId) }
private fun updateStateProfileFollowAndClearApprovalFlag(profileId: String) =
setState {
copy(
userFollowing = userFollowing + profileId,
shouldApproveProfileAction = null,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import net.primal.android.attachments.domain.CdnImage
import net.primal.android.core.compose.AvatarThumbnailCustomBorder
import net.primal.android.core.compose.PrimalDivider
import net.primal.android.core.compose.preview.PrimalPreview
import net.primal.android.core.compose.profile.approvals.ApproveBookmarkAlertDialog
import net.primal.android.core.errors.UiError
import net.primal.android.core.ext.openUriSafely
import net.primal.android.notes.feed.NoteRepostOrQuoteBottomSheet
Expand All @@ -55,7 +56,6 @@ import net.primal.android.notes.feed.model.FeedPostAction
import net.primal.android.notes.feed.model.FeedPostUi
import net.primal.android.notes.feed.model.toNoteContentUi
import net.primal.android.notes.feed.note.NoteContract.UiEvent
import net.primal.android.notes.feed.note.ui.ConfirmFirstBookmarkAlertDialog
import net.primal.android.notes.feed.note.ui.FeedNoteActionsRow
import net.primal.android.notes.feed.note.ui.FeedNoteHeader
import net.primal.android.notes.feed.note.ui.NoteContent
Expand Down Expand Up @@ -221,7 +221,7 @@ private fun FeedNoteCard(
}

if (state.shouldApproveBookmark) {
ConfirmFirstBookmarkAlertDialog(
ApproveBookmarkAlertDialog(
onBookmarkConfirmed = {
eventPublisher(
UiEvent.BookmarkAction(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.primal.android.profile.details

import net.primal.android.core.compose.profile.approvals.ProfileApproval
import net.primal.android.core.compose.profile.model.ProfileDetailsUi
import net.primal.android.core.compose.profile.model.ProfileStatsUi
import net.primal.android.core.errors.UiError
Expand All @@ -26,6 +27,7 @@ interface ProfileDetailsContract {
ProfileFeedSpec.AuthoredMedia,
),
val error: ProfileError? = null,
val shouldApproveProfileAction: ProfileApproval? = null,
val zapError: UiError? = null,
val zappingState: ZappingState = ZappingState(),
) {
Expand All @@ -48,27 +50,26 @@ interface ProfileDetailsContract {
}

sealed class UiEvent {
data class FollowAction(val profileId: String) : UiEvent()
data class UnfollowAction(val profileId: String) : UiEvent()
data class AddProfileFeedAction(
val profileId: String,
val feedTitle: String,
val feedDescription: String,
) : UiEvent()

data class ZapProfile(
val profileId: String,
val profileLnUrlDecoded: String?,
val zapDescription: String? = null,
val zapAmount: ULong? = null,
) : UiEvent()

data class FollowAction(val profileId: String, val forceUpdate: Boolean) : UiEvent()
data class UnfollowAction(val profileId: String, val forceUpdate: Boolean) : UiEvent()
data class RemoveProfileFeedAction(val profileId: String) : UiEvent()
data class MuteAction(val profileId: String) : UiEvent()
data class UnmuteAction(val profileId: String) : UiEvent()
data object RequestProfileUpdate : UiEvent()
data class ReportAbuse(val type: ReportType, val profileId: String, val noteId: String? = null) : UiEvent()
data object DismissError : UiEvent()
data object DismissZapError : UiEvent()
data object DismissConfirmFollowUnfollowAlertDialog : UiEvent()
}
}
Loading
Loading