-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from sheikh-20/develop
Develop
- Loading branch information
Showing
18 changed files
with
612 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
app/src/main/java/com/application/moviesapp/data/api/response/MovieReviewDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.application.moviesapp.data.api.response | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class MovieReviewDto( | ||
|
||
@SerialName("id") | ||
val id: Int?, | ||
|
||
@SerialName("page") | ||
val page: Int?, | ||
|
||
@SerialName("results") | ||
val results: List<Result?>?, | ||
|
||
@SerialName("total_pages") | ||
val totalPages: Int?, | ||
|
||
@SerialName("total_results") | ||
val totalResults: Int? | ||
) { | ||
|
||
@Serializable | ||
data class Result( | ||
|
||
@SerialName("author") | ||
val author: String?, | ||
|
||
@SerialName("author_details") | ||
val authorDetails: AuthorDetails?, | ||
|
||
@SerialName("content") | ||
val content: String?, | ||
|
||
@SerialName("created_at") | ||
val createdAt: String?, | ||
|
||
@SerialName("id") | ||
val id: String?, | ||
|
||
@SerialName("updated_at") | ||
val updatedAt: String?, | ||
|
||
@SerialName("url") | ||
val url: String? | ||
) { | ||
|
||
@Serializable | ||
data class AuthorDetails( | ||
|
||
@SerialName("avatar_path") | ||
val avatarPath: String?, | ||
|
||
@SerialName("name") | ||
val name: String?, | ||
|
||
@SerialName("rating") | ||
val rating: Double?, | ||
|
||
@SerialName("username") | ||
val username: String? | ||
) | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
app/src/main/java/com/application/moviesapp/data/api/response/TvSeriesReviewDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.application.moviesapp.data.api.response | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class TvSeriesReviewDto( | ||
|
||
@SerialName("id") | ||
val id: Int?, | ||
|
||
@SerialName("page") | ||
val page: Int?, | ||
|
||
@SerialName("results") | ||
val results: List<Result?>?, | ||
|
||
@SerialName("total_pages") | ||
val totalPages: Int?, | ||
|
||
@SerialName("total_results") | ||
val totalResults: Int? | ||
) { | ||
|
||
@Serializable | ||
data class Result( | ||
|
||
@SerialName("author") | ||
val author: String?, | ||
|
||
@SerialName("author_details") | ||
val authorDetails: AuthorDetails?, | ||
|
||
@SerialName("content") | ||
val content: String?, | ||
|
||
@SerialName("created_at") | ||
val createdAt: String?, | ||
|
||
@SerialName("id") | ||
val id: String?, | ||
|
||
@SerialName("updated_at") | ||
val updatedAt: String?, | ||
|
||
@SerialName("url") | ||
val url: String? | ||
) { | ||
|
||
@Serializable | ||
data class AuthorDetails( | ||
|
||
@SerialName("avatar_path") | ||
val avatarPath: String?, | ||
|
||
@SerialName("name") | ||
val name: String?, | ||
|
||
@SerialName("rating") | ||
val rating: Double?, | ||
|
||
@SerialName("username") | ||
val username: String? | ||
) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/com/application/moviesapp/data/mappers/MovieReviewMappers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.application.moviesapp.data.mappers | ||
|
||
import com.application.moviesapp.data.api.response.MovieReviewDto | ||
import com.application.moviesapp.domain.model.MovieReview | ||
|
||
fun MovieReviewDto.Result.toDomain(): MovieReview { | ||
return MovieReview( | ||
author = this.author, | ||
content = this.content, | ||
createdAt = this.createdAt, | ||
id = this.id, | ||
updatedAt = this.updatedAt, | ||
url = this.url, | ||
authorDetails = MovieReview.AuthorDetails( | ||
avatarPath = this.authorDetails?.avatarPath, | ||
name = this.authorDetails?.name, | ||
username = this.authorDetails?.username, | ||
rating = this.authorDetails?.rating | ||
) | ||
) | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/application/moviesapp/data/mappers/TvSeriesReviewMappers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.application.moviesapp.data.mappers | ||
|
||
import com.application.moviesapp.data.api.response.TvSeriesReviewDto | ||
import com.application.moviesapp.domain.model.MovieReview | ||
import com.application.moviesapp.domain.model.TvSeriesReview | ||
|
||
fun TvSeriesReviewDto.Result.toDomain(): TvSeriesReview { | ||
return TvSeriesReview( | ||
author = this.author, | ||
content = this.content, | ||
createdAt = this.createdAt, | ||
id = this.id, | ||
updatedAt = this.updatedAt, | ||
url = this.url, | ||
authorDetails = TvSeriesReview.AuthorDetails( | ||
avatarPath = this.authorDetails?.avatarPath, | ||
name = this.authorDetails?.name, | ||
username = this.authorDetails?.username, | ||
rating = this.authorDetails?.rating | ||
) | ||
) | ||
} |
49 changes: 49 additions & 0 deletions
49
app/src/main/java/com/application/moviesapp/data/remote/MovieReviewPagingSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.application.moviesapp.data.remote | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.application.moviesapp.data.api.MoviesApi | ||
import com.application.moviesapp.data.api.response.MovieReviewDto | ||
import com.application.moviesapp.data.api.response.MovieSearchDto | ||
import timber.log.Timber | ||
|
||
class MovieReviewPagingSource(private val moviesApi: MoviesApi, private val movieId: Int): PagingSource<Int, MovieReviewDto.Result>() { | ||
|
||
private companion object { | ||
const val TAG = "MovieReviewPagingSource" | ||
} | ||
|
||
override fun getRefreshKey(state: PagingState<Int, MovieReviewDto.Result>): Int? { | ||
return state.anchorPosition?.let { anchorPosition -> | ||
state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1) | ||
?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1) | ||
} | ||
} | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, MovieReviewDto.Result> { | ||
|
||
Timber.tag(TAG).d("%s Called!", TAG) | ||
|
||
return try { | ||
val page = params.key ?: 1 | ||
|
||
val apiResult = moviesApi.getMovieReview(movieId = movieId, page = page) | ||
val movies = if (apiResult.isSuccessful) { | ||
apiResult.body() | ||
} else if (apiResult.code() == 400 || apiResult.code() == 401 || apiResult.code() == 403) { | ||
throw Throwable() | ||
} else { | ||
throw Throwable() | ||
} | ||
|
||
LoadResult.Page( | ||
data = movies?.results?.map { it ?: MovieReviewDto.Result(null, null, null, null, null, null, null)} ?: listOf(), | ||
prevKey = if (page == 1) null else page.minus(1), | ||
nextKey = if (movies?.results?.isEmpty() == true) null else page.plus(1), | ||
) | ||
} catch (throwable: Throwable) { | ||
Timber.tag(TAG).e(throwable) | ||
LoadResult.Error(throwable) | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
app/src/main/java/com/application/moviesapp/data/remote/TvSeriesPagingSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.application.moviesapp.data.remote | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.application.moviesapp.data.api.MoviesApi | ||
import com.application.moviesapp.data.api.response.MovieReviewDto | ||
import com.application.moviesapp.data.api.response.TvSeriesReviewDto | ||
import timber.log.Timber | ||
|
||
class TvSeriesReviewPagingSource(private val moviesApi: MoviesApi, private val seriesId: Int): PagingSource<Int, TvSeriesReviewDto.Result>() { | ||
|
||
private companion object { | ||
const val TAG = "TvSeriesReviewPagingSource" | ||
} | ||
|
||
override fun getRefreshKey(state: PagingState<Int, TvSeriesReviewDto.Result>): Int? { | ||
return state.anchorPosition?.let { anchorPosition -> | ||
state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1) | ||
?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1) | ||
} | ||
} | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, TvSeriesReviewDto.Result> { | ||
|
||
Timber.tag(TAG).d("%s Called!", TAG) | ||
|
||
return try { | ||
val page = params.key ?: 1 | ||
|
||
val apiResult = moviesApi.getTvSeriesReview(seriesId = seriesId, page = page) | ||
val movies = if (apiResult.isSuccessful) { | ||
apiResult.body() | ||
} else if (apiResult.code() == 400 || apiResult.code() == 401 || apiResult.code() == 403) { | ||
throw Throwable() | ||
} else { | ||
throw Throwable() | ||
} | ||
|
||
LoadResult.Page( | ||
data = movies?.results?.map { it ?: TvSeriesReviewDto.Result(null, null, null, null, null, null, null)} ?: listOf(), | ||
prevKey = if (page == 1) null else page.minus(1), | ||
nextKey = if (movies?.results?.isEmpty() == true) null else page.plus(1), | ||
) | ||
} catch (throwable: Throwable) { | ||
Timber.tag(TAG).e(throwable) | ||
LoadResult.Error(throwable) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.