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,
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)
}
}

View File

@ -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
},
)
}
}

View File

@ -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(

View File

@ -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,