Skip to content

Commit 1f01aae

Browse files
apirequest optimized
apirequest optimized
1 parent 93213e7 commit 1f01aae

File tree

11 files changed

+82
-51
lines changed

11 files changed

+82
-51
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.github.code.gambit.data.remote
2+
3+
import com.github.code.gambit.data.remote.responses.ListResponse
4+
import com.github.code.gambit.data.remote.responses.Response
5+
import com.github.code.gambit.utility.InternalServerException
6+
import com.github.code.gambit.utility.sharedpreference.LastEvaluatedKeyManager
7+
import java.lang.IllegalStateException
8+
9+
suspend fun <T> apiRequest(
10+
lekManager: LastEvaluatedKeyManager,
11+
keyType: LastEvaluatedKeyManager.KeyType,
12+
call: suspend (lek: String) -> ListResponse<T>
13+
): List<T> {
14+
val response = call.invoke(lekManager.getLastEvalKey(LastEvaluatedKeyManager.KeyType.FILE))
15+
response.body?.let { body ->
16+
body.lastEvaluatedKey?.let { lekManager.putLastEvalKey(it, keyType) }
17+
return body.items
18+
} ?: response.error?.let { throw InternalServerException(it) }
19+
?: throw IllegalStateException("Internal Server Error V2")
20+
}
21+
22+
suspend fun <T> apiRequest(
23+
call: suspend () -> Response<T>
24+
): T {
25+
val response = call.invoke()
26+
response.body?.let { body ->
27+
return body
28+
} ?: response.error?.let { throw InternalServerException(it) }
29+
?: throw IllegalStateException("Internal Server Error V2")
30+
}
Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.github.code.gambit.data.remote.services.file
22

33
import com.github.code.gambit.data.entity.network.FileNetworkEntity
4-
import com.github.code.gambit.data.remote.responses.ListResponse
4+
import com.github.code.gambit.data.remote.apiRequest
55
import com.github.code.gambit.data.remote.services.ApiService
66
import com.github.code.gambit.utility.sharedpreference.LastEvaluatedKeyManager
77
import com.github.code.gambit.utility.sharedpreference.UserManager
8-
import java.lang.Exception
98

