2022-12-11 01:38:03 +05:30
|
|
|
import Cache
|
2021-09-25 13:48:22 +05:30
|
|
|
import Foundation
|
2022-12-11 01:38:03 +05:30
|
|
|
import Logging
|
2021-09-25 13:48:22 +05:30
|
|
|
import Siesta
|
|
|
|
import SwiftUI
|
2022-12-11 01:38:03 +05:30
|
|
|
import SwiftyJSON
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2022-12-12 14:51:46 +05:30
|
|
|
final class SubscribedChannelsModel: ObservableObject, CacheModel {
|
2022-12-11 20:45:42 +05:30
|
|
|
static var shared = SubscribedChannelsModel()
|
2022-12-11 01:38:03 +05:30
|
|
|
let logger = Logger(label: "stream.yattee.cache.channels")
|
|
|
|
|
|
|
|
static let diskConfig = DiskConfig(name: "channels")
|
|
|
|
static let memoryConfig = MemoryConfig()
|
|
|
|
|
2022-12-12 14:51:46 +05:30
|
|
|
let storage = try? Storage<String, JSON>(
|
2022-12-11 20:45:42 +05:30
|
|
|
diskConfig: SubscribedChannelsModel.diskConfig,
|
|
|
|
memoryConfig: SubscribedChannelsModel.memoryConfig,
|
2022-12-12 14:51:46 +05:30
|
|
|
transformer: BaseCacheModel.jsonTransformer
|
2022-12-11 01:38:03 +05:30
|
|
|
)
|
2022-11-25 02:06:05 +05:30
|
|
|
|
2022-12-11 17:08:57 +05:30
|
|
|
@Published var isLoading = false
|
2021-09-25 13:48:22 +05:30
|
|
|
@Published var channels = [Channel]()
|
2022-12-16 14:05:10 +05:30
|
|
|
@Published var error: RequestError?
|
|
|
|
|
2022-11-25 02:06:05 +05:30
|
|
|
var accounts: AccountsModel { .shared }
|
2021-10-17 04:18:58 +05:30
|
|
|
|
2021-10-21 03:51:50 +05:30
|
|
|
var resource: Resource? {
|
|
|
|
accounts.api.subscriptions
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var all: [Channel] {
|
|
|
|
channels.sorted { $0.name.lowercased() < $1.name.lowercased() }
|
|
|
|
}
|
|
|
|
|
|
|
|
func subscribe(_ channelID: String, onSuccess: @escaping () -> Void = {}) {
|
2021-11-15 04:36:01 +05:30
|
|
|
accounts.api.subscribe(channelID) {
|
|
|
|
self.scheduleLoad(onSuccess: onSuccess)
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func unsubscribe(_ channelID: String, onSuccess: @escaping () -> Void = {}) {
|
2021-11-15 04:36:01 +05:30
|
|
|
accounts.api.unsubscribe(channelID) {
|
|
|
|
self.scheduleLoad(onSuccess: onSuccess)
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func isSubscribing(_ channelID: String) -> Bool {
|
|
|
|
channels.contains { $0.id == channelID }
|
|
|
|
}
|
|
|
|
|
|
|
|
func load(force: Bool = false, onSuccess: @escaping () -> Void = {}) {
|
2022-12-11 17:08:57 +05:30
|
|
|
guard accounts.app.supportsSubscriptions, !isLoading, accounts.signedIn, let account = accounts.current else {
|
2021-12-20 03:57:20 +05:30
|
|
|
channels = []
|
2021-11-15 04:36:01 +05:30
|
|
|
return
|
|
|
|
}
|
2021-12-20 03:57:20 +05:30
|
|
|
|
2022-12-11 01:38:03 +05:30
|
|
|
loadCachedChannels(account)
|
|
|
|
|
2022-12-11 17:08:57 +05:30
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
guard let self else { return }
|
|
|
|
let request = force ? self.resource?.load() : self.resource?.loadIfNeeded()
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2022-12-11 17:08:57 +05:30
|
|
|
if request != nil {
|
|
|
|
self.isLoading = true
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
2022-12-11 17:08:57 +05:30
|
|
|
|
|
|
|
request?
|
|
|
|
.onCompletion { [weak self] _ in
|
|
|
|
self?.isLoading = false
|
|
|
|
}
|
|
|
|
.onSuccess { resource in
|
2022-12-16 14:05:10 +05:30
|
|
|
self.error = nil
|
2022-12-11 17:08:57 +05:30
|
|
|
if let channels: [Channel] = resource.typedContent() {
|
|
|
|
self.channels = channels
|
2022-12-14 04:37:32 +05:30
|
|
|
channels.forEach { ChannelsCacheModel.shared.storeIfMissing($0) }
|
2022-12-11 17:08:57 +05:30
|
|
|
self.storeChannels(account: account, channels: channels)
|
2022-12-13 17:44:20 +05:30
|
|
|
FeedModel.shared.calculateUnwatchedFeed()
|
2022-12-11 17:08:57 +05:30
|
|
|
onSuccess()
|
|
|
|
}
|
|
|
|
}
|
2022-12-16 14:05:10 +05:30
|
|
|
.onFailure { self.error = $0 }
|
2022-12-11 17:08:57 +05:30
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
|
2022-12-11 01:38:03 +05:30
|
|
|
func loadCachedChannels(_ account: Account) {
|
|
|
|
let cache = getChannels(account: account)
|
|
|
|
if !cache.isEmpty {
|
2022-12-11 17:08:57 +05:30
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.channels = cache
|
|
|
|
}
|
2022-12-11 01:38:03 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func storeChannels(account: Account, channels: [Channel]) {
|
2022-12-12 14:51:46 +05:30
|
|
|
let date = iso8601DateFormatter.string(from: Date())
|
2022-12-11 01:38:03 +05:30
|
|
|
logger.info("caching channels \(channelsDateCacheKey(account)) -- \(date)")
|
|
|
|
|
2022-12-14 04:37:32 +05:30
|
|
|
channels.forEach { ChannelsCacheModel.shared.storeIfMissing($0) }
|
|
|
|
|
2022-12-11 01:38:03 +05:30
|
|
|
let dateObject: JSON = ["date": date]
|
|
|
|
let channelsObject: JSON = ["channels": channels.map(\.json).map(\.object)]
|
|
|
|
|
2022-12-12 14:51:46 +05:30
|
|
|
try? storage?.setObject(dateObject, forKey: channelsDateCacheKey(account))
|
|
|
|
try? storage?.setObject(channelsObject, forKey: channelsCacheKey(account))
|
2022-12-11 01:38:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func getChannels(account: Account) -> [Channel] {
|
|
|
|
logger.info("getting channels \(channelsDateCacheKey(account))")
|
|
|
|
|
2022-12-12 14:51:46 +05:30
|
|
|
if let json = try? storage?.object(forKey: channelsCacheKey(account)),
|
2022-12-11 01:38:03 +05:30
|
|
|
let channels = json.dictionaryValue["channels"]
|
|
|
|
{
|
2023-03-01 01:33:02 +05:30
|
|
|
return channels.arrayValue.compactMap { json in
|
2022-12-14 04:37:32 +05:30
|
|
|
let channel = Channel.from(json)
|
|
|
|
if !channel.hasExtendedDetails,
|
|
|
|
let cache = ChannelsCacheModel.shared.retrieve(channel.cacheKey)
|
|
|
|
{
|
2023-03-01 01:33:02 +05:30
|
|
|
return cache.channel
|
2022-12-14 04:37:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return channel
|
|
|
|
}
|
2022-12-11 01:38:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2021-11-15 04:36:01 +05:30
|
|
|
private func scheduleLoad(onSuccess: @escaping () -> Void) {
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
|
2021-09-25 13:48:22 +05:30
|
|
|
self.load(force: true, onSuccess: onSuccess)
|
|
|
|
}
|
|
|
|
}
|
2022-12-11 01:38:03 +05:30
|
|
|
|
|
|
|
private func channelsCacheKey(_ account: Account) -> String {
|
|
|
|
"channels-\(account.id)"
|
|
|
|
}
|
|
|
|
|
|
|
|
private func channelsDateCacheKey(_ account: Account) -> String {
|
|
|
|
"channels-\(account.id)-date"
|
|
|
|
}
|
2022-12-11 17:08:57 +05:30
|
|
|
|
2022-12-12 14:51:46 +05:30
|
|
|
func getChannelsTime(account: Account) -> Date? {
|
|
|
|
if let json = try? storage?.object(forKey: channelsDateCacheKey(account)),
|
2022-12-11 17:08:57 +05:30
|
|
|
let string = json.dictionaryValue["date"]?.string,
|
2022-12-12 14:51:46 +05:30
|
|
|
let date = iso8601DateFormatter.date(from: string)
|
2022-12-11 17:08:57 +05:30
|
|
|
{
|
|
|
|
return date
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-12 14:51:46 +05:30
|
|
|
var channelsTime: Date? {
|
2022-12-11 17:08:57 +05:30
|
|
|
if let account = accounts.current {
|
2022-12-12 14:51:46 +05:30
|
|
|
return getChannelsTime(account: account)
|
2022-12-11 17:08:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var formattedCacheTime: String {
|
2022-12-12 14:51:46 +05:30
|
|
|
getFormattedDate(channelsTime)
|
2022-12-11 17:08:57 +05:30
|
|
|
}
|
2022-12-21 04:21:04 +05:30
|
|
|
|
|
|
|
func onAccountChange() {
|
|
|
|
channels = []
|
|
|
|
load(force: true)
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|