diff options
| author | Fuwn <[email protected]> | 2025-07-11 06:32:46 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-07-11 06:32:46 -0700 |
| commit | 2e079945cb3c68ed05750757b59f0acc59155010 (patch) | |
| tree | 23901f3b5ef4956f7983bccc2922b9b9378dfcbb /Sora/Data | |
| parent | feat: Development commit (diff) | |
| download | sora-testing-2e079945cb3c68ed05750757b59f0acc59155010.tar.xz sora-testing-2e079945cb3c68ed05750757b59f0acc59155010.zip | |
feat: Development commit
Diffstat (limited to 'Sora/Data')
| -rw-r--r-- | Sora/Data/CollectionPickerOption.swift | 9 | ||||
| -rw-r--r-- | Sora/Data/Danbooru/DanbooruPostParser.swift | 4 | ||||
| -rw-r--r-- | Sora/Data/ImageCacheManager.swift | 25 | ||||
| -rw-r--r-- | Sora/Data/Scroll/ScrollPositionPreference.swift | 6 | ||||
| -rw-r--r-- | Sora/Data/Scroll/ScrollPositionPreferenceKey.swift | 19 | ||||
| -rw-r--r-- | Sora/Data/Settings/SettingsManager.swift | 7 |
6 files changed, 25 insertions, 45 deletions
diff --git a/Sora/Data/CollectionPickerOption.swift b/Sora/Data/CollectionPickerOption.swift index 008d559..751e71c 100644 --- a/Sora/Data/CollectionPickerOption.swift +++ b/Sora/Data/CollectionPickerOption.swift @@ -18,16 +18,17 @@ enum CollectionPickerOption: Identifiable, Hashable { } } - var name: (_ settings: SettingsManager) -> String { + @MainActor + func name(settings: SettingsManager) -> String { switch self { case .all: - return { _ in "All" } + return "All" case .folder(let id): - return { settings in settings.folderName(forID: id) ?? "Unknown Folder" } + return settings.folderName(forID: id) ?? "Unknown Folder" case .uncategorized: - return { _ in "Uncategorised" } + return "Uncategorised" } } } diff --git a/Sora/Data/Danbooru/DanbooruPostParser.swift b/Sora/Data/Danbooru/DanbooruPostParser.swift index fa62b5b..73db0cc 100644 --- a/Sora/Data/Danbooru/DanbooruPostParser.swift +++ b/Sora/Data/Danbooru/DanbooruPostParser.swift @@ -11,7 +11,7 @@ class DanbooruPostParser { let decoder = JSONDecoder() decoder.dateDecodingStrategy = .custom { decoder in - self.parseDate( + Self.parseDate( try (try decoder.singleValueContainer()).decode(String.self) ) ?? Date() } @@ -25,7 +25,7 @@ class DanbooruPostParser { } } - private func parseDate(_ input: String) -> Date? { + private static func parseDate(_ input: String) -> Date? { let isoFormatter = ISO8601DateFormatter() isoFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds] diff --git a/Sora/Data/ImageCacheManager.swift b/Sora/Data/ImageCacheManager.swift index ed68c67..8b886df 100644 --- a/Sora/Data/ImageCacheManager.swift +++ b/Sora/Data/ImageCacheManager.swift @@ -1,6 +1,7 @@ import Combine import SwiftUI +@MainActor final class ImageCacheManager { // MARK: - Singleton static let shared = ImageCacheManager() @@ -13,9 +14,6 @@ final class ImageCacheManager { .appendingPathComponent("SoraImageCache", isDirectory: true) ) private var cancellables = Set<AnyCancellable>() - private let cancellablesQueue = DispatchQueue( - label: "\(Bundle.main.bundleIdentifier!).ImageCacheManager.cancellablesQueue" - ) private let downloadQueue = OperationQueue() // MARK: - Initialisation @@ -25,16 +23,19 @@ final class ImageCacheManager { // MARK: - Public Methods func preloadImages(_ urls: [URL]) { - urls.forEach { url in + for url in urls { downloadQueue.addOperation { - self.cancellablesQueue.sync { - URLSession.shared.dataTaskPublisher(for: url) - .map { CachedURLResponse(response: $0.response, data: $0.data) } - .sink( - receiveCompletion: { _ in () }, - receiveValue: { [self] in cache.storeCachedResponse($0, for: URLRequest(url: url)) } - ) - .store(in: &self.cancellables) + let cancellable = URLSession.shared.dataTaskPublisher(for: url) + .map { CachedURLResponse(response: $0.response, data: $0.data) } + .sink( + receiveCompletion: { _ in () }, + receiveValue: { [weak self] cachedResponse in + self?.cache.storeCachedResponse(cachedResponse, for: URLRequest(url: url)) + } + ) + + DispatchQueue.main.async { [weak self] in + self?.cancellables.insert(cancellable) } } } diff --git a/Sora/Data/Scroll/ScrollPositionPreference.swift b/Sora/Data/Scroll/ScrollPositionPreference.swift deleted file mode 100644 index fb36bb1..0000000 --- a/Sora/Data/Scroll/ScrollPositionPreference.swift +++ /dev/null @@ -1,6 +0,0 @@ -import SwiftUI - -struct ScrollPositionPreference: Equatable { - let id: BooruPost.ID - let yPosition: CGFloat -} diff --git a/Sora/Data/Scroll/ScrollPositionPreferenceKey.swift b/Sora/Data/Scroll/ScrollPositionPreferenceKey.swift deleted file mode 100644 index 6b78bdc..0000000 --- a/Sora/Data/Scroll/ScrollPositionPreferenceKey.swift +++ /dev/null @@ -1,19 +0,0 @@ -import SwiftUI - -struct ScrollPositionPreferenceKey: PreferenceKey { - typealias Value = ScrollPositionPreference? - - static var defaultValue: Value = nil - - static func reduce(value: inout Value, nextValue: () -> Value) { - guard let next = nextValue() else { return } - - if let current = value { - if abs(next.yPosition) < abs(current.yPosition) { - value = next - } - } else { - value = next - } - } -} diff --git a/Sora/Data/Settings/SettingsManager.swift b/Sora/Data/Settings/SettingsManager.swift index 58583f1..435df1c 100644 --- a/Sora/Data/Settings/SettingsManager.swift +++ b/Sora/Data/Settings/SettingsManager.swift @@ -2,6 +2,7 @@ import SwiftUI +@MainActor class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_length // MARK: - Stored Properties @AppStorage("detailViewType") @@ -34,7 +35,7 @@ class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_l @AppStorage("uniformThumbnailGrid") private var _uniformThumbnailGrid: Bool = false - private var syncObservation: NSObjectProtocol? + @preconcurrency private var syncObservation: (any NSObjectProtocol & Sendable)? #if os(macOS) @AppStorage("saveTagsToFile") @@ -262,7 +263,9 @@ class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_l object: NSUbiquitousKeyValueStore.default, queue: .main ) { [weak self] _ in - self?.syncFromCloud() + Task { @MainActor in + self?.syncFromCloud() + } } loadBookmarksCache() |