save state for read progress
This commit is contained in:
@@ -21,6 +21,11 @@ struct GlobalState: Codable {
|
||||
var comicName: String? = nil
|
||||
}
|
||||
|
||||
struct LocalState: Codable {
|
||||
var chapter: Int
|
||||
var page: Int
|
||||
}
|
||||
|
||||
struct Metadata: Decodable {
|
||||
var title: String
|
||||
var original_language: String
|
||||
@@ -51,10 +56,10 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
||||
var previousImage: UIImage?
|
||||
var currentImage: UIImage?
|
||||
var nextImage: UIImage?
|
||||
var currentPage = 1
|
||||
var mode = PageTurnMode.leftToRight
|
||||
var metadata: Metadata!
|
||||
var currentChapter: Int!
|
||||
var currentPage: Int! = nil
|
||||
var currentChapter: Int! = nil
|
||||
var currentPath: URL!
|
||||
|
||||
var leftTap: UITapGestureRecognizer!
|
||||
@@ -85,23 +90,6 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
||||
setup()
|
||||
}
|
||||
|
||||
func setupHomeView() {
|
||||
loadComics()
|
||||
|
||||
let layout = UICollectionViewFlowLayout()
|
||||
let spacing: CGFloat = 8
|
||||
layout.minimumInteritemSpacing = spacing
|
||||
layout.minimumLineSpacing = spacing
|
||||
|
||||
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
|
||||
collectionView.translatesAutoresizingMaskIntoConstraints = false
|
||||
collectionView.dataSource = self
|
||||
collectionView.delegate = self
|
||||
collectionView.backgroundColor = .white
|
||||
collectionView.register(ImageCell.self, forCellWithReuseIdentifier: "ImageCell")
|
||||
homeView.addSubview(collectionView)
|
||||
}
|
||||
|
||||
func setup() {
|
||||
homeView.translatesAutoresizingMaskIntoConstraints = false
|
||||
readerView.translatesAutoresizingMaskIntoConstraints = false
|
||||
@@ -118,8 +106,11 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
||||
readerView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
||||
readerView.widthAnchor.constraint(equalTo: view.widthAnchor),
|
||||
])
|
||||
loadComics()
|
||||
setupImageView()
|
||||
setupGestures()
|
||||
setupTopBar()
|
||||
if let name = globalState.comicName {
|
||||
globalState.comicName = name
|
||||
saveGlobalState()
|
||||
readComic(name: name)
|
||||
} else {
|
||||
@@ -129,6 +120,21 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
func setupHomeView() {
|
||||
let layout = UICollectionViewFlowLayout()
|
||||
let spacing: CGFloat = 8
|
||||
layout.minimumInteritemSpacing = spacing
|
||||
layout.minimumLineSpacing = spacing
|
||||
|
||||
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
|
||||
collectionView.translatesAutoresizingMaskIntoConstraints = false
|
||||
collectionView.dataSource = self
|
||||
collectionView.delegate = self
|
||||
collectionView.backgroundColor = .white
|
||||
collectionView.register(ImageCell.self, forCellWithReuseIdentifier: "ImageCell")
|
||||
homeView.addSubview(collectionView)
|
||||
}
|
||||
|
||||
func loadComics() {
|
||||
do {
|
||||
var directories: [URL] = []
|
||||
@@ -178,6 +184,29 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
func saveLocalState() {
|
||||
do {
|
||||
try JSONEncoder().encode(LocalState(chapter: currentChapter, page: currentPage)).write(
|
||||
to: currentPath.appendingPathComponent("state.json"))
|
||||
} catch {
|
||||
print("failed to save local state")
|
||||
}
|
||||
}
|
||||
|
||||
func loadLocalState() {
|
||||
do {
|
||||
let json = Data(
|
||||
try String(
|
||||
contentsOfFile: currentPath.appendingPathComponent("state.json").path
|
||||
).utf8)
|
||||
let l = try JSONDecoder().decode(LocalState.self, from: json)
|
||||
currentPage = l.page
|
||||
currentChapter = l.chapter
|
||||
} catch {
|
||||
print("failed to load local state")
|
||||
}
|
||||
}
|
||||
|
||||
func setupGestures() {
|
||||
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(_:)))
|
||||
swipeLeft.direction = .left
|
||||
@@ -197,10 +226,10 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
||||
if let path = new_path {
|
||||
currentPath = path
|
||||
metadata = getMetadata(path: path)!
|
||||
globalState.comicName = metadata.title
|
||||
saveGlobalState()
|
||||
loadLocalState()
|
||||
setImages(path: path, metadata: metadata)
|
||||
setupImageView()
|
||||
setupGestures()
|
||||
setupTopBar()
|
||||
imageView.image = currentImage
|
||||
}
|
||||
}
|
||||
@@ -518,31 +547,30 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
||||
let (chapter, page) = getChapterAndPageFromTurn(
|
||||
chapter: currentChapter, page: currentPage, turn: turn)
|
||||
if (chapter, page) == (currentChapter, currentPage) { return }
|
||||
let (c, p) = getChapterAndPageFromTurn(chapter: chapter, page: page, turn: turn)
|
||||
let vol = Int(metadata.chapters[currentChapter - 1].volume)
|
||||
switch turn {
|
||||
case .next:
|
||||
previousImage = currentImage
|
||||
currentImage = nextImage
|
||||
imageView.image = currentImage
|
||||
let (c, p) = getChapterAndPageFromTurn(chapter: chapter, page: page, turn: turn)
|
||||
nextImage = getImage(chapter: c, volume: vol, page: p, path: currentPath)
|
||||
case .previous:
|
||||
nextImage = currentImage
|
||||
currentImage = previousImage
|
||||
imageView.image = currentImage
|
||||
let (c, p) = getChapterAndPageFromTurn(chapter: chapter, page: page, turn: turn)
|
||||
previousImage = getImage(chapter: c, volume: vol, page: p, path: currentPath)
|
||||
}
|
||||
currentPage = page
|
||||
currentChapter = chapter
|
||||
imageView.image = currentImage
|
||||
saveLocalState()
|
||||
}
|
||||
|
||||
func getImage(chapter: Int, volume: Int, page: Int, path: URL) -> UIImage! {
|
||||
// let supportedExtensions = ["png", "jpg", "jpeg"]
|
||||
|
||||
var testPath = path
|
||||
testPath.appendPathComponent(String(format: "volume_%04d", volume))
|
||||
testPath.appendPathComponent(String(format: "chapter_%04d_image_%04d.png", chapter, page - 1))
|
||||
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
|
||||
@@ -594,6 +622,24 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
||||
|
||||
func setImages(path: URL, metadata: Metadata) {
|
||||
var directories: [URL] = []
|
||||
if currentPage != nil && currentChapter != nil {
|
||||
currentImage = getImage(
|
||||
chapter: currentChapter, volume: Int(metadata.chapters[currentChapter - 1].volume),
|
||||
page: currentPage, path: currentPath)
|
||||
|
||||
let (nc, np) = getChapterAndPageFromTurn(
|
||||
chapter: currentChapter, page: currentPage, turn: .next)
|
||||
nextImage = getImage(
|
||||
chapter: nc, volume: Int(metadata.chapters[nc - 1].volume),
|
||||
page: np, path: currentPath)
|
||||
|
||||
let (pc, pp) = getChapterAndPageFromTurn(
|
||||
chapter: currentChapter, page: currentPage, turn: .previous)
|
||||
previousImage = getImage(
|
||||
chapter: pc, volume: Int(metadata.chapters[pc - 1].volume),
|
||||
page: pp, path: currentPath)
|
||||
return
|
||||
}
|
||||
|
||||
do {
|
||||
let contents = try fileManager.contentsOfDirectory(
|
||||
@@ -616,6 +662,9 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
||||
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
|
||||
} catch {
|
||||
print("failed to set images")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user