diff --git a/ImageViewer/ViewController.swift b/ImageViewer/ViewController.swift index 02279e8..13b8b46 100644 --- a/ImageViewer/ViewController.swift +++ b/ImageViewer/ViewController.swift @@ -193,8 +193,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { metadataList[dir] = metadata loadLocalState() let volCovP = ProgressIndices(v: progress.v, c: 0, i: 0) - let volumeDir = currentPath.appendingPathComponent("volumes") - let archivePath = volumeDir.appendingPathComponent(metadata.volumes[volCovP.v].archive) + let archivePath = getArchiveURL(volCovP.v) let archive = try! Archive(url: archivePath, accessMode: .read) let filename = metadata.volumes[volCovP.v].chapters[volCovP.c].images[volCovP.i].filename! let targetEntry = archive[filename]! @@ -697,37 +696,31 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { } func readComic(name: String) { + imageView.image = nil readerView.isHidden = false homeView.isHidden = true setNeedsStatusBarAppearanceUpdate() if let path = getPathFromComicName(name: name) { currentPath = path - metadata = metadataList[path] globalState.comicName = metadata.title saveGlobalState() loadLocalState() - if mode != .scroll { - scrollingCollectionView.isHidden = true - leftView.isHidden = false - rightView.isHidden = false - setImages(path: path) + scrollingCollectionView.isHidden = mode != .scroll + leftView.isHidden = mode == .scroll + rightView.isHidden = mode == .scroll + imageView.isHidden = mode == .scroll + if mode == .scroll { + scrollingCollectionView.reloadData() } else { - setupScrolling() + setImages(path: path) } } else { print("path not found") } } - func setupScrolling() { - leftView.isHidden = true - rightView.isHidden = true - scrollingCollectionView.isHidden = false - scrollingCollectionView.reloadData() - } - func countFiles() -> Int { var count = 0 if let enumerator = fileManager.enumerator( @@ -1044,10 +1037,8 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { default: break } if prev == mode { return } - if mode == .scroll { - setupScrolling() - } else { - let archiveURL = currentPath.appendingPathComponent("volumes").appendingPathComponent(metadata.volumes[progress.v].archive) + if mode != .scroll { + let archiveURL = getArchiveURL(progress.v) imageLoader.loadImage( archiveURL: archiveURL, filename: getImagePath(progress: progress), @@ -1062,6 +1053,10 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { leftView.isHidden = mode == .scroll rightView.isHidden = mode == .scroll imageView.isHidden = mode == .scroll + // This does not optmize for the case where you switch between scrolling and non-scrolling in the same comic. However this badly supported because of the lac of state conversion. + if mode == .scroll { + scrollingCollectionView.reloadData() + } togglePageTurnDropdown() saveLocalState() } @@ -1194,16 +1189,15 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { imageLoader.setActiveWindow([path]) - let archiveURL = currentPath.appendingPathComponent("volumes").appendingPathComponent(metadata.volumes[progress.v].archive) + let archiveURL = getArchiveURL(progress.v) imageLoader.loadImage(archiveURL: archiveURL, filename: getImagePath(progress: progress), scaling: scaling, screenSize: UIScreen.main.bounds.size) { [weak self] image in self?.imageView.image = image } for _ in 0 ..< preloadCount { newProgress = getProgressIndicesFromTurn(turn: .next, progress: newProgress) - let archive = metadata.volumes[newProgress.v].archive! let filename = getImagePath(progress: newProgress) - let archiveURL = currentPath.appendingPathComponent("volumes").appendingPathComponent(archive) + let archiveURL = getArchiveURL(newProgress.v) imageLoader.preloadImage(archiveURL: archiveURL, filename: filename, scaling: scaling, screenSize: UIScreen.main.bounds.size) } @@ -1273,6 +1267,10 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { info.text = text } + func getArchiveURL(_ volumeProgress: Int) -> URL { + return currentPath.appendingPathComponent("volumes").appendingPathComponent(metadata.volumes[volumeProgress].archive) + } + func getImagePath(progress: ProgressIndices) -> String { let (v, c, i) = (progress.v, progress.c, progress.i) let modernPath = metadata.volumes[v].chapters[c].images[i].filename! @@ -1326,7 +1324,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { func setImages(path _: URL) { let scaling: UIView.ContentMode = mode == .scroll ? .scaleAspectFill : .scaleAspectFit imageLoader.loadImage( - archiveURL: currentPath.appendingPathComponent("volumes").appendingPathComponent(metadata.volumes[progress.v].archive), + archiveURL: getArchiveURL(progress.v), filename: getImagePath(progress: progress), scaling: scaling, screenSize: UIScreen.main.bounds.size @@ -1338,7 +1336,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { var newProgress = progress for _ in 0 ..< preloadCount { newProgress = getProgressIndicesFromTurn(turn: .next, progress: newProgress) - imageLoader.preloadImage(archiveURL: currentPath.appendingPathComponent("volumes").appendingPathComponent(metadata.volumes[progress.v].archive), filename: getImagePath(progress: newProgress), scaling: scaling, screenSize: UIScreen.main.bounds.size) + imageLoader.preloadImage(archiveURL: getArchiveURL(progress.v), filename: getImagePath(progress: newProgress), scaling: scaling, screenSize: UIScreen.main.bounds.size) } } @@ -1357,7 +1355,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { super.viewWillTransition(to: size, with: coordinator) if mode != .scroll { imageLoader.loadImage( - archiveURL: currentPath.appendingPathComponent("volumes").appendingPathComponent(metadata.volumes[progress.v].archive), + archiveURL: getArchiveURL(progress.v), filename: getImagePath(progress: progress), scaling: .scaleAspectFit, screenSize: size @@ -1365,7 +1363,6 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { self.imageView.image = image } } else { - scrollingCollectionView.reloadData() changedOrientation = true } } @@ -1453,7 +1450,7 @@ extension ViewController: UICollectionViewDataSource, UICollectionViewDelegateFl } } imageLoader.loadImage( - archiveURL: currentPath.appendingPathComponent("volumes").appendingPathComponent(metadata.volumes[progress.v].archive), + archiveURL: getArchiveURL(progress.v), filename: getImagePath(progress: newProgress), scaling: scaling, screenSize: UIScreen.main.bounds.size) { image in cell.imageView.image = image