generated from android/android-dev-challenge-compose
-
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.
- Loading branch information
Showing
6 changed files
with
170 additions
and
37 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
24 changes: 24 additions & 0 deletions
24
app/src/main/java/com/example/androiddevchallenge/Navigation.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,24 @@ | ||
package com.example.androiddevchallenge | ||
|
||
import androidx.navigation.NavController | ||
import androidx.navigation.compose.navigate | ||
import java.util.* | ||
|
||
sealed class Route(val path: String) { | ||
object Home : Route("home") | ||
object Detail : Route("detail/{id}") { | ||
val idArgument = "id" | ||
fun pathWithId(id: UUID) = path.replace("{id}", id.toString()) | ||
} | ||
} | ||
|
||
|
||
class NavigationActions(navController: NavController) { | ||
val openDetail: (UUID) -> Unit = { id -> | ||
navController.navigate(Route.Detail.pathWithId(id)) | ||
} | ||
|
||
val navigateUp: () -> Unit = { | ||
navController.popBackStack() | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
app/src/main/java/com/example/androiddevchallenge/ui/ChameleonAdoptionApp.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,49 @@ | ||
package com.example.androiddevchallenge.ui | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.navigation.NavType | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.composable | ||
import androidx.navigation.compose.navArgument | ||
import androidx.navigation.compose.rememberNavController | ||
import com.example.androiddevchallenge.NavigationActions | ||
import com.example.androiddevchallenge.Route | ||
import com.example.androiddevchallenge.data.CHAMELEONS | ||
import com.example.androiddevchallenge.ui.detail.Detail | ||
import com.example.androiddevchallenge.ui.home.Home | ||
import com.example.androiddevchallenge.ui.theme.MyTheme | ||
import java.util.* | ||
|
||
@Composable | ||
fun ChameleonAdoptionApp(darkTheme: Boolean) { | ||
val navController = rememberNavController() | ||
val navActions = remember(navController) { NavigationActions(navController) } | ||
|
||
MyTheme(darkTheme = darkTheme) { | ||
NavHost(navController, startDestination = Route.Home.path) { | ||
composable(Route.Home.path) { | ||
Home( | ||
darkTheme, | ||
CHAMELEONS.values.toList(), | ||
openDetail = navActions.openDetail, | ||
) | ||
} | ||
composable( | ||
Route.Detail.path, | ||
arguments = listOf( | ||
navArgument(Route.Detail.idArgument) { type = NavType.StringType }, | ||
) | ||
) { backStackEntry -> | ||
val id = backStackEntry.arguments?.getString(Route.Detail.idArgument) | ||
requireNotNull(id) | ||
val chameleon = CHAMELEONS.getValue(UUID.fromString(id)) | ||
|
||
Detail( | ||
chameleon = chameleon, | ||
navActions.navigateUp | ||
) | ||
} | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
app/src/main/java/com/example/androiddevchallenge/ui/detail/Detail.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,53 @@ | ||
package com.example.androiddevchallenge.ui.detail | ||
|
||
import androidx.compose.material.Surface | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import com.example.androiddevchallenge.R | ||
import com.example.androiddevchallenge.data.Chameleon | ||
import com.example.androiddevchallenge.data.Species | ||
import com.example.androiddevchallenge.ui.theme.MyTheme | ||
import java.util.* | ||
|
||
@Composable | ||
fun Detail(chameleon: Chameleon, navigateUp: () -> Unit) { | ||
Surface { | ||
|
||
} | ||
} | ||
|
||
// Previews | ||
|
||
@Preview("Light Theme", widthDp = 360, heightDp = 640) | ||
@Composable | ||
private fun LightPreview() { | ||
MyTheme { | ||
Detail( | ||
chameleon = Chameleon( | ||
id = UUID.randomUUID(), | ||
name = "Frodo", | ||
location = "Rivendell", | ||
species = Species.PANTHER, | ||
image = R.drawable.ic_chameleon_one, | ||
), | ||
navigateUp = {} | ||
) | ||
} | ||
} | ||
|
||
@Preview("Dark Theme", widthDp = 360, heightDp = 640) | ||
@Composable | ||
private fun DarkPreview() { | ||
MyTheme(darkTheme = true) { | ||
Detail( | ||
chameleon = Chameleon( | ||
id = UUID.randomUUID(), | ||
name = "Frodo", | ||
location = "Rivendell", | ||
species = Species.PANTHER, | ||
image = R.drawable.ic_chameleon_one, | ||
), | ||
navigateUp = {} | ||
) | ||
} | ||
} |
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