summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-03-01 04:37:10 -0800
committerFuwn <[email protected]>2025-03-01 04:37:10 -0800
commitffa1c05f14400889905a450477a71443c5fe1741 (patch)
treea03b022615fb47cf1546153a0a91a58f74ad4da3
parentfeat: Development commit (diff)
downloadsora-testing-ffa1c05f14400889905a450477a71443c5fe1741.tar.xz
sora-testing-ffa1c05f14400889905a450477a71443c5fe1741.zip
feat: Development commit
-rw-r--r--Sora/Data/ImageCacheManager.swift36
-rw-r--r--Sora/Data/Settings/SettingsManager.swift4
-rw-r--r--Sora/Views/Post/Details/PostDetailsCarouselView.swift21
-rw-r--r--Sora/Views/Settings/Section/SettingsDetailsView.swift12
4 files changed, 73 insertions, 0 deletions
diff --git a/Sora/Data/ImageCacheManager.swift b/Sora/Data/ImageCacheManager.swift
new file mode 100644
index 0000000..cc5cb7e
--- /dev/null
+++ b/Sora/Data/ImageCacheManager.swift
@@ -0,0 +1,36 @@
+import Combine
+import SwiftUI
+
+class ImageCacheManager {
+ static let shared = ImageCacheManager()
+ private let cache: URLCache
+ private var cancellables: Set<AnyCancellable> = []
+
+ init() {
+ cache = URLCache(
+ memoryCapacity: 100 * 1_024 * 1_024, // 100 MB
+ diskCapacity: 500 * 1_024 * 1_024, // 500 MB
+ diskPath: "SoraImageCache"
+ )
+
+ URLCache.shared = cache
+ }
+
+ func preloadImages(_ urls: [URL]) {
+ urls.forEach { url in
+ URLSession.shared.dataTaskPublisher(for: url)
+ .sink(
+ receiveCompletion: { _ in () },
+ receiveValue: { data, response in
+ let cachedResponse = CachedURLResponse(
+ response: response,
+ data: data
+ )
+
+ self.cache.storeCachedResponse(cachedResponse, for: URLRequest(url: url))
+ }
+ )
+ .store(in: &cancellables)
+ }
+ }
+}
diff --git a/Sora/Data/Settings/SettingsManager.swift b/Sora/Data/Settings/SettingsManager.swift
index 6035de3..3c4dc77 100644
--- a/Sora/Data/Settings/SettingsManager.swift
+++ b/Sora/Data/Settings/SettingsManager.swift
@@ -41,6 +41,9 @@ class SettingsManager: ObservableObject {
var saveTagsToFile = false
#endif
+ @AppStorage("preloadedCarouselImages")
+ var preloadedCarouselImages = 3
+
var bookmarks: [SettingsBookmark] {
get {
if let bookmarks = try? JSONDecoder().decode([SettingsBookmark].self, from: bookmarksData) {
@@ -129,6 +132,7 @@ class SettingsManager: ObservableObject {
displayRatingsData = Self.defaultRatingsData()
blurRatingsData = Self.initializeRatingsData(enabledRatings: [.explicit])
displayDetailsInformationBar = true
+ preloadedCarouselImages = 3
#if os(macOS)
saveTagsToFile = false
diff --git a/Sora/Views/Post/Details/PostDetailsCarouselView.swift b/Sora/Views/Post/Details/PostDetailsCarouselView.swift
index 378b9cc..de14477 100644
--- a/Sora/Views/Post/Details/PostDetailsCarouselView.swift
+++ b/Sora/Views/Post/Details/PostDetailsCarouselView.swift
@@ -7,6 +7,7 @@ struct PostDetailsCarouselView: View {
let focusedPost: BooruPost?
@Binding var loadingStage: BooruPostLoadingState
@State private var currentIndex: Int
+ private let cacheManager = ImageCacheManager.shared
init(
posts: [BooruPost],
@@ -61,9 +62,29 @@ struct PostDetailsCarouselView: View {
}
.onChange(of: currentIndex) {
if currentIndex == posts.count - 1 { manager.loadNextPage() }
+
+ preloadNearbyImages()
}
+ .onAppear(perform: preloadNearbyImages)
#if !os(macOS)
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
#endif
}
+
+ private func preloadNearbyImages() {
+ let preloadRange = settings.preloadedCarouselImages
+ let startIndex = max(0, currentIndex - preloadRange)
+ let endIndex = min(posts.count - 1, currentIndex + preloadRange)
+ var urlsToPreload: [URL] = []
+
+ for index in startIndex...endIndex {
+ if let url = imageURL(post: posts[index]) {
+ urlsToPreload.append(url)
+ }
+
+ urlsToPreload.append(posts[index].previewURL)
+ }
+
+ cacheManager.preloadImages(urlsToPreload)
+ }
}
diff --git a/Sora/Views/Settings/Section/SettingsDetailsView.swift b/Sora/Views/Settings/Section/SettingsDetailsView.swift
index 5abd394..885ef6f 100644
--- a/Sora/Views/Settings/Section/SettingsDetailsView.swift
+++ b/Sora/Views/Settings/Section/SettingsDetailsView.swift
@@ -21,5 +21,17 @@ struct SettingsDetailsView: View {
Text("Saves post tags in a file alongside the downloaded image.")
}
#endif
+
+ #if os(macOS)
+ Picker("Preloaded Images", selection: $settings.preloadedCarouselImages) {
+ ForEach(1...10, id: \.self) { columns in Text("\(columns)") }
+ }
+ #else
+ Stepper(
+ "Preloaded Images: \(settings.preloadedCarouselImages)",
+ value: $settings.preloadedCarouselImages,
+ in: 1...10
+ )
+ #endif
}
}