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!
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
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
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..