109
class FileServiceImpl(
1110
val apiService: ApiService,
@@ -16,39 +15,33 @@ class FileServiceImpl(
1615
private val userId get() = userManager.getUserId()
1716

1817
override suspend fun getFiles(): List<FileNetworkEntity> {
19-
val lek: String = lekManager.getLastEvalKey(LastEvaluatedKeyManager.KeyType.FILE)
20-
val listResponse: ListResponse<FileNetworkEntity> = apiService.getFiles(userId, lek, null)
21-
if (listResponse.body == null) {
22-
throw Exception(listResponse.error)
18+
return apiRequest(lekManager, LastEvaluatedKeyManager.KeyType.FILE) { lek ->
19+
apiService.getFiles(userId, lek, null)
2320
}
24-
if (listResponse.body?.lastEvaluatedKey != null) {
25-
lekManager.putLastEvalKey(
26-
listResponse.body?.lastEvaluatedKey!!,
27-
LastEvaluatedKeyManager.KeyType.FILE
28-
)
29-
}
30-
return listResponse.body?.items!!
3121
}
3222

3323
override suspend fun filterFiles(start: String, end: String): List<FileNetworkEntity> {
34-
val response = apiService.filterFiles(userId, null, start, end)
35-
return response.body?.items!!
24+
return apiRequest(lekManager, LastEvaluatedKeyManager.KeyType.FILTER_FILE) {
25+
apiService.filterFiles(userId, null, start, end)
26+
}
3627
}
3728

3829
override suspend fun searchFile(searchParam: String): List<FileNetworkEntity> {
39-
val response = apiService.getFiles(userId, null, searchParam)
40-
return response.body?.items!!
30+
return apiRequest(lekManager, LastEvaluatedKeyManager.KeyType.SEARCH_FILE) {
31+
apiService.getFiles(userId, null, searchParam)
32+
}
4133
}
4234

4335
override suspend fun uploadFile(fileNetworkEntity: FileNetworkEntity): FileNetworkEntity {
44-
return apiService.uploadFiles(userId, fileNetworkEntity).body!!
36+
return apiRequest { apiService.uploadFiles(userId, fileNetworkEntity) }
4537
}
4638

4739
override suspend fun deleteFile(fileId: String): Boolean {
4840
var id = fileId
4941
if (fileId.contains("FILE#")) {
5042
id = fileId.split("#")[1]
5143
}
52-
return apiService.deleteFile(userId, id).statusCode == 200
44+
val response = apiRequest { apiService.deleteFile(userId, id) }
45+
return true
5346
}
5447
}
Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,28 @@
11
package com.github.code.gambit.data.remote.services.url
22

33
import com.github.code.gambit.data.entity.network.UrlNetworkEntity
4-
import com.github.code.gambit.data.remote.responses.ListResponse
4+
import com.github.code.gambit.data.remote.apiRequest
55
import com.github.code.gambit.data.remote.services.ApiService
66
import com.github.code.gambit.utility.sharedpreference.LastEvaluatedKeyManager
77

88
class UrlServiceImpl(val apiService: ApiService, private val lekManager: LastEvaluatedKeyManager) : UrlService {
99

1010
override suspend fun getUrls(fileId: String): List<UrlNetworkEntity> {
11-
val lek: String = lekManager.getLastEvalKey(LastEvaluatedKeyManager.KeyType.URL)
12-
val listResponse: ListResponse<UrlNetworkEntity> = if (lek == "") {
11+
return apiRequest(lekManager, LastEvaluatedKeyManager.KeyType.URL) {
1312
apiService.getUrls(fileId.split("#")[1])
14-
} else {
15-
apiService.getUrls(fileId.split("#")[1])
16-
}
17-
if (listResponse.body?.lastEvaluatedKey != null) {
18-
lekManager.putLastEvalKey(
19-
listResponse.body?.lastEvaluatedKey!!,
20-
LastEvaluatedKeyManager.KeyType.URL
21-
)
2213
}
23-
return listResponse.body?.items!!
2414
}
2515

2616
override suspend fun generateUrl(fileId: String, urlNetworkEntity: UrlNetworkEntity): String {
2717
val id = fileId.split("#")[1]
28-
return apiService.generateUrl(id, urlNetworkEntity).body!!
18+
return apiRequest { apiService.generateUrl(id, urlNetworkEntity) }
2919
}
3020

3121
override suspend fun updateUrl(fileId: String, urlId: String, urlNetworkEntity: UrlNetworkEntity): UrlNetworkEntity {
32-
return apiService.updateUrl(fileId, urlId, urlNetworkEntity).body!!
22+
return apiRequest { apiService.updateUrl(fileId, urlId, urlNetworkEntity) }
3323
}
3424

3525
override suspend fun deleteUrl(fileId: String, urlId: String): UrlNetworkEntity {
36-
return apiService.deleteUrl(fileId, urlId).body!!
26+
return apiRequest { apiService.deleteUrl(fileId, urlId) }
3727
}
3828
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.code.gambit.data.remote.services.user
22

33
import com.github.code.gambit.data.entity.network.UserNetworkEntity
4+
import com.github.code.gambit.data.remote.apiRequest
45
import com.github.code.gambit.data.remote.services.ApiService
56
import com.github.code.gambit.utility.sharedpreference.UserManager
67

@@ -9,16 +10,14 @@ class UserServiceImpl(val apiService: ApiService, private val userManager: UserM
910
private val userId get() = userManager.getUserId()
1011

1112
override suspend fun getUser(): UserNetworkEntity {
12-
val id = userId
13-
val user = apiService.getUser(id)
14-
return user.body!!
13+
return apiRequest { apiService.getUser(userId) }
1514
}
1615

1716
override suspend fun updateUser(userNetworkEntity: UserNetworkEntity): UserNetworkEntity {
18-
return apiService.updateUser(userId, userNetworkEntity).body!!
17+
return apiRequest { apiService.updateUser(userId, userNetworkEntity) }
1918
}
2019

2120
override suspend fun deleteUser(): UserNetworkEntity {
22-
return apiService.deleteUser(userId).body!!
21+
return apiRequest { apiService.deleteUser(userId) }
2322
}
2423
}

app/src/main/java/com/github/code/gambit/di/NetworkModule.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import com.github.code.gambit.data.remote.NetworkDataSource
1010
import com.github.code.gambit.data.remote.NetworkDataSourceImpl
1111
import com.github.code.gambit.data.remote.services.ApiService
1212
import com.github.code.gambit.data.remote.services.NetworkInterceptor
13+
import com.github.code.gambit.data.remote.services.auth.AuthService
14+
import com.github.code.gambit.data.remote.services.auth.AuthServiceImpl
1315
import com.github.code.gambit.data.remote.services.file.FileService
1416
import com.github.code.gambit.data.remote.services.file.FileServiceImpl
1517
import com.github.code.gambit.data.remote.services.url.UrlService
@@ -104,6 +106,12 @@ object NetworkModule {
104106
return UserServiceImpl(apiService, userManager)
105107
}
106108

109+
@Singleton
110+
@Provides
111+
fun provideAuthService(): AuthService {
112+
return AuthServiceImpl()
113+
}
114+
107115
@Singleton
108116
@Provides
109117
fun provideNetworkDataSource(

app/src/main/java/com/github/code/gambit/di/RepositoryModule.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import com.github.code.gambit.data.local.CacheDataSource
44
import com.github.code.gambit.data.mapper.aws.UserAttributeMapper
55
import com.github.code.gambit.data.remote.NetworkDataSource
66
import com.github.code.gambit.data.remote.services.auth.AuthService
7-
import com.github.code.gambit.data.remote.services.auth.AuthServiceImpl
87
import com.github.code.gambit.repositories.auth.AuthRepository
98
import com.github.code.gambit.repositories.auth.AuthRepositoryImpl
109
import com.github.code.gambit.repositories.fileupload.FileUploadRepository
@@ -31,12 +30,6 @@ object RepositoryModule {
3130
return UserAttributeMapper()
3231
}
3332

34-
@Singleton
35-
@Provides
36-
fun provideAuthService(): AuthService {
37-
return AuthServiceImpl()
38-
}
39-
4033
@Singleton
4134
@Provides
4235
fun provideAuthRepository(

app/src/main/java/com/github/code/gambit/repositories/home/HomeRepositoryImpl.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ constructor(
3030
emit(ServiceResult.Success(files))
3131
} catch (internet: NoInternetException) {
3232
emit(ServiceResult.Error(internet))
33+
val files = cacheDataSource.getFiles()
34+
emit(ServiceResult.Success(files))
3335
} catch (exception: Exception) {
3436
emit(ServiceResult.Error(exception))
3537
}

app/src/main/java/com/github/code/gambit/ui/fragment/home/main/HomeFragment.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ class HomeFragment : Fragment(R.layout.fragment_home), FileUrlClickCallback, Bot
125125
private fun registerEventCallbacks() {
126126
viewModel.homeState.observe(viewLifecycleOwner) {
127127
when (it) {
128-
is HomeState.Error -> longToast(it.message)
128+
is HomeState.Error -> {
129+
binding.linearProgress.hide()
130+
longToast(it.message)
131+
}
129132
is HomeState.FilesLoaded -> {
130133
if (!it.isSearchResult) {
131134
if (isFirstLoading) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.github.code.gambit.utility
2+
3+
import java.lang.Exception
4+
5+
class InternalServerException(message: String) : Exception(message)

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,27 @@ constructor(@ApplicationContext context: Context) : PreferenceManager(context) {
1010
when (keyType) {
1111
KeyType.FILE -> put(Key.FILE_LEK, lastEvaluatedKey)
1212
KeyType.URL -> put(Key.URL_LEK, lastEvaluatedKey)
13+
KeyType.FILTER_FILE -> put(Key.FILTER_FILE_LEK, lastEvaluatedKey)
14+
KeyType.SEARCH_FILE -> put(Key.SEARCH_FILE_LEK, lastEvaluatedKey)
1315
}
1416
}
1517

1618
fun getLastEvalKey(keyType: KeyType): String {
1719
return when (keyType) {
1820
KeyType.FILE -> get(Key.FILE_LEK)
1921
KeyType.URL -> get(Key.URL_LEK)
22+
KeyType.FILTER_FILE -> get(Key.FILTER_FILE_LEK)
23+
KeyType.SEARCH_FILE -> get(Key.SEARCH_FILE_LEK)
2024
}
2125
}
2226

2327
fun flush() {
24-
putLastEvalKey("", KeyType.FILE)
25-
putLastEvalKey("", KeyType.URL)
28+
val empty = ""
29+
putLastEvalKey(empty, KeyType.FILE)
30+
putLastEvalKey(empty, KeyType.URL)
31+
putLastEvalKey(empty, KeyType.FILTER_FILE)
32+
putLastEvalKey(empty, KeyType.SEARCH_FILE)
2633
}
2734

28-
enum class KeyType { FILE, URL }
35+
enum class KeyType { FILE, URL, FILTER_FILE, SEARCH_FILE }
2936
}

0 commit comments

Comments
 (0)