View AddEditPlanetViewModel.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
class AddEditPlanetViewModel @Inject constructor( | |
private val addPlanetUseCase: AddPlanetUseCase, | |
... | |
): ViewModel() { | |
... | |
fun savePlanet() { | |
viewModelScope.launch { | |
addPlanetUseCase( |
View AddEditPlanetViewModel.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
fun setPlanetName(name: String) { | |
_uiState.update { it.copy(planetName = name) } | |
} | |
fun setPlanetDistanceLy(distanceLy: Float) { | |
_uiState.update { it.copy(planetDistanceLy = distanceLy) } | |
} |
View AddEditPlanetViewModel.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
@HiltViewModel | |
class AddEditPlanetViewModel @Inject constructor( | |
savedStateHandle: SavedStateHandle, | |
private val planetsRepository: PlanetsRepository | |
): ViewModel() { | |
private val planetId: String? = savedStateHandle[PlanetsDestinationsArgs.PLANET_ID_ARG] | |
private val _uiState = MutableStateFlow(AddEditPlanetUiState()) | |
val uiState: StateFlow<AddEditPlanetUiState> = _uiState.asStateFlow() |
View AddEditPlanetViewModel.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
data class AddEditPlanetUiState( | |
val planetName: String = "", | |
val planetDistanceLy: Float = 1.0F, | |
val planetDiscovered: Date = Date(), | |
val isLoading: Boolean = false, | |
val isPlanetSaved: Boolean = false | |
) | |
@HiltViewModel | |
class AddEditPlanetViewModel @Inject constructor(): ViewModel() { |
View PlanetsListViewModel.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
fun addSamplePlanets() { | |
viewModelScope.launch { | |
val planets = arrayOf( | |
Planet(name = "Skaro", distanceLy = 0.5F, discovered = Date()), | |
Planet(name = "Trenzalore", distanceLy = 5F, discovered = Date()), | |
Planet(name = "Galifrey", distanceLy = 80F, discovered = Date()), | |
) | |
planets.forEach { addPlanetUseCase(it) } | |
} | |
} |
View PlanetsListViewModel.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
fun deletePlanet(planetId: String) { | |
viewModelScope.launch { | |
planetsRepository.deletePlanet(planetId) | |
} | |
} | |
fun refreshPlanetsList() { | |
viewModelScope.launch { | |
planetsRepository.refreshPlanets() | |
} |
View PlanetsListViewModel.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
package com.tdcolvin.planetspotters.ui.planetslist | |
... | |
@HiltViewModel | |
class PlanetsListViewModel @Inject constructor( | |
planetsRepository: PlanetsRepository | |
): ViewModel() { | |
private val planets = planetsRepository.getPlanetsFlow() |
View PlanetsListViewModel.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
data class PlanetsListUiState( | |
val planets: List<Planet> = emptyList(), | |
val isLoading: Boolean = false, | |
val isError: Boolean = false | |
) |
View LocalDataSource.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
interface LocalDataSource { | |
fun getPlanetsFlow(): Flow<WorkResult<List<Planet>>> | |
fun getPlanetFlow(planetId: String): Flow<WorkResult<Planet?>> | |
suspend fun setPlanets(planets: List<Planet>) | |
suspend fun addPlanet(planet: Planet) | |
suspend fun deletePlanet(planetId: String) | |
} |
View PlanetsRepository.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
override suspend fun addPlanet(planet: Planet) { | |
val planetWithId = remoteDataSource.addPlanet(planet) | |
localDataSource.addPlanet(planetWithId) | |
} | |
override suspend fun deletePlanet(planetId: String) { | |
remoteDataSource.deletePlanet(planetId) | |
localDataSource.deletePlanet(planetId) | |
} |
NewerOlder