Skip to content

Commit

Permalink
Handle error and loading on BreedsScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
bpedryc committed Nov 22, 2023
1 parent 0275873 commit e374c35
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.animation.core.TweenSpec
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
Expand All @@ -15,38 +16,54 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import co.touchlab.kampkit.android.R
import co.touchlab.kampkit.domain.breed.Breed
import co.touchlab.kampkit.ui.breedDetails.BreedDetailsViewModel
import co.touchlab.kermit.Logger
import co.touchlab.kampkit.ui.breedDetails.BreedDetailsViewState

@Composable
fun BreedDetailsScreen(
viewModel: BreedDetailsViewModel,
log: Logger
) {
fun BreedDetailsScreen(viewModel: BreedDetailsViewModel) {
val state by viewModel.detailsState.collectAsStateWithLifecycle()
val error = state.error
Box(Modifier.fillMaxSize()) {
state.error?.let { error ->
Text(error, Modifier.align(Alignment.Center), color = Color.Red)
}
if (state.error == null) {
Row(Modifier.align(Alignment.Center)) {
Text(state.breed.name)
Spacer(Modifier.width(4.dp))
FavoriteIcon(
breed = state.breed,
onClick = viewModel::onFavoriteClick
)
}
when {
state.isLoading -> Loading()
error != null -> Error(error)
else -> DetailsContents(
state = state,
onFavoriteClick = viewModel::onFavoriteClick
)
}
}
}

@Composable
private fun BoxScope.DetailsContents(
state: BreedDetailsViewState,
onFavoriteClick: () -> Unit
) {
Row(Modifier.align(Alignment.Center)) {
Text(state.breed?.name ?: "")
Spacer(Modifier.width(4.dp))
state.breed?.let { breed ->
FavoriteIcon(
breed = breed,
onClick = onFavoriteClick
)
}
}
}
@Composable
private fun Error(error: String) {
Text(error)
}
@Composable
fun Loading() {
Text("Loading")
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fun Empty() {
}

@Composable
fun Error(error: String) {
private fun Error(error: String) {
Column(
modifier = Modifier
.fillMaxSize()
Expand Down
25 changes: 25 additions & 0 deletions ios/KaMPKitiOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,30 @@
path = Breeds;
sourceTree = "<group>";
};
579753BDA8BAA9A5A399C611 /* bartlomiejpedryc.xcuserdatad */ = {
isa = PBXGroup;
children = (
57975F3C51683ABE14191024 /* xcschemes */,
);
path = bartlomiejpedryc.xcuserdatad;
sourceTree = "<group>";
};
5797556592C6669DF1E7702C /* xcuserdata */ = {
isa = PBXGroup;
children = (
579753BDA8BAA9A5A399C611 /* bartlomiejpedryc.xcuserdatad */,
);
name = xcuserdata;
path = KaMPKitiOS.xcodeproj/xcuserdata;
sourceTree = "<group>";
};
57975F3C51683ABE14191024 /* xcschemes */ = {
isa = PBXGroup;
children = (
);
path = xcschemes;
sourceTree = "<group>";
};
6278498AD96A4D949D39BF44 /* Frameworks */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -141,6 +165,7 @@
F1465EFE23AA94BF0055F7C3 /* Products */,
DF9BBECBCD175B90105DA8D9 /* Pods */,
6278498AD96A4D949D39BF44 /* Frameworks */,
5797556592C6669DF1E7702C /* xcuserdata */,
);
sourceTree = "<group>";
};
Expand Down
23 changes: 23 additions & 0 deletions ios/KaMPKitiOS.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"pins" : [
{
"identity" : "kmp-nativecoroutines",
"kind" : "remoteSourceControl",
"location" : "https://github.com/rickclephas/KMP-NativeCoroutines.git",
"state" : {
"revision" : "a1269d3052fcc32d3861eaea32b715d58a46fe32",
"version" : "1.0.0-ALPHA-9"
}
},
{
"identity" : "rxswift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ReactiveX/RxSwift.git",
"state" : {
"revision" : "9dcaa4b333db437b0fbfaf453fad29069044a8b4",
"version" : "6.6.0"
}
}
],
"version" : 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package co.touchlab.kampkit.ui.breedDetails
import co.touchlab.kampkit.domain.breed.Breed

data class BreedDetailsViewState(
val breed: Breed = Breed(),
val breed: Breed? = null,
val error: String? = null,
val isLoading: Boolean = false
) {
Expand Down

0 comments on commit e374c35

Please sign in to comment.