Skip to content

Commit ec69be9

Browse files
committed
Code refactoring.
Renamed request class. Moved url to routes class.
1 parent 20ee1b3 commit ec69be9

File tree

6 files changed

+37
-30
lines changed

6 files changed

+37
-30
lines changed

app/src/main/java/ytemplate/android/data/MyPostRepository.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import ytemplate.android.data.datasource.LocalPostDataSource
66
import ytemplate.android.data.datasource.RemotePostDataSource
77
import ytemplate.android.data.model.PostRequest
88
import ytemplate.android.database.model.Post
9-
import ytemplate.android.infra.AppRequest
9+
import ytemplate.android.infra.AppResult
1010
import javax.inject.Inject
1111

1212
/**
@@ -26,7 +26,7 @@ interface MyPostRepository {
2626
/**
2727
* Fetch all post data from remote server and push to the local database.
2828
*/
29-
suspend fun fetchAllPost(): AppRequest<List<Post>?>
29+
suspend fun fetchAllPost(): AppResult<List<Post>?>
3030
}
3131

3232
/**
@@ -48,9 +48,9 @@ class MyPostRepositoryImpl @Inject constructor(
4848
remotePostDataSource.addPost(post)
4949
}
5050

51-
override suspend fun fetchAllPost(): AppRequest<List<Post>?> {
51+
override suspend fun fetchAllPost(): AppResult<List<Post>?> {
5252
val remoteData = remotePostDataSource.fetchAllPost()
53-
if (remoteData is AppRequest.Success) {
53+
if (remoteData is AppResult.Success) {
5454
val dataList = remoteData.data!!.map {
5555
Post(
5656
id = it.id,
@@ -63,9 +63,9 @@ class MyPostRepositoryImpl @Inject constructor(
6363
localPostDataSource.addAll(
6464
dataList
6565
)
66-
return AppRequest.Success(dataList)
66+
return AppResult.Success(dataList)
6767
} else {
68-
return AppRequest.Error((remoteData as AppRequest.Error).exception)
68+
return AppResult.Error((remoteData as AppResult.Error).exception)
6969
}
7070
}
7171
}
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package ytemplate.android.data.datasource
22

3+
34
import io.ktor.client.*
45
import io.ktor.client.call.*
56
import io.ktor.client.request.*
67
import ytemplate.android.data.model.PostDTO
78
import ytemplate.android.data.model.PostRequest
8-
import ytemplate.android.infra.AppRequest
9+
import ytemplate.android.infra.AppResult
10+
import ytemplate.android.infra.Routes
911
import javax.inject.Inject
1012

1113
/**
@@ -15,29 +17,29 @@ interface RemotePostDataSource {
1517
/**
1618
* Fetch all post, will fetch all data from remote server
1719
*/
18-
suspend fun fetchAllPost(): AppRequest<List<PostDTO>?>
20+
suspend fun fetchAllPost(): AppResult<List<PostDTO>?>
1921

2022
/**
2123
* Add post will add post data to remote server.
2224
*/
23-
suspend fun addPost(postRequest: PostRequest): AppRequest<Boolean>
25+
suspend fun addPost(postRequest: PostRequest): AppResult<Boolean>
2426
}
2527

