Skip to content

HomeViewModel and ScoreRepository refactor #21

@zachseidner1

Description

@zachseidner1

We merged #20 to unblock people, but I think there's still some improvements that could be made.

HomeViewModel improvements

  • HomeUiState should be refactored to expose an ApiResponse instead of just a data class with data.
    • This way, in the UI we can do a when statement, and show loading and error states appropriately.
  • Right now, we have a function updateGameList that we have to remember to call whenever we we receive an update from scoreRepository.upcomingGamesFlow. But the whole point of flows is that so we don't have to remember to call these update functions and we can just have it update automatically!
    • So instead of making an update function, we should just use map, to transform the flow exposed by the repository into our UiState accordingly. If we do this, we can probably remove the init block entirely, since we'll just be sending down a mapped flow from the repository, so we don't need to attach any observers to it, we'll just exposing a read-only mapped version.
  • The date functions you made should go in a separate DateUtil.kt file. Also, you should try simplifying them using DateTimeFormatter

ScoreRepository Improvements

Before asking for improvements for this one, I made a generic function toResult that looks like this:

/**
 * Maps an ApolloResponse to a generic Kotlin result. It either provides the data with no errors, or
 * a failure response containing the error message in the throwable.
 */
fun <T : Operation.Data> ApolloResponse<T>.toResult(): Result<T> {
    if (hasErrors() || exception != null) {
        return Result.failure(
            exception?.cause ?: RuntimeException(
                errors?.firstOrNull()?.message ?: "Unknown error occurred"
            )
        )
    }
    return try {
        Result.success(dataOrThrow())
    } catch (e: NoDataException) {
        Result.failure(e)
    }
}

I think we should use this function for all of our Apollo calls, this way we can more easily see if it has errors or if the response is clean. So add this function to a file, then call it in ScoreRepository. Then make sure to handle the error and loading states appropriately. We can just fetch this data once when the app loads, in the init block of ScoreRepository. Use appScope for your repository scope, which I provided you with.

Metadata

Metadata

Assignees

Labels

infraRelated to improving the codebase

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions