fix some bugs and add more info

This commit is contained in:
2025-07-16 00:27:33 +02:00
parent 15c448611e
commit ebc4d1ada0

View File

@@ -1,9 +1,11 @@
//TODO: Implement support for bonus chapters
//TODO: Implement scrolling support
//TODO: Implement Anilist support?
//TODO: Evict the oldest image from cache
import UIKit
let preloadCount = 10
let preloadCount = 3
enum PageTurn {
case next
@@ -30,7 +32,7 @@ struct LocalState: Codable {
var chapter: Int
var page: Int
var turnMode: PageTurnMode = .leftToRight
var backgroundColor: String = "clear"
var backgroundColor: String = "black"
}
struct Metadata: Decodable {
@@ -114,6 +116,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
homeView.translatesAutoresizingMaskIntoConstraints = false
readerView.translatesAutoresizingMaskIntoConstraints = false
readerView.isHidden = true
readerView.backgroundColor = .black
homeView.isHidden = false
view.addSubview(homeView)
view.addSubview(readerView)
@@ -172,8 +175,9 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
for dir in directories {
comics.append(
Comic(
cover: UIImage(
contentsOfFile: dir.appendingPathComponent("cover.jpg").path)!,
cover:
UIImage(
contentsOfFile: dir.appendingPathComponent("cover.jpg").path)!,
metadata: try JSONDecoder().decode(
Metadata.self,
from:
@@ -209,7 +213,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
case .black: "black"
case .red: "red"
case .blue: "blue"
default: "clear"
default: "black"
}
}
@@ -220,12 +224,12 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
case "black": .black
case "red": .red
case "blue": .blue
default: .clear
default: .black
}
}
func saveLocalState() {
let color = self.readerView.backgroundColor ?? .white
let color = readerView.backgroundColor ?? .black
localStateQueue.async {
do {
try JSONEncoder().encode(
@@ -280,13 +284,6 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
saveGlobalState()
loadLocalState()
setImages(path: path, metadata: metadata)
info.text = """
\(metadata.title)
\(metadata.chapters[currentChapter - 1].title)
Volume \(Int(metadata.chapters[currentChapter - 1].volume)) of \(Int(metadata.chapters.last!.volume))
Chapter \(currentChapter!) of \(Int(metadata.chapters.last!.chapter))
Page \(currentPage!) of \(metadata.chapters[currentChapter - 1].pages)
"""
}
}
@@ -368,7 +365,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
func setupBottomBarInfo() {
info.textAlignment = .center
info.numberOfLines = 5
info.numberOfLines = 6
info.translatesAutoresizingMaskIntoConstraints = false
info.textColor = .white
bottomBarView.addSubview(info)
@@ -689,32 +686,42 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
}
saveLocalState()
updateInfo()
}
func updateInfo() {
info.text = """
\(metadata.title)
\(metadata.chapters[currentChapter - 1].title)
Volume \(Int(metadata.chapters[currentChapter - 1].volume)) of \(Int(metadata.chapters.last!.volume))
Chapter \(currentChapter!) of \(Int(metadata.chapters.last!.chapter))
Page \(currentPage!) of \(metadata.chapters[currentChapter - 1].pages)
Image size: \(Int(imageView.image!.size.width))x\(Int(imageView.image!.size.height))
"""
}
func getImagePath(chapter: Int, volume: Int, page: Int, path: URL) -> URL! {
let modernPath = path.appendingPathComponent(String(format: "volume_%04d", volume))
.appendingPathComponent(String(format: "chapter_%04d_page_%04d.png", chapter, page))
if fileManager.fileExists(atPath: modernPath.path) {
return modernPath
}
//TODO: Remove this
let newPath = path.appendingPathComponent(String(format: "volume_%04d", volume))
.appendingPathComponent(
// TODO: it should not be necessary to sub 1
String(format: "chapter_%04d_image_%04d.png", chapter, page - 1))
if fileManager.fileExists(atPath: newPath.path) {
return newPath
}
//TODO: Remove this
let alternatePath = path.appendingPathComponent(String(format: "volume_%04d", volume))
.appendingPathComponent(
String(format: "chapter%03d_image_%03d.png", chapter, page - 1))
if !fileManager.fileExists(atPath: alternatePath.path) {
print("Did not find image at path")
return nil
if fileManager.fileExists(atPath: alternatePath.path) {
return alternatePath
}
return alternatePath
//
print("Did not find image at path")
return nil
}
func getChapterAndPageFromTurn(chapter: Int, page: Int, turn: PageTurn) -> (Int, Int) {
@@ -770,6 +777,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
) {
[weak self] image in
self?.imageView.image = image
self?.updateInfo()
}
var (chapter, page) = getChapterAndPageFromTurn(
@@ -806,13 +814,21 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
let ps: [String] = try fileManager.contentsOfDirectory(atPath: dir.path).sorted()
let current = dir.appendingPathComponent(ps[0])
imageView.image = UIImage(contentsOfFile: current.path)
updateInfo()
let path_meta = current.lastPathComponent.components(separatedBy: "_")
assert(path_meta[0] == "chapter")
currentChapter = Int(path_meta[1])!
assert(path_meta[2] == "image")
// TODO: this should not be necessary to add 1 to
currentPage = Int(path_meta[3].components(separatedBy: ".")[0])! + 1
if path_meta[2] == "page" {
currentPage = Int(path_meta[3].components(separatedBy: ".")[0])
if currentPage == nil {
print("unable to set currentPage")
}
} else {
// TODO: Remove this
assert(path_meta[2] == "image")
currentPage = Int(path_meta[3].components(separatedBy: ".")[0])! + 1
}
} catch {
print("failed to set images")
}