Giter Club home page Giter Club logo

android-dev-challenge-compose-week1's Introduction

๐Ÿฑ Kitty adoption app - Week #1

Workflow result

๐Ÿ“œ Description

This is a simple master/detail app with a fake list of clickable data displaying more detailed information about it.

๐Ÿ’ก Motivation and Context

My goal was mainly to see how Jetpack Compose handles Navigation and Multiple form factor/layouts.

I'm proud of my work because I started late (was in holiday when challenge was first announced) and didn't spent a lot of time on this.
Still, I managed to submit a working version on time implementing Jetpack navigation and dedicated support of tablet.


I used MVVM, Unidirectional Data Flow and UI State pattern.
Each state has its own composable, see CatsScreen.kt.

Show me the code!
@Composable
fun CatsScreen(viewModel: CatsViewModel, selectedCat: CatModel?, onCatSelected: (CatModel) -> Unit) {
    Scaffold(
        topBar = { /* */ },
        content = {
            val state by viewModel.catsState.observeAsState(CatsScreenState.Loading)
            CatsStateDispatcher(uiState = state, selectedCat, onCatSelected)
        }
    )
}

@Composable
fun CatsStateDispatcher(uiState: CatsScreenState, selectedCat: CatModel?, onCatSelected: (CatModel) -> Unit) {
    when (uiState) {
        CatsScreenState.Loading -> LoadingCatsContent()
        is CatsScreenState.Error -> ErrorCatsContent(uiState.cause)
        CatsScreenState.Empty -> EmptyCatsContent()
        is CatsScreenState.Loaded -> LoadedCatsContent(uiState.cats, selectedCat, onCatSelected)
    }
}

I implemented a MainLayout composable to choose how to present the UI depending on device configuration, see MainActivity.kt/MainLayout.

Show me the code!
sealed class NavRoute(val path: String) {
    object CatsList : NavRoute("cats")
    object CatDetails : NavRoute("cat.details")
}

@Composable
fun MainLayout(catRepository: CatRepository = (CatRepository((FakeCatDataSource())))) {
    val catsViewModel = viewModel<CatsViewModel>(factory = CatsViewModelFactory(catRepository))
    Surface(color = MaterialTheme.colors.background) {
        if (booleanResource(R.bool.is_tablet)) {
            var selectedCatUUID by rememberSaveable { mutableStateOf<UUID?>(null) }
            val selectedCat = selectedCatUUID?.let { uuid ->
                catsViewModel.findCatByUUID(uuid)
            }
            if (booleanResource(R.bool.is_portrait)) {
                MainLayoutTabletPortrait(catsViewModel, selectedCat) { cat ->
                    selectedCatUUID = cat.uuid
                }
            } else {
                MainLayoutTabletLandscape(catsViewModel, selectedCat) { cat ->
                    selectedCatUUID = cat.uuid
                }
            }
        } else {
            val navController = rememberNavController()
            NavHost(navController, startDestination = NavRoute.CatsList.path) {
                composable(NavRoute.CatsList.path) {
                    CatsScreen(catsViewModel, null) { cat ->
                        navController.navigate("${NavRoute.CatDetails.path}/${cat.uuid}")
                    }
                }
                composable("${NavRoute.CatDetails.path}/{uuid}") { backStackEntry ->
                    val uuid = UUID.fromString(backStackEntry.arguments?.get("uuid") as String)
                    val cat = catsViewModel.findCatByUUID(uuid)
                    CatDetailsScreen(cat) { navController.popBackStack() }
                }
            }
        }
    }
}

I didn't see guidelines regarding how to handle tablet and side-by-side layouts, maybe there is something better ๐Ÿคทโ€โ™‚๏ธ.

๐Ÿ“ธ Screenshots

๐ŸŒž Light Mode

List Details Tablet

๐ŸŒš Dark Mode

List Details Tablet

License

Copyright 2020 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

android-dev-challenge-compose-week1's People

Contributors

opatry avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.