Skip to content

Commit

Permalink
Fix detekt issues
Browse files Browse the repository at this point in the history
  • Loading branch information
markocic committed Nov 28, 2024
1 parent fbe9c92 commit f00a04b
Showing 5 changed files with 143 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -88,38 +88,11 @@ fun ExplorePeople(
) {
var lastFollowUnfollowProfileId by rememberSaveable { mutableStateOf<String?>(null) }

if (state.shouldApproveFollow) {
ConfirmFollowUnfollowProfileAlertDialog(
onClose = { eventPublisher(ExplorePeopleContract.UiEvent.DismissConfirmFollowUnfollowAlertDialog) },
onActionConfirmed = {
lastFollowUnfollowProfileId?.let {
eventPublisher(
ExplorePeopleContract.UiEvent.FollowUser(
userId = it,
forceUpdate = true,
),
)
}
},
profileAction = ProfileAction.Follow,
)
}
if (state.shouldApproveUnfollow) {
ConfirmFollowUnfollowProfileAlertDialog(
onClose = { eventPublisher(ExplorePeopleContract.UiEvent.DismissConfirmFollowUnfollowAlertDialog) },
onActionConfirmed = {
lastFollowUnfollowProfileId?.let {
eventPublisher(
ExplorePeopleContract.UiEvent.UnfollowUser(
userId = it,
forceUpdate = true,
),
)
}
},
profileAction = ProfileAction.Unfollow,
)
}
ApprovalAlertDialogs(
state = state,
eventPublisher = eventPublisher,
lastFollowUnfollowProfileId = lastFollowUnfollowProfileId,
)

if (state.loading && state.people.isEmpty()) {
HeightAdjustableLoadingLazyListPlaceholder(
@@ -178,6 +151,46 @@ fun ExplorePeople(
}
}

@Composable
private fun ApprovalAlertDialogs(
state: ExplorePeopleContract.UiState,
eventPublisher: (ExplorePeopleContract.UiEvent) -> Unit,
lastFollowUnfollowProfileId: String?,
) {
if (state.shouldApproveFollow) {
ConfirmFollowUnfollowProfileAlertDialog(
onClose = { eventPublisher(ExplorePeopleContract.UiEvent.DismissConfirmFollowUnfollowAlertDialog) },
onActionConfirmed = {
lastFollowUnfollowProfileId?.let {
eventPublisher(
ExplorePeopleContract.UiEvent.FollowUser(
userId = it,
forceUpdate = true,
),
)
}
},
profileAction = ProfileAction.Follow,
)
}
if (state.shouldApproveUnfollow) {
ConfirmFollowUnfollowProfileAlertDialog(
onClose = { eventPublisher(ExplorePeopleContract.UiEvent.DismissConfirmFollowUnfollowAlertDialog) },
onActionConfirmed = {
lastFollowUnfollowProfileId?.let {
eventPublisher(
ExplorePeopleContract.UiEvent.UnfollowUser(
userId = it,
forceUpdate = true,
),
)
}
},
profileAction = ProfileAction.Unfollow,
)
}
}

@Composable
private fun ExplorePersonListItem(
modifier: Modifier = Modifier,
Original file line number Diff line number Diff line change
@@ -105,9 +105,17 @@ class ExplorePeopleViewModel @Inject constructor(
is WssException, is NostrPublishException, is ProfileRepository.FollowListNotFound ->
setState { copy(error = UiError.FailedToFollowUser(error)) }

is ProfileRepository.PossibleFollowListCorruption -> setState { copy(shouldApproveFollow = true) }

is MissingRelaysException -> setState { copy(error = UiError.MissingRelaysConfiguration(error)) }
is ProfileRepository.PossibleFollowListCorruption -> setState {
copy(
shouldApproveFollow = true,
)
}

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

else -> setState { copy(error = UiError.GenericError()) }
}
@@ -135,9 +143,17 @@ class ExplorePeopleViewModel @Inject constructor(
is WssException, is NostrPublishException, is ProfileRepository.FollowListNotFound ->
setState { copy(error = UiError.FailedToUnfollowUser(error)) }

is ProfileRepository.PossibleFollowListCorruption -> setState { copy(shouldApproveUnfollow = true) }

is MissingRelaysException -> setState { copy(error = UiError.MissingRelaysConfiguration(error)) }
is ProfileRepository.PossibleFollowListCorruption -> setState {
copy(
shouldApproveUnfollow = true,
)
}

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

else -> setState { copy(error = UiError.GenericError()) }
}
Original file line number Diff line number Diff line change
@@ -14,21 +14,21 @@ fun ConfirmFollowUnfollowProfileAlertDialog(
onActionConfirmed: () -> Unit,
profileAction: ProfileAction,
) {
val (title, text, positive, negative) = when (profileAction) {
val messages = when (profileAction) {
ProfileAction.Follow -> {
arrayOf(
stringResource(id = R.string.context_confirm_follow_title),
stringResource(id = R.string.context_confirm_follow_text),
stringResource(id = R.string.context_confirm_follow_positive),
stringResource(id = R.string.context_confirm_follow_negative),
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),
)
}
ProfileAction.Unfollow -> {
arrayOf(
stringResource(id = R.string.context_confirm_unfollow_title),
stringResource(id = R.string.context_confirm_unfollow_text),
stringResource(id = R.string.context_confirm_unfollow_positive),
stringResource(id = R.string.context_confirm_unfollow_negative),
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),
)
}
}
@@ -37,31 +37,38 @@ fun ConfirmFollowUnfollowProfileAlertDialog(
onDismissRequest = onClose,
title = {
Text(
text = title,
text = messages.title,
style = AppTheme.typography.titleLarge,
)
},
text = {
Text(
text = text,
text = messages.text,
style = AppTheme.typography.bodyLarge,
)
},
dismissButton = {
TextButton(onClick = onClose) {
Text(text = negative)
Text(text = messages.negative)
}
},
confirmButton = {
TextButton(onClick = onActionConfirmed) {
Text(
text = positive,
text = messages.positive,
)
}
},
)
}

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

enum class ProfileAction {
Follow,
Unfollow,
Original file line number Diff line number Diff line change
@@ -76,38 +76,11 @@ private fun ProfileFollowsScreen(
onErrorDismiss = { eventPublisher(ProfileFollowsContract.UiEvent.DismissError) },
)

if (state.shouldApproveFollow) {
ConfirmFollowUnfollowProfileAlertDialog(
onClose = { eventPublisher(ProfileFollowsContract.UiEvent.DismissConfirmFollowUnfollowAlertDialog) },
onActionConfirmed = {
lastFollowUnfollowProfileId?.let {
eventPublisher(
ProfileFollowsContract.UiEvent.FollowProfile(
profileId = it,
forceUpdate = true,
),
)
}
},
profileAction = ProfileAction.Follow,
)
}
if (state.shouldApproveUnfollow) {
ConfirmFollowUnfollowProfileAlertDialog(
onClose = { eventPublisher(ProfileFollowsContract.UiEvent.DismissConfirmFollowUnfollowAlertDialog) },
onActionConfirmed = {
lastFollowUnfollowProfileId?.let {
eventPublisher(
ProfileFollowsContract.UiEvent.UnfollowProfile(
profileId = it,
forceUpdate = true,
),
)
}
},
profileAction = ProfileAction.Unfollow,
)
}
FollowApprovalAlertDialogs(
state = state,
eventPublisher = eventPublisher,
lastFollowUnfollowProfileId = lastFollowUnfollowProfileId,
)

Scaffold(
modifier = Modifier,
@@ -156,6 +129,46 @@ private fun ProfileFollowsScreen(
)
}

@Composable
private fun FollowApprovalAlertDialogs(
state: ProfileFollowsContract.UiState,
eventPublisher: (ProfileFollowsContract.UiEvent) -> Unit,
lastFollowUnfollowProfileId: String?,
) {
if (state.shouldApproveFollow) {
ConfirmFollowUnfollowProfileAlertDialog(
onClose = { eventPublisher(ProfileFollowsContract.UiEvent.DismissConfirmFollowUnfollowAlertDialog) },
onActionConfirmed = {
lastFollowUnfollowProfileId?.let {
eventPublisher(
ProfileFollowsContract.UiEvent.FollowProfile(
profileId = it,
forceUpdate = true,
),
)
}
},
profileAction = ProfileAction.Follow,
)
}
if (state.shouldApproveUnfollow) {
ConfirmFollowUnfollowProfileAlertDialog(
onClose = { eventPublisher(ProfileFollowsContract.UiEvent.DismissConfirmFollowUnfollowAlertDialog) },
onActionConfirmed = {
lastFollowUnfollowProfileId?.let {
eventPublisher(
ProfileFollowsContract.UiEvent.UnfollowProfile(
profileId = it,
forceUpdate = true,
),
)
}
},
profileAction = ProfileAction.Unfollow,
)
}
}

@Composable
private fun FollowsLazyColumn(
paddingValues: PaddingValues,
Original file line number Diff line number Diff line change
@@ -138,21 +138,21 @@ class ProfileRepository @Inject constructor(
forceUpdate: Boolean,
reducer: Set<String>.() -> Set<String>,
) = withContext(dispatchers.io()) {
val userFollowList = userAccountFetcher.fetchUserFollowListOrNull(userId = userId)
?: throw FollowListNotFound()
val userFollowList = userAccountFetcher.fetchUserFollowListOrNull(userId = userId)
?: throw FollowListNotFound()

if (userFollowList.following.isEmpty() && !forceUpdate) {
if (userFollowList.following.isEmpty() && !forceUpdate) {
throw PossibleFollowListCorruption()
}

userRepository.updateFollowList(userId, userFollowList)

setFollowList(
userId = userId,
contacts = userFollowList.following.reducer(),
content = userFollowList.followListEventContent ?: "",
)
}
setFollowList(
userId = userId,
contacts = userFollowList.following.reducer(),
content = userFollowList.followListEventContent ?: "",
)
}

@Throws(NostrPublishException::class)
suspend fun setFollowList(

0 comments on commit f00a04b

Please sign in to comment.