-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlickerRemoteDataSourceImpl.kt
47 lines (38 loc) · 1.44 KB
/
FlickerRemoteDataSourceImpl.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.mctech.showcase.feature.flicker_data.photo.remote
import com.mctech.showcase.feature.flicker_data.photo.remote.api.FlickerPhotoApi
import com.mctech.showcase.feature.flicker_domain.entity.FlickerPhoto
/**
* @author MAYCON CARDOSO on 2020-02-28.
*/
class FlickerRemoteDataSourceImpl(
private val api: FlickerPhotoApi
) : FlickerRemoteDataSource {
private var hasLoadedAll = false
override suspend fun loadPhotosByPage(tag: String, page: Int): List<FlickerPhoto> {
// Reset state.
if (page == 1) hasLoadedAll = false
// It's done. There is no pages anymore.
// So in order to avoid unnecessary requests, we cancel the flow.
if (hasLoadedAll) return listOf()
// Call api to get results.
val result = api.getPaginatedPhotos(tag, page).photos.also {
// Every photo has been loaded. There is no pages anymore.
if (it.page == it.pages) {
hasLoadedAll = true
}
}
// Map result to domain entity.
return result.photo.filter {
it.thumbnailUtl?.isNotBlank() ?: false &&
it.originalPhotoUrl?.isNotBlank() ?: false
}.map {
FlickerPhoto(
id = it.id,
tag = tag,
title = it.title,
sourceUrl = it.originalPhotoUrl.orEmpty(),
thumbnailUrl = it.thumbnailUtl.orEmpty()
)
}
}
}