-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#24 Login with trakt and sync history in movies
- Loading branch information
1 parent
8b98fee
commit 44dffa6
Showing
95 changed files
with
1,810 additions
and
17 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
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,54 @@ | ||
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed | ||
plugins { | ||
alias(libs.plugins.com.android.library) | ||
alias(libs.plugins.org.jetbrains.kotlin.android) | ||
|
||
kotlin("kapt") | ||
alias(libs.plugins.hilt.android) | ||
} | ||
|
||
android { | ||
namespace = "io.filmtime.data.api.trakt" | ||
compileSdk = 34 | ||
|
||
defaultConfig { | ||
minSdk = 27 | ||
|
||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
consumerProguardFiles("consumer-rules.pro") | ||
} | ||
|
||
buildTypes { | ||
release { | ||
isMinifyEnabled = false | ||
proguardFiles( | ||
getDefaultProguardFile("proguard-android-optimize.txt"), | ||
"proguard-rules.pro", | ||
) | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
} | ||
kotlinOptions { | ||
jvmTarget = "17" | ||
} | ||
} | ||
|
||
dependencies { | ||
|
||
implementation(project(":data:model")) | ||
implementation(project(":data:network")) | ||
implementation(project(":data:storage:trakt")) | ||
|
||
implementation(libs.hilt.android) | ||
kapt(libs.dagger.hilt.android.compiler) | ||
|
||
implementation(libs.core.ktx) | ||
implementation(libs.appcompat) | ||
implementation(libs.material) | ||
testImplementation(libs.junit) | ||
androidTestImplementation(libs.androidx.test.ext.junit) | ||
androidTestImplementation(libs.espresso.core) | ||
} |
Empty file.
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 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
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,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
</manifest> |
10 changes: 10 additions & 0 deletions
10
data/api/trakt/src/main/java/io/filmtime/data/api/trakt/TraktAccessTokenExt.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,10 @@ | ||
package io.filmtime.data.api.trakt | ||
|
||
import io.filmtime.data.model.TraktTokens | ||
import io.filmtime.data.network.trakt.TraktAccessTokenResponse | ||
|
||
fun TraktAccessTokenResponse.toAccessToken() = | ||
TraktTokens( | ||
accessToken = accessToken, | ||
refreshToken = refreshToken, | ||
) |
10 changes: 10 additions & 0 deletions
10
data/api/trakt/src/main/java/io/filmtime/data/api/trakt/TraktAuthRemoteSource.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,10 @@ | ||
package io.filmtime.data.api.trakt | ||
|
||
import io.filmtime.data.model.GeneralError | ||
import io.filmtime.data.model.Result | ||
import io.filmtime.data.model.TraktTokens | ||
|
||
interface TraktAuthRemoteSource { | ||
|
||
suspend fun getAccessToken(code: String): Result<TraktTokens, GeneralError> | ||
} |
40 changes: 40 additions & 0 deletions
40
data/api/trakt/src/main/java/io/filmtime/data/api/trakt/TraktAuthRemoteSourceImpl.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,40 @@ | ||
package io.filmtime.data.api.trakt | ||
|
||
import io.filmtime.data.model.GeneralError | ||
import io.filmtime.data.model.Result | ||
import io.filmtime.data.model.TraktTokens | ||
import io.filmtime.data.network.BuildConfig | ||
import io.filmtime.data.network.adapter.NetworkResponse | ||
import io.filmtime.data.network.trakt.TraktAuthService | ||
import io.filmtime.data.network.trakt.TraktGetTokenRequest | ||
import javax.inject.Inject | ||
|
||
class TraktAuthRemoteSourceImpl @Inject constructor( | ||
private val traktAuthService: TraktAuthService, | ||
) : TraktAuthRemoteSource { | ||
|
||
override suspend fun getAccessToken(code: String): Result<TraktTokens, GeneralError> { | ||
val result = traktAuthService.getAccessToken( | ||
body = TraktGetTokenRequest( | ||
code = code, | ||
clientID = BuildConfig.TRAKT_CLIENT_ID, | ||
clientSecret = BuildConfig.TRAKT_CLIENT_SECRET, | ||
grantType = "authorization_code", | ||
redirectURI = "filmtime://", | ||
), | ||
) | ||
return when (result) { | ||
is NetworkResponse.ApiError -> Result.Failure(GeneralError.ApiError(result.body.error, result.code)) | ||
is NetworkResponse.NetworkError -> Result.Failure(GeneralError.NetworkError) | ||
is NetworkResponse.Success -> { | ||
val response = result.body | ||
if (response == null) { | ||
Result.Failure(GeneralError.UnknownError(Throwable("Access token response is null"))) | ||
} else { | ||
Result.Success(response.toAccessToken()) | ||
} | ||
} | ||
is NetworkResponse.UnknownError -> Result.Failure(GeneralError.UnknownError(result.error)) | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
data/api/trakt/src/main/java/io/filmtime/data/api/trakt/TraktSearchRemoteSource.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,16 @@ | ||
package io.filmtime.data.api.trakt | ||
|
||
import io.filmtime.data.model.GeneralError | ||
import io.filmtime.data.model.Result | ||
|
||
enum class TmdbType { | ||
MOVIE, | ||
SHOW, | ||
// EPISODE, | ||
// PERSON, | ||
} | ||
|
||
interface TraktSearchRemoteSource { | ||
|
||
suspend fun getByTmdbId(id: String, type: TmdbType? = null): Result<Long, GeneralError> | ||
} |
25 changes: 25 additions & 0 deletions
25
data/api/trakt/src/main/java/io/filmtime/data/api/trakt/TraktSearchRemoteSourceImpl.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,25 @@ | ||
package io.filmtime.data.api.trakt | ||
|
||
import io.filmtime.data.model.GeneralError | ||
import io.filmtime.data.model.Result | ||
import io.filmtime.data.network.adapter.NetworkResponse | ||
import io.filmtime.data.network.trakt.TraktSearchService | ||
import javax.inject.Inject | ||
|
||
class TraktSearchRemoteSourceImpl @Inject constructor( | ||
private val traktIDLookupService: TraktSearchService, | ||
) : TraktSearchRemoteSource { | ||
override suspend fun getByTmdbId(id: String, type: TmdbType?): Result<Long, GeneralError> { | ||
return when (val result = traktIDLookupService.movieIDLookup(idType = "tmdb", id = id)) { | ||
is NetworkResponse.ApiError -> TODO() // Will be handled in #31 | ||
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 | ||
return Result.Success(movieItemId) | ||
} | ||
|
||
is NetworkResponse.UnknownError -> TODO() // Will be handled in #31 | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
data/api/trakt/src/main/java/io/filmtime/data/api/trakt/TraktSourceModule.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,26 @@ | ||
package io.filmtime.data.api.trakt | ||
|
||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
|
||
@InstallIn(SingletonComponent::class) | ||
@Module | ||
abstract class TraktSourceModule { | ||
|
||
@Binds | ||
abstract fun bindsTraktAuthRemoteSource( | ||
sourceImpl: TraktAuthRemoteSourceImpl, | ||
): TraktAuthRemoteSource | ||
|
||
@Binds | ||
abstract fun bindsTraktSearchRemoteSource( | ||
sourceImpl: TraktSearchRemoteSourceImpl, | ||
): TraktSearchRemoteSource | ||
|
||
@Binds | ||
abstract fun bindsTraktSyncRemoteSource( | ||
sourceImpl: TraktSyncRemoteSourceImpl, | ||
): TraktSyncRemoteSource | ||
} |
13 changes: 13 additions & 0 deletions
13
data/api/trakt/src/main/java/io/filmtime/data/api/trakt/TraktSyncRemoteSource.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,13 @@ | ||
package io.filmtime.data.api.trakt | ||
|
||
import io.filmtime.data.model.GeneralError | ||
import io.filmtime.data.model.Result | ||
|
||
interface TraktSyncRemoteSource { | ||
|
||
suspend fun getAllHistories(): Result<Nothing, GeneralError> | ||
|
||
suspend fun getHistoryById(id: String): Result<Boolean, GeneralError> | ||
|
||
suspend fun addToHistory(id: String): Result<Unit, GeneralError> | ||
} |
76 changes: 76 additions & 0 deletions
76
data/api/trakt/src/main/java/io/filmtime/data/api/trakt/TraktSyncRemoteSourceImpl.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,76 @@ | ||
package io.filmtime.data.api.trakt | ||
|
||
import io.filmtime.data.model.GeneralError | ||
import io.filmtime.data.model.Result | ||
import io.filmtime.data.network.adapter.NetworkResponse | ||
import io.filmtime.data.network.trakt.AddHistoryRequest | ||
import io.filmtime.data.network.trakt.HistoryIDS | ||
import io.filmtime.data.network.trakt.MovieHistory | ||
import io.filmtime.data.network.trakt.TraktSyncService | ||
import io.filmtime.data.storage.trakt.TraktAuthLocalSource | ||
import kotlinx.coroutines.flow.firstOrNull | ||
import javax.inject.Inject | ||
|
||
class TraktSyncRemoteSourceImpl | ||
@Inject constructor( | ||
private val traktSyncService: TraktSyncService, | ||
private val traktAuthLocalSource: TraktAuthLocalSource, | ||
) : TraktSyncRemoteSource { | ||
override suspend fun getAllHistories(): Result<Nothing, GeneralError> { | ||
// TODO: move check token in a function | ||
traktAuthLocalSource.tokens.firstOrNull() ?: return Result.Failure(GeneralError.ApiError("Unauthorized", 401)) | ||
val result = traktSyncService.getWatchedHistory( | ||
type = "movies", | ||
accessToken = "", | ||
) | ||
return when (result) { | ||
is NetworkResponse.ApiError -> TODO() | ||
is NetworkResponse.NetworkError -> TODO() | ||
is NetworkResponse.Success -> TODO() | ||
is NetworkResponse.UnknownError -> TODO() | ||
} | ||
} | ||
|
||
override suspend fun getHistoryById(id: String): Result<Boolean, GeneralError> { | ||
// TODO: move check token in a function | ||
val tokens = | ||
traktAuthLocalSource.tokens.firstOrNull() ?: return Result.Failure(GeneralError.ApiError("Unauthorized", 401)) | ||
val result = traktSyncService.getHistoryById( | ||
type = "movies", | ||
id = id, | ||
accessToken = "Bearer " + tokens.accessToken, | ||
) | ||
return when (result) { | ||
is NetworkResponse.ApiError -> Result.Failure(GeneralError.ApiError(result.body.error, result.code)) | ||
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 | ||
Result.Success(watched) | ||
} | ||
} | ||
} | ||
|
||
override suspend fun addToHistory(id: String): Result<Unit, GeneralError> { | ||
val tokens = | ||
traktAuthLocalSource.tokens.firstOrNull() ?: return Result.Failure(GeneralError.ApiError("Unauthorized", 401)) | ||
val result = traktSyncService.addMovieToHistory( | ||
accessToken = "Bearer " + tokens.accessToken, | ||
body = AddHistoryRequest( | ||
movies = listOf( | ||
MovieHistory( | ||
ids = HistoryIDS( | ||
trakt = id.toLong(), | ||
), | ||
), | ||
), | ||
), | ||
) | ||
return when (result) { | ||
is NetworkResponse.ApiError -> Result.Failure(GeneralError.ApiError(result.body.error, result.code)) | ||
is NetworkResponse.NetworkError -> Result.Failure(GeneralError.NetworkError) | ||
is NetworkResponse.UnknownError -> Result.Failure(GeneralError.UnknownError(result.error)) | ||
is NetworkResponse.Success -> Result.Success(Unit) | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
data/model/src/main/java/io/filmtime/data/model/TraktTokens.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,6 @@ | ||
package io.filmtime.data.model | ||
|
||
data class TraktTokens( | ||
val accessToken: String, | ||
val refreshToken: String, | ||
) |
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
Oops, something went wrong.