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

@ -753,7 +753,8 @@ class PostDetailScreen(
.fillMaxSize(), .fillMaxSize(),
) { ) {
Column( Column(
modifier = Modifier modifier =
Modifier
.then( .then(
if (uiState.enableButtonsToScrollBetweenComments) { if (uiState.enableButtonsToScrollBetweenComments) {
Modifier.nestedScroll(buttonBarScrollConnection) Modifier.nestedScroll(buttonBarScrollConnection)
@ -1319,13 +1320,13 @@ class PostDetailScreen(
OptionId.PurgeCreator, OptionId.PurgeCreator,
buildString { buildString {
append( append(
LocalXmlStrings.current.adminActionPurge LocalXmlStrings.current.adminActionPurge,
) )
append(" ") append(" ")
append( append(
creator.readableName( creator.readableName(
uiState.preferNicknames uiState.preferNicknames,
) ),
) )
}, },
) )
@ -1344,7 +1345,8 @@ class PostDetailScreen(
OptionId.Edit -> { OptionId.Edit -> {
detailOpener.openReply( detailOpener.openReply(
originalPost = PostModel(id = comment.postId), originalPost = PostModel(id = comment.postId),
originalComment = comment.parentId?.let { originalComment =
comment.parentId?.let {
CommentModel(id = it) CommentModel(id = it)
}, },
editedComment = comment, editedComment = comment,
@ -1358,7 +1360,7 @@ class PostDetailScreen(
contentId = comment.id, contentId = comment.id,
) )
navigationCoordinator.pushScreen( navigationCoordinator.pushScreen(
screen screen,
) )
} }
@ -1380,7 +1382,7 @@ class PostDetailScreen(
contentId = comment.id, contentId = comment.id,
) )
navigationCoordinator.pushScreen( navigationCoordinator.pushScreen(
screen screen,
) )
} }
@ -1418,7 +1420,7 @@ class PostDetailScreen(
contentId = comment.id, contentId = comment.id,
) )
navigationCoordinator.pushScreen( navigationCoordinator.pushScreen(
screen screen,
) )
} }
@ -1430,7 +1432,7 @@ class PostDetailScreen(
contentId = userId, contentId = userId,
) )
navigationCoordinator.pushScreen( navigationCoordinator.pushScreen(
screen screen,
) )
} }
} }
@ -1588,7 +1590,8 @@ class PostDetailScreen(
OptionId.Edit -> { OptionId.Edit -> {
detailOpener.openReply( detailOpener.openReply(
originalPost = PostModel(id = comment.postId), originalPost = PostModel(id = comment.postId),
originalComment = comment.parentId?.let { originalComment =
comment.parentId?.let {
CommentModel(id = it) CommentModel(id = it)
}, },
editedComment = comment, editedComment = comment,
@ -1728,7 +1731,8 @@ class PostDetailScreen(
) )
} else if (uiState.searching) { } else if (uiState.searching) {
Text( Text(
modifier = Modifier.fillMaxWidth() modifier =
Modifier.fillMaxWidth()
.padding(top = Spacing.xs), .padding(top = Spacing.xs),
textAlign = TextAlign.Center, textAlign = TextAlign.Center,
text = LocalXmlStrings.current.messageEmptyList, text = LocalXmlStrings.current.messageEmptyList,

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,