google-developer-training / basic-android-kotlin-compose-training-mars-photos

Solution code for Android Basics in Kotlin course
https://developer.android.com/courses/android-basics-kotlin/course?gclid=CjwKCAjw4c-ZBhAEEiwAZ105RTyT-iaLHzrhMBUXdMhO230ZDwOwxxI2x4RgK8DwBxK8t1h0wmU_QxoCi4YQAvD_BwE
Apache License 2.0
66 stars 54 forks source link

Is it possible to get data from MarsUiState.Success in different Screens without load/call it again and again? #137

Open petrospap opened 3 months ago

petrospap commented 3 months ago

Hello, I am sorry for those rookie questions, I want to say that this is not an issue, so is OK if you ignore or delete this post. :)

I am totally new to Android studio/Kotlin, and some (plain and easy) issues that I have is extremely difficult to find solution. Taking this course https://developer.android.com/codelabs/basic-android-kotlin-compose-load-images (and many others), I try to use data (Object) from MarsUiState.Success in another Screen/Fragment WITHOUT to load/call it again and again.

An example how to use it now, in MarsPhotosApp

@Composable
fun Navigation(navController: NavHostController) {
    // load once
    val marsViewModel: MarsViewModel = viewModel(factory = MarsViewModel.Factory)

    NavHost(navController,
        startDestination = Screens.Home.route,
    ) {
        composable(Screens.Home.route) {
        // Dont call marsViewModel here, duplicates on Screens.Map
            // val marsViewModel: MarsViewModel = viewModel(factory = MarsViewModel.Factory)
            HomeScreen(
                marsUiState = marsViewModel.marsUiState,
                retryAction = marsViewModel::getMarsPhotos,
                //contentPadding = it
            )
        }
        composable(Screens.Map.route) {
            // How to get different data aka moonPhotos? Build a new model?
            // val marsViewModel: MarsViewModel = viewModel(factory = MarsViewModel.Factory)

            MapScreen(
                marsUiState = marsViewModel.marsUiState,
                retryAction = marsViewModel::getMarsPhotos,
                photoId = "0"
            )
        }
        composable(Screens.About.route) {
            AboutScreen()
        }

    }
}

The photo data does not change, so there no reason to call again and again when (marsUiState) on every Screen/Fragment

Ideal (for me) is to ran in MainActivity

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        enableEdgeToEdge()
        super.onCreate(savedInstanceState)

        setContent {
            MarsPhotosTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                ) {
                    val marsViewModel: MarsViewModel = viewModel(factory = MarsViewModel.Factory)
                      StartApp(
                              marsUiState = marsViewModel.marsUiState,
                              retryAction = marsViewModel::getMarsPhotos,
                         )
                }
            }
        }
    }

    @Composable
    fun StartApp(
    marsUiState: MarsUiState,
        retryAction: () -> Unit,
        modifier: Modifier = Modifier,
        // contentPadding: PaddingValues = PaddingValues(0.dp),
    ) {
          when (marsUiState) {
              is MarsUiState.Loading -> LoadingScreen(modifier = modifier.fillMaxSize())
              is MarsUiState.Success -> runApp(
                  marsUiState.photos, 
                  modifier = modifier.fillMaxWidth()
              )
              is MarsUiState.Error -> ErrorScreen(modifier = modifier.fillMaxSize())
          }
    }

    private fun runApp(
        photos: List<MarsPhoto>,
    ){
        // Start mainApp, navigation, etc
        // how to get photos in other Screens without call again marsUiState
        // photos already loaded!
        Log.d("PHOTOS", photos.toString())
    }
}

Any idea how to to get photos in other Screens without call again marsUiState Thank you!

PS: In Add repository and Manual DI

Get the solution code

Download zip does not include solution..