-
-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df1d59e
commit 3fe3d83
Showing
7 changed files
with
127 additions
and
3 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
74 changes: 74 additions & 0 deletions
74
app/src/main/java/com/battlelancer/seriesguide/sync/TraktNotesSync.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,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 | ||
} | ||
|
||
} |
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