Skip to content

Commit 9fff250

Browse files
authored
Pagination for file listing
Fixes #41: Pagination implemented in file listing
2 parents 6367e07 + c85d182 commit 9fff250

File tree

6 files changed

+32
-18
lines changed

6 files changed

+32
-18
lines changed

app/src/main/java/com/github/code/gambit/data/remote/responses/ListResponse.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class BodyTemplate<T>(
1919
@Expose
2020
var items: List<T>,
2121

22-
@SerializedName("lastEvaluatedKey")
22+
@SerializedName("LastEvaluatedKey")
2323
@Expose
24-
var lastEvaluatedKey: String
24+
var lastEvaluatedKey: String?
2525
)

app/src/main/java/com/github/code/gambit/data/remote/services/ApiService.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,41 @@ import retrofit2.http.GET
1212
import retrofit2.http.POST
1313
import retrofit2.http.PUT
1414
import retrofit2.http.Path
15+
import retrofit2.http.Query
1516

1617
interface ApiService {
1718

18-
@GET("user/{userId}")
19+
@GET("user/{${AppConstant.API_PATH.USER_ID}}")
1920
suspend fun getUser(@Path("userId") userId: String): Response<UserNetworkEntity>
2021

21-
@PUT("user/{userId}")
22+
@PUT("user/{${AppConstant.API_PATH.USER_ID}}")
2223
suspend fun updateUser(@Path("userId") userId: String, @Body userNetworkEntity: UserNetworkEntity): Response<UserNetworkEntity>
2324

24-
@DELETE("user/{userId}")
25+
@DELETE("user/{${AppConstant.API_PATH.USER_ID}}")
2526
suspend fun deleteUser(@Path("userId") userId: String): Response<UserNetworkEntity>
2627

27-
@GET("user/{userId}/file")
28-
suspend fun getFiles(@Path("userId") userId: String): ListResponse<FileNetworkEntity>
28+
@GET("user/{${AppConstant.API_PATH.USER_ID}}/file")
29+
suspend fun getFiles(@Path("userId") userId: String, @Query(AppConstant.API_QUERY.FILE_LEK) lastEvalKey: String?): ListResponse<FileNetworkEntity>
2930

30-
@POST("user/{userId}/file")
31+
@POST("user/{${AppConstant.API_PATH.USER_ID}}/file")
3132
suspend fun uploadFiles(@Path("userId") userId: String, @Body fileNetworkEntity: FileNetworkEntity): Response<FileNetworkEntity>
3233

33-
@DELETE("user/{userId}/file/{fileId}")
34+
@DELETE("user/{${AppConstant.API_PATH.USER_ID}}/file/{${AppConstant.API_PATH.FILE_ID}}")
3435
suspend fun deleteFile(@Path(AppConstant.API_PATH.USER_ID) userId: String, @Path(AppConstant.API_PATH.FILE_ID) fileId: String): Response<FileNetworkEntity>
3536

36-
@GET("file/{fileId}/url")
37+
@GET("file/{${AppConstant.API_PATH.FILE_ID}}/url")
3738
suspend fun getUrls(@Path(AppConstant.API_PATH.FILE_ID) fileId: String): ListResponse<UrlNetworkEntity>
3839

39-
@POST("file/{fileId}/url")
40+
@POST("file/{${AppConstant.API_PATH.FILE_ID}}/url")
4041
suspend fun generateUrl(@Path(AppConstant.API_PATH.FILE_ID) fileId: String): Response<UrlNetworkEntity>
4142

42-
@PUT("file/{fileId}/url/{urlId}")
43+
@PUT("file/{${AppConstant.API_PATH.FILE_ID}}/url/{${AppConstant.API_PATH.URL_ID}}")
4344
suspend fun updateUrl(
4445
@Path(AppConstant.API_PATH.FILE_ID) fileId: String,
4546
@Path(AppConstant.API_PATH.URL_ID) urlId: String,
4647
@Body urlNetworkEntity: UrlNetworkEntity
4748
): Response<UrlNetworkEntity>
4849

49-
@DELETE("file/{fileId}/url/{urlId}")
50+
@DELETE("file/{${AppConstant.API_PATH.FILE_ID}}/url/{${AppConstant.API_PATH.URL_ID}}")
5051
suspend fun deleteUrl(@Path(AppConstant.API_PATH.FILE_ID) fileId: String, @Path(AppConstant.API_PATH.URL_ID) urlId: String): Response<UrlNetworkEntity>
5152
}

app/src/main/java/com/github/code/gambit/data/remote/services/file/FileServiceImpl.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ class FileServiceImpl(
1212
private val userId get() = userManager.getUserId()
1313

1414
override suspend fun getFiles(): List<FileNetworkEntity> {
15-
return apiService.getFiles(userId).body.items
15+
var lek: String? = userManager.getFileLastEvaluatedKey()
16+
if (lek == "") {
17+
lek = null
18+
}
19+
val listResponse = apiService.getFiles(userId, lek)
20+
userManager.putFileLastEvaluatedKey(listResponse.body.lastEvaluatedKey)
21+
return listResponse.body.items
1622
}
1723

1824
override suspend fun uploadFile(fileNetworkEntity: FileNetworkEntity): FileNetworkEntity {

app/src/main/java/com/github/code/gambit/utility/AppConstant.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ object AppConstant {
3030
const val URL_ID = "urlId"
3131
}
3232

33+
object API_QUERY {
34+
const val FILE_LEK = "LastEvaluatedKey"
35+
}
36+
3337
object Worker {
3438
const val FILE_URI_KEY = "FILE_URI"
3539
const val FILE_NAME_KEY = "FILE_NAME"

app/src/main/java/com/github/code/gambit/utility/sharedpreference/PreferenceManager.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ abstract class PreferenceManager(var context: Context) {
1111
val pref: SharedPreferences = context.getSharedPreferences(prefName, Context.MODE_PRIVATE)
1212
private val editor: SharedPreferences.Editor get() = pref.edit()
1313

14-
fun <T> put(key: Key, value: T) {
14+
fun <T> put(key: Key, value: T?) {
1515
when (value) {
1616
is String -> {
1717
editor.putString(key.value, value).apply()
@@ -48,7 +48,6 @@ enum class Key(val value: String) {
4848
AUTHSTATE("IS-AUTHENTICATED"),
4949
EMAIL("EMAIL"),
5050
USER("USER"),
51-
USERID("USER-ID"),
52-
USERNAME("USER-NAME"),
53-
TOKEN("ID-TOKEN")
51+
TOKEN("ID-TOKEN"),
52+
FILE_LEK("FILE-LAST-EVALUATED-KEY")
5453
}

app/src/main/java/com/github/code/gambit/utility/sharedpreference/UserManager.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import dagger.hilt.android.qualifiers.ApplicationContext
77
class UserManager
88
constructor(@ApplicationContext context: Context) : PreferenceManager(context) {
99

10+
fun putFileLastEvaluatedKey(lastEvaluatedKey: String?) = put(Key.FILE_LEK, lastEvaluatedKey)
11+
12+
fun getFileLastEvaluatedKey() = get<String>(Key.FILE_LEK)
13+
1014
fun setAuthenticated(value: Boolean) = put(Key.AUTHSTATE, value)
1115

1216
private fun setUserEmail(email: String) = put(Key.EMAIL, email)

0 commit comments

Comments
 (0)