mirror of
https://github.com/yattee/yattee.git
synced 2025-08-09 20:24:06 +00:00
Feed count model
This commit is contained in:
@@ -10,9 +10,8 @@ final class FeedModel: ObservableObject, CacheModel {
|
||||
@Published var isLoading = false
|
||||
@Published var videos = [Video]()
|
||||
@Published private var page = 1
|
||||
@Published var unwatched = [Account: Int]()
|
||||
@Published var unwatchedByChannel = [Account: [Channel.ID: Int]]()
|
||||
|
||||
private var feedCount = UnwatchedFeedCountModel.shared
|
||||
private var cacheModel = FeedCacheModel.shared
|
||||
private var accounts = AccountsModel.shared
|
||||
|
||||
@@ -125,12 +124,12 @@ final class FeedModel: ObservableObject, CacheModel {
|
||||
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
guard let self else { return }
|
||||
if unwatchedCount != self.unwatched[account] {
|
||||
self.unwatched[account] = unwatchedCount
|
||||
if unwatchedCount != self.feedCount.unwatched[account] {
|
||||
self.feedCount.unwatched[account] = unwatchedCount
|
||||
}
|
||||
|
||||
let byChannel = Dictionary(grouping: unwatched) { $0.channel.id }.mapValues(\.count)
|
||||
self.unwatchedByChannel[account] = byChannel
|
||||
self.feedCount.unwatchedByChannel[account] = byChannel
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -156,13 +155,13 @@ final class FeedModel: ObservableObject, CacheModel {
|
||||
|
||||
var canMarkAllFeedAsWatched: Bool {
|
||||
guard let account = accounts.current, accounts.signedIn else { return false }
|
||||
return (unwatched[account] ?? 0) > 0
|
||||
return (feedCount.unwatched[account] ?? 0) > 0
|
||||
}
|
||||
|
||||
func canMarkChannelAsWatched(_ channelID: Channel.ID) -> Bool {
|
||||
guard let account = accounts.current, accounts.signedIn else { return false }
|
||||
|
||||
return unwatchedByChannel[account]?.keys.contains(channelID) ?? false
|
||||
return feedCount.unwatchedByChannel[account]?.keys.contains(channelID) ?? false
|
||||
}
|
||||
|
||||
func markChannelAsWatched(_ channelID: Channel.ID) {
|
||||
@@ -256,7 +255,7 @@ final class FeedModel: ObservableObject, CacheModel {
|
||||
|
||||
var canPlayUnwatchedFeed: Bool {
|
||||
guard let account = accounts.current, accounts.signedIn else { return false }
|
||||
return (unwatched[account] ?? 0) > 0
|
||||
return (feedCount.unwatched[account] ?? 0) > 0
|
||||
}
|
||||
|
||||
var feedTime: Date? {
|
||||
|
32
Model/UnwatchedFeedCountModel.swift
Normal file
32
Model/UnwatchedFeedCountModel.swift
Normal file
@@ -0,0 +1,32 @@
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
final class UnwatchedFeedCountModel: ObservableObject {
|
||||
static let shared = UnwatchedFeedCountModel()
|
||||
|
||||
@Published var unwatched = [Account: Int]()
|
||||
@Published var unwatchedByChannel = [Account: [Channel.ID: Int]]()
|
||||
|
||||
private var accounts = AccountsModel.shared
|
||||
|
||||
var unwatchedText: Text? {
|
||||
if let account = accounts.current,
|
||||
!account.anonymous,
|
||||
let count = unwatched[account]
|
||||
{
|
||||
return Text(String(count))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func unwatchedByChannelText(_ channel: Channel) -> Text? {
|
||||
if let account = accounts.current,
|
||||
!account.anonymous,
|
||||
let count = unwatchedByChannel[account]?[channel.id]
|
||||
{
|
||||
return Text(String(count))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user