diff --git a/app/src/main/java/com/activityartapp/domain/useCase/activities/GetActivitiesByPageFromRemote.kt b/app/src/main/java/com/activityartapp/domain/useCase/activities/GetActivitiesByPageFromRemote.kt index cd6125da..6f583b4d 100644 --- a/app/src/main/java/com/activityartapp/domain/useCase/activities/GetActivitiesByPageFromRemote.kt +++ b/app/src/main/java/com/activityartapp/domain/useCase/activities/GetActivitiesByPageFromRemote.kt @@ -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> { return try { Response.Success(data = api.getActivities( authHeader = "Bearer $code", page = page, - perPage = ACTIVITIES_PER_PAGE + perPage = activitiesPerPage ) .toList() .filter { diff --git a/app/src/main/java/com/activityartapp/domain/useCase/activities/GetActivitiesFromDiskAndRemote.kt b/app/src/main/java/com/activityartapp/domain/useCase/activities/GetActivitiesFromDiskAndRemote.kt index 63cf3868..affaea04 100644 --- a/app/src/main/java/com/activityartapp/domain/useCase/activities/GetActivitiesFromDiskAndRemote.kt +++ b/app/src/main/java/com/activityartapp/domain/useCase/activities/GetActivitiesFromDiskAndRemote.kt @@ -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( @@ -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() - 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)