2628
class RemotePostDataSourceImpl @Inject constructor(private val httpClient: HttpClient) :
2729
RemotePostDataSource {
28-
override suspend fun fetchAllPost(): AppRequest<List<PostDTO>?> {
30+
override suspend fun fetchAllPost(): AppResult<List<PostDTO>?> {
2931
val data = try {
3032
val data: List<PostDTO> = httpClient.get {
31-
url("https://jsonplaceholder.typicode.com/posts")
33+
url(Routes.GET_POST)
3234
}.body()
33-
AppRequest.Success(data)
35+
AppResult.Success(data)
3436
} catch (exc: Exception) {
35-
AppRequest.Error(exc)
37+
AppResult.Error(exc)
3638
}
3739
return data
3840
}
3941

40-
override suspend fun addPost(postRequest: PostRequest): AppRequest<Boolean> {
41-
return AppRequest.Loading
42+
override suspend fun addPost(postRequest: PostRequest): AppResult<Boolean> {
43+
return AppResult.Loading
4244
}
4345
}

app/src/main/java/ytemplate/android/infra/ApiRequest.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,33 @@ import kotlinx.coroutines.flow.onStart
88
/**
99
* AppRequest is generic interface for maintaining every request status.
1010
*/
11-
sealed interface AppRequest<out T> {
11+
sealed interface AppResult<out T> {
1212
/**
1313
* For the success request
1414
* @param data: Original data
1515
*/
16-
data class Success<T>(val data: T) : AppRequest<T>
16+
data class Success<T>(val data: T) : AppResult<T>
1717

1818
/**
1919
* For the failure request
2020
* @param exception: Exception
2121
*/
22-
data class Error(val exception: Throwable? = null) : AppRequest<Nothing>
22+
data class Error(val exception: Throwable? = null) : AppResult<Nothing>
2323

2424
/**
2525
* For maintaining the loading state,
2626
*/
27-
object Loading : AppRequest<Nothing>
27+
object Loading : AppResult<Nothing>
2828
}
2929

3030
/**
3131
* Extension for converting to flow to result
3232
*/
33-
fun <T> Flow<T>.asResult(): Flow<AppRequest<T>> {
33+
fun <T> Flow<T>.asResult(): Flow<AppResult<T>> {
3434
return this
35-
.map<T, AppRequest<T>> {
36-
AppRequest.Success(it)
35+
.map<T, AppResult<T>> {
36+
AppResult.Success(it)
3737
}
38-
.onStart { emit(AppRequest.Loading) }
39-
.catch { emit(AppRequest.Error(it)) }
38+
.onStart { emit(AppResult.Loading) }
39+
.catch { emit(AppResult.Error(it)) }
4040
}

app/src/main/java/ytemplate/android/infra/Routes.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ package ytemplate.android.infra
44
* Routes class, for maintaining the all routes
55
*/
66
object Routes {
7-
private const val BASE_URL = ""
7+
const val GET_POST = "https://jsonplaceholder.typicode.com/posts"
88
}

app/src/main/java/ytemplate/android/remote/RemoteModule.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ import javax.inject.Singleton
1515
/**
1616
* RemoteModule, for maintaining the all network related DI operations.
1717
*/
18+
19+
private const val CONNECTION_TIMEOUT = 60L
20+
private const val CONNECTION_READ_TIME_OUT = 60L
21+
private const val CONNECTION_WRITE_TIME_OUT = 60L
1822
@Module
1923
@InstallIn(SingletonComponent::class)
2024
class RemoteModule {
2125

26+
2227
/**
2328
* Providing the HttpClient instance for the remote communication.
2429
*/
@@ -36,9 +41,9 @@ class RemoteModule {
3641

3742
config {
3843
followRedirects(true)
39-
connectTimeout(60L, TimeUnit.SECONDS)
40-
readTimeout(60L, TimeUnit.SECONDS)
41-
writeTimeout(60L, TimeUnit.SECONDS)
44+
connectTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS)
45+
readTimeout(CONNECTION_READ_TIME_OUT, TimeUnit.SECONDS)
46+
writeTimeout(CONNECTION_WRITE_TIME_OUT, TimeUnit.SECONDS)
4247
}
4348
//addInterceptor()
4449
}

app/src/test/java/ytemplate/android/data/MyPostRepositoryImplTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import org.junit.Before
1818
import org.junit.Test
1919
import ytemplate.android.data.datasource.LocalPostDataSourceImpl
2020
import ytemplate.android.data.datasource.RemotePostDataSourceImpl
21-
import ytemplate.android.infra.AppRequest
21+
import ytemplate.android.infra.AppResult
2222

2323
class MyPostRepositoryImplTest {
2424
private val remotePostDataMock = RemotePostDataMock()
@@ -42,7 +42,7 @@ class MyPostRepositoryImplTest {
4242
localPostDataSource = LocalPostDataSourceImpl(fakePostDao)
4343
)
4444
val data = repository.fetchAllPost()
45-
assert(data is AppRequest.Success)
45+
assert(data is AppResult.Success)
4646
}
4747

4848
@OptIn(ExperimentalCoroutinesApi::class)

0 commit comments

Comments
 (0)