Skip to content

Instantly share code, notes, and snippets.

View krzdabrowski's full-sized avatar

Krzysztof Dąbrowski krzdabrowski

View GitHub Profile
@Composable
fun NavigationHost(
navController: NavHostController,
factories: Set<NavigationFactory>,
modifier: Modifier = Modifier
) {
NavHost(
navController = navController,
startDestination = NavigationDestination.Rockets.route,
modifier = modifier
@Composable
fun RocketsRoute(
viewModel: RocketsViewModel = hiltViewModel()
) {
(...)
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
RocketsScreen(
uiState = uiState,
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(
@HiltViewModel
class RocketsViewModel @Inject constructor(
(...)
private val refreshRocketsUseCase: RefreshRocketsUseCase,
(...)
)
fun interface RefreshRocketsUseCase : suspend () -> Result<Unit>
suspend fun refreshRockets(
rocketRepository: RocketRepository,
): Result<Unit> = resultOf {
rocketRepository.refreshRockets()
}
private fun refreshRockets(): Flow<PartialState> = flow<PartialState> {
refreshRocketsUseCase()
.onFailure {
emit(Error(it))
}
}.onStart {
emit(Loading)
}
@Module
@InstallIn(SingletonComponent::class)
internal object RocketModule {
@Provides
@Singleton
fun provideRocketApi(
retrofit: Retrofit
): RocketApi {
return retrofit.create(RocketApi::class.java)
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() }
private const val APP_DATABASE_NAME = "app_database_name"
@Module
@InstallIn(SingletonComponent::class)
internal object DatabaseModule {
@Singleton
@Provides
fun provideAppDatabase(
@ApplicationContext context: Context
@Dao
interface RocketDao {
@Query("SELECT * FROM RocketCached")
fun getRockets(): Flow<List<RocketCached>>
@Upsert
suspend fun saveRockets(rockets: List<RocketCached>)
}