|
| 1 | +import { defineStore, storeToRefs } from 'pinia'; |
| 2 | + |
| 3 | +import { ref, watch } from 'vue'; |
| 4 | + |
| 5 | +import type { TraktSyncActivities } from '~/models/trakt/trakt-sync.model'; |
| 6 | + |
| 7 | +import { TraktService } from '~/services/trakt.service'; |
| 8 | +import { storage } from '~/utils/browser/browser-storage.utils'; |
| 9 | +import { compareDateObject, toDateObject } from '~/utils/date.utils'; |
| 10 | + |
| 11 | +export const useActivityStore = defineStore('data.activity', () => { |
| 12 | + const activity = ref<TraktSyncActivities>(); |
| 13 | + const loading = ref(false); |
| 14 | + |
| 15 | + const clearState = () => { |
| 16 | + activity.value = undefined; |
| 17 | + }; |
| 18 | + |
| 19 | + const saveState = async () => storage.local.set('data.activity', activity.value); |
| 20 | + const restoreState = async () => { |
| 21 | + const state = await storage.local.get<TraktSyncActivities>('data.activity'); |
| 22 | + if (state) activity.value = state; |
| 23 | + }; |
| 24 | + |
| 25 | + const fetchActivity = async () => { |
| 26 | + console.info('Fetching activity'); |
| 27 | + loading.value = true; |
| 28 | + |
| 29 | + try { |
| 30 | + activity.value = await TraktService.activity(); |
| 31 | + await saveState(); |
| 32 | + } catch (error) { |
| 33 | + console.error('Failed to fetch activity', error); |
| 34 | + throw error; |
| 35 | + } finally { |
| 36 | + loading.value = false; |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + const initActivityStore = async () => { |
| 41 | + await restoreState(); |
| 42 | + |
| 43 | + watch(activity, (next, prev) => { |
| 44 | + const changed = compareDateObject(toDateObject(prev), toDateObject(next)); |
| 45 | + console.info('Activity changed', changed); |
| 46 | + |
| 47 | + if ( |
| 48 | + changed?.episodes?.watched_at || |
| 49 | + changed?.movies?.watched_at || |
| 50 | + changed?.shows?.hidden_at || |
| 51 | + changed?.seasons?.hidden_at || |
| 52 | + changed?.movies?.hidden_at |
| 53 | + ) { |
| 54 | + TraktService.evict.progress(); |
| 55 | + TraktService.evict.history(); |
| 56 | + TraktService.evict.calendar(); |
| 57 | + console.info('Evicted progress, history and calendar'); |
| 58 | + } |
| 59 | + if ( |
| 60 | + changed?.watchlist?.updated_at || |
| 61 | + changed?.episodes?.watchlisted_at || |
| 62 | + changed?.seasons?.watchlisted_at || |
| 63 | + changed?.shows?.watchlisted_at || |
| 64 | + changed?.movies?.watchlisted_at |
| 65 | + ) { |
| 66 | + TraktService.evict.watchlist(); |
| 67 | + console.info('Evicted watchlist'); |
| 68 | + } |
| 69 | + if (changed?.collaborations?.updated_at || changed?.lists?.updated_at) { |
| 70 | + TraktService.evict.lists(); |
| 71 | + console.info('Evicted lists'); |
| 72 | + } |
| 73 | + if (changed?.episodes?.collected_at || changed?.movies?.collected_at) { |
| 74 | + TraktService.evict.collection(); |
| 75 | + console.info('Evicted collection'); |
| 76 | + } |
| 77 | + if (changed?.favorites?.updated_at) { |
| 78 | + TraktService.evict.favorites(); |
| 79 | + console.info('Evicted favorites'); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + await fetchActivity(); |
| 84 | + }; |
| 85 | + |
| 86 | + return { activity, loading, fetchActivity, clearState, saveState, restoreState, initActivityStore }; |
| 87 | +}); |
| 88 | + |
| 89 | +export const useActivityStoreRefs = () => storeToRefs(useActivityStore()); |
0 commit comments