Skip to content

Commit

Permalink
#16 Use int as tmdb and trakt ids instead of Long and String
Browse files Browse the repository at this point in the history
  • Loading branch information
moallemi committed Jun 16, 2024
1 parent 626c4ff commit 3e6507b
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ enum class TmdbType {

interface TraktSearchRemoteSource {

suspend fun getByTmdbId(id: String, type: TmdbType? = null): Result<Long, GeneralError>
suspend fun getByTmdbId(id: Int, type: TmdbType? = null): Result<Int, GeneralError>
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import javax.inject.Inject
class TraktSearchRemoteSourceImpl @Inject constructor(
private val traktIDLookupService: TraktSearchService,
) : TraktSearchRemoteSource {
override suspend fun getByTmdbId(id: String, type: TmdbType?): Result<Long, GeneralError> {

override suspend fun getByTmdbId(id: Int, type: TmdbType?): Result<Int, GeneralError> {
return when (val result = traktIDLookupService.movieIDLookup(idType = "tmdb", id = id)) {
is NetworkResponse.ApiError -> {
val errorResponse = result.body
Expand All @@ -18,7 +19,7 @@ class TraktSearchRemoteSourceImpl @Inject constructor(
is NetworkResponse.NetworkError -> Result.Failure(GeneralError.NetworkError)
is NetworkResponse.Success -> {
val body = result.body ?: emptyList()
val movieItemId = body.find { it.movie.ids.tmdb == id.toLong() }?.movie?.ids?.trakt ?: -1
val movieItemId = body.find { it.movie.ids.tmdb == id }?.movie?.ids?.trakt ?: -1
return Result.Success(movieItemId)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface TraktSyncRemoteSource {

suspend fun getAllHistories(): Result<Nothing, GeneralError>

suspend fun getHistoryById(id: String): Result<Boolean, GeneralError>
suspend fun getHistoryById(id: Int): Result<Boolean, GeneralError>

suspend fun addToHistory(id: String): Result<Unit, GeneralError>
suspend fun addToHistory(id: Int): Result<Unit, GeneralError>
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TraktSyncRemoteSourceImpl
}
}

override suspend fun getHistoryById(id: String): Result<Boolean, GeneralError> {
override suspend fun getHistoryById(id: Int): Result<Boolean, GeneralError> {
// TODO: move check token in a function
val tokens =
traktAuthLocalSource.tokens.firstOrNull() ?: return Result.Failure(GeneralError.ApiError("Unauthorized", 401))
Expand All @@ -45,13 +45,13 @@ class TraktSyncRemoteSourceImpl
is NetworkResponse.NetworkError -> Result.Failure(GeneralError.NetworkError)
is NetworkResponse.UnknownError -> Result.Failure(GeneralError.UnknownError(result.error))
is NetworkResponse.Success -> {
val watched = result.body?.any { it.movie.ids.trakt == id.toLong() } ?: false
val watched = result.body?.any { it.movie.ids.trakt == id } ?: false
Result.Success(watched)
}
}
}

override suspend fun addToHistory(id: String): Result<Unit, GeneralError> {
override suspend fun addToHistory(id: Int): Result<Unit, GeneralError> {
val tokens =
traktAuthLocalSource.tokens.firstOrNull() ?: return Result.Failure(GeneralError.ApiError("Unauthorized", 401))
val result = traktSyncService.addMovieToHistory(
Expand All @@ -60,7 +60,7 @@ class TraktSyncRemoteSourceImpl
movies = listOf(
MovieHistory(
ids = HistoryIDS(
trakt = id.toLong(),
trakt = id,
),
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ data class MovieHistory(

@Serializable
data class HistoryIDS(
val trakt: Long? = null,
val tvdb: Long? = null,
val trakt: Int? = null,
val tvdb: Int? = null,
val imdb: String? = null,
val tmdb: Long? = null,
val tmdb: Int? = null,
val slug: String? = null,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ data class Movie(

@Serializable
data class IDS(
val trakt: Long,
val trakt: Int,
val slug: String,
val imdb: String,
val tmdb: Long,
val tmdb: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ interface TraktSearchService {
@GET("search/{id_type}/{id}?type=movie")
suspend fun movieIDLookup(
@Path("id_type") idType: String,
@Path("id") id: String,
@Path("id") id: Int,
): NetworkResponse<List<TraktMovieIDLookupResponse>, TraktErrorResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.POST
import retrofit2.http.Path
import retrofit2.http.Query

interface TraktSyncService {

Expand All @@ -18,8 +19,9 @@ interface TraktSyncService {
@GET("sync/history/{type}/{id}")
suspend fun getHistoryById(
@Path("type") type: String,
@Path("id") id: String,
@Path("id") id: Int,
@Header("Authorization") accessToken: String,
@Query("limit") limit: Int = 240,
): NetworkResponse<List<HistoryMovie>, TraktErrorResponse>

@POST("sync/history")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ internal class TmdbMovieRepositoryImpl @Inject constructor(
is Result.Failure -> result
is Result.Success -> {
movieDao.storeMovie(result.data.toEntity())
when (val traktIdResult = traktMovieSearchRemoteSource.getByTmdbId(result.data.ids.tmdbId.toString())) {
when (val traktIdResult = traktMovieSearchRemoteSource.getByTmdbId(result.data.ids.tmdbId!!)) {
is Result.Failure -> result
is Result.Success -> {
val traktId = traktIdResult.data.toInt()
when (val watched = traktSyncRemoteSource.getHistoryById(traktId.toString())) {
val traktId = traktIdResult.data
when (val watched = traktSyncRemoteSource.getHistoryById(traktId)) {
is Result.Failure -> result.run {
copy(
data = data.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import io.filmtime.data.model.Result

interface TraktHistoryRepository {

suspend fun addToHistory(id: String): Result<Unit, GeneralError>
suspend fun addToHistory(traktId: Int): Result<Unit, GeneralError>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import javax.inject.Inject
class TraktHistoryRepositoryImpl @Inject constructor(
private val traktSyncRemoteSource: TraktSyncRemoteSource,
) : TraktHistoryRepository {
override suspend fun addToHistory(id: String): Result<Unit, GeneralError> {
return traktSyncRemoteSource.addToHistory(id)

override suspend fun addToHistory(traktId: Int): Result<Unit, GeneralError> {
return traktSyncRemoteSource.addToHistory(traktId)
}
}

0 comments on commit 3e6507b

Please sign in to comment.