3

I'm trying Jetpack Compose and I've created a simple app to consume the Star Wars API with Ktor, Room, Hilt, Paging3 and Material3.

I can't understand how to propagate any eventual error caught during the data fetch.

AppDatabase.kt

@Database(
    entities = [
        SWCharacter::class
    ], version = 2, exportSchema = false
)
abstract class AppDatabase : RoomDatabase() {
    abstract fun people(): PeopleDao
}

PeopleService.kt

class PeopleService(private val httpClient: HttpClient) {
    suspend fun getPeople(page: Int?): SwapiResult<SWCharacter> {
        return httpClient.get(path = "api/people") {
            parameter("page", page)
        }
    }
}

RemoteMediator.kt

@ExperimentalPagingApi
class PeopleRemoteMediator @Inject constructor(private val database: AppDatabase, private val service: PeopleService) : RemoteMediator<Int, SWCharacter>() {
    private val dao: PeopleDao = database.people()

    override suspend fun initialize(): InitializeAction {
        return if (database.people().getCount() > 0) {
            InitializeAction.SKIP_INITIAL_REFRESH
        } else {
            InitializeAction.LAUNCH_INITIAL_REFRESH
        }
    }

    override suspend fun load(loadType: LoadType, state: PagingState<Int, SWCharacter>): MediatorResult {
        return try {
            val loadPage = when (loadType) {
                LoadType.REFRESH -> 1
                LoadType.PREPEND -> return MediatorResult.Success(endOfPaginationReached = true)
                LoadType.APPEND -> {
                    2
                }
            }

            val response = service.getPeople(page = loadPage)

            database.withTransaction {
                if (loadType == LoadType.REFRESH) {
                    dao.clearAll()
                }

                dao.insertAll(response.results)
            }

            MediatorResult.Success(endOfPaginationReached = response.next == null)

        } catch (e: IOException) {
            MediatorResult.Error(e)
        } catch (e: ResponseException) {
            MediatorResult.Error(e)
        }
    }
}

and PeopleRepository.kt

class PeopleRepository @Inject constructor(private val database: AppDatabase, private val service: PeopleService) {

    @ExperimentalPagingApi
    fun fetchPeople(): Flow<PagingData<SWCharacter>> {
        return Pager(
            PagingConfig(pageSize = 10, enablePlaceholders = false, prefetchDistance = 3),
            remoteMediator = PeopleRemoteMediator(database, service),
            pagingSourceFactory = { database.people().pagingSource() }
        ).flow
    }
}

I consume all the above code inside a ViewModel

@ExperimentalPagingApi
@HiltViewModel
class PeopleViewModel @Inject constructor(private val repo: PeopleRepository) : ViewModel() {
    private val _uiState = mutableStateOf<UIState>(UIState.Loading)
    val uiState: State<UIState>
        get() = _uiState

    init {
        viewModelScope.launch {
            try {
                val people = repo.fetchPeople()
                _uiState.value = UIState.Success(data = people)
            } catch (e: Exception) {
                _uiState.value = UIState.Error(message = e.message ?: "Error")
            }
        }
    }
}

sealed class UIState {
    object Loading : UIState()
    data class Success<T>(val data: T) : UIState()
    data class Error(val message: String) : UIState()
}

To simulate an error, I'll change the endpoint address to i.e.:

return httpClient.get(path = "api/peoplethatnotexists")

The error is caught correctly in the RemoteMediator, but I don't know how to catch it in the view model.

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Valerio
  • 3,297
  • 3
  • 27
  • 44

1 Answers1

1

In order to listen to RemoteMediator errors, you can add a LoadStateListener to your RecyclerView adapter as follows:

mAdapter = MyAdapter(...).apply {
    addLoadStateListener { loadState ->
        val mediatorLoadState: LoadState? = loadState.mediator?.refresh

        if (mediatorLoadState is LoadState.Error) {
            // Herein you can implement your fallback 
            // as per mediatorLoadState.error 
        }
    }
}

Likewise, you can change loadState.mediator?.xxx by the RemoteMediator's state you want to listen to.