Skip to content
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
17 changes: 17 additions & 0 deletions app/src/main/kotlin/com/arflix/tv/data/api/TraktApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,16 @@ interface TraktApi {
@Query("page") page: Int = 1,
@Query("limit") limit: Int = 100
): List<TraktPublicListItem>

// ========== User Profile ==========

@GET("users/me")
@retrofit2.http.Headers("Content-Type: application/json")
suspend fun getMe(
@Header("Authorization") auth: String,
@Header("trakt-api-key") clientId: String,
@Header("trakt-api-version") version: String = "2"
): TraktUserProfile
}

// ========== Request Bodies ==========
Expand Down Expand Up @@ -925,3 +935,10 @@ data class TraktBulkEpisodeItem(
val number: Int,
@SerializedName("watched_at") val watchedAt: String? = null
)

// ========== User Profile ==========

data class TraktUserProfile(
val username: String? = null,
val name: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ class MdbListRepository @Inject constructor(

suspend fun isConnected(): Boolean = key() != null

/**
* Fetches the MDBList username for the currently connected API key.
* Returns null gracefully if not connected or if the request fails.
*/
suspend fun fetchUsername(): String? = withContext(Dispatchers.IO) {
try {
val k = key() ?: return@withContext null
api.getUser(k).username
} catch (e: kotlinx.coroutines.CancellationException) {
throw e
} catch (_: Exception) {
null
}
}

// ===== Watchlist =====

suspend fun addToWatchlist(mediaType: MediaType, tmdbId: Int): Boolean =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,22 @@ class TraktRepository @Inject constructor(
return "Bearer $token"
}

/**
* Fetches the Trakt username for the currently authenticated profile.
* Returns null gracefully if not authenticated or if the request fails — a
* failed username fetch must never break the auth or sync flow.
*/
suspend fun fetchUsername(): String? = withContext(Dispatchers.IO) {
try {
val auth = getAuthHeader() ?: return@withContext null
traktApi.getMe(auth = auth, clientId = clientId).username
} catch (e: kotlinx.coroutines.CancellationException) {
throw e
} catch (_: Exception) {
null
}
}

private suspend fun hasStoredTraktTokenForCurrentProfile(): Boolean {
ensureProfileCacheScope()
val prefs = context.traktDataStore.data.first()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1137,29 +1137,36 @@ class TraktSyncService @Inject constructor(
}

/**
* Get last sync time
* Get the persisted sync summary for the active profile.
*/
suspend fun getLastSyncTime(): String? = withContext(Dispatchers.IO) {
suspend fun getLastSyncSummary(): TraktSyncSummary? = withContext(Dispatchers.IO) {
try {
val userId = getUserId() ?: return@withContext null
if (getSupabaseAuth() == null) return@withContext null

// PostgREST requires "eq." prefix for equality filtering
val syncStates = executeSupabaseCall("get sync state (last sync)") { auth ->
val syncStates = executeSupabaseCall("get sync state summary") { auth ->
supabaseApi.getSyncState(
auth,
userId = "eq.$userId",
profileId = "eq.${activeProfileId()}"
)
}
syncStates.firstOrNull()?.lastSyncAt
syncStates.firstOrNull()?.let { state ->
TraktSyncSummary(
lastSyncAt = state.lastSyncAt,
moviesSynced = state.moviesSynced,
episodesSynced = state.episodesSynced
)
}
} catch (e: Exception) {
if (e is kotlinx.coroutines.CancellationException) throw e

null
}
}

suspend fun getLastSyncTime(): String? = getLastSyncSummary()?.lastSyncAt

// ========== Private Helpers ==========

private fun buildEpisodeKey(
Expand Down Expand Up @@ -2039,6 +2046,12 @@ class TraktSyncService @Inject constructor(

// ========== Data Classes ==========

data class TraktSyncSummary(
val lastSyncAt: String?,
val moviesSynced: Int,
val episodesSynced: Int
)

data class SyncProgress(
val status: SyncStatus = SyncStatus.IDLE,
val message: String = "",
Expand Down
Loading
Loading