From 14c9aa6a4e9b7ab714d0f9bf171f6d91243f2a00 Mon Sep 17 00:00:00 2001 From: Muhammad Danu Firdaus Date: Mon, 25 Dec 2023 21:38:48 +0700 Subject: [PATCH] Update network resource --- .../moov/data/MoovNetworkBoundResource.kt | 10 ++++---- .../data/source/remote/RemoteDataSource.kt | 24 +++++++++---------- .../source/remote/network/MoovApiResponse.kt | 7 ------ .../source/remote/network/MovieApiResponse.kt | 7 ++++++ .../{MoovApiService.kt => MovieApiService.kt} | 8 +++---- .../{MoovResponse.kt => MovieResponse.kt} | 6 ++--- 6 files changed, 31 insertions(+), 31 deletions(-) delete mode 100644 app/src/main/java/com/salt/apps/moov/data/source/remote/network/MoovApiResponse.kt create mode 100644 app/src/main/java/com/salt/apps/moov/data/source/remote/network/MovieApiResponse.kt rename app/src/main/java/com/salt/apps/moov/data/source/remote/network/{MoovApiService.kt => MovieApiService.kt} (75%) rename app/src/main/java/com/salt/apps/moov/data/source/remote/response/{MoovResponse.kt => MovieResponse.kt} (92%) diff --git a/app/src/main/java/com/salt/apps/moov/data/MoovNetworkBoundResource.kt b/app/src/main/java/com/salt/apps/moov/data/MoovNetworkBoundResource.kt index 411f83d..b036fa2 100644 --- a/app/src/main/java/com/salt/apps/moov/data/MoovNetworkBoundResource.kt +++ b/app/src/main/java/com/salt/apps/moov/data/MoovNetworkBoundResource.kt @@ -1,6 +1,6 @@ package com.salt.apps.moov.data -import com.salt.apps.moov.data.source.remote.network.MoovApiResponse +import com.salt.apps.moov.data.source.remote.network.MovieApiResponse import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.emitAll import kotlinx.coroutines.flow.first @@ -9,7 +9,7 @@ import kotlinx.coroutines.flow.map inline fun moovNetworkBoundResource( crossinline query: () -> Flow, - crossinline fetch: suspend () -> Flow>, + crossinline fetch: suspend () -> Flow>, crossinline saveFetchResult: suspend (RequestType) -> Unit, crossinline shouldFetch: (ResultType?) -> Boolean = { true } ) = flow { @@ -18,13 +18,13 @@ inline fun moovNetworkBoundResource( if (shouldFetch(data)) { emit(Resource.Loading(data)) when (val response = fetch().first()) { - is MoovApiResponse.Success -> { + is MovieApiResponse.Success -> { saveFetchResult(response.data) emitAll(query().map { Resource.Success(it) }) } - is MoovApiResponse.Empty -> emitAll(query().map { Resource.Success(it) }) - is MoovApiResponse.Error -> emit(Resource.Error(response.errorMessage)) + is MovieApiResponse.Empty -> emitAll(query().map { Resource.Success(it) }) + is MovieApiResponse.Error -> emit(Resource.Error(response.errorMessage)) } } else { emitAll(query().map { Resource.Success(it) }) diff --git a/app/src/main/java/com/salt/apps/moov/data/source/remote/RemoteDataSource.kt b/app/src/main/java/com/salt/apps/moov/data/source/remote/RemoteDataSource.kt index 7bd9f83..0001c8b 100644 --- a/app/src/main/java/com/salt/apps/moov/data/source/remote/RemoteDataSource.kt +++ b/app/src/main/java/com/salt/apps/moov/data/source/remote/RemoteDataSource.kt @@ -1,7 +1,7 @@ package com.salt.apps.moov.data.source.remote -import com.salt.apps.moov.data.source.remote.network.MoovApiResponse -import com.salt.apps.moov.data.source.remote.network.MoovApiService +import com.salt.apps.moov.data.source.remote.network.MovieApiResponse +import com.salt.apps.moov.data.source.remote.network.MovieApiService import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flowOn @@ -10,27 +10,27 @@ import javax.inject.Singleton @Singleton class RemoteDataSource @Inject constructor( - private val moovApiService: MoovApiService + private val movieApiService: MovieApiService ) { suspend fun getPopularMovies() = flow { try { - val response = moovApiService.getPopularMovies() + val response = movieApiService.getPopularMovies() val data = response.results - if (data.isNotEmpty()) emit(MoovApiResponse.Success(data)) - else emit(MoovApiResponse.Empty) + if (data.isNotEmpty()) emit(MovieApiResponse.Success(data)) + else emit(MovieApiResponse.Empty) } catch (e: Exception) { - emit(MoovApiResponse.Error(e.message.toString())) + emit(MovieApiResponse.Error(e.message.toString())) } }.flowOn(Dispatchers.IO) - suspend fun getUpComingMovies() = flow { + suspend fun getUpcomingMovies() = flow { try { - val response = moovApiService.getUpcomingMovies() + val response = movieApiService.getUpcomingMovies() val data = response.results - if (data.isNotEmpty()) emit(MoovApiResponse.Success(data)) - else (emit(MoovApiResponse.Empty)) + if (data.isNotEmpty()) emit(MovieApiResponse.Success(data)) + else (emit(MovieApiResponse.Empty)) } catch (e: Exception) { - emit(MoovApiResponse.Error(e.message.toString())) + emit(MovieApiResponse.Error(e.message.toString())) } }.flowOn(Dispatchers.IO) } \ No newline at end of file diff --git a/app/src/main/java/com/salt/apps/moov/data/source/remote/network/MoovApiResponse.kt b/app/src/main/java/com/salt/apps/moov/data/source/remote/network/MoovApiResponse.kt deleted file mode 100644 index cb7d49c..0000000 --- a/app/src/main/java/com/salt/apps/moov/data/source/remote/network/MoovApiResponse.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.salt.apps.moov.data.source.remote.network - -sealed class MoovApiResponse { - data class Success(val data: T) : MoovApiResponse() - data class Error(val errorMessage: String) : MoovApiResponse() - object Empty : MoovApiResponse() -} \ No newline at end of file diff --git a/app/src/main/java/com/salt/apps/moov/data/source/remote/network/MovieApiResponse.kt b/app/src/main/java/com/salt/apps/moov/data/source/remote/network/MovieApiResponse.kt new file mode 100644 index 0000000..0ce32bb --- /dev/null +++ b/app/src/main/java/com/salt/apps/moov/data/source/remote/network/MovieApiResponse.kt @@ -0,0 +1,7 @@ +package com.salt.apps.moov.data.source.remote.network + +sealed class MovieApiResponse { + data class Success(val data: T) : MovieApiResponse() + data class Error(val errorMessage: String) : MovieApiResponse() + object Empty : MovieApiResponse() +} \ No newline at end of file diff --git a/app/src/main/java/com/salt/apps/moov/data/source/remote/network/MoovApiService.kt b/app/src/main/java/com/salt/apps/moov/data/source/remote/network/MovieApiService.kt similarity index 75% rename from app/src/main/java/com/salt/apps/moov/data/source/remote/network/MoovApiService.kt rename to app/src/main/java/com/salt/apps/moov/data/source/remote/network/MovieApiService.kt index 42b51ce..f3e287f 100644 --- a/app/src/main/java/com/salt/apps/moov/data/source/remote/network/MoovApiService.kt +++ b/app/src/main/java/com/salt/apps/moov/data/source/remote/network/MovieApiService.kt @@ -1,19 +1,19 @@ package com.salt.apps.moov.data.source.remote.network -import com.salt.apps.moov.data.source.remote.response.MoovResponse +import com.salt.apps.moov.data.source.remote.response.MovieResponse import retrofit2.http.GET import retrofit2.http.Query -interface MoovApiService { +interface MovieApiService { @GET("movie/popular") suspend fun getPopularMovies( @Query("language") language: String = "en-US", @Query("page") page: Int = 1 - ): MoovResponse + ): MovieResponse @GET("movie/upcoming") suspend fun getUpcomingMovies( @Query("language") language: String = "en-US", @Query("page") page: Int = 1 - ): MoovResponse + ): MovieResponse } \ No newline at end of file diff --git a/app/src/main/java/com/salt/apps/moov/data/source/remote/response/MoovResponse.kt b/app/src/main/java/com/salt/apps/moov/data/source/remote/response/MovieResponse.kt similarity index 92% rename from app/src/main/java/com/salt/apps/moov/data/source/remote/response/MoovResponse.kt rename to app/src/main/java/com/salt/apps/moov/data/source/remote/response/MovieResponse.kt index 2f0e2df..bd07f08 100644 --- a/app/src/main/java/com/salt/apps/moov/data/source/remote/response/MoovResponse.kt +++ b/app/src/main/java/com/salt/apps/moov/data/source/remote/response/MovieResponse.kt @@ -2,15 +2,15 @@ package com.salt.apps.moov.data.source.remote.response import com.google.gson.annotations.SerializedName -data class MoovResponse( +data class MovieResponse( @field:SerializedName("page") val page: Int, @field:SerializedName("results") - val results: List + val results: List ) -data class MoovItem( +data class MovieItem( @field:SerializedName("id") val id: Int,