mirror of
https://github.com/yattee/yattee.git
synced 2024-11-09 15:58:20 +00:00
Refactor
This commit is contained in:
parent
b621eba236
commit
f39b440b21
@ -3,6 +3,7 @@ import Foundation
|
|||||||
extension ChannelPlaylist {
|
extension ChannelPlaylist {
|
||||||
static var fixture: ChannelPlaylist {
|
static var fixture: ChannelPlaylist {
|
||||||
ChannelPlaylist(
|
ChannelPlaylist(
|
||||||
|
id: "fixture-channel-playlist",
|
||||||
title: "Playlist with a very long title that will not fit easily in the screen",
|
title: "Playlist with a very long title that will not fit easily in the screen",
|
||||||
thumbnailURL: URL(string: "https://i.ytimg.com/vi/hT_nvWreIhg/hqdefault.jpg?sqp=-oaymwEWCKgBEF5IWvKriqkDCQgBFQAAiEIYAQ==&rs=AOn4CLAAD21_-Bo6Td1z3cV-UFyoi1flEg")!,
|
thumbnailURL: URL(string: "https://i.ytimg.com/vi/hT_nvWreIhg/hqdefault.jpg?sqp=-oaymwEWCKgBEF5IWvKriqkDCQgBFQAAiEIYAQ==&rs=AOn4CLAAD21_-Bo6Td1z3cV-UFyoi1flEg")!,
|
||||||
channel: Video.fixture.channel,
|
channel: Video.fixture.channel,
|
||||||
|
@ -18,17 +18,17 @@ struct ChannelPlaylistsCacheModel: CacheModel {
|
|||||||
|
|
||||||
func storePlaylist(playlist: ChannelPlaylist) {
|
func storePlaylist(playlist: ChannelPlaylist) {
|
||||||
let date = iso8601DateFormatter.string(from: Date())
|
let date = iso8601DateFormatter.string(from: Date())
|
||||||
logger.info("STORE \(playlistCacheKey(playlist.id)) -- \(date)")
|
logger.info("STORE \(playlist.cacheKey) -- \(date)")
|
||||||
let feedTimeObject: JSON = ["date": date]
|
let feedTimeObject: JSON = ["date": date]
|
||||||
let playlistObject: JSON = ["playlist": playlist.json.object]
|
let playlistObject: JSON = ["playlist": playlist.json.object]
|
||||||
try? storage?.setObject(feedTimeObject, forKey: playlistTimeCacheKey(playlist.id))
|
try? storage?.setObject(feedTimeObject, forKey: playlistTimeCacheKey(playlist.cacheKey))
|
||||||
try? storage?.setObject(playlistObject, forKey: playlistCacheKey(playlist.id))
|
try? storage?.setObject(playlistObject, forKey: playlist.cacheKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
func retrievePlaylist(_ id: ChannelPlaylist.ID) -> ChannelPlaylist? {
|
func retrievePlaylist(_ playlist: ChannelPlaylist) -> ChannelPlaylist? {
|
||||||
logger.info("RETRIEVE \(playlistCacheKey(id))")
|
logger.info("RETRIEVE \(playlist.cacheKey)")
|
||||||
|
|
||||||
if let json = try? storage?.object(forKey: playlistCacheKey(id)).dictionaryValue["playlist"] {
|
if let json = try? storage?.object(forKey: playlist.cacheKey).dictionaryValue["playlist"] {
|
||||||
return ChannelPlaylist.from(json)
|
return ChannelPlaylist.from(json)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,11 +50,7 @@ struct ChannelPlaylistsCacheModel: CacheModel {
|
|||||||
getFormattedDate(getPlaylistsTime(id))
|
getFormattedDate(getPlaylistsTime(id))
|
||||||
}
|
}
|
||||||
|
|
||||||
private func playlistCacheKey(_ playlist: ChannelPlaylist.ID) -> String {
|
private func playlistTimeCacheKey(_ cacheKey: ChannelPlaylist.ID) -> String {
|
||||||
"channelplaylists-\(playlist)"
|
"\(cacheKey)-time"
|
||||||
}
|
|
||||||
|
|
||||||
private func playlistTimeCacheKey(_ playlist: ChannelPlaylist.ID) -> String {
|
|
||||||
"\(playlistCacheKey(playlist))-time"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,13 +2,17 @@ import Foundation
|
|||||||
import SwiftyJSON
|
import SwiftyJSON
|
||||||
|
|
||||||
struct ChannelPlaylist: Identifiable {
|
struct ChannelPlaylist: Identifiable {
|
||||||
var id: String = UUID().uuidString
|
var id: String
|
||||||
var title: String
|
var title: String
|
||||||
var thumbnailURL: URL?
|
var thumbnailURL: URL?
|
||||||
var channel: Channel?
|
var channel: Channel?
|
||||||
var videos = [Video]()
|
var videos = [Video]()
|
||||||
var videosCount: Int?
|
var videosCount: Int?
|
||||||
|
|
||||||
|
var cacheKey: String {
|
||||||
|
"channelplaylists-\(id)"
|
||||||
|
}
|
||||||
|
|
||||||
var json: JSON {
|
var json: JSON {
|
||||||
[
|
[
|
||||||
"id": id,
|
"id": id,
|
||||||
|
@ -49,4 +49,17 @@ struct ContentItem: Identifiable {
|
|||||||
var contentType: ContentType {
|
var contentType: ContentType {
|
||||||
video.isNil ? (channel.isNil ? (playlist.isNil ? .placeholder : .playlist) : .channel) : .video
|
video.isNil ? (channel.isNil ? (playlist.isNil ? .placeholder : .playlist) : .channel) : .video
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cacheKey: String {
|
||||||
|
switch contentType {
|
||||||
|
case .video:
|
||||||
|
return video.cacheKey
|
||||||
|
case .playlist:
|
||||||
|
return playlist.cacheKey
|
||||||
|
case .channel:
|
||||||
|
return channel.cacheKey
|
||||||
|
case .placeholder:
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ struct ChannelPlaylistView: View {
|
|||||||
.environment(\.listingStyle, channelPlaylistListingStyle)
|
.environment(\.listingStyle, channelPlaylistListingStyle)
|
||||||
.onAppear {
|
.onAppear {
|
||||||
if let playlist = presentedPlaylist,
|
if let playlist = presentedPlaylist,
|
||||||
let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(playlist.id)
|
let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(playlist)
|
||||||
{
|
{
|
||||||
store.replace(cache)
|
store.replace(cache)
|
||||||
}
|
}
|
||||||
|
@ -82,8 +82,8 @@ struct FavoriteItemView: View {
|
|||||||
ChannelsCacheModel.shared.store(channel)
|
ChannelsCacheModel.shared.store(channel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case let .channelPlaylist(_, id, _):
|
case let .channelPlaylist(_, id, title):
|
||||||
if let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(id),
|
if let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(.init(id: id, title: title)),
|
||||||
!cache.videos.isEmpty
|
!cache.videos.isEmpty
|
||||||
{
|
{
|
||||||
contentItems = ContentItem.array(of: cache.videos)
|
contentItems = ContentItem.array(of: cache.videos)
|
||||||
|
@ -54,7 +54,7 @@ struct PlaylistVideosView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loadCachedResource() {
|
func loadCachedResource() {
|
||||||
if let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(playlist.id) {
|
if let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(playlist.channelPlaylist) {
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.channelPlaylist.replace(cache)
|
self.channelPlaylist.replace(cache)
|
||||||
}
|
}
|
||||||
|
@ -197,7 +197,8 @@ struct PlaylistsView: View {
|
|||||||
|
|
||||||
func loadCachedResource() {
|
func loadCachedResource() {
|
||||||
if !selectedPlaylistID.isEmpty,
|
if !selectedPlaylistID.isEmpty,
|
||||||
let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(selectedPlaylistID)
|
let currentPlaylist,
|
||||||
|
let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(currentPlaylist.channelPlaylist)
|
||||||
{
|
{
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.channelPlaylist.replace(cache)
|
self.channelPlaylist.replace(cache)
|
||||||
|
@ -18,6 +18,7 @@ struct ContentItemView: View {
|
|||||||
placeholderItem()
|
placeholderItem()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.id(item.cacheKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ViewBuilder func videoItem(_ video: Video) -> some View {
|
@ViewBuilder func videoItem(_ video: Video) -> some View {
|
||||||
|
Loading…
Reference in New Issue
Block a user