Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Tidy up trakt-auth modules #1159

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/artifacts/traktauth_jvm.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 0 additions & 52 deletions api/trakt-auth/android/build.gradle.kts

This file was deleted.

2 changes: 1 addition & 1 deletion api/trakt/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies {
implementation(projects.core.base)

api(libs.trakt.api)
api(projects.api.traktAuth.api)
api(projects.data.traktauth)
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.okhttp)
implementation(libs.ktor.client.auth)
Expand Down
11 changes: 9 additions & 2 deletions api/trakt/src/main/java/app/tivi/trakt/TraktComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import app.moviebase.trakt.api.TraktSeasonsApi
import app.moviebase.trakt.api.TraktShowsApi
import app.moviebase.trakt.api.TraktSyncApi
import app.moviebase.trakt.api.TraktUsersApi
import app.tivi.data.traktauth.RefreshTraktTokensInteractor
import app.tivi.data.traktauth.TraktOAuthInfo
import app.tivi.data.traktauth.store.AuthStore
import app.tivi.inject.ApplicationScope
import app.tivi.trakt.store.AuthStore
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.plugins.auth.Auth
import io.ktor.client.plugins.auth.providers.BearerTokens
Expand All @@ -41,6 +43,7 @@ interface TraktComponent {
client: OkHttpClient,
authStore: AuthStore,
oauthInfo: TraktOAuthInfo,
refreshTokens: Lazy<RefreshTraktTokensInteractor>,
): Trakt = Trakt {
traktApiKey = oauthInfo.clientId
maxRetriesOnException = 3
Expand All @@ -59,7 +62,11 @@ interface TraktComponent {
}
}

// FIXME: Need to implement refreshTokens {}
refreshTokens {
refreshTokens.value.invoke()?.let {
BearerTokens(it.accessToken, it.refreshToken)
}
}

sendWithoutRequest { request ->
request.url.host == "api.trakt.tv"
Expand Down
1 change: 0 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ dependencies {
implementation(projects.common.ui.compose)
implementation(projects.data.dbRoom)
implementation(projects.api.trakt)
implementation(projects.api.traktAuth.android)
implementation(projects.api.tmdb)
implementation(projects.domain)
implementation(projects.tasks.android)
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/app/tivi/home/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import app.tivi.common.compose.shouldUseDarkColors
import app.tivi.common.compose.shouldUseDynamicColors
import app.tivi.common.compose.theme.TiviTheme
import app.tivi.core.analytics.Analytics
import app.tivi.data.traktauth.LoginToTraktInteractor
import app.tivi.data.traktauth.TraktAuthActivityComponent
import app.tivi.extensions.unsafeLazy
import app.tivi.inject.ActivityComponent
import app.tivi.inject.ActivityScope
Expand Down Expand Up @@ -83,6 +85,9 @@ class MainActivity : TiviActivity() {
// Copied from setContent {} ext-fun
setOwners()

// Register for Login activity results
component.login.register()

component.contentViewSetter.setContentView(this, composeView)
}

Expand Down Expand Up @@ -115,13 +120,15 @@ class MainActivity : TiviActivity() {
abstract class MainActivityComponent(
@get:Provides val activity: Activity,
@Component val applicationComponent: ApplicationComponent = ApplicationComponent.from(activity),
) : ActivityComponent {
) : ActivityComponent,
TraktAuthActivityComponent {
abstract val tiviDateFormatter: TiviDateFormatter
abstract val textCreator: TiviTextCreator
abstract val preferences: TiviPreferences
abstract val analytics: Analytics
abstract val contentViewSetter: ContentViewSetter
abstract val screens: ComposeScreens
abstract val login: LoginToTraktInteractor
abstract val viewModel: () -> MainActivityViewModel
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/app/tivi/home/MainActivityViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ package app.tivi.home

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.tivi.data.traktauth.TraktAuthState
import app.tivi.domain.executeSync
import app.tivi.domain.interactors.ClearTraktAuthState
import app.tivi.domain.interactors.UpdateUserDetails
import app.tivi.domain.observers.ObserveTraktAuthState
import app.tivi.domain.observers.ObserveUserDetails
import app.tivi.trakt.TraktAuthState
import app.tivi.util.Logger
import io.ktor.client.plugins.ResponseException
import io.ktor.http.HttpStatusCode
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/app/tivi/inject/AppComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ import app.tivi.appinitializers.EmojiInitializer
import app.tivi.appinitializers.PreferencesInitializer
import app.tivi.appinitializers.TimberInitializer
import app.tivi.appinitializers.TmdbInitializer
import app.tivi.data.traktauth.TraktOAuthInfo
import app.tivi.tmdb.TmdbOAuthInfo
import app.tivi.trakt.TraktConstants
import app.tivi.trakt.TraktOAuthInfo
import app.tivi.util.AppCoroutineDispatchers
import kotlinx.coroutines.Dispatchers
import me.tatarka.inject.annotations.IntoSet
Expand Down Expand Up @@ -58,7 +57,7 @@ interface AppComponent {
): TraktOAuthInfo = TraktOAuthInfo(
clientId = BuildConfig.TRAKT_CLIENT_ID,
clientSecret = BuildConfig.TRAKT_CLIENT_SECRET,
redirectUri = "${appInfo.packageName}://${TraktConstants.URI_AUTH_CALLBACK_PATH}",
redirectUri = "${appInfo.packageName}://auth/oauth2callback",
)

@Provides
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/app/tivi/inject/ApplicationComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import app.tivi.data.episodes.EpisodeBinds
import app.tivi.data.followedshows.FollowedShowsBinds
import app.tivi.data.popularshows.PopularShowsBinds
import app.tivi.data.recommendedshows.RecommendedShowsBinds
import app.tivi.data.relatedshows.RelatedShowsBinds
import app.tivi.data.search.SearchBinds
import app.tivi.data.showimages.ShowImagesBinds
import app.tivi.data.shows.ShowsBinds
import app.tivi.data.traktauth.RelatedShowsBinds
import app.tivi.data.traktauth.TraktAuthComponent
import app.tivi.data.traktusers.TraktUsersBinds
import app.tivi.data.trendingshows.TrendingShowsBinds
import app.tivi.data.watchedshows.WatchedShowsBinds
Expand All @@ -39,7 +40,6 @@ import app.tivi.settings.PreferencesComponent
import app.tivi.tasks.TasksComponent
import app.tivi.tasks.TiviWorkerFactory
import app.tivi.tmdb.TmdbComponent
import app.tivi.trakt.TraktAuthComponent
import app.tivi.trakt.TraktComponent
import app.tivi.util.LoggerComponent
import app.tivi.util.PowerControllerComponent
Expand Down
2 changes: 1 addition & 1 deletion data/episodes/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ plugins {

dependencies {
api(projects.data.models)
api(projects.data.traktauth)
implementation(projects.data.db)
implementation(projects.data.legacy) // remove this eventually

api(projects.api.trakt)
api(projects.api.traktAuth.api)
api(projects.api.tmdb)

implementation(libs.kotlininject.runtime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import app.tivi.data.models.Episode
import app.tivi.data.models.EpisodeWatchEntry
import app.tivi.data.models.PendingAction
import app.tivi.data.models.Season
import app.tivi.data.traktauth.TraktAuthRepository
import app.tivi.data.traktauth.TraktAuthState
import app.tivi.data.util.inPast
import app.tivi.data.util.syncerForEntity
import app.tivi.inject.ApplicationScope
import app.tivi.trakt.TraktAuthState
import app.tivi.trakt.TraktManager
import app.tivi.util.Logger
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
Expand All @@ -55,7 +55,7 @@ class SeasonsEpisodesRepository(
private val traktSeasonsDataSource: SeasonsEpisodesDataSource,
private val traktEpisodeDataSource: TraktEpisodeDataSource,
private val tmdbEpisodeDataSource: TmdbEpisodeDataSource,
private val traktManager: TraktManager,
private val traktAuthRepository: TraktAuthRepository,
logger: Logger,
) {
private val seasonSyncer = syncerForEntity(
Expand Down Expand Up @@ -194,7 +194,7 @@ class SeasonsEpisodesRepository(
it.isNotEmpty() && processPendingAdditions(it)
}

if (traktManager.state.value == TraktAuthState.LOGGED_IN) {
if (traktAuthRepository.state.value == TraktAuthState.LOGGED_IN) {
updateShowEpisodeWatches(showId)
}
}
Expand Down Expand Up @@ -312,13 +312,13 @@ class SeasonsEpisodesRepository(
needUpdate = true
}

if (needUpdate && traktManager.state.value == TraktAuthState.LOGGED_IN) {
if (needUpdate && traktAuthRepository.state.value == TraktAuthState.LOGGED_IN) {
fetchEpisodeWatchesFromRemote(episodeId)
}
}

suspend fun updateShowEpisodeWatches(showId: Long) {
if (traktManager.state.value != TraktAuthState.LOGGED_IN) return
if (traktAuthRepository.state.value != TraktAuthState.LOGGED_IN) return

val response = traktSeasonsDataSource.getShowEpisodeWatches(showId)

Expand All @@ -343,7 +343,7 @@ class SeasonsEpisodesRepository(
* @return true if a network service was updated
*/
private suspend fun processPendingDeletes(entries: List<EpisodeWatchEntry>): Boolean {
if (traktManager.state.value == TraktAuthState.LOGGED_IN) {
if (traktAuthRepository.state.value == TraktAuthState.LOGGED_IN) {
val localOnlyDeletes = entries.filter { it.traktId == null }
// If we've got deletes which are local only, just remove them from the DB
if (localOnlyDeletes.isNotEmpty()) {
Expand All @@ -370,7 +370,7 @@ class SeasonsEpisodesRepository(
* @return true if a network service was updated
*/
private suspend fun processPendingAdditions(entries: List<EpisodeWatchEntry>): Boolean {
if (traktManager.state.value == TraktAuthState.LOGGED_IN) {
if (traktAuthRepository.state.value == TraktAuthState.LOGGED_IN) {
traktSeasonsDataSource.addEpisodeWatches(entries)
// Now update the database
episodeWatchStore.updateEntriesWithAction(entries.map { it.id }, PendingAction.NOTHING)
Expand Down
2 changes: 1 addition & 1 deletion data/followedshows/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ plugins {

dependencies {
api(projects.data.models)
api(projects.data.traktauth)
implementation(projects.data.db)
implementation(projects.data.legacy) // remove this eventually

api(projects.api.trakt)
api(projects.api.traktAuth.api)
api(projects.api.tmdb)

api(libs.store)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import app.tivi.data.daos.TiviShowDao
import app.tivi.data.daos.getIdOrSavePlaceholder
import app.tivi.data.models.FollowedShowEntry
import app.tivi.data.models.PendingAction
import app.tivi.data.traktauth.TraktAuthRepository
import app.tivi.data.traktauth.TraktAuthState
import app.tivi.data.util.ItemSyncerResult
import app.tivi.data.util.inPast
import app.tivi.data.util.syncerForEntity
import app.tivi.data.views.FollowedShowsWatchStats
import app.tivi.inject.ApplicationScope
import app.tivi.trakt.TraktAuthState
import app.tivi.trakt.TraktManager
import app.tivi.util.Logger
import kotlin.time.Duration.Companion.hours
import kotlinx.coroutines.flow.Flow
Expand All @@ -43,7 +43,7 @@ class FollowedShowsRepository(
private val followedShowsDao: FollowedShowsDao,
private val followedShowsLastRequestStore: FollowedShowsLastRequestStore,
private val dataSource: FollowedShowsDataSource,
private val traktManager: TraktManager,
private val traktAuthRepository: TraktAuthRepository,
private val logger: Logger,
private val showDao: TiviShowDao,
) {
Expand Down Expand Up @@ -109,7 +109,7 @@ class FollowedShowsRepository(
}

suspend fun syncFollowedShows(): ItemSyncerResult<FollowedShowEntry> {
val listId = when (traktManager.state.value) {
val listId = when (traktAuthRepository.state.value) {
TraktAuthState.LOGGED_IN -> getFollowedTraktListId()
else -> null
}
Expand Down Expand Up @@ -149,7 +149,7 @@ class FollowedShowsRepository(
return
}

if (listId != null && traktManager.state.value == TraktAuthState.LOGGED_IN) {
if (listId != null && traktAuthRepository.state.value == TraktAuthState.LOGGED_IN) {
val shows = pending.mapNotNull { showDao.getShowWithId(it.showId) }
logger.v("processPendingAdditions. Entries mapped: %s", shows)

Expand Down Expand Up @@ -178,7 +178,7 @@ class FollowedShowsRepository(
return
}

if (listId != null && traktManager.state.value == TraktAuthState.LOGGED_IN) {
if (listId != null && traktAuthRepository.state.value == TraktAuthState.LOGGED_IN) {
val shows = pending.mapNotNull { showDao.getShowWithId(it.showId) }
logger.v("processPendingDelete. Entries mapped: %s", shows)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package app.tivi.data.relatedshows
package app.tivi.data.traktauth

import app.tivi.inject.ApplicationScope
import me.tatarka.inject.annotations.Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package app.tivi.data.relatedshows
package app.tivi.data.traktauth

import app.tivi.data.models.RelatedShowEntry
import app.tivi.data.models.TiviShow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package app.tivi.data.relatedshows
package app.tivi.data.traktauth

import app.tivi.data.daos.LastRequestDao
import app.tivi.data.lastrequests.EntityLastRequestStore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package app.tivi.data.relatedshows
package app.tivi.data.traktauth

import app.tivi.data.daos.RelatedShowsDao
import app.tivi.data.daos.TiviShowDao
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package app.tivi.data.relatedshows
package app.tivi.data.traktauth

import app.moviebase.tmdb.Tmdb3
import app.moviebase.tmdb.model.TmdbShow
Expand Down
Loading