Skip to content

Commit

Permalink
Update network resource
Browse files Browse the repository at this point in the history
  • Loading branch information
muhxdan committed Dec 25, 2023
1 parent dedae3b commit 14c9aa6
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,7 +9,7 @@ import kotlinx.coroutines.flow.map

inline fun <ResultType, RequestType> moovNetworkBoundResource(
crossinline query: () -> Flow<ResultType>,
crossinline fetch: suspend () -> Flow<MoovApiResponse<RequestType>>,
crossinline fetch: suspend () -> Flow<MovieApiResponse<RequestType>>,
crossinline saveFetchResult: suspend (RequestType) -> Unit,
crossinline shouldFetch: (ResultType?) -> Boolean = { true }
) = flow {
Expand All @@ -18,13 +18,13 @@ inline fun <ResultType, RequestType> 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) })
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.salt.apps.moov.data.source.remote.network

sealed class MovieApiResponse<out R> {
data class Success<out T>(val data: T) : MovieApiResponse<T>()
data class Error(val errorMessage: String) : MovieApiResponse<Nothing>()
object Empty : MovieApiResponse<Nothing>()
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<MoovItem>
val results: List<MovieItem>
)

data class MoovItem(
data class MovieItem(
@field:SerializedName("id")
val id: Int,

Expand Down

0 comments on commit 14c9aa6

Please sign in to comment.