forked from touchlab/KaMPKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from tooploox/task/navigation
Navigation solution
- Loading branch information
Showing
30 changed files
with
648 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
app/src/main/kotlin/co/touchlab/kampkit/android/ui/BreedDetailsScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package co.touchlab.kampkit.android.ui | ||
|
||
import androidx.compose.animation.Crossfade | ||
import androidx.compose.animation.core.FastOutSlowInEasing | ||
import androidx.compose.animation.core.TweenSpec | ||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.* | ||
import androidx.compose.material.CircularProgressIndicator | ||
import androidx.compose.material.Text | ||
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.kampkit.ui.breedDetails.BreedDetailsViewState | ||
|
||
@Composable | ||
fun BreedDetailsScreen(viewModel: BreedDetailsViewModel) { | ||
val state by viewModel.detailsState.collectAsStateWithLifecycle() | ||
val error = state.error | ||
Box(Modifier.fillMaxSize()) { | ||
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 BoxScope.Error(error: String) { | ||
Text( | ||
text = error, | ||
color = Color.Red, | ||
modifier = Modifier.align(Alignment.Center) | ||
) | ||
} | ||
|
||
@Composable | ||
fun BoxScope.Loading() { | ||
CircularProgressIndicator(Modifier.align(Alignment.Center)) | ||
} | ||
|
||
@Composable | ||
fun FavoriteIcon( | ||
breed: Breed, | ||
onClick: () -> Unit | ||
) { | ||
Crossfade( | ||
targetState = !breed.favorite, | ||
animationSpec = TweenSpec( | ||
durationMillis = 500, | ||
easing = FastOutSlowInEasing | ||
), | ||
modifier = Modifier.clickable { onClick() } | ||
) { fav -> | ||
if (fav) { | ||
Image( | ||
painter = painterResource(id = R.drawable.ic_favorite_border_24px), | ||
contentDescription = stringResource(R.string.favorite_breed, breed.name) | ||
) | ||
} else { | ||
Image( | ||
painter = painterResource(id = R.drawable.ic_favorite_24px), | ||
contentDescription = stringResource(R.string.unfavorite_breed, breed.name) | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
app/src/main/kotlin/co/touchlab/kampkit/android/ui/MainNavCoordinator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package co.touchlab.kampkit.android.ui | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.navigation.NavController | ||
import androidx.navigation.NavType | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.composable | ||
import androidx.navigation.compose.rememberNavController | ||
import androidx.navigation.navArgument | ||
import org.koin.androidx.compose.get | ||
import org.koin.androidx.compose.koinViewModel | ||
import org.koin.core.parameter.parametersOf | ||
|
||
private const val BREEDS = "breeds" | ||
private const val BREED_DETAILS = "breedDetails" | ||
private const val BREED_ID_ARG = "breedId" | ||
|
||
@Composable | ||
fun MainNavCoordinator() { | ||
val navController = rememberNavController() | ||
NavHost(navController = navController, startDestination = "breeds") { | ||
composable(BREEDS) { | ||
BreedsScreen( | ||
viewModel = koinViewModel(), | ||
onBreedDetailsNavRequest = { breedId -> | ||
navController.navigateToBreedDetails(breedId) | ||
}, | ||
log = get { parametersOf("BreedsScreen") } | ||
) | ||
} | ||
composable( | ||
route = "$BREED_DETAILS/{$BREED_ID_ARG}", | ||
arguments = listOf(navArgument(BREED_ID_ARG) { type = NavType.LongType }) | ||
) { | ||
val breedId = it.arguments?.getLong(BREED_ID_ARG) | ||
BreedDetailsScreen( | ||
viewModel = koinViewModel { parametersOf(breedId) }, | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun NavController.navigateToBreedDetails(breedId: Long) { | ||
navigate("$BREED_DETAILS/$breedId") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.