Spaces:
Running
Running
File size: 7,854 Bytes
71f9056 21678bc 71f9056 21678bc 71f9056 21678bc 71f9056 21678bc 71f9056 21678bc 71f9056 21678bc 71f9056 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
/**
* VideoCharm - Local Storage Management
* Handles all local storage operations for history and favorites
*/
const StorageManager = (function() {
// Storage keys
const KEYS = {
HISTORY: 'videocharm_history',
FAVORITES: 'videocharm_favorites',
LIKES: 'videocharm_likes',
VIEWS: 'videocharm_views'
};
// Maximum storage age (7 days in milliseconds)
const MAX_HISTORY_AGE = 7 * 24 * 60 * 60 * 1000;
/**
* Initialize storage with default values if not exists
*/
function initStorage() {
if (!localStorage.getItem(KEYS.HISTORY)) {
localStorage.setItem(KEYS.HISTORY, JSON.stringify([]));
}
if (!localStorage.getItem(KEYS.FAVORITES)) {
localStorage.setItem(KEYS.FAVORITES, JSON.stringify([]));
}
if (!localStorage.getItem(KEYS.LIKES)) {
localStorage.setItem(KEYS.LIKES, JSON.stringify([]));
}
if (!localStorage.getItem(KEYS.VIEWS)) {
localStorage.setItem(KEYS.VIEWS, JSON.stringify(0));
}
// Clean up old history entries
cleanupHistory();
}
/**
* Remove history items older than MAX_HISTORY_AGE
*/
function cleanupHistory() {
const history = getHistory();
const now = new Date().getTime();
const filteredHistory = history.filter(item => {
return (now - item.timestamp) < MAX_HISTORY_AGE;
});
if (filteredHistory.length !== history.length) {
localStorage.setItem(KEYS.HISTORY, JSON.stringify(filteredHistory));
}
}
/**
* Get all history items
* @returns {Array} History items
*/
function getHistory() {
try {
return JSON.parse(localStorage.getItem(KEYS.HISTORY)) || [];
} catch (e) {
console.error('Error parsing history from localStorage:', e);
return [];
}
}
/**
* Get all favorites
* @returns {Array} Favorite items
*/
function getFavorites() {
try {
return JSON.parse(localStorage.getItem(KEYS.FAVORITES)) || [];
} catch (e) {
console.error('Error parsing favorites from localStorage:', e);
return [];
}
}
/**
* Get all liked video IDs
* @returns {Array} Liked video IDs
*/
function getLikes() {
try {
return JSON.parse(localStorage.getItem(KEYS.LIKES)) || [];
} catch (e) {
console.error('Error parsing likes from localStorage:', e);
return [];
}
}
/**
* Get view count
* @returns {Number} Total view count
*/
function getViews() {
try {
return parseInt(JSON.parse(localStorage.getItem(KEYS.VIEWS))) || 0;
} catch (e) {
console.error('Error parsing views from localStorage:', e);
return 0;
}
}
/**
* Add a video to history
* @param {Object} video Video object to add
*/
function addToHistory(video) {
if (!video || !video.id || !video.url) return;
const history = getHistory();
// Check if video already exists in history
const existingIndex = history.findIndex(item => item.id === video.id);
if (existingIndex !== -1) {
// Update timestamp of existing item
history[existingIndex].timestamp = new Date().getTime();
// Update thumbnail if available
if (video.thumbnail) {
history[existingIndex].thumbnail = video.thumbnail;
}
} else {
// Create simplified record for storage
const historyItem = {
id: video.id,
url: video.url,
title: video.title || '精彩视频',
timestamp: new Date().getTime(),
duration: video.duration || 0,
thumbnail: video.thumbnail || null
};
// Add to beginning of array
history.unshift(historyItem);
}
// Save updated history
localStorage.setItem(KEYS.HISTORY, JSON.stringify(history));
// Increment view count
incrementViews();
}
/**
* Toggle favorite status for a video
* @param {Object} video Video object
* @returns {Boolean} New favorite status
*/
function toggleFavorite(video) {
if (!video || !video.id) return false;
const favorites = getFavorites();
const existingIndex = favorites.findIndex(item => item.id === video.id);
if (existingIndex !== -1) {
// Remove from favorites
favorites.splice(existingIndex, 1);
localStorage.setItem(KEYS.FAVORITES, JSON.stringify(favorites));
return false;
} else {
// Create simplified record for storage
const favoriteItem = {
id: video.id,
url: video.url,
title: video.title || '精彩视频',
timestamp: new Date().getTime(),
duration: video.duration || 0,
thumbnail: video.thumbnail || null
};
// Add to favorites
favorites.unshift(favoriteItem);
localStorage.setItem(KEYS.FAVORITES, JSON.stringify(favorites));
return true;
}
}
/**
* Check if a video is favorited
* @param {String} videoId Video ID to check
* @returns {Boolean} Whether video is favorited
*/
function isFavorite(videoId) {
if (!videoId) return false;
const favorites = getFavorites();
return favorites.some(item => item.id === videoId);
}
/**
* Toggle like status for a video
* @param {String} videoId Video ID
* @returns {Boolean} New like status
*/
function toggleLike(videoId) {
if (!videoId) return false;
const likes = getLikes();
const index = likes.indexOf(videoId);
if (index !== -1) {
// Remove like
likes.splice(index, 1);
localStorage.setItem(KEYS.LIKES, JSON.stringify(likes));
return false;
} else {
// Add like
likes.push(videoId);
localStorage.setItem(KEYS.LIKES, JSON.stringify(likes));
return true;
}
}
/**
* Check if a video is liked
* @param {String} videoId Video ID to check
* @returns {Boolean} Whether video is liked
*/
function isLiked(videoId) {
if (!videoId) return false;
const likes = getLikes();
return likes.includes(videoId);
}
/**
* Increment view count
*/
function incrementViews() {
const views = getViews();
localStorage.setItem(KEYS.VIEWS, JSON.stringify(views + 1));
}
/**
* Clear all history
*/
function clearHistory() {
localStorage.setItem(KEYS.HISTORY, JSON.stringify([]));
}
/**
* Clear all favorites
*/
function clearFavorites() {
localStorage.setItem(KEYS.FAVORITES, JSON.stringify([]));
}
// Public API
return {
init: initStorage,
getHistory: getHistory,
getFavorites: getFavorites,
getLikes: getLikes,
getViews: getViews,
addToHistory: addToHistory,
toggleFavorite: toggleFavorite,
isFavorite: isFavorite,
toggleLike: toggleLike,
isLiked: isLiked,
clearHistory: clearHistory,
clearFavorites: clearFavorites
};
})(); |