mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 19:57:04 +05:30
feat(common): make user profile page correctly handle errors
This commit is contained in:
parent
3335c79591
commit
e57dc1a6cf
1 changed files with 11 additions and 3 deletions
|
@ -17,6 +17,7 @@ import androidx.compose.ui.Modifier
|
|||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.unit.dp
|
||||
import dev.msfjarvis.claw.common.NetworkState
|
||||
import dev.msfjarvis.claw.common.NetworkState.Error
|
||||
import dev.msfjarvis.claw.common.NetworkState.Loading
|
||||
import dev.msfjarvis.claw.common.NetworkState.Success
|
||||
import dev.msfjarvis.claw.common.res.ClawIcons
|
||||
|
@ -33,13 +34,20 @@ fun UserProfile(
|
|||
getProfile: suspend (username: String) -> User,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
val user by produceState<NetworkState>(Loading) { value = Success(getProfile(username)) }
|
||||
val user by
|
||||
produceState<NetworkState>(Loading) {
|
||||
runCatching { getProfile(username) }
|
||||
.fold(
|
||||
onSuccess = { profile -> value = Success(profile) },
|
||||
onFailure = { value = Error("Failed to load profile for $username") }
|
||||
)
|
||||
}
|
||||
when (user) {
|
||||
is Success<*> -> {
|
||||
UserProfileInternal((user as Success<User>).data)
|
||||
}
|
||||
is NetworkState.Error -> {
|
||||
NetworkError((user as NetworkState.Error).message)
|
||||
is Error -> {
|
||||
NetworkError((user as Error).message)
|
||||
}
|
||||
Loading -> ProgressBar(modifier)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue