enhancement: improve login use case (#930)

This commit is contained in:
Diego Beraldin 2024-06-05 07:59:48 +02:00 committed by GitHub
parent 5cb0c28352
commit 541cf7255d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 970 additions and 963 deletions

View File

@ -55,12 +55,12 @@ internal class DefaultLoginUseCase(
instance = instance, instance = instance,
jwt = auth, jwt = auth,
) )
val existingId = accountRepository.getBy(username, instance)?.id val existing = accountRepository.getBy(username, instance)
val id = val accountId =
existingId ?: run { if (existing == null) {
// new account with a copy of the anonymous settings // new account with a copy of the anonymous settings
// (except a couple of fields from the Lemmy accounts) // (except a couple of fields from the Lemmy accounts)
val res = accountRepository.createAccount(account) val newAccountId = accountRepository.createAccount(account)
val anonymousSettings = val anonymousSettings =
settingsRepository.getSettings(null) settingsRepository.getSettings(null)
.copy( .copy(
@ -69,20 +69,22 @@ internal class DefaultLoginUseCase(
) )
settingsRepository.createSettings( settingsRepository.createSettings(
settings = anonymousSettings, settings = anonymousSettings,
accountId = res, accountId = newAccountId,
) )
res newAccountId
} else {
existing.id ?: 0
} }
val oldActiveAccountId = accountRepository.getActive()?.id val oldActiveAccountId = accountRepository.getActive()?.id
if (oldActiveAccountId != null) { if (oldActiveAccountId != null) {
accountRepository.setActive(oldActiveAccountId, false) accountRepository.setActive(oldActiveAccountId, false)
} }
accountRepository.setActive(id, true) accountRepository.setActive(accountId, true)
communitySortRepository.clear() communitySortRepository.clear()
communityPreferredLanguageRepository.clear() communityPreferredLanguageRepository.clear()
val newSettings = settingsRepository.getSettings(id) val newSettings = settingsRepository.getSettings(accountId)
settingsRepository.changeCurrentSettings(newSettings) settingsRepository.changeCurrentSettings(newSettings)
} }
} }

View File

@ -138,16 +138,16 @@ class CreateCommentScreen(
uiState.originalPost?.also { originalPost -> uiState.originalPost?.also { originalPost ->
notificationCenter.send( notificationCenter.send(
event = event =
NotificationCenterEvent.PostUpdated( NotificationCenterEvent.PostUpdated(
originalPost.copy( originalPost.copy(
comments = comments =
if (effect.new) { if (effect.new) {
originalPost.comments + 1 originalPost.comments + 1
} else { } else {
originalPost.comments originalPost.comments
}, },
),
), ),
),
) )
} }
navigationCoordinator.popScreen() navigationCoordinator.popScreen()
@ -171,11 +171,11 @@ class CreateCommentScreen(
navigationIcon = { navigationIcon = {
Image( Image(
modifier = modifier =
Modifier.padding(start = Spacing.s).onClick( Modifier.padding(start = Spacing.s).onClick(
onClick = { onClick = {
navigationCoordinator.popScreen() navigationCoordinator.popScreen()
}, },
), ),
imageVector = Icons.Default.Close, imageVector = Icons.Default.Close,
contentDescription = null, contentDescription = null,
colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.onBackground), colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.onBackground),
@ -184,15 +184,15 @@ class CreateCommentScreen(
title = { title = {
Text( Text(
text = text =
when { when {
uiState.editedComment != null -> { uiState.editedComment != null -> {
LocalXmlStrings.current.editCommentTitle LocalXmlStrings.current.editCommentTitle
} }
else -> { else -> {
LocalXmlStrings.current.createCommentTitle LocalXmlStrings.current.createCommentTitle
} }
}, },
color = MaterialTheme.colorScheme.onBackground, color = MaterialTheme.colorScheme.onBackground,
style = MaterialTheme.typography.titleMedium, style = MaterialTheme.typography.titleMedium,
) )
@ -208,9 +208,9 @@ class CreateCommentScreen(
) )
}, },
onClick = onClick =
rememberCallback(model) { rememberCallback(model) {
model.reduce(CreateCommentMviModel.Intent.SaveDraft) model.reduce(CreateCommentMviModel.Intent.SaveDraft)
}, },
) )
} }
IconButton( IconButton(
@ -222,9 +222,9 @@ class CreateCommentScreen(
) )
}, },
onClick = onClick =
rememberCallback(model) { rememberCallback(model) {
model.reduce(CreateCommentMviModel.Intent.Send) model.reduce(CreateCommentMviModel.Intent.Send)
}, },
) )
}, },
) )
@ -232,21 +232,21 @@ class CreateCommentScreen(
) { padding -> ) { padding ->
Box( Box(
modifier = modifier =
Modifier Modifier
.padding( .padding(
top = padding.calculateTopPadding(), top = padding.calculateTopPadding(),
) )
.consumeWindowInsets(padding) .consumeWindowInsets(padding)
.safeImePadding() .safeImePadding()
.fillMaxSize(), .fillMaxSize(),
) { ) {
// reference post or comment // reference post or comment
Box( Box(
modifier = modifier =
Modifier Modifier
.align(Alignment.TopCenter) .align(Alignment.TopCenter)
.fillMaxWidth() .fillMaxWidth()
.verticalScroll(rememberScrollState()), .verticalScroll(rememberScrollState()),
) { ) {
val referenceModifier = val referenceModifier =
Modifier.padding( Modifier.padding(
@ -270,14 +270,14 @@ class CreateCommentScreen(
showScores = uiState.showScores, showScores = uiState.showScores,
downVoteEnabled = uiState.downVoteEnabled, downVoteEnabled = uiState.downVoteEnabled,
options = options =
buildList { buildList {
add( add(
Option( Option(
OptionId.SeeRaw, OptionId.SeeRaw,
LocalXmlStrings.current.postActionSeeRaw, LocalXmlStrings.current.postActionSeeRaw,
), ),
) )
}, },
onOptionSelected = { onOptionSelected = {
rawContent = originalComment rawContent = originalComment
}, },
@ -290,11 +290,11 @@ class CreateCommentScreen(
PostCard( PostCard(
modifier = referenceModifier, modifier = referenceModifier,
postLayout = postLayout =
if (uiState.postLayout == PostLayout.Card) { if (uiState.postLayout == PostLayout.Card) {
uiState.postLayout uiState.postLayout
} else { } else {
PostLayout.Full PostLayout.Full
}, },
fullHeightImage = uiState.fullHeightImages, fullHeightImage = uiState.fullHeightImages,
fullWidthImage = uiState.fullWidthImages, fullWidthImage = uiState.fullWidthImages,
post = originalPost, post = originalPost,
@ -306,14 +306,14 @@ class CreateCommentScreen(
showScores = uiState.showScores, showScores = uiState.showScores,
downVoteEnabled = uiState.downVoteEnabled, downVoteEnabled = uiState.downVoteEnabled,
options = options =
buildList { buildList {
add( add(
Option( Option(
OptionId.SeeRaw, OptionId.SeeRaw,
LocalXmlStrings.current.postActionSeeRaw, LocalXmlStrings.current.postActionSeeRaw,
), ),
) )
}, },
onOptionSelected = { onOptionSelected = {
rawContent = originalPost rawContent = originalPost
}, },
@ -325,42 +325,42 @@ class CreateCommentScreen(
// form fields // form fields
Column( Column(
modifier = modifier =
Modifier Modifier
.align(Alignment.BottomCenter) .align(Alignment.BottomCenter)
.background(MaterialTheme.colorScheme.background) .background(MaterialTheme.colorScheme.background)
.fillMaxWidth(), .fillMaxWidth(),
verticalArrangement = Arrangement.spacedBy(Spacing.xs), verticalArrangement = Arrangement.spacedBy(Spacing.xs),
) { ) {
SectionSelector( SectionSelector(
titles = titles =
listOf( listOf(
LocalXmlStrings.current.createPostTabEditor, LocalXmlStrings.current.createPostTabEditor,
LocalXmlStrings.current.createPostTabPreview, LocalXmlStrings.current.createPostTabPreview,
), ),
currentSection = currentSection =
when (uiState.section) { when (uiState.section) {
CreatePostSection.Preview -> 1 CreatePostSection.Preview -> 1
else -> 0 else -> 0
}, },
onSectionSelected = onSectionSelected =
rememberCallbackArgs { id -> rememberCallbackArgs { id ->
val section = val section =
when (id) { when (id) {
1 -> CreatePostSection.Preview 1 -> CreatePostSection.Preview
else -> CreatePostSection.Edit else -> CreatePostSection.Edit
} }
model.reduce(CreateCommentMviModel.Intent.ChangeSection(section)) model.reduce(CreateCommentMviModel.Intent.ChangeSection(section))
}, },
) )
if (uiState.section == CreatePostSection.Edit) { if (uiState.section == CreatePostSection.Edit) {
TextFormattingBar( TextFormattingBar(
modifier = modifier =
Modifier.padding( Modifier.padding(
top = Spacing.s, top = Spacing.s,
start = Spacing.s, start = Spacing.s,
end = Spacing.s, end = Spacing.s,
), ),
textFieldValue = uiState.textValue, textFieldValue = uiState.textValue,
onTextFieldValueChanged = { value -> onTextFieldValueChanged = { value ->
model.reduce(CreateCommentMviModel.Intent.ChangeTextValue(value)) model.reduce(CreateCommentMviModel.Intent.ChangeTextValue(value))
@ -376,16 +376,16 @@ class CreateCommentScreen(
) )
TextField( TextField(
modifier = modifier =
Modifier Modifier
.focusRequester(commentFocusRequester) .focusRequester(commentFocusRequester)
.heightIn(min = 300.dp, max = 400.dp) .heightIn(min = 300.dp, max = 400.dp)
.fillMaxWidth(), .fillMaxWidth(),
colors = colors =
TextFieldDefaults.colors( TextFieldDefaults.colors(
focusedContainerColor = Color.Transparent, focusedContainerColor = Color.Transparent,
unfocusedContainerColor = Color.Transparent, unfocusedContainerColor = Color.Transparent,
disabledContainerColor = Color.Transparent, disabledContainerColor = Color.Transparent,
), ),
label = { label = {
Text( Text(
text = LocalXmlStrings.current.createCommentBody, text = LocalXmlStrings.current.createCommentBody,
@ -395,11 +395,11 @@ class CreateCommentScreen(
textStyle = typography.bodyMedium, textStyle = typography.bodyMedium,
value = uiState.textValue, value = uiState.textValue,
keyboardOptions = keyboardOptions =
KeyboardOptions( KeyboardOptions(
keyboardType = KeyboardType.Text, keyboardType = KeyboardType.Text,
autoCorrect = true, autoCorrect = true,
capitalization = KeyboardCapitalization.Sentences, capitalization = KeyboardCapitalization.Sentences,
), ),
onValueChange = { value -> onValueChange = { value ->
model.reduce(CreateCommentMviModel.Intent.ChangeTextValue(value)) model.reduce(CreateCommentMviModel.Intent.ChangeTextValue(value))
}, },
@ -417,15 +417,15 @@ class CreateCommentScreen(
} else { } else {
Box( Box(
modifier = modifier =
Modifier Modifier
.heightIn(min = 300.dp, max = 500.dp) .heightIn(min = 300.dp, max = 500.dp)
.fillMaxWidth(), .fillMaxWidth(),
) { ) {
PostCardBody( PostCardBody(
modifier = modifier =
Modifier Modifier
.padding(Spacing.s) .padding(Spacing.s)
.verticalScroll(rememberScrollState()), .verticalScroll(rememberScrollState()),
text = uiState.textValue.text, text = uiState.textValue.text,
autoLoadImages = uiState.autoLoadImages, autoLoadImages = uiState.autoLoadImages,
) )
@ -435,23 +435,23 @@ class CreateCommentScreen(
if (uiState.currentUser.isNotEmpty()) { if (uiState.currentUser.isNotEmpty()) {
Text( Text(
modifier = modifier =
Modifier Modifier
.fillMaxWidth() .fillMaxWidth()
.padding( .padding(
start = Spacing.m, start = Spacing.m,
end = Spacing.m, end = Spacing.m,
bottom = Spacing.s, bottom = Spacing.s,
), ),
text = text =
buildString { buildString {
append(LocalXmlStrings.current.postReplySourceAccount) append(LocalXmlStrings.current.postReplySourceAccount)
append(" ") append(" ")
append(uiState.currentUser) append(uiState.currentUser)
if (uiState.currentInstance.isNotEmpty()) { if (uiState.currentInstance.isNotEmpty()) {
append("@") append("@")
append(uiState.currentInstance) append(uiState.currentInstance)
} }
}, },
color = MaterialTheme.colorScheme.onBackground, color = MaterialTheme.colorScheme.onBackground,
style = MaterialTheme.typography.labelSmall, style = MaterialTheme.typography.labelSmall,
textDecoration = TextDecoration.Underline, textDecoration = TextDecoration.Underline,
@ -509,14 +509,14 @@ class CreateCommentScreen(
languages = uiState.availableLanguages, languages = uiState.availableLanguages,
currentLanguageId = uiState.currentLanguageId, currentLanguageId = uiState.currentLanguageId,
onSelect = onSelect =
rememberCallbackArgs { langId -> rememberCallbackArgs { langId ->
model.reduce(CreateCommentMviModel.Intent.ChangeLanguage(langId)) model.reduce(CreateCommentMviModel.Intent.ChangeLanguage(langId))
selectLanguageDialogOpen = false selectLanguageDialogOpen = false
}, },
onDismiss = onDismiss =
rememberCallback { rememberCallback {
selectLanguageDialogOpen = false selectLanguageDialogOpen = false
}, },
) )
} }
} }

View File

@ -267,11 +267,11 @@ class CreateCommentViewModel(
languageId = languageId, languageId = languageId,
date = epochMillis(), date = epochMillis(),
reference = reference =
if (currentState.originalComment != null) { if (currentState.originalComment != null) {
currentState.originalComment.text currentState.originalComment.text
} else { } else {
currentState.originalPost?.title currentState.originalPost?.title
}, },
) )
if (draftId == null) { if (draftId == null) {
draftRepository.create( draftRepository.create(

View File

@ -84,7 +84,8 @@ class PostDetailViewModel(
} }
if (uiState.value.post.id == 0L) { if (uiState.value.post.id == 0L) {
val post = itemCache.getPost(postId) ?: PostModel() val post = itemCache.getPost(postId) ?: PostModel()
val downVoteEnabled = siteRepository.isDownVoteEnabled(identityRepository.authToken.value) val downVoteEnabled =
siteRepository.isDownVoteEnabled(identityRepository.authToken.value)
updateState { updateState {
it.copy( it.copy(
post = post, post = post,