add bottom bar with info

This commit is contained in:
2025-07-15 19:27:20 +02:00
parent c755599fbc
commit 747fa39056

View File

@@ -1,3 +1,7 @@
//TODO: Implement support for bonus chapters
//TODO: Implement scrolling support
//TODO: Actually implement caching and do I/O on background thread
import UIKit
enum PageTurn {
@@ -24,7 +28,7 @@ struct GlobalState: Codable {
struct LocalState: Codable {
var chapter: Int
var page: Int
var turnMode: PageTurnMode
var turnMode: PageTurnMode = .leftToRight
var backgroundColor: String = "clear"
}
@@ -69,6 +73,8 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
var topTap: UITapGestureRecognizer!
let topBarView = UIView()
let bottomBarView = UIView()
let info = UILabel()
let pageTurnDropdownView = UIView()
let pageTurnDropdownButton = UIButton()
@@ -123,7 +129,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
loadComics()
setupImageView()
setupGestures()
setupTopBar()
setupBar()
setupHomeView()
if let name = globalState.comicName {
readComic(name: name)
@@ -261,8 +267,8 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
func readComic(name: String) {
readerView.isHidden = false
homeView.isHidden = true
let new_path = getPathFromComicName(name: name)
if let path = new_path {
setNeedsStatusBarAppearanceUpdate()
if let path = getPathFromComicName(name: name) {
currentPath = path
metadata = getMetadata(path: path)!
globalState.comicName = metadata.title
@@ -270,6 +276,12 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
loadLocalState()
setImages(path: path, metadata: metadata)
imageView.image = currentImage
info.text = """
\(metadata.chapters[currentChapter - 1].title)
Volume \(Int(metadata.chapters[currentChapter - 1].volume))
Chapter \(currentChapter!) of \(Int(metadata.chapters.last!.chapter))
Page \(currentPage!) of \(metadata.chapters[currentChapter - 1].pages)
"""
}
}
@@ -320,28 +332,51 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
topView.addGestureRecognizer(topTap)
}
func setupTopBar() {
func setupBar() {
topBarView.translatesAutoresizingMaskIntoConstraints = false
topBarView.backgroundColor = UIColor.black.withAlphaComponent(0.8) // Or any style
topBarView.backgroundColor = UIColor.black.withAlphaComponent(0.8)
topBarView.isHidden = true
readerView.addSubview(topBarView)
bottomBarView.translatesAutoresizingMaskIntoConstraints = false
bottomBarView.backgroundColor = UIColor.black.withAlphaComponent(0.8)
bottomBarView.isHidden = true
readerView.addSubview(bottomBarView)
NSLayoutConstraint.activate([
topBarView.topAnchor.constraint(equalTo: readerView.safeAreaLayoutGuide.topAnchor),
topBarView.leadingAnchor.constraint(equalTo: readerView.leadingAnchor),
topBarView.trailingAnchor.constraint(equalTo: readerView.trailingAnchor),
topBarView.heightAnchor.constraint(equalToConstant: 64),
bottomBarView.bottomAnchor.constraint(equalTo: readerView.bottomAnchor),
bottomBarView.leadingAnchor.constraint(equalTo: readerView.leadingAnchor),
bottomBarView.trailingAnchor.constraint(equalTo: readerView.trailingAnchor),
bottomBarView.heightAnchor.constraint(equalToConstant: 128),
])
setupBackgroundColorDropdown()
setupPageTurnDropdown()
setupHomeButton()
setupBottomBarInfo()
}
func setupBottomBarInfo() {
info.textAlignment = .center
info.numberOfLines = 0
info.text = ""
info.translatesAutoresizingMaskIntoConstraints = false
info.textColor = .white
bottomBarView.addSubview(info)
NSLayoutConstraint.activate([
info.centerXAnchor.constraint(equalTo: bottomBarView.centerXAnchor),
info.centerYAnchor.constraint(equalTo: bottomBarView.centerYAnchor),
])
}
func setupHomeButton() {
homeButton.setTitle("Home", for: .normal)
homeButton.setTitleColor(.white, for: .normal)
homeButton.isHidden = true
homeButton.translatesAutoresizingMaskIntoConstraints = false
topBarView.addSubview(homeButton)
@@ -349,29 +384,30 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
self, action: #selector(goHome), for: .touchDown
)
NSLayoutConstraint.activate([
homeButton.trailingAnchor.constraint(
equalTo: topBarView.trailingAnchor, constant: -32
),
homeButton.centerYAnchor.constraint(
equalTo: topBarView.centerYAnchor),
homeButton.topAnchor.constraint(
equalTo: topBarView.topAnchor),
homeButton.bottomAnchor.constraint(
equalTo: topBarView.bottomAnchor),
homeButton.trailingAnchor.constraint(equalTo: topBarView.trailingAnchor, constant: -32),
homeButton.centerYAnchor.constraint(equalTo: topBarView.centerYAnchor),
homeButton.topAnchor.constraint(equalTo: topBarView.topAnchor),
homeButton.bottomAnchor.constraint(equalTo: topBarView.bottomAnchor),
])
}
override var prefersStatusBarHidden: Bool {
return homeView.isHidden
}
@objc func goHome() {
readerView.isHidden = true
homeView.isHidden = false
globalState.comicName = nil
hideTopBar()
setNeedsStatusBarAppearanceUpdate()
saveGlobalState()
}
func setupBackgroundColorDropdown() {
backgroundColorDropdownButton.setTitle("Background color ▼", for: .normal)
backgroundColorDropdownButton.setTitleColor(.white, for: .normal)
backgroundColorDropdownButton.isHidden = true
backgroundColorDropdownButton.translatesAutoresizingMaskIntoConstraints = false
topBarView.addSubview(backgroundColorDropdownButton)
@@ -438,7 +474,6 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
func setupPageTurnDropdown() {
pageTurnDropdownButton.setTitle("Page Turn Mode ▼", for: .normal)
pageTurnDropdownButton.setTitleColor(.white, for: .normal)
pageTurnDropdownButton.isHidden = true
pageTurnDropdownButton.translatesAutoresizingMaskIntoConstraints = false
topBarView.addSubview(pageTurnDropdownButton)
@@ -552,10 +587,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
func toggleTopBar() {
topBarView.isHidden.toggle()
pageTurnDropdownButton.isHidden = topBarView.isHidden
backgroundColorDropdownButton.isHidden = topBarView.isHidden
homeButton.isHidden = topBarView.isHidden
bottomBarView.isHidden.toggle()
if topBarView.isHidden {
pageTurnDropdownView.isHidden = true
@@ -563,6 +595,12 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
}
}
func hideTopBar() {
topBarView.isHidden = true
pageTurnDropdownView.isHidden = true
backgroundColorDropdownView.isHidden = true
}
@objc func handleTopTap() {
toggleTopBar()
}
@@ -624,31 +662,45 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
let vol = Int(metadata.chapters[currentChapter - 1].volume)
switch turn {
case .next:
if nextImage == nil { return }
previousImage = currentImage
currentImage = nextImage
nextImage = getImage(chapter: c, volume: vol, page: p, path: currentPath)
nextImage = getImage(chapter: c, volume: vol, page: p, path: self.currentPath)
case .previous:
if previousImage == nil { return }
nextImage = currentImage
currentImage = previousImage
previousImage = getImage(chapter: c, volume: vol, page: p, path: currentPath)
previousImage = getImage(chapter: c, volume: vol, page: p, path: self.currentPath)
}
currentPage = page
currentChapter = chapter
imageView.image = currentImage
saveLocalState()
info.text = """
\(metadata.chapters[currentChapter - 1].title)
Volume \(Int(metadata.chapters[currentChapter - 1].volume))
Chapter \(currentChapter!) of \(Int(metadata.chapters.last!.chapter))
Page \(currentPage!) of \(metadata.chapters[currentChapter - 1].pages)
"""
}
func getImage(chapter: Int, volume: Int, page: Int, path: URL) -> UIImage! {
var testPath = path
testPath.appendPathComponent(String(format: "volume_%04d", volume))
testPath.appendPathComponent(
// TODO: it should not be necessary to sub 1
String(format: "chapter_%04d_image_%04d.png", chapter, page - 1))
if !fileManager.fileExists(atPath: testPath.path) {
print("Did not find image at path")
return nil
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) {
//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
}
return UIImage(contentsOfFile: alternatePath.path)!
}
return UIImage(contentsOfFile: testPath.path)!
return UIImage(contentsOfFile: newPath.path)!
}
func getChapterAndPageFromTurn(chapter: Int, page: Int, turn: PageTurn) -> (Int, Int) {
@@ -668,7 +720,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
if chapter > 1 {
return (
chapter - 1,
metadata.chapters[chapter - 1].pages
metadata.chapters[chapter - 2].pages
)
} else {
return (chapter, page)
@@ -797,7 +849,6 @@ extension ViewController: UICollectionViewDataSource, UICollectionViewDelegateFl
return cell
}
// Size for cells in a grid layout
func collectionView(
_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath