Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler-Lopez committed Feb 19, 2023
2 parents 48e1409 + f92db19 commit 5cfb618
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@ import javax.inject.Inject
class GetActivitiesByPageFromRemote @Inject constructor(
private val api: AthleteApi // Impl of API
) {
companion object {
private const val ACTIVITIES_PER_PAGE = 200
}

suspend operator fun invoke(
code: String,
page: Int
page: Int,
activitiesPerPage: Int
): Response<List<Activity>> {
return try {
Response.Success(data = api.getActivities(
authHeader = "Bearer $code",
page = page,
perPage = ACTIVITIES_PER_PAGE
perPage = activitiesPerPage
)
.toList()
.filter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class GetActivitiesFromDiskAndRemote @Inject constructor(
private const val MAXIMUM_USAGE = 25
private const val MAXIMUM_PAGE = 10
private const val PAGE_FIRST = 1
private const val ACTIVITIES_PER_PAGE = 200
}

suspend operator fun invoke(
Expand Down Expand Up @@ -56,23 +57,28 @@ class GetActivitiesFromDiskAndRemote @Inject constructor(
}
}

println("got usage, it was $usage")

var page = PAGE_FIRST
var activitiesFromRemoteUnique = true
var continueReadingFromRemote = true
val remoteActivities = mutableListOf<Activity>()
while (usage < MAXIMUM_USAGE && page < MAXIMUM_PAGE && activitiesFromRemoteUnique) {
while (usage < MAXIMUM_USAGE && page < MAXIMUM_PAGE && continueReadingFromRemote) {
/** Load this page of activities from remote **/
getActivitiesByPageFromRemote(code = athlete.accessToken, page = page)
getActivitiesByPageFromRemote(
code = athlete.accessToken,
page = page,
activitiesPerPage = ACTIVITIES_PER_PAGE
)
.doOnSuccess {
page++
insertAthleteUsageIntoRemote(athlete.athleteId, ++usage)

/** If there are no common activities between remote & cache, continue loading **/
val newActivities = data.filter {
!cachedActivitiesIds.contains(it.id)
}

activitiesFromRemoteUnique = newActivities.size == data.size
val activitiesAreAllUnique = newActivities.size == data.size
val activityResponseWasEmpty = data.isNotEmpty()
continueReadingFromRemote = activitiesAreAllUnique && activityResponseWasEmpty

/** Add all activities **/
remoteActivities.addAll(newActivities)
Expand Down

0 comments on commit 5cfb618

Please sign in to comment.