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
@Composable | |
fun RocketsRoute( | |
viewModel: RocketsViewModel = hiltViewModel() | |
) { | |
(...) | |
val uiState by viewModel.uiState.collectAsStateWithLifecycle() | |
RocketsScreen( | |
uiState = uiState, |
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
abstract class BaseViewModel<UI_STATE : Parcelable, PARTIAL_UI_STATE, EVENT, INTENT>( | |
(...) | |
) : ViewModel(), | |
IntentDelegate<INTENT, PARTIAL_UI_STATE> by IntentDelegateImpl(), | |
InternalChangesDelegate<PARTIAL_UI_STATE> by InternalChangesDelegateImpl(), | |
EventDelegate<EVENT> by EventDelegateImpl() { | |
init { | |
viewModelScope.launch { | |
merge( |
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 RocketsViewModel @Inject constructor( | |
(...) | |
private val refreshRocketsUseCase: RefreshRocketsUseCase, | |
(...) | |
) |
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 interface RefreshRocketsUseCase : suspend () -> Result<Unit> | |
suspend fun refreshRockets( | |
rocketRepository: RocketRepository, | |
): Result<Unit> = resultOf { | |
rocketRepository.refreshRockets() | |
} |
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
private fun refreshRockets(): Flow<PartialState> = flow<PartialState> { | |
refreshRocketsUseCase() | |
.onFailure { | |
emit(Error(it)) | |
} | |
}.onStart { | |
emit(Loading) | |
} |
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
@Module | |
@InstallIn(SingletonComponent::class) | |
internal object RocketModule { | |
@Provides | |
@Singleton | |
fun provideRocketApi( | |
retrofit: Retrofit | |
): RocketApi { | |
return retrofit.create(RocketApi::class.java) |
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 RocketRepositoryImpl @Inject constructor( | |
private val rocketApi: RocketApi, | |
private val rocketDao: RocketDao | |
) : RocketRepository { | |
override fun getRockets(): Flow<List<Rocket>> { | |
return rocketDao | |
.getRockets() | |
.map { rocketsCached -> | |
rocketsCached.map { it.toDomainModel() } |
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
private const val APP_DATABASE_NAME = "app_database_name" | |
@Module | |
@InstallIn(SingletonComponent::class) | |
internal object DatabaseModule { | |
@Singleton | |
@Provides | |
fun provideAppDatabase( | |
@ApplicationContext context: Context |
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
@Dao | |
interface RocketDao { | |
@Query("SELECT * FROM RocketCached") | |
fun getRockets(): Flow<List<RocketCached>> | |
@Upsert | |
suspend fun saveRockets(rockets: List<RocketCached>) | |
} |