Tusker/Tusker/Screens/Gallery/ImageGalleryDataSource.swift

77 lines
2.3 KiB
Swift
Raw Normal View History

2024-03-19 18:58:51 +00:00
//
// ImageGalleryDataSource.swift
// Tusker
//
// Created by Shadowfacts on 3/18/24.
// Copyright © 2024 Shadowfacts. All rights reserved.
//
import Foundation
import GalleryVC
import TuskerComponents
class ImageGalleryDataSource: GalleryDataSource {
let url: URL
let cache: ImageCache
let sourceView: UIView
init(url: URL, cache: ImageCache, sourceView: UIView) {
self.url = url
self.cache = cache
self.sourceView = sourceView
}
func galleryItemsCount() -> Int {
1
}
func galleryContentViewController(forItemAt index: Int) -> any GalleryVC.GalleryContentViewController {
if let entry = cache.get(url, loadOriginal: true) {
let gifController: GIFController? =
if url.pathExtension == "gif",
let data = entry.data {
GIFController(gifData: data)
} else {
nil
}
return ImageGalleryContentViewController(
url: url,
caption: nil,
originalData: entry.data,
image: entry.image,
gifController: gifController
)
} else {
return LoadingGalleryContentViewController {
let (data, image) = await self.cache.get(self.url, loadOriginal: true)
if let image {
let gifController: GIFController? =
if self.url.pathExtension == "gif",
let data {
GIFController(gifData: data)
} else {
nil
}
return ImageGalleryContentViewController(
url: self.url,
caption: nil,
originalData: data,
image: image,
gifController: gifController
)
} else {
return nil
}
}
}
}
func galleryContentTransitionSourceView(forItemAt index: Int) -> UIView? {
sourceView
}
func galleryApplicationActivities(forItemAt index: Int) -> [UIActivity]? {
[SaveToPhotosActivity()]
}
}