Skip to content

Commit

Permalink
draft: Show notes: sync with Trakt
Browse files Browse the repository at this point in the history
  • Loading branch information
UweTrottmann committed Sep 5, 2024
1 parent df1d59e commit 3fe3d83
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.battlelancer.seriesguide.util.TaskManager
import com.uwetrottmann.androidutils.AndroidUtils
import com.uwetrottmann.tmdb2.services.ConfigurationService
import com.uwetrottmann.trakt5.services.Sync
import com.uwetrottmann.trakt5.services.Users
import dagger.Lazy
import timber.log.Timber
import javax.inject.Inject
Expand All @@ -54,6 +55,9 @@ class SgSyncAdapter(context: Context) : AbstractThreadedSyncAdapter(context, tru
@Inject
lateinit var traktSync: Lazy<Sync>

@Inject
lateinit var traktUsers: Lazy<Users>

@Inject
lateinit var movieTools: Lazy<MovieTools>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public enum Step {
TRAKT(R.string.trakt, 0),
TRAKT_EPISODES(R.string.trakt, R.string.episodes),
TRAKT_RATINGS(R.string.trakt, R.string.ratings),
TRAKT_NOTES(R.string.trakt, R.string.title_notes),
TRAKT_MOVIES(R.string.trakt, R.string.movies);

public final int serviceRes;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2024 Uwe Trottmann

package com.battlelancer.seriesguide.sync

import com.battlelancer.seriesguide.traktapi.SgTrakt
import com.battlelancer.seriesguide.traktapi.TraktSettings
import com.battlelancer.seriesguide.util.Errors
import com.battlelancer.seriesguide.util.TimeTools
import com.uwetrottmann.trakt5.TraktV2
import com.uwetrottmann.trakt5.entities.NoteResponse
import com.uwetrottmann.trakt5.entities.UserSlug
import org.threeten.bp.OffsetDateTime
import timber.log.Timber

/**
* Downloads notes, currently only for shows, from Trakt.
*
* This will do nothing if the user is not a Trakt VIP.
*/
class TraktNotesSync(
private val traktSync: TraktSync
) {
private val context = traktSync.context

fun syncForShows(updatedAt: OffsetDateTime?): Boolean {
if (updatedAt == null) {
Timber.e("downloadForShows: null updatedAt")
return false
}

val isInitialSync = TraktSettings.isInitialSyncShowNotes(context)

// Do not sync if notes have not changed, or is not initial sync
val lastUpdatedAt = TraktSettings.getLastNotesUpdatedAt(context)
if (!TimeTools.isAfterMillis(updatedAt, lastUpdatedAt)) {
Timber.d("downloadForShows: no changes since %tF %tT", lastUpdatedAt, lastUpdatedAt)
return true
}

var page = 1
var pageCount = 1
do {
var notesResponse: List<NoteResponse>?
val response = traktSync.users
.notes(UserSlug.ME, "shows", page, null, null)
.execute()
if (!response.isSuccessful) {
if (TraktV2.isNotVip(response)) {
return true // Do not error
}
if (SgTrakt.isUnauthorized(context, response)) {
return false
}
Errors.logAndReport("get notes", response)
return false
}
processShowNotes(response.body(), isInitialSync)
pageCount = response.headers()["x-pagination-page-count"]?.toIntOrNull() ?: 1
page++
} while (page <= pageCount)

if (isInitialSync) {
TraktSettings.setInitialSyncShowNotesCompleted(context)
}

return true
}

private fun processShowNotes(response: List<NoteResponse>?, initialSync: Boolean) {
// TODO
}

}
11 changes: 11 additions & 0 deletions app/src/main/java/com/battlelancer/seriesguide/sync/TraktSync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.github.michaelbull.result.getOrElse
import com.uwetrottmann.androidutils.AndroidUtils
import com.uwetrottmann.trakt5.entities.LastActivityMore
import com.uwetrottmann.trakt5.services.Sync
import com.uwetrottmann.trakt5.services.Users
import retrofit2.Response
import timber.log.Timber

Expand All @@ -27,6 +28,7 @@ class TraktSync(
val context: Context,
val movieTools: MovieTools,
val sync: Sync,
val users: Users,
val progress: SyncProgress
) {
private fun noConnection(): Boolean {
Expand Down Expand Up @@ -89,6 +91,15 @@ class TraktSync(
progress.recordError()
return SgSyncAdapter.UpdateResult.INCOMPLETE
}
// Download notes
if (!onlyRatings) {
progress.publish(SyncProgress.Step.TRAKT_NOTES)
if (noConnection()) return SgSyncAdapter.UpdateResult.INCOMPLETE
if (!TraktNotesSync(this).syncForShows(lastActivity.notes.updated_at)) {
progress.recordError()
return SgSyncAdapter.UpdateResult.INCOMPLETE
}
}
}

// MOVIES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ object TraktSettings {
private const val KEY_LAST_MOVIES_WATCHED_AT
: String = "trakt.last_activity.movies.watched"

private const val KEY_LAST_NOTES_UPDATED_AT
: String = "trakt.last_activity.notes.updated"

/**
* Unused, but kept for reference.
*
Expand All @@ -66,6 +69,9 @@ object TraktSettings {
private const val KEY_HAS_MERGED_MOVIES
: String = "com.battlelancer.seriesguide.trakt.mergedmovies"

private const val KEY_HAS_MERGED_SHOW_NOTES
: String = "trakt.notes.shows.merged"

/**
* Used in settings_basic.xml.
*/
Expand Down Expand Up @@ -136,6 +142,14 @@ object TraktSettings {
.getLong(KEY_LAST_MOVIES_WATCHED_AT, 0)
}

/**
* The last time notes were updated or 0 if no value exists.
*/
fun getLastNotesUpdatedAt(context: Context): Long {
return PreferenceManager.getDefaultSharedPreferences(context)
.getLong(KEY_LAST_NOTES_UPDATED_AT, 0)
}

/**
* If either collection, watchlist or watched list have changes newer than last stored.
*/
Expand Down Expand Up @@ -227,10 +241,26 @@ object TraktSettings {
}
}

/**
* Returns if show notes have not been synced with the current Trakt account.
*/
fun isInitialSyncShowNotes(context: Context): Boolean {
return !PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(KEY_HAS_MERGED_SHOW_NOTES, false)
}

fun setInitialSyncShowNotesCompleted(context: Context) {
PreferenceManager.getDefaultSharedPreferences(context).edit {
putBoolean(KEY_HAS_MERGED_SHOW_NOTES, true)
}
}


fun resetToInitialSync(context: Context) {
PreferenceManager.getDefaultSharedPreferences(context).edit {
putBoolean(KEY_HAS_MERGED_EPISODES, false)
putBoolean(KEY_HAS_MERGED_MOVIES, false)
putBoolean(KEY_HAS_MERGED_SHOW_NOTES, false)
// Not actually necessary, but also reset timestamps for episodes and movies
putLong(KEY_LAST_EPISODES_WATCHED_AT, 0)
putLong(KEY_LAST_EPISODES_COLLECTED_AT, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import com.github.michaelbull.result.andThen
import com.github.michaelbull.result.mapError
import com.github.michaelbull.result.runCatching
import com.uwetrottmann.trakt5.entities.BaseShow
import com.uwetrottmann.trakt5.entities.LastActivities
import com.uwetrottmann.trakt5.entities.LastActivity
import com.uwetrottmann.trakt5.entities.LastActivityMore
import com.uwetrottmann.trakt5.entities.LastActivityUpdated
import com.uwetrottmann.trakt5.entities.Ratings
import com.uwetrottmann.trakt5.entities.Show
import com.uwetrottmann.trakt5.enums.Extended
Expand Down Expand Up @@ -140,6 +140,7 @@ object TraktTools2 {
val episodes: LastActivityMore,
val shows: LastActivity,
val movies: LastActivityMore,
val notes: LastActivityUpdated,
)

fun getLastActivity(context: Context): Result<LastActivities, TraktError> {
Expand All @@ -157,12 +158,14 @@ object TraktTools2 {
val episodes = lastActivities?.episodes
val shows = lastActivities?.shows
val movies = lastActivities?.movies
if (episodes != null && shows != null && movies != null) {
val notes = lastActivities?.notes
if (episodes != null && shows != null && movies != null && notes != null) {
return@andThen Ok(
LastActivities(
episodes = episodes,
shows = shows,
movies = movies
movies = movies,
notes = notes
)
)
} else {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
<string name="context_marknext">Watched next episode</string>
<string name="action_episodes_switch_view">Switch view</string>
<string name="next_to_watch">Next to watch</string>
<string name="title_notes">Notes</string>
<string name="title_note">Note</string>

<!-- History -->
Expand Down

0 comments on commit 3fe3d83

Please sign in to comment.