fixed a continue watching bug where removed content won't reappearing even after watching it again

This commit is contained in:
tapframe 2025-12-11 18:56:51 +05:30
parent bf22e559c5
commit 8b5a707daa

View file

@ -29,7 +29,7 @@ class StorageService {
private watchProgressCacheTimestamp = 0; private watchProgressCacheTimestamp = 0;
private readonly WATCH_PROGRESS_CACHE_TTL = 5000; // 5 seconds private readonly WATCH_PROGRESS_CACHE_TTL = 5000; // 5 seconds
private constructor() {} private constructor() { }
public static getInstance(): StorageService { public static getInstance(): StorageService {
if (!StorageService.instance) { if (!StorageService.instance) {
@ -88,7 +88,7 @@ class StorageService {
const map = JSON.parse(json) as Record<string, number>; const map = JSON.parse(json) as Record<string, number>;
map[this.buildWpKeyString(id, type, episodeId)] = deletedAtMs || Date.now(); map[this.buildWpKeyString(id, type, episodeId)] = deletedAtMs || Date.now();
await mmkvStorage.setItem(key, JSON.stringify(map)); await mmkvStorage.setItem(key, JSON.stringify(map));
} catch {} } catch { }
} }
public async clearWatchProgressTombstone( public async clearWatchProgressTombstone(
@ -105,7 +105,7 @@ class StorageService {
delete map[k]; delete map[k];
await mmkvStorage.setItem(key, JSON.stringify(map)); await mmkvStorage.setItem(key, JSON.stringify(map));
} }
} catch {} } catch { }
} }
public async getWatchProgressTombstones(): Promise<Record<string, number>> { public async getWatchProgressTombstones(): Promise<Record<string, number>> {
@ -220,7 +220,7 @@ class StorageService {
lastUpdated: Date.now() lastUpdated: Date.now()
}; };
await this.setWatchProgress(id, type, updatedProgress, episodeId); await this.setWatchProgress(id, type, updatedProgress, episodeId);
logger.log(`[StorageService] Updated progress duration from ${(existingProgress.duration/60).toFixed(0)}min to ${(newDuration/60).toFixed(0)}min`); logger.log(`[StorageService] Updated progress duration from ${(existingProgress.duration / 60).toFixed(0)}min to ${(newDuration / 60).toFixed(0)}min`);
} }
} catch (error) { } catch (error) {
logger.error('Error updating progress duration:', error); logger.error('Error updating progress duration:', error);
@ -247,7 +247,7 @@ class StorageService {
if (newestTombAt && (progress.lastUpdated == null || progress.lastUpdated <= newestTombAt)) { if (newestTombAt && (progress.lastUpdated == null || progress.lastUpdated <= newestTombAt)) {
return; return;
} }
} catch {} } catch { }
// Check if progress has actually changed significantly, unless forceWrite is requested // Check if progress has actually changed significantly, unless forceWrite is requested
if (!options?.forceWrite) { if (!options?.forceWrite) {
@ -266,6 +266,21 @@ class StorageService {
const timestamp = (options?.preserveTimestamp && typeof progress.lastUpdated === 'number') const timestamp = (options?.preserveTimestamp && typeof progress.lastUpdated === 'number')
? progress.lastUpdated ? progress.lastUpdated
: Date.now(); : Date.now();
try {
const removedMap = await this.getContinueWatchingRemoved();
const removedKey = this.buildWpKeyString(id, type);
const removedAt = removedMap[removedKey];
if (removedAt != null && timestamp > removedAt) {
logger.log(`♻️ [StorageService] restoring content to continue watching due to new progress: ${type}:${id}`);
await this.removeContinueWatchingRemoved(id, type);
}
} catch (e) {
// Ignore error checks for restoration to prevent blocking save
}
const updated = { ...progress, lastUpdated: timestamp }; const updated = { ...progress, lastUpdated: timestamp };
await mmkvStorage.setItem(key, JSON.stringify(updated)); await mmkvStorage.setItem(key, JSON.stringify(updated));
@ -309,7 +324,7 @@ class StorageService {
// Only notify if we have subscribers // Only notify if we have subscribers
if (this.watchProgressSubscribers.length > 0) { if (this.watchProgressSubscribers.length > 0) {
this.watchProgressSubscribers.forEach(callback => callback()); this.watchProgressSubscribers.forEach(callback => callback());
} }
} }
@ -364,7 +379,7 @@ class StorageService {
// Notify subscribers // Notify subscribers
this.notifyWatchProgressSubscribers(); this.notifyWatchProgressSubscribers();
// Emit explicit remove event for sync layer // Emit explicit remove event for sync layer
try { this.watchProgressRemoveListeners.forEach(l => l(id, type, episodeId)); } catch {} try { this.watchProgressRemoveListeners.forEach(l => l(id, type, episodeId)); } catch { }
} catch (error) { } catch (error) {
logger.error('Error removing watch progress:', error); logger.error('Error removing watch progress:', error);
} }
@ -419,7 +434,7 @@ class StorageService {
exactTime?: number exactTime?: number
): Promise<void> { ): Promise<void> {
try { try {
const existingProgress = await this.getWatchProgress(id, type, episodeId); const existingProgress = await this.getWatchProgress(id, type, episodeId);
if (existingProgress) { if (existingProgress) {
// Preserve the highest Trakt progress and currentTime values to avoid accidental regressions // Preserve the highest Trakt progress and currentTime values to avoid accidental regressions
const highestTraktProgress = (() => { const highestTraktProgress = (() => {
@ -623,17 +638,17 @@ class StorageService {
const durationDiff = Math.abs(calculatedDuration - localProgress.duration); const durationDiff = Math.abs(calculatedDuration - localProgress.duration);
if (durationDiff > 300) { // More than 5 minutes difference if (durationDiff > 300) { // More than 5 minutes difference
duration = calculatedDuration; duration = calculatedDuration;
logger.log(`[StorageService] Updated duration based on exact time: ${(localProgress.duration/60).toFixed(0)}min → ${(duration/60).toFixed(0)}min`); logger.log(`[StorageService] Updated duration based on exact time: ${(localProgress.duration / 60).toFixed(0)}min → ${(duration / 60).toFixed(0)}min`);
} }
} else if (localProgress.duration > 0) { } else if (localProgress.duration > 0) {
// Use percentage calculation with local duration // Use percentage calculation with local duration
currentTime = (traktProgress / 100) * localProgress.duration; currentTime = (traktProgress / 100) * localProgress.duration;
} else { } else {
// No local duration, check stored duration // No local duration, check stored duration
const storedDuration = await this.getContentDuration(id, type, episodeId); const storedDuration = await this.getContentDuration(id, type, episodeId);
duration = storedDuration || 0; duration = storedDuration || 0;
if (!duration || duration <= 0) { if (!duration || duration <= 0) {
if (exactTime && exactTime > 0) { if (exactTime && exactTime > 0) {
duration = (exactTime / traktProgress) * 100; duration = (exactTime / traktProgress) * 100;
currentTime = exactTime; currentTime = exactTime;
@ -649,20 +664,20 @@ class StorageService {
currentTime = (traktProgress / 100) * duration; currentTime = (traktProgress / 100) * duration;
} }
} else { } else {
currentTime = exactTime && exactTime > 0 ? exactTime : (traktProgress / 100) * duration; currentTime = exactTime && exactTime > 0 ? exactTime : (traktProgress / 100) * duration;
} }
} }
const updatedProgress: WatchProgress = { const updatedProgress: WatchProgress = {
...localProgress, ...localProgress,
currentTime, currentTime,
duration, duration,
lastUpdated: traktTimestamp, lastUpdated: traktTimestamp,
traktSynced: true, traktSynced: true,
traktLastSynced: Date.now(), traktLastSynced: Date.now(),
traktProgress traktProgress
}; };
await this.setWatchProgress(id, type, updatedProgress, episodeId); await this.setWatchProgress(id, type, updatedProgress, episodeId);
// Progress update logging removed // Progress update logging removed
} }