Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Edit profile part 5 - Navigation + loading and error states #232

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ val accountModule = module {
EditAccountViewModel(
get(),
get(),
get(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ interface EditAccountInteractions {
fun onNewHeaderSelected(uri: Uri, file: File) = Unit
fun onLockClicked() = Unit
fun onBotClicked() = Unit
fun onRetryClicked() = Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -42,6 +41,8 @@ import org.mozilla.social.core.designsystem.icon.MoSoIcons
import org.mozilla.social.core.designsystem.theme.MoSoTheme
import org.mozilla.social.core.ui.TransparentNoTouchOverlay
import org.mozilla.social.core.ui.appbar.MoSoCloseableTopAppBar
import org.mozilla.social.core.ui.error.GenericError
import org.mozilla.social.core.ui.loading.MaxSizeLoading
import org.mozilla.social.feature.account.Header
import org.mozilla.social.feature.account.R

Expand All @@ -51,7 +52,7 @@ internal fun EditAccountScreen(
) {
EditAccountScreen(
editAccountInteractions = viewModel,
editAccountUiState = viewModel.editAccountUiState.collectAsState().value,
uiState = viewModel.editAccountUiState.collectAsState().value,
isUploading = viewModel.isUploading.collectAsState().value,
)

Expand All @@ -61,39 +62,59 @@ internal fun EditAccountScreen(
@Composable
fun EditAccountScreen(
editAccountInteractions: EditAccountInteractions,
editAccountUiState: Resource<EditAccountUiState>,
uiState: Resource<EditAccountUiState>,
isUploading: Boolean,
) {
MoSoSurface(
modifier = Modifier
.fillMaxSize(),
) {
Box(modifier = Modifier.fillMaxSize()) {
Box(
modifier = Modifier
.systemBarsPadding()
.imePadding(),
) {
when (editAccountUiState) {
is Resource.Loading -> {}
is Resource.Loaded -> {
LoadedState(
editAccountInteractions = editAccountInteractions,
uiState = editAccountUiState.data,
)
Column(
modifier = Modifier
.systemBarsPadding()
.imePadding(),
) {
MoSoCloseableTopAppBar(
title = (uiState as? Resource.Loaded)?.data?.topBarTitle ?: "",
actions = {
if (uiState is Resource.Loaded) {
MoSoButton(
modifier = Modifier
.padding(8.dp)
.height(38.dp),
onClick = { editAccountInteractions.onSaveClicked() }
) {
Text(text = stringResource(id = R.string.edit_account_save_button))
}
}
},
showDivider = false,
)


is Resource.Error -> {}
when (uiState) {
is Resource.Loading -> {
MaxSizeLoading()
}
is Resource.Loaded -> {
LoadedState(
editAccountInteractions = editAccountInteractions,
uiState = uiState.data,
)
}
}

if (isUploading) {
TransparentNoTouchOverlay()
CircularProgressIndicator(
modifier = Modifier.align(Alignment.Center),
)
is Resource.Error -> {
GenericError(
onRetryClicked = editAccountInteractions::onRetryClicked
)
}
}
}

if (isUploading) {
TransparentNoTouchOverlay()
MaxSizeLoading()
}
}
}

Expand All @@ -105,21 +126,6 @@ private fun LoadedState(
val context = LocalContext.current

Column {
MoSoCloseableTopAppBar(
title = uiState.topBarTitle,
actions = {
MoSoButton(
modifier = Modifier
.padding(8.dp)
.height(38.dp),
onClick = { editAccountInteractions.onSaveClicked() }
) {
Text(text = stringResource(id = R.string.edit_account_save_button))
}
},
showDivider = false,
)

val avatarSelectionLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.PickVisualMedia()
) { uri ->
Expand Down Expand Up @@ -270,7 +276,7 @@ private fun BotAndLock(
private fun PreviewEditAccountScreen() {
MoSoTheme {
EditAccountScreen(
editAccountUiState = Resource.Loaded(
uiState = Resource.Loaded(
data = EditAccountUiState(
topBarTitle = "John",
headerUrl = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import org.mozilla.social.common.Resource
import org.mozilla.social.common.utils.StringFactory
import org.mozilla.social.core.data.repository.AccountRepository
import org.mozilla.social.core.domain.AccountIdBlocking
import org.mozilla.social.core.navigation.usecases.PopNavBackstack
import org.mozilla.social.feature.account.R
import timber.log.Timber
import java.io.File

class EditAccountViewModel(
private val accountRepository: AccountRepository,
accountIdBlocking: AccountIdBlocking,
private val popNavBackstack: PopNavBackstack,
) : ViewModel(), EditAccountInteractions {

private val accountId = accountIdBlocking()
Expand All @@ -42,6 +44,7 @@ class EditAccountViewModel(
}

private fun loadAccount() {
_editAccountUiState.update { Resource.Loading() }
viewModelScope.launch {
try {
val account = accountRepository.getAccountFromDatabase(accountId)!!
Expand All @@ -63,6 +66,7 @@ class EditAccountViewModel(
}
} catch (e: Exception) {
Timber.e(e)
_editAccountUiState.update { Resource.Error(e) }
}
}
}
Expand Down Expand Up @@ -106,7 +110,7 @@ class EditAccountViewModel(
locked = data.lockChecked,
bot = data.botChecked,
)
//TODO navigate back
popNavBackstack()
} catch (e: Exception) {
_errorToastMessage.emit(StringFactory.resource(R.string.edit_account_save_failed))
Timber.e(e)
Expand Down Expand Up @@ -166,6 +170,10 @@ class EditAccountViewModel(
}
}

override fun onRetryClicked() {
loadAccount()
}

companion object {
const val MAX_BIO_LENGTH = 500
}
Expand Down