From 541cf7255dd9f3ab054766e2427aca0d314c0f9c Mon Sep 17 00:00:00 2001 From: Diego Beraldin Date: Wed, 5 Jun 2024 07:59:48 +0200 Subject: [PATCH] enhancement: improve login use case (#930) --- .../identity/usecase/DefaultLoginUseCase.kt | 18 +- .../unit/createcomment/CreateCommentScreen.kt | 256 +-- .../createcomment/CreateCommentViewModel.kt | 10 +- .../unit/postdetail/PostDetailScreen.kt | 1646 +++++++++-------- .../unit/postdetail/PostDetailViewModel.kt | 3 +- 5 files changed, 970 insertions(+), 963 deletions(-) diff --git a/domain/identity/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/identity/usecase/DefaultLoginUseCase.kt b/domain/identity/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/identity/usecase/DefaultLoginUseCase.kt index 0eb33f2d8..a581e5956 100644 --- a/domain/identity/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/identity/usecase/DefaultLoginUseCase.kt +++ b/domain/identity/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/domain/identity/usecase/DefaultLoginUseCase.kt @@ -55,12 +55,12 @@ internal class DefaultLoginUseCase( instance = instance, jwt = auth, ) - val existingId = accountRepository.getBy(username, instance)?.id - val id = - existingId ?: run { + val existing = accountRepository.getBy(username, instance) + val accountId = + if (existing == null) { // new account with a copy of the anonymous settings // (except a couple of fields from the Lemmy accounts) - val res = accountRepository.createAccount(account) + val newAccountId = accountRepository.createAccount(account) val anonymousSettings = settingsRepository.getSettings(null) .copy( @@ -69,20 +69,22 @@ internal class DefaultLoginUseCase( ) settingsRepository.createSettings( settings = anonymousSettings, - accountId = res, + accountId = newAccountId, ) - res + newAccountId + } else { + existing.id ?: 0 } val oldActiveAccountId = accountRepository.getActive()?.id if (oldActiveAccountId != null) { accountRepository.setActive(oldActiveAccountId, false) } - accountRepository.setActive(id, true) + accountRepository.setActive(accountId, true) communitySortRepository.clear() communityPreferredLanguageRepository.clear() - val newSettings = settingsRepository.getSettings(id) + val newSettings = settingsRepository.getSettings(accountId) settingsRepository.changeCurrentSettings(newSettings) } } diff --git a/unit/createcomment/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/createcomment/CreateCommentScreen.kt b/unit/createcomment/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/createcomment/CreateCommentScreen.kt index e1c0edb00..bcc2420a4 100644 --- a/unit/createcomment/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/createcomment/CreateCommentScreen.kt +++ b/unit/createcomment/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/createcomment/CreateCommentScreen.kt @@ -138,16 +138,16 @@ class CreateCommentScreen( uiState.originalPost?.also { originalPost -> notificationCenter.send( event = - NotificationCenterEvent.PostUpdated( - originalPost.copy( - comments = - if (effect.new) { - originalPost.comments + 1 - } else { - originalPost.comments - }, + NotificationCenterEvent.PostUpdated( + originalPost.copy( + comments = + if (effect.new) { + originalPost.comments + 1 + } else { + originalPost.comments + }, + ), ), - ), ) } navigationCoordinator.popScreen() @@ -171,11 +171,11 @@ class CreateCommentScreen( navigationIcon = { Image( modifier = - Modifier.padding(start = Spacing.s).onClick( - onClick = { - navigationCoordinator.popScreen() - }, - ), + Modifier.padding(start = Spacing.s).onClick( + onClick = { + navigationCoordinator.popScreen() + }, + ), imageVector = Icons.Default.Close, contentDescription = null, colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.onBackground), @@ -184,15 +184,15 @@ class CreateCommentScreen( title = { Text( text = - when { - uiState.editedComment != null -> { - LocalXmlStrings.current.editCommentTitle - } + when { + uiState.editedComment != null -> { + LocalXmlStrings.current.editCommentTitle + } - else -> { - LocalXmlStrings.current.createCommentTitle - } - }, + else -> { + LocalXmlStrings.current.createCommentTitle + } + }, color = MaterialTheme.colorScheme.onBackground, style = MaterialTheme.typography.titleMedium, ) @@ -208,9 +208,9 @@ class CreateCommentScreen( ) }, onClick = - rememberCallback(model) { - model.reduce(CreateCommentMviModel.Intent.SaveDraft) - }, + rememberCallback(model) { + model.reduce(CreateCommentMviModel.Intent.SaveDraft) + }, ) } IconButton( @@ -222,9 +222,9 @@ class CreateCommentScreen( ) }, onClick = - rememberCallback(model) { - model.reduce(CreateCommentMviModel.Intent.Send) - }, + rememberCallback(model) { + model.reduce(CreateCommentMviModel.Intent.Send) + }, ) }, ) @@ -232,21 +232,21 @@ class CreateCommentScreen( ) { padding -> Box( modifier = - Modifier - .padding( - top = padding.calculateTopPadding(), - ) - .consumeWindowInsets(padding) - .safeImePadding() - .fillMaxSize(), + Modifier + .padding( + top = padding.calculateTopPadding(), + ) + .consumeWindowInsets(padding) + .safeImePadding() + .fillMaxSize(), ) { // reference post or comment Box( modifier = - Modifier - .align(Alignment.TopCenter) - .fillMaxWidth() - .verticalScroll(rememberScrollState()), + Modifier + .align(Alignment.TopCenter) + .fillMaxWidth() + .verticalScroll(rememberScrollState()), ) { val referenceModifier = Modifier.padding( @@ -270,14 +270,14 @@ class CreateCommentScreen( showScores = uiState.showScores, downVoteEnabled = uiState.downVoteEnabled, options = - buildList { - add( - Option( - OptionId.SeeRaw, - LocalXmlStrings.current.postActionSeeRaw, - ), - ) - }, + buildList { + add( + Option( + OptionId.SeeRaw, + LocalXmlStrings.current.postActionSeeRaw, + ), + ) + }, onOptionSelected = { rawContent = originalComment }, @@ -290,11 +290,11 @@ class CreateCommentScreen( PostCard( modifier = referenceModifier, postLayout = - if (uiState.postLayout == PostLayout.Card) { - uiState.postLayout - } else { - PostLayout.Full - }, + if (uiState.postLayout == PostLayout.Card) { + uiState.postLayout + } else { + PostLayout.Full + }, fullHeightImage = uiState.fullHeightImages, fullWidthImage = uiState.fullWidthImages, post = originalPost, @@ -306,14 +306,14 @@ class CreateCommentScreen( showScores = uiState.showScores, downVoteEnabled = uiState.downVoteEnabled, options = - buildList { - add( - Option( - OptionId.SeeRaw, - LocalXmlStrings.current.postActionSeeRaw, - ), - ) - }, + buildList { + add( + Option( + OptionId.SeeRaw, + LocalXmlStrings.current.postActionSeeRaw, + ), + ) + }, onOptionSelected = { rawContent = originalPost }, @@ -325,42 +325,42 @@ class CreateCommentScreen( // form fields Column( modifier = - Modifier - .align(Alignment.BottomCenter) - .background(MaterialTheme.colorScheme.background) - .fillMaxWidth(), + Modifier + .align(Alignment.BottomCenter) + .background(MaterialTheme.colorScheme.background) + .fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(Spacing.xs), ) { SectionSelector( titles = - listOf( - LocalXmlStrings.current.createPostTabEditor, - LocalXmlStrings.current.createPostTabPreview, - ), + listOf( + LocalXmlStrings.current.createPostTabEditor, + LocalXmlStrings.current.createPostTabPreview, + ), currentSection = - when (uiState.section) { - CreatePostSection.Preview -> 1 - else -> 0 - }, + when (uiState.section) { + CreatePostSection.Preview -> 1 + else -> 0 + }, onSectionSelected = - rememberCallbackArgs { id -> - val section = - when (id) { - 1 -> CreatePostSection.Preview - else -> CreatePostSection.Edit - } - model.reduce(CreateCommentMviModel.Intent.ChangeSection(section)) - }, + rememberCallbackArgs { id -> + val section = + when (id) { + 1 -> CreatePostSection.Preview + else -> CreatePostSection.Edit + } + model.reduce(CreateCommentMviModel.Intent.ChangeSection(section)) + }, ) if (uiState.section == CreatePostSection.Edit) { TextFormattingBar( modifier = - Modifier.padding( - top = Spacing.s, - start = Spacing.s, - end = Spacing.s, - ), + Modifier.padding( + top = Spacing.s, + start = Spacing.s, + end = Spacing.s, + ), textFieldValue = uiState.textValue, onTextFieldValueChanged = { value -> model.reduce(CreateCommentMviModel.Intent.ChangeTextValue(value)) @@ -376,16 +376,16 @@ class CreateCommentScreen( ) TextField( modifier = - Modifier - .focusRequester(commentFocusRequester) - .heightIn(min = 300.dp, max = 400.dp) - .fillMaxWidth(), + Modifier + .focusRequester(commentFocusRequester) + .heightIn(min = 300.dp, max = 400.dp) + .fillMaxWidth(), colors = - TextFieldDefaults.colors( - focusedContainerColor = Color.Transparent, - unfocusedContainerColor = Color.Transparent, - disabledContainerColor = Color.Transparent, - ), + TextFieldDefaults.colors( + focusedContainerColor = Color.Transparent, + unfocusedContainerColor = Color.Transparent, + disabledContainerColor = Color.Transparent, + ), label = { Text( text = LocalXmlStrings.current.createCommentBody, @@ -395,11 +395,11 @@ class CreateCommentScreen( textStyle = typography.bodyMedium, value = uiState.textValue, keyboardOptions = - KeyboardOptions( - keyboardType = KeyboardType.Text, - autoCorrect = true, - capitalization = KeyboardCapitalization.Sentences, - ), + KeyboardOptions( + keyboardType = KeyboardType.Text, + autoCorrect = true, + capitalization = KeyboardCapitalization.Sentences, + ), onValueChange = { value -> model.reduce(CreateCommentMviModel.Intent.ChangeTextValue(value)) }, @@ -417,15 +417,15 @@ class CreateCommentScreen( } else { Box( modifier = - Modifier - .heightIn(min = 300.dp, max = 500.dp) - .fillMaxWidth(), + Modifier + .heightIn(min = 300.dp, max = 500.dp) + .fillMaxWidth(), ) { PostCardBody( modifier = - Modifier - .padding(Spacing.s) - .verticalScroll(rememberScrollState()), + Modifier + .padding(Spacing.s) + .verticalScroll(rememberScrollState()), text = uiState.textValue.text, autoLoadImages = uiState.autoLoadImages, ) @@ -435,23 +435,23 @@ class CreateCommentScreen( if (uiState.currentUser.isNotEmpty()) { Text( modifier = - Modifier - .fillMaxWidth() - .padding( - start = Spacing.m, - end = Spacing.m, - bottom = Spacing.s, - ), + Modifier + .fillMaxWidth() + .padding( + start = Spacing.m, + end = Spacing.m, + bottom = Spacing.s, + ), text = - buildString { - append(LocalXmlStrings.current.postReplySourceAccount) - append(" ") - append(uiState.currentUser) - if (uiState.currentInstance.isNotEmpty()) { - append("@") - append(uiState.currentInstance) - } - }, + buildString { + append(LocalXmlStrings.current.postReplySourceAccount) + append(" ") + append(uiState.currentUser) + if (uiState.currentInstance.isNotEmpty()) { + append("@") + append(uiState.currentInstance) + } + }, color = MaterialTheme.colorScheme.onBackground, style = MaterialTheme.typography.labelSmall, textDecoration = TextDecoration.Underline, @@ -509,14 +509,14 @@ class CreateCommentScreen( languages = uiState.availableLanguages, currentLanguageId = uiState.currentLanguageId, onSelect = - rememberCallbackArgs { langId -> - model.reduce(CreateCommentMviModel.Intent.ChangeLanguage(langId)) - selectLanguageDialogOpen = false - }, + rememberCallbackArgs { langId -> + model.reduce(CreateCommentMviModel.Intent.ChangeLanguage(langId)) + selectLanguageDialogOpen = false + }, onDismiss = - rememberCallback { - selectLanguageDialogOpen = false - }, + rememberCallback { + selectLanguageDialogOpen = false + }, ) } } diff --git a/unit/createcomment/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/createcomment/CreateCommentViewModel.kt b/unit/createcomment/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/createcomment/CreateCommentViewModel.kt index 933d612a9..1ef81dfce 100644 --- a/unit/createcomment/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/createcomment/CreateCommentViewModel.kt +++ b/unit/createcomment/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/createcomment/CreateCommentViewModel.kt @@ -267,11 +267,11 @@ class CreateCommentViewModel( languageId = languageId, date = epochMillis(), reference = - if (currentState.originalComment != null) { - currentState.originalComment.text - } else { - currentState.originalPost?.title - }, + if (currentState.originalComment != null) { + currentState.originalComment.text + } else { + currentState.originalPost?.title + }, ) if (draftId == null) { draftRepository.create( diff --git a/unit/postdetail/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/postdetail/PostDetailScreen.kt b/unit/postdetail/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/postdetail/PostDetailScreen.kt index 6a0cca57a..24ac8c691 100644 --- a/unit/postdetail/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/postdetail/PostDetailScreen.kt +++ b/unit/postdetail/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/postdetail/PostDetailScreen.kt @@ -308,11 +308,11 @@ class PostDetailScreen( TopAppBar( windowInsets = - if (settings.edgeToEdge) { - WindowInsets(0, topInset.roundToInt(), 0, 0) - } else { - TopAppBarDefaults.windowInsets - }, + if (settings.edgeToEdge) { + WindowInsets(0, topInset.roundToInt(), 0, 0) + } else { + TopAppBarDefaults.windowInsets + }, scrollBehavior = scrollBehavior, title = { Text( @@ -326,18 +326,18 @@ class PostDetailScreen( actions = { Image( modifier = - Modifier - .padding(horizontal = Spacing.xs) - .onClick( - onClick = { - val sheet = - SortBottomSheet( - comments = true, - values = uiState.availableSortTypes.map { it.toInt() }, - ) - navigationCoordinator.showBottomSheet(sheet) - }, - ), + Modifier + .padding(horizontal = Spacing.xs) + .onClick( + onClick = { + val sheet = + SortBottomSheet( + comments = true, + values = uiState.availableSortTypes.map { it.toInt() }, + ) + navigationCoordinator.showBottomSheet(sheet) + }, + ), imageVector = uiState.sortType.toIcon(), contentDescription = null, colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.onBackground), @@ -476,13 +476,13 @@ class PostDetailScreen( var optionsOffset by remember { mutableStateOf(Offset.Zero) } Image( modifier = - Modifier.onGloballyPositioned { - optionsOffset = it.positionInParent() - }.onClick( - onClick = { - optionsExpanded = true - }, - ), + Modifier.onGloballyPositioned { + optionsOffset = it.positionInParent() + }.onClick( + onClick = { + optionsExpanded = true + }, + ), imageVector = Icons.Default.MoreVert, contentDescription = null, colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.onBackground), @@ -493,10 +493,10 @@ class PostDetailScreen( optionsExpanded = false }, offset = - DpOffset( - x = optionsOffset.x.toLocalDp(), - y = optionsOffset.y.toLocalDp(), - ), + DpOffset( + x = optionsOffset.x.toLocalDp(), + y = optionsOffset.y.toLocalDp(), + ), ) { options.forEach { option -> DropdownMenuItem( @@ -582,8 +582,8 @@ class PostDetailScreen( BanUserScreen( userId = userId, communityId = - uiState.post.community?.id - ?: 0, + uiState.post.community?.id + ?: 0, newValue = uiState.post.creator?.banned != true, postId = uiState.post.id, ) @@ -661,11 +661,11 @@ class PostDetailScreen( if (navigationCoordinator.canPop.value) { Image( modifier = - Modifier.onClick( - onClick = { - navigationCoordinator.popScreen() - }, - ), + Modifier.onClick( + onClick = { + navigationCoordinator.popScreen() + }, + ), imageVector = Icons.AutoMirrored.Default.ArrowBack, contentDescription = null, colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.onBackground), @@ -678,56 +678,56 @@ class PostDetailScreen( AnimatedVisibility( visible = isFabVisible, enter = - slideInVertically( - initialOffsetY = { it * 2 }, - ), + slideInVertically( + initialOffsetY = { it * 2 }, + ), exit = - slideOutVertically( - targetOffsetY = { it * 2 }, - ), + slideOutVertically( + targetOffsetY = { it * 2 }, + ), ) { FloatingActionButtonMenu( modifier = - Modifier.then( - if (uiState.enableButtonsToScrollBetweenComments) { - Modifier.padding( - bottom = Spacing.s + bottomNavigationInset, - ) - } else { - Modifier - }, - ), + Modifier.then( + if (uiState.enableButtonsToScrollBetweenComments) { + Modifier.padding( + bottom = Spacing.s + bottomNavigationInset, + ) + } else { + Modifier + }, + ), items = - buildList { - this += - FloatingActionButtonMenuItem( - icon = Icons.Default.ExpandLess, - text = LocalXmlStrings.current.actionBackToTop, - onSelected = - rememberCallback { - scope.launch { - runCatching { - lazyListState.scrollToItem(0) - topAppBarState.heightOffset = 0f - topAppBarState.contentOffset = 0f - } - } - }, - ) - if (uiState.isLogged && !isOnOtherInstance) { + buildList { this += FloatingActionButtonMenuItem( - icon = Icons.AutoMirrored.Default.Reply, - text = LocalXmlStrings.current.actionReply, + icon = Icons.Default.ExpandLess, + text = LocalXmlStrings.current.actionBackToTop, onSelected = - rememberCallback { - detailOpener.openReply( - originalPost = uiState.post, - ) - }, + rememberCallback { + scope.launch { + runCatching { + lazyListState.scrollToItem(0) + topAppBarState.heightOffset = 0f + topAppBarState.contentOffset = 0f + } + } + }, ) - } - }, + if (uiState.isLogged && !isOnOtherInstance) { + this += + FloatingActionButtonMenuItem( + icon = Icons.AutoMirrored.Default.Reply, + text = LocalXmlStrings.current.actionReply, + onSelected = + rememberCallback { + detailOpener.openReply( + originalPost = uiState.post, + ) + }, + ) + } + }, ) } }, @@ -746,60 +746,61 @@ class PostDetailScreen( } Box( modifier = - Modifier - .padding( - top = padding.calculateTopPadding(), - ) - .fillMaxSize(), + Modifier + .padding( + top = padding.calculateTopPadding(), + ) + .fillMaxSize(), ) { Column( - modifier = Modifier - .then( - if (uiState.enableButtonsToScrollBetweenComments) { - Modifier.nestedScroll(buttonBarScrollConnection) - } else { - Modifier - }, - ), + modifier = + Modifier + .then( + if (uiState.enableButtonsToScrollBetweenComments) { + Modifier.nestedScroll(buttonBarScrollConnection) + } else { + Modifier + }, + ), ) { if (uiState.searching) { TextField( modifier = - Modifier - .padding( - horizontal = Spacing.xs, - vertical = Spacing.s, - ).fillMaxWidth(), + Modifier + .padding( + horizontal = Spacing.xs, + vertical = Spacing.s, + ).fillMaxWidth(), label = { Text(text = LocalXmlStrings.current.exploreSearchPlaceholder) }, singleLine = true, value = uiState.searchText, keyboardOptions = - KeyboardOptions( - keyboardType = KeyboardType.Text, - imeAction = ImeAction.Search, - ), + KeyboardOptions( + keyboardType = KeyboardType.Text, + imeAction = ImeAction.Search, + ), onValueChange = { value -> model.reduce(PostDetailMviModel.Intent.SetSearch(value)) }, trailingIcon = { Icon( modifier = - Modifier.onClick( - onClick = - rememberCallback(model) { - if (uiState.searchText.isNotEmpty()) { - model.reduce(PostDetailMviModel.Intent.SetSearch("")) - } - }, - ), + Modifier.onClick( + onClick = + rememberCallback(model) { + if (uiState.searchText.isNotEmpty()) { + model.reduce(PostDetailMviModel.Intent.SetSearch("")) + } + }, + ), imageVector = - if (uiState.searchText.isEmpty()) { - Icons.Default.Search - } else { - Icons.Default.Clear - }, + if (uiState.searchText.isEmpty()) { + Icons.Default.Search + } else { + Icons.Default.Clear + }, contentDescription = null, ) }, @@ -809,24 +810,24 @@ class PostDetailScreen( rememberPullRefreshState( refreshing = uiState.refreshing, onRefresh = - rememberCallback(model) { - model.reduce(PostDetailMviModel.Intent.Refresh) - }, + rememberCallback(model) { + model.reduce(PostDetailMviModel.Intent.Refresh) + }, ) Box( modifier = - Modifier - .then( - if (settings.hideNavigationBarWhileScrolling) { - Modifier.nestedScroll(scrollBehavior.nestedScrollConnection) - } else { - Modifier - }, - ) - .nestedScroll(fabNestedScrollConnection) - .nestedScroll(keyboardScrollConnection) - .pullRefresh(pullRefreshState), + Modifier + .then( + if (settings.hideNavigationBarWhileScrolling) { + Modifier.nestedScroll(scrollBehavior.nestedScrollConnection) + } else { + Modifier + }, + ) + .nestedScroll(fabNestedScrollConnection) + .nestedScroll(keyboardScrollConnection) + .pullRefresh(pullRefreshState), ) { LazyColumn( modifier = Modifier.fillMaxSize(), @@ -836,18 +837,18 @@ class PostDetailScreen( PostCard( post = uiState.post, isFromModerator = - uiState.post.creator?.id.let { creatorId -> - uiState.isModerator && + uiState.post.creator?.id.let { creatorId -> + uiState.isModerator && uiState.moderators.containsId( creatorId, ) - }, + }, postLayout = - if (uiState.postLayout == PostLayout.Card) { - uiState.postLayout - } else { - PostLayout.Full - }, + if (uiState.postLayout == PostLayout.Card) { + uiState.postLayout + } else { + PostLayout.Full + }, fullHeightImage = uiState.fullHeightImages, fullWidthImage = uiState.fullWidthImages, includeFullBody = true, @@ -859,64 +860,64 @@ class PostDetailScreen( blurNsfw = false, downVoteEnabled = uiState.downVoteEnabled, onOpenCommunity = - rememberCallbackArgs { community, instance -> - detailOpener.openCommunityDetail(community, instance) - }, + rememberCallbackArgs { community, instance -> + detailOpener.openCommunityDetail(community, instance) + }, onOpenCreator = - rememberCallbackArgs { user, instance -> - detailOpener.openUserDetail(user, instance) - }, + rememberCallbackArgs { user, instance -> + detailOpener.openUserDetail(user, instance) + }, onOpenPost = - rememberCallbackArgs { p, instance -> - detailOpener.openPostDetail(p, instance) - }, + rememberCallbackArgs { p, instance -> + detailOpener.openPostDetail(p, instance) + }, onOpenWeb = - rememberCallbackArgs { url -> - navigationCoordinator.pushScreen( - WebViewScreen(url), - ) - }, + rememberCallbackArgs { url -> + navigationCoordinator.pushScreen( + WebViewScreen(url), + ) + }, onUpVote = - rememberCallback(model) { - if (uiState.isLogged && !isOnOtherInstance) { - model.reduce( - PostDetailMviModel.Intent.UpVotePost(), - ) - } - }, + rememberCallback(model) { + if (uiState.isLogged && !isOnOtherInstance) { + model.reduce( + PostDetailMviModel.Intent.UpVotePost(), + ) + } + }, onDownVote = - rememberCallback(model) { - if (uiState.isLogged && !isOnOtherInstance) { - model.reduce( - PostDetailMviModel.Intent.DownVotePost(), - ) - } - }, + rememberCallback(model) { + if (uiState.isLogged && !isOnOtherInstance) { + model.reduce( + PostDetailMviModel.Intent.DownVotePost(), + ) + } + }, onSave = - rememberCallback(model) { - model.reduce( - PostDetailMviModel.Intent.SavePost( - post = uiState.post, - ), - ) - }, - onReply = - rememberCallback { - if (uiState.isLogged && !isOnOtherInstance) { - detailOpener.openReply( - originalPost = uiState.post, + rememberCallback(model) { + model.reduce( + PostDetailMviModel.Intent.SavePost( + post = uiState.post, + ), ) - } - }, + }, + onReply = + rememberCallback { + if (uiState.isLogged && !isOnOtherInstance) { + detailOpener.openReply( + originalPost = uiState.post, + ) + } + }, onOpenImage = - rememberCallbackArgs { url -> - navigationCoordinator.pushScreen( - ZoomableImageScreen( - url = url, - source = uiState.post.community?.readableHandle.orEmpty(), - ), - ) - }, + rememberCallbackArgs { url -> + navigationCoordinator.pushScreen( + ZoomableImageScreen( + url = url, + source = uiState.post.community?.readableHandle.orEmpty(), + ), + ) + }, ) if (uiState.postLayout != PostLayout.Card) { HorizontalDivider(modifier = Modifier.padding(vertical = Spacing.interItem)) @@ -928,12 +929,12 @@ class PostDetailScreen( item { FlowRow( modifier = - Modifier.padding( - top = Spacing.xxs, - bottom = Spacing.s, - start = Spacing.s, - end = Spacing.s, - ), + Modifier.padding( + top = Spacing.xxs, + bottom = Spacing.s, + start = Spacing.s, + end = Spacing.s, + ), horizontalArrangement = Arrangement.spacedBy(Spacing.xxs), ) { Text( @@ -954,19 +955,19 @@ class PostDetailScreen( } Text( modifier = - Modifier.onClick( - onClick = { - val post = - PostModel( - id = crossPost.id, - community = community, + Modifier.onClick( + onClick = { + val post = + PostModel( + id = crossPost.id, + community = community, + ) + detailOpener.openPostDetail( + post = post, + otherInstance = otherInstanceName, ) - detailOpener.openPostDetail( - post = post, - otherInstance = otherInstanceName, - ) - }, - ), + }, + ), text = string, style = MaterialTheme.typography.bodyMedium, ) @@ -1009,16 +1010,16 @@ class PostDetailScreen( ) }, backgroundColor = - upVoteColor - ?: defaultUpvoteColor, + upVoteColor + ?: defaultUpvoteColor, onTriggered = - rememberCallback { - model.reduce( - PostDetailMviModel.Intent.UpVoteComment( - comment.id, - ), - ) - }, + rememberCallback { + model.reduce( + PostDetailMviModel.Intent.UpVoteComment( + comment.id, + ), + ) + }, ) ActionOnSwipe.DownVote -> @@ -1034,16 +1035,16 @@ class PostDetailScreen( ) }, backgroundColor = - downVoteColor - ?: defaultDownVoteColor, + downVoteColor + ?: defaultDownVoteColor, onTriggered = - rememberCallback(model) { - model.reduce( - PostDetailMviModel.Intent.DownVoteComment( - comment.id, - ), - ) - }, + rememberCallback(model) { + model.reduce( + PostDetailMviModel.Intent.DownVoteComment( + comment.id, + ), + ) + }, ) } @@ -1057,15 +1058,15 @@ class PostDetailScreen( ) }, backgroundColor = - replyColor - ?: defaultReplyColor, + replyColor + ?: defaultReplyColor, onTriggered = - rememberCallback { - detailOpener.openReply( - originalPost = uiState.post, - originalComment = comment, - ) - }, + rememberCallback { + detailOpener.openReply( + originalPost = uiState.post, + originalComment = comment, + ) + }, ) ActionOnSwipe.Save -> @@ -1078,16 +1079,16 @@ class PostDetailScreen( ) }, backgroundColor = - saveColor - ?: defaultSaveColor, + saveColor + ?: defaultSaveColor, onTriggered = - rememberCallback(model) { - model.reduce( - PostDetailMviModel.Intent.SaveComment( - commentId = comment.id, - ), - ) - }, + rememberCallback(model) { + model.reduce( + PostDetailMviModel.Intent.SaveComment( + commentId = comment.id, + ), + ) + }, ) else -> null @@ -1099,37 +1100,37 @@ class PostDetailScreen( modifier = Modifier.fillMaxWidth(), enabled = uiState.swipeActionsEnabled, onGestureBegin = - rememberCallback(model) { - model.reduce(PostDetailMviModel.Intent.HapticIndication) - }, + rememberCallback(model) { + model.reduce(PostDetailMviModel.Intent.HapticIndication) + }, swipeToStartActions = - if (uiState.isLogged && !isOnOtherInstance) { - uiState.actionsOnSwipeToStartComments.toSwipeActions() - } else { - emptyList() - }, + if (uiState.isLogged && !isOnOtherInstance) { + uiState.actionsOnSwipeToStartComments.toSwipeActions() + } else { + emptyList() + }, swipeToEndActions = - if (uiState.isLogged && !isOnOtherInstance) { - uiState.actionsOnSwipeToEndComments.toSwipeActions() - } else { - emptyList() - }, + if (uiState.isLogged && !isOnOtherInstance) { + uiState.actionsOnSwipeToEndComments.toSwipeActions() + } else { + emptyList() + }, content = { CommentCard( modifier = - Modifier - .background(MaterialTheme.colorScheme.background) - .then( - if (comment.id == commentIdToHighlight) { - Modifier.background( - MaterialTheme.colorScheme.surfaceColorAtElevation( - 5.dp, - ).copy(alpha = 0.75f), - ) - } else { - Modifier - }, - ), + Modifier + .background(MaterialTheme.colorScheme.background) + .then( + if (comment.id == commentIdToHighlight) { + Modifier.background( + MaterialTheme.colorScheme.surfaceColorAtElevation( + 5.dp, + ).copy(alpha = 0.75f), + ) + } else { + Modifier + }, + ), comment = comment, isOp = comment.creator?.id == uiState.post.creator?.id, showBot = true, @@ -1142,302 +1143,303 @@ class PostDetailScreen( actionButtonsActive = uiState.isLogged, downVoteEnabled = uiState.downVoteEnabled, onToggleExpanded = - rememberCallback(model) { - model.reduce( - PostDetailMviModel.Intent.ToggleExpandComment( - comment.id, - ), - ) - }, - onClick = - rememberCallback(model) { - model.reduce( - PostDetailMviModel.Intent.ToggleExpandComment( - comment.id, - ), - ) - }, - onDoubleClick = - if (!uiState.doubleTapActionEnabled) { - null - } else { rememberCallback(model) { model.reduce( - PostDetailMviModel.Intent.UpVoteComment( - commentId = comment.id, - feedback = true, + PostDetailMviModel.Intent.ToggleExpandComment( + comment.id, ), ) - } - }, + }, + onClick = + rememberCallback(model) { + model.reduce( + PostDetailMviModel.Intent.ToggleExpandComment( + comment.id, + ), + ) + }, + onDoubleClick = + if (!uiState.doubleTapActionEnabled) { + null + } else { + rememberCallback(model) { + model.reduce( + PostDetailMviModel.Intent.UpVoteComment( + commentId = comment.id, + feedback = true, + ), + ) + } + }, onUpVote = - rememberCallback(model) { - if (uiState.isLogged && !isOnOtherInstance) { - model.reduce( - PostDetailMviModel.Intent.UpVoteComment( - commentId = comment.id, - ), - ) - } - }, + rememberCallback(model) { + if (uiState.isLogged && !isOnOtherInstance) { + model.reduce( + PostDetailMviModel.Intent.UpVoteComment( + commentId = comment.id, + ), + ) + } + }, onDownVote = - rememberCallback(model) { - if (uiState.isLogged && !isOnOtherInstance) { - model.reduce( - PostDetailMviModel.Intent.DownVoteComment( - commentId = comment.id, - ), - ) - } - }, + rememberCallback(model) { + if (uiState.isLogged && !isOnOtherInstance) { + model.reduce( + PostDetailMviModel.Intent.DownVoteComment( + commentId = comment.id, + ), + ) + } + }, onSave = - rememberCallback(model) { - if (uiState.isLogged && !isOnOtherInstance) { - model.reduce( - PostDetailMviModel.Intent.SaveComment( - commentId = comment.id, + rememberCallback(model) { + if (uiState.isLogged && !isOnOtherInstance) { + model.reduce( + PostDetailMviModel.Intent.SaveComment( + commentId = comment.id, + ), + ) + } + }, + onReply = + rememberCallback { + if (uiState.isLogged && !isOnOtherInstance) { + detailOpener.openReply( + originalPost = uiState.post, + originalComment = comment, + ) + } + }, + onOpenCreator = + rememberCallbackArgs { user, instance -> + detailOpener.openUserDetail( + user, + instance, + ) + }, + onOpenCommunity = + rememberCallbackArgs { community, instance -> + detailOpener.openCommunityDetail( + community, + instance, + ) + }, + onOpenPost = + rememberCallbackArgs { p, instance -> + detailOpener.openPostDetail(p, instance) + }, + onOpenWeb = + rememberCallbackArgs { url -> + navigationCoordinator.pushScreen( + WebViewScreen(url), + ) + }, + onImageClick = + rememberCallbackArgs { url -> + navigationCoordinator.pushScreen( + ZoomableImageScreen( + url = url, + source = uiState.post.community?.readableHandle.orEmpty(), ), ) - } - }, - onReply = - rememberCallback { - if (uiState.isLogged && !isOnOtherInstance) { - detailOpener.openReply( - originalPost = uiState.post, - originalComment = comment, - ) - } - }, - onOpenCreator = - rememberCallbackArgs { user, instance -> - detailOpener.openUserDetail( - user, - instance, - ) - }, - onOpenCommunity = - rememberCallbackArgs { community, instance -> - detailOpener.openCommunityDetail( - community, - instance, - ) - }, - onOpenPost = - rememberCallbackArgs { p, instance -> - detailOpener.openPostDetail(p, instance) - }, - onOpenWeb = - rememberCallbackArgs { url -> - navigationCoordinator.pushScreen( - WebViewScreen(url), - ) - }, - onImageClick = - rememberCallbackArgs { url -> - navigationCoordinator.pushScreen( - ZoomableImageScreen( - url = url, - source = uiState.post.community?.readableHandle.orEmpty(), - ), - ) - }, + }, options = - buildList { - this += - Option( - OptionId.SeeRaw, - LocalXmlStrings.current.postActionSeeRaw, - ) - this += - Option( - OptionId.Report, - LocalXmlStrings.current.postActionReport, - ) - if (comment.creator?.id == uiState.currentUserId) { + buildList { this += Option( - OptionId.Edit, - LocalXmlStrings.current.postActionEdit, + OptionId.SeeRaw, + LocalXmlStrings.current.postActionSeeRaw, ) this += Option( - OptionId.Delete, - LocalXmlStrings.current.commentActionDelete, + OptionId.Report, + LocalXmlStrings.current.postActionReport, ) - } - if (uiState.isModerator) { - this += - Option( - OptionId.DistinguishComment, - if (comment.distinguished) { - LocalXmlStrings.current.modActionUnmarkAsDistinguished - } else { - LocalXmlStrings.current.modActionMarkAsDistinguished - }, - ) - this += - Option( - OptionId.Remove, - LocalXmlStrings.current.modActionRemove, - ) - this += - Option( - OptionId.BanUser, - if (comment.creator?.banned == true) { - LocalXmlStrings.current.modActionAllow - } else { - LocalXmlStrings.current.modActionBan - }, - ) - comment.creator?.id?.also { creatorId -> - if (uiState.currentUserId != creatorId) { + if (comment.creator?.id == uiState.currentUserId) { + this += + Option( + OptionId.Edit, + LocalXmlStrings.current.postActionEdit, + ) + this += + Option( + OptionId.Delete, + LocalXmlStrings.current.commentActionDelete, + ) + } + if (uiState.isModerator) { + this += + Option( + OptionId.DistinguishComment, + if (comment.distinguished) { + LocalXmlStrings.current.modActionUnmarkAsDistinguished + } else { + LocalXmlStrings.current.modActionMarkAsDistinguished + }, + ) + this += + Option( + OptionId.Remove, + LocalXmlStrings.current.modActionRemove, + ) + this += + Option( + OptionId.BanUser, + if (comment.creator?.banned == true) { + LocalXmlStrings.current.modActionAllow + } else { + LocalXmlStrings.current.modActionBan + }, + ) + comment.creator?.id?.also { creatorId -> + if (uiState.currentUserId != creatorId) { + this += + Option( + OptionId.AddMod, + if (uiState.moderators.containsId( + creatorId, + ) + ) { + LocalXmlStrings.current.modActionRemoveMod + } else { + LocalXmlStrings.current.modActionAddMod + }, + ) + } + } + } + if (uiState.isAdmin) { + this += + Option( + OptionId.Purge, + LocalXmlStrings.current.adminActionPurge, + ) + comment.creator?.also { creator -> this += Option( - OptionId.AddMod, - if (uiState.moderators.containsId( - creatorId, + OptionId.PurgeCreator, + buildString { + append( + LocalXmlStrings.current.adminActionPurge, + ) + append(" ") + append( + creator.readableName( + uiState.preferNicknames, + ), ) - ) { - LocalXmlStrings.current.modActionRemoveMod - } else { - LocalXmlStrings.current.modActionAddMod }, ) } } - } - if (uiState.isAdmin) { - this += - Option( - OptionId.Purge, - LocalXmlStrings.current.adminActionPurge, - ) - comment.creator?.also { creator -> - this += - Option( - OptionId.PurgeCreator, - buildString { - append( - LocalXmlStrings.current.adminActionPurge - ) - append(" ") - append( - creator.readableName( - uiState.preferNicknames - ) - ) - }, - ) - } - } - }, + }, onOptionSelected = - rememberCallbackArgs( - model, - ) { optionId -> - when (optionId) { - OptionId.Delete -> { - commentIdToDelete = comment.id - } + rememberCallbackArgs( + model, + ) { optionId -> + when (optionId) { + OptionId.Delete -> { + commentIdToDelete = comment.id + } - OptionId.Edit -> { - detailOpener.openReply( - originalPost = PostModel(id = comment.postId), - originalComment = comment.parentId?.let { - CommentModel(id = it) - }, - editedComment = comment, - ) - } - - OptionId.Report -> { - val screen = - ModerateWithReasonScreen( - actionId = ModerateWithReasonAction.ReportComment.toInt(), - contentId = comment.id, + OptionId.Edit -> { + detailOpener.openReply( + originalPost = PostModel(id = comment.postId), + originalComment = + comment.parentId?.let { + CommentModel(id = it) + }, + editedComment = comment, ) - navigationCoordinator.pushScreen( - screen - ) - } + } - OptionId.SeeRaw -> { - rawContent = comment - } - - OptionId.DistinguishComment -> - model.reduce( - PostDetailMviModel.Intent.ModDistinguishComment( - comment.id, - ), - ) - - OptionId.Remove -> { - val screen = - ModerateWithReasonScreen( - actionId = ModerateWithReasonAction.RemoveComment.toInt(), - contentId = comment.id, - ) - navigationCoordinator.pushScreen( - screen - ) - } - - OptionId.BanUser -> { - comment.creator?.id?.also { userId -> + OptionId.Report -> { val screen = - BanUserScreen( - userId = userId, - communityId = - uiState.post.community?.id - ?: 0, - newValue = comment.creator?.banned != true, - commentId = comment.id, + ModerateWithReasonScreen( + actionId = ModerateWithReasonAction.ReportComment.toInt(), + contentId = comment.id, ) navigationCoordinator.pushScreen( screen, ) } - } - OptionId.AddMod -> { - comment.creator?.id?.also { userId -> + OptionId.SeeRaw -> { + rawContent = comment + } + + OptionId.DistinguishComment -> model.reduce( - PostDetailMviModel.Intent.ModToggleModUser( - userId, + PostDetailMviModel.Intent.ModDistinguishComment( + comment.id, ), ) - } - } - OptionId.Purge -> { - val screen = - ModerateWithReasonScreen( - actionId = ModerateWithReasonAction.PurgeComment.toInt(), - contentId = comment.id, - ) - navigationCoordinator.pushScreen( - screen - ) - } - - OptionId.PurgeCreator -> { - comment.creator?.id?.also { userId -> + OptionId.Remove -> { val screen = ModerateWithReasonScreen( - actionId = ModerateWithReasonAction.PurgeUser.toInt(), - contentId = userId, + actionId = ModerateWithReasonAction.RemoveComment.toInt(), + contentId = comment.id, ) navigationCoordinator.pushScreen( - screen + screen, ) } - } - else -> Unit - } - }, + OptionId.BanUser -> { + comment.creator?.id?.also { userId -> + val screen = + BanUserScreen( + userId = userId, + communityId = + uiState.post.community?.id + ?: 0, + newValue = comment.creator?.banned != true, + commentId = comment.id, + ) + navigationCoordinator.pushScreen( + screen, + ) + } + } + + OptionId.AddMod -> { + comment.creator?.id?.also { userId -> + model.reduce( + PostDetailMviModel.Intent.ModToggleModUser( + userId, + ), + ) + } + } + + OptionId.Purge -> { + val screen = + ModerateWithReasonScreen( + actionId = ModerateWithReasonAction.PurgeComment.toInt(), + contentId = comment.id, + ) + navigationCoordinator.pushScreen( + screen, + ) + } + + OptionId.PurgeCreator -> { + comment.creator?.id?.also { userId -> + val screen = + ModerateWithReasonScreen( + actionId = ModerateWithReasonAction.PurgeUser.toInt(), + contentId = userId, + ) + navigationCoordinator.pushScreen( + screen, + ) + } + } + + else -> Unit + } + }, ) }, ) @@ -1451,209 +1453,210 @@ class PostDetailScreen( showScores = uiState.showScores, actionButtonsActive = uiState.isLogged, onToggleExpanded = - rememberCallback(model) { - model.reduce( - PostDetailMviModel.Intent.ToggleExpandComment( - comment.id, - ), - ) - }, + rememberCallback(model) { + model.reduce( + PostDetailMviModel.Intent.ToggleExpandComment( + comment.id, + ), + ) + }, onClick = - rememberCallback(model) { - model.reduce( - PostDetailMviModel.Intent.ToggleExpandComment( - comment.id, - ), - ) - }, + rememberCallback(model) { + model.reduce( + PostDetailMviModel.Intent.ToggleExpandComment( + comment.id, + ), + ) + }, onUpVote = - rememberCallback(model) { - if (uiState.isLogged && !isOnOtherInstance) { - model.reduce( - PostDetailMviModel.Intent.UpVoteComment( - commentId = comment.id, - ), - ) - } - }, - onDownVote = - rememberCallback(model) { - if (uiState.isLogged && !isOnOtherInstance) { - model.reduce( - PostDetailMviModel.Intent.DownVoteComment( - commentId = comment.id, - ), - ) - } - }, - onSave = - rememberCallback(model) { - if (uiState.isLogged && !isOnOtherInstance) { - model.reduce( - PostDetailMviModel.Intent.SaveComment( - commentId = comment.id, - ), - ) - } - }, - onReply = - rememberCallback { - if (uiState.isLogged && !isOnOtherInstance) { - detailOpener.openReply( - originalPost = uiState.post, - originalComment = comment, - ) - } - }, - onOpenCreator = - rememberCallbackArgs { user -> - detailOpener.openUserDetail( - user, - otherInstanceName, - ) - }, - options = - buildList { - this += - Option( - OptionId.SeeRaw, - LocalXmlStrings.current.postActionSeeRaw, - ) - this += - Option( - OptionId.Report, - LocalXmlStrings.current.postActionReport, - ) - if (comment.creator?.id == uiState.currentUserId) { - this += - Option( - OptionId.Edit, - LocalXmlStrings.current.postActionEdit, - ) - this += - Option( - OptionId.Delete, - LocalXmlStrings.current.commentActionDelete, - ) - } - if (uiState.isModerator) { - this += - Option( - OptionId.DistinguishComment, - if (comment.distinguished) { - LocalXmlStrings.current.modActionUnmarkAsDistinguished - } else { - LocalXmlStrings.current.modActionMarkAsDistinguished - }, - ) - this += - Option( - OptionId.Remove, - LocalXmlStrings.current.modActionRemove, - ) - this += - Option( - OptionId.BanUser, - if (comment.creator?.banned == true) { - LocalXmlStrings.current.modActionAllow - } else { - LocalXmlStrings.current.modActionBan - }, - ) - comment.creator?.id?.also { creatorId -> - if (uiState.currentUserId != creatorId) { - this += - Option( - OptionId.AddMod, - if (uiState.moderators.containsId( - creatorId, - ) - ) { - LocalXmlStrings.current.modActionRemoveMod - } else { - LocalXmlStrings.current.modActionAddMod - }, - ) - } - } - } - }, - onOptionSelected = - rememberCallbackArgs(model) { optionId -> - when (optionId) { - OptionId.Delete -> { - commentIdToDelete = comment.id - } - - OptionId.Edit -> { - detailOpener.openReply( - originalPost = PostModel(id = comment.postId), - originalComment = comment.parentId?.let { - CommentModel(id = it) - }, - editedComment = comment, - ) - } - - OptionId.Report -> { - val screen = - ModerateWithReasonScreen( - actionId = ModerateWithReasonAction.ReportComment.toInt(), - contentId = comment.id, - ) - navigationCoordinator.pushScreen(screen) - } - - OptionId.SeeRaw -> { - rawContent = comment - } - - OptionId.DistinguishComment -> + rememberCallback(model) { + if (uiState.isLogged && !isOnOtherInstance) { model.reduce( - PostDetailMviModel.Intent.ModDistinguishComment( - comment.id, + PostDetailMviModel.Intent.UpVoteComment( + commentId = comment.id, ), ) - - OptionId.Remove -> { - val screen = - ModerateWithReasonScreen( - actionId = ModerateWithReasonAction.RemoveComment.toInt(), - contentId = comment.id, - ) - navigationCoordinator.pushScreen(screen) } - - OptionId.BanUser -> { - comment.creator?.id?.also { userId -> - val screen = - BanUserScreen( - userId = userId, - communityId = - uiState.post.community?.id - ?: 0, - newValue = comment.creator?.banned != true, - commentId = comment.id, - ) - navigationCoordinator.pushScreen( - screen, + }, + onDownVote = + rememberCallback(model) { + if (uiState.isLogged && !isOnOtherInstance) { + model.reduce( + PostDetailMviModel.Intent.DownVoteComment( + commentId = comment.id, + ), + ) + } + }, + onSave = + rememberCallback(model) { + if (uiState.isLogged && !isOnOtherInstance) { + model.reduce( + PostDetailMviModel.Intent.SaveComment( + commentId = comment.id, + ), + ) + } + }, + onReply = + rememberCallback { + if (uiState.isLogged && !isOnOtherInstance) { + detailOpener.openReply( + originalPost = uiState.post, + originalComment = comment, + ) + } + }, + onOpenCreator = + rememberCallbackArgs { user -> + detailOpener.openUserDetail( + user, + otherInstanceName, + ) + }, + options = + buildList { + this += + Option( + OptionId.SeeRaw, + LocalXmlStrings.current.postActionSeeRaw, + ) + this += + Option( + OptionId.Report, + LocalXmlStrings.current.postActionReport, + ) + if (comment.creator?.id == uiState.currentUserId) { + this += + Option( + OptionId.Edit, + LocalXmlStrings.current.postActionEdit, ) + this += + Option( + OptionId.Delete, + LocalXmlStrings.current.commentActionDelete, + ) + } + if (uiState.isModerator) { + this += + Option( + OptionId.DistinguishComment, + if (comment.distinguished) { + LocalXmlStrings.current.modActionUnmarkAsDistinguished + } else { + LocalXmlStrings.current.modActionMarkAsDistinguished + }, + ) + this += + Option( + OptionId.Remove, + LocalXmlStrings.current.modActionRemove, + ) + this += + Option( + OptionId.BanUser, + if (comment.creator?.banned == true) { + LocalXmlStrings.current.modActionAllow + } else { + LocalXmlStrings.current.modActionBan + }, + ) + comment.creator?.id?.also { creatorId -> + if (uiState.currentUserId != creatorId) { + this += + Option( + OptionId.AddMod, + if (uiState.moderators.containsId( + creatorId, + ) + ) { + LocalXmlStrings.current.modActionRemoveMod + } else { + LocalXmlStrings.current.modActionAddMod + }, + ) + } } } + }, + onOptionSelected = + rememberCallbackArgs(model) { optionId -> + when (optionId) { + OptionId.Delete -> { + commentIdToDelete = comment.id + } - OptionId.AddMod -> { - comment.creator?.id?.also { userId -> + OptionId.Edit -> { + detailOpener.openReply( + originalPost = PostModel(id = comment.postId), + originalComment = + comment.parentId?.let { + CommentModel(id = it) + }, + editedComment = comment, + ) + } + + OptionId.Report -> { + val screen = + ModerateWithReasonScreen( + actionId = ModerateWithReasonAction.ReportComment.toInt(), + contentId = comment.id, + ) + navigationCoordinator.pushScreen(screen) + } + + OptionId.SeeRaw -> { + rawContent = comment + } + + OptionId.DistinguishComment -> model.reduce( - PostDetailMviModel.Intent.ModToggleModUser( - userId, + PostDetailMviModel.Intent.ModDistinguishComment( + comment.id, ), ) - } - } - else -> Unit - } - }, + OptionId.Remove -> { + val screen = + ModerateWithReasonScreen( + actionId = ModerateWithReasonAction.RemoveComment.toInt(), + contentId = comment.id, + ) + navigationCoordinator.pushScreen(screen) + } + + OptionId.BanUser -> { + comment.creator?.id?.also { userId -> + val screen = + BanUserScreen( + userId = userId, + communityId = + uiState.post.community?.id + ?: 0, + newValue = comment.creator?.banned != true, + commentId = comment.id, + ) + navigationCoordinator.pushScreen( + screen, + ) + } + } + + OptionId.AddMod -> { + comment.creator?.id?.also { userId -> + model.reduce( + PostDetailMviModel.Intent.ModToggleModUser( + userId, + ), + ) + } + } + + else -> Unit + } + }, ) } } @@ -1671,25 +1674,25 @@ class PostDetailScreen( ) { Button( onClick = - rememberCallback(model) { - model.reduce( - PostDetailMviModel.Intent.FetchMoreComments( - parentId = comment.id, - ), - ) - }, + rememberCallback(model) { + model.reduce( + PostDetailMviModel.Intent.FetchMoreComments( + parentId = comment.id, + ), + ) + }, ) { Text( text = - buildString { - append(LocalXmlStrings.current.postDetailLoadMoreComments) - comment.comments?.takeIf { it > 0 } - ?.also { count -> - append(" (") - append(count) - append(")") - } - }, + buildString { + append(LocalXmlStrings.current.postDetailLoadMoreComments) + comment.comments?.takeIf { it > 0 } + ?.also { count -> + append(" (") + append(count) + append(")") + } + }, style = MaterialTheme.typography.labelSmall, ) } @@ -1719,8 +1722,8 @@ class PostDetailScreen( if (uiState.post.comments == 0) { Text( modifier = - Modifier.fillMaxWidth() - .padding(top = Spacing.xs), + Modifier.fillMaxWidth() + .padding(top = Spacing.xs), textAlign = TextAlign.Center, text = LocalXmlStrings.current.messageEmptyComments, style = MaterialTheme.typography.bodyMedium, @@ -1728,8 +1731,9 @@ class PostDetailScreen( ) } else if (uiState.searching) { Text( - modifier = Modifier.fillMaxWidth() - .padding(top = Spacing.xs), + modifier = + Modifier.fillMaxWidth() + .padding(top = Spacing.xs), textAlign = TextAlign.Center, text = LocalXmlStrings.current.messageEmptyList, style = MaterialTheme.typography.bodyMedium, @@ -1738,8 +1742,8 @@ class PostDetailScreen( } else { Text( modifier = - Modifier.fillMaxWidth() - .padding(top = Spacing.xs), + Modifier.fillMaxWidth() + .padding(top = Spacing.xs), textAlign = TextAlign.Center, text = LocalXmlStrings.current.messageErrorLoadingComments, style = MaterialTheme.typography.bodyMedium, @@ -1749,9 +1753,9 @@ class PostDetailScreen( Spacer(modifier = Modifier.weight(1f)) Button( onClick = - rememberCallback(model) { - model.reduce(PostDetailMviModel.Intent.Refresh) - }, + rememberCallback(model) { + model.reduce(PostDetailMviModel.Intent.Refresh) + }, ) { Text( text = LocalXmlStrings.current.buttonRetry, @@ -1781,35 +1785,35 @@ class PostDetailScreen( if (uiState.enableButtonsToScrollBetweenComments) { Row( modifier = - Modifier - .align(Alignment.BottomCenter) - .fillMaxWidth() - .onGloballyPositioned { - if (bottomBarHeightPx == 0f) { - bottomBarHeightPx = it.size.toSize().height + Modifier + .align(Alignment.BottomCenter) + .fillMaxWidth() + .onGloballyPositioned { + if (bottomBarHeightPx == 0f) { + bottomBarHeightPx = it.size.toSize().height + } } - } - .padding(bottom = bottomNavigationInset) - .offset { - IntOffset( - x = 0, - y = -bottomBarOffsetHeightPx.roundToInt(), - ) - } - .background(color = MaterialTheme.colorScheme.background.copy(alpha = 0.45f)), + .padding(bottom = bottomNavigationInset) + .offset { + IntOffset( + x = 0, + y = -bottomBarOffsetHeightPx.roundToInt(), + ) + } + .background(color = MaterialTheme.colorScheme.background.copy(alpha = 0.45f)), ) { if (uiState.isNavigationSupported) { Icon( modifier = - Modifier - .weight(1f) - .padding(vertical = Spacing.s) - .onClick( - onClick = - rememberCallback(model) { - model.reduce(PostDetailMviModel.Intent.NavigatePrevious) - }, - ), + Modifier + .weight(1f) + .padding(vertical = Spacing.s) + .onClick( + onClick = + rememberCallback(model) { + model.reduce(PostDetailMviModel.Intent.NavigatePrevious) + }, + ), imageVector = Icons.AutoMirrored.Default.NavigateBefore, contentDescription = null, tint = MaterialTheme.colorScheme.onBackground, @@ -1817,38 +1821,38 @@ class PostDetailScreen( } Icon( modifier = - Modifier - .weight(1f) - .padding(vertical = Spacing.s) - .onClick( - onClick = - rememberCallback(lazyListState) { - val idx = lazyListState.firstVisibleItemIndex - model.reduce( - PostDetailMviModel.Intent.NavigatePreviousComment(idx), - ) - }, - ), + Modifier + .weight(1f) + .padding(vertical = Spacing.s) + .onClick( + onClick = + rememberCallback(lazyListState) { + val idx = lazyListState.firstVisibleItemIndex + model.reduce( + PostDetailMviModel.Intent.NavigatePreviousComment(idx), + ) + }, + ), imageVector = Icons.Default.KeyboardArrowUp, contentDescription = null, tint = MaterialTheme.colorScheme.onBackground, ) Icon( modifier = - Modifier - .weight(1f) - .padding(vertical = Spacing.s) - .onClick( - onClick = - rememberCallback(lazyListState) { - val idx = - lazyListState.firstVisibleItemIndex + - lazyListState.layoutInfo.visibleItemsInfo.size - model.reduce( - PostDetailMviModel.Intent.NavigateNextComment(idx), - ) - }, - ), + Modifier + .weight(1f) + .padding(vertical = Spacing.s) + .onClick( + onClick = + rememberCallback(lazyListState) { + val idx = + lazyListState.firstVisibleItemIndex + + lazyListState.layoutInfo.visibleItemsInfo.size + model.reduce( + PostDetailMviModel.Intent.NavigateNextComment(idx), + ) + }, + ), imageVector = Icons.Default.KeyboardArrowDown, contentDescription = null, tint = MaterialTheme.colorScheme.onBackground, @@ -1856,15 +1860,15 @@ class PostDetailScreen( if (uiState.isNavigationSupported) { Icon( modifier = - Modifier - .weight(1f) - .padding(vertical = Spacing.s) - .onClick( - onClick = - rememberCallback(model) { - model.reduce(PostDetailMviModel.Intent.NavigateNext) - }, - ), + Modifier + .weight(1f) + .padding(vertical = Spacing.s) + .onClick( + onClick = + rememberCallback(model) { + model.reduce(PostDetailMviModel.Intent.NavigateNext) + }, + ), imageVector = Icons.AutoMirrored.Default.NavigateNext, contentDescription = null, tint = MaterialTheme.colorScheme.onBackground, @@ -1888,24 +1892,24 @@ class PostDetailScreen( downVotes = content.downvotes, isLogged = uiState.isLogged, onDismiss = - rememberCallback { - rawContent = null - }, + rememberCallback { + rawContent = null + }, onQuote = - rememberCallbackArgs { quotation -> - rawContent = null - if (quotation != null) { - detailOpener.openReply( - originalPost = content, - initialText = - buildString { - append("> ") - append(quotation) - append("\n\n") - }, - ) - } - }, + rememberCallbackArgs { quotation -> + rawContent = null + if (quotation != null) { + detailOpener.openReply( + originalPost = content, + initialText = + buildString { + append("> ") + append(quotation) + append("\n\n") + }, + ) + } + }, ) } @@ -1918,25 +1922,25 @@ class PostDetailScreen( publishDate = content.publishDate, updateDate = content.updateDate, onDismiss = - rememberCallback { - rawContent = null - }, + rememberCallback { + rawContent = null + }, onQuote = - rememberCallbackArgs { quotation -> - rawContent = null - if (quotation != null) { - detailOpener.openReply( - originalPost = uiState.post, - originalComment = content, - initialText = - buildString { - append("> ") - append(quotation) - append("\n\n") - }, - ) - } - }, + rememberCallbackArgs { quotation -> + rawContent = null + if (quotation != null) { + detailOpener.openReply( + originalPost = uiState.post, + originalComment = content, + initialText = + buildString { + append("> ") + append(quotation) + append("\n\n") + }, + ) + } + }, ) } } diff --git a/unit/postdetail/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/postdetail/PostDetailViewModel.kt b/unit/postdetail/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/postdetail/PostDetailViewModel.kt index 7b6709ab3..1b67a5980 100644 --- a/unit/postdetail/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/postdetail/PostDetailViewModel.kt +++ b/unit/postdetail/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/unit/postdetail/PostDetailViewModel.kt @@ -84,7 +84,8 @@ class PostDetailViewModel( } if (uiState.value.post.id == 0L) { val post = itemCache.getPost(postId) ?: PostModel() - val downVoteEnabled = siteRepository.isDownVoteEnabled(identityRepository.authToken.value) + val downVoteEnabled = + siteRepository.isDownVoteEnabled(identityRepository.authToken.value) updateState { it.copy( post = post,