ensure info is present when starting to read

This commit is contained in:
2025-08-05 00:24:56 +02:00
parent 3ba35d0e49
commit 6df2175372

View File

@@ -404,6 +404,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
} catch { } catch {
print("Encoding or writing failed:", error) print("Encoding or writing failed:", error)
} }
self.updateInfo()
} }
} }
@@ -731,8 +732,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
page: currentPage, page: currentPage,
path: currentPath), path: currentPath),
scaling: .scaleAspectFit scaling: .scaleAspectFit
) { ) { [weak self] image in
[weak self] image in
self?.imageView.image = image self?.imageView.image = image
} }
} }
@@ -767,18 +767,6 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
backgroundColorDropdownView.isHidden.toggle() backgroundColorDropdownView.isHidden.toggle()
} }
// func gestureRecognizer(
// _ gestureRecognizer: UIGestureRecognizer,
// shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer
// ) -> Bool {
// if gestureRecognizer == leftTap || gestureRecognizer == rightTap,
// otherGestureRecognizer == topTap
// {
// return true
// }
// return false
// }
func gestureRecognizer( func gestureRecognizer(
_ gestureRecognizer: UIGestureRecognizer, _ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer
@@ -877,17 +865,16 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
chapter: currentChapter, page: currentPage, turn: turn) chapter: currentChapter, page: currentPage, turn: turn)
if (chapter, page) == (currentChapter, currentPage) { return } if (chapter, page) == (currentChapter, currentPage) { return }
var vol = Int(metadata.chapters[chapter - 1].volume) var vol = Int(metadata.chapters[chapter - 1].volume)
currentPage = page
currentChapter = chapter
if let path = getImagePath(chapter: chapter, volume: vol, page: page, path: currentPath) { if let path = getImagePath(chapter: chapter, volume: vol, page: page, path: currentPath) {
imageLoader.loadImage(at: path, scaling: scaling) { imageLoader.loadImage(at: path, scaling: scaling) { [weak self] image in
[weak self] image in
self?.imageView.image = image self?.imageView.image = image
self?.updateInfo() self?.updateInfo()
} }
} else { } else {
return return
} }
currentPage = page
currentChapter = chapter
for _ in 0...preloadCount { for _ in 0...preloadCount {
(chapter, page) = getChapterAndPageFromTurn( (chapter, page) = getChapterAndPageFromTurn(
@@ -921,10 +908,15 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
Chapter \(currentChapter!) of \(Int(metadata.last_chapter)) Chapter \(currentChapter!) of \(Int(metadata.last_chapter))
Page \(currentPage!) of \(metadata.chapters[currentChapter - 1].pages) Page \(currentPage!) of \(metadata.chapters[currentChapter - 1].pages)
""" """
// if let size = imageLoader.originalImage.cgImage { if let size = imageLoader.size[
// text += getImagePath(
// "\nImage size: \(Int(size.width))x\(Int(size.height))" chapter: currentChapter, volume: Int(metadata.chapters[currentChapter - 1].volume),
// } page: currentPage,
path: currentPath
).path]
{
text += "\nImage size: \(Int(size.width))x\(Int(size.height))"
}
info.text = text info.text = text
} }
@@ -989,8 +981,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
chapter: currentChapter, volume: vol, page: currentPage, path: currentPath chapter: currentChapter, volume: vol, page: currentPage, path: currentPath
), ),
scaling: scaling scaling: scaling
) { ) { [weak self] image in
[weak self] image in
self?.imageView.image = image self?.imageView.image = image
self?.updateInfo() self?.updateInfo()
} }
@@ -1277,9 +1268,10 @@ enum Rotation {
} }
class ImageLoader { class ImageLoader {
let cache = NSCache<NSString, UIImage>() private let cache = NSCache<NSString, UIImage>()
let horCache = NSCache<NSString, UIImage>() private let horCache = NSCache<NSString, UIImage>()
private var loadingTasks: [String: [((UIImage?) -> Void)]] = [:] private var loadingTasks: [String: [((UIImage?) -> Void)]] = [:]
var size: [String: CGSize] = [:]
init() { init() {
// 128 MiB // 128 MiB
@@ -1324,6 +1316,7 @@ class ImageLoader {
queue.async { [weak self] in queue.async { [weak self] in
guard let self = self else { return } guard let self = self else { return }
guard var image = UIImage(contentsOfFile: path.path) else { return } guard var image = UIImage(contentsOfFile: path.path) else { return }
self.size[path.path] = image.size
// If you turn pages fast, completion will not be nil and as such only the needed scaled image should be prepared // If you turn pages fast, completion will not be nil and as such only the needed scaled image should be prepared
if completion == nil || rotation == .vertical { if completion == nil || rotation == .vertical {
@@ -1351,7 +1344,7 @@ class ImageLoader {
} }
DispatchQueue.main.async { DispatchQueue.main.async {
self.loadingTasks[path.path]?.forEach { $0((image)) } self.loadingTasks[path.path]?.forEach { $0(image) }
self.loadingTasks.removeValue(forKey: path.path) self.loadingTasks.removeValue(forKey: path.path)
} }
} }