fix regression related to scrollPos
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
buildServer.json
|
buildServer.json
|
||||||
.swiftlint.yml
|
.swiftlint.yml
|
||||||
|
/ImageViewer.xcodeproj/xcuserdata
|
||||||
|
@@ -1,7 +1,9 @@
|
|||||||
//TODO: Implement support for bonus chapters
|
//TODO: Support for bonus chapters
|
||||||
//TODO: Implement Anilist support?
|
//TODO: Anilist support?
|
||||||
//TODO: Properly avoid swallowing of input from UICollectionView used for scrolling
|
//TODO: Properly avoid swallowing of input from UICollectionView used for scrolling
|
||||||
//TODO: Convert between state for normal and scrolling page turn
|
//TODO: Convert between state for normal and scrolling page turn
|
||||||
|
//TODO: Support reading with scrolling and landscape mode
|
||||||
|
//FIXME: Update comicCollectionView when switching between landscape and portrait mode
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
@@ -82,6 +84,8 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
|||||||
var currentChapter: Int! = nil
|
var currentChapter: Int! = nil
|
||||||
var currentPath: URL!
|
var currentPath: URL!
|
||||||
|
|
||||||
|
var changedOrientation = false
|
||||||
|
|
||||||
var leftTap: UITapGestureRecognizer!
|
var leftTap: UITapGestureRecognizer!
|
||||||
var rightTap: UITapGestureRecognizer!
|
var rightTap: UITapGestureRecognizer!
|
||||||
var topTap: UITapGestureRecognizer!
|
var topTap: UITapGestureRecognizer!
|
||||||
@@ -247,13 +251,18 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
|||||||
|
|
||||||
func saveLocalState() {
|
func saveLocalState() {
|
||||||
let color = readerView.backgroundColor ?? .black
|
let color = readerView.backgroundColor ?? .black
|
||||||
|
let screenSize = UIScreen.main.bounds.size
|
||||||
|
var scrollOffset = scrollingCollectionView.contentOffset
|
||||||
|
if screenSize.width > screenSize.height {
|
||||||
|
scrollOffset.y *= (screenSize.height / screenSize.width)
|
||||||
|
}
|
||||||
let progress: ReadProgress =
|
let progress: ReadProgress =
|
||||||
switch self.mode {
|
switch self.mode {
|
||||||
case .leftToRight:
|
case .leftToRight:
|
||||||
ReadProgress.leftToRight(chapter: self.currentChapter, page: self.currentPage)
|
ReadProgress.leftToRight(chapter: currentChapter, page: currentPage)
|
||||||
case .rightToLeft:
|
case .rightToLeft:
|
||||||
ReadProgress.rightToLeft(chapter: self.currentChapter, page: self.currentPage)
|
ReadProgress.rightToLeft(chapter: currentChapter, page: currentPage)
|
||||||
case .scroll: ReadProgress.scroll(scrollingCollectionView.contentOffset)
|
case .scroll: ReadProgress.scroll(scrollOffset)
|
||||||
}
|
}
|
||||||
queue.async {
|
queue.async {
|
||||||
do {
|
do {
|
||||||
@@ -278,27 +287,27 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
|||||||
let local = try JSONDecoder().decode(LocalState.self, from: json)
|
let local = try JSONDecoder().decode(LocalState.self, from: json)
|
||||||
switch local.progress {
|
switch local.progress {
|
||||||
case .leftToRight(let chapter, let page):
|
case .leftToRight(let chapter, let page):
|
||||||
self.currentChapter = chapter
|
currentChapter = chapter
|
||||||
self.currentPage = page
|
currentPage = page
|
||||||
self.mode = .leftToRight
|
mode = .leftToRight
|
||||||
case .rightToLeft(let chapter, let page):
|
case .rightToLeft(let chapter, let page):
|
||||||
self.currentChapter = chapter
|
currentChapter = chapter
|
||||||
self.currentPage = page
|
currentPage = page
|
||||||
self.mode = .rightToLeft
|
mode = .rightToLeft
|
||||||
case .scroll(let point):
|
case .scroll(let point):
|
||||||
if self.scrollPos == nil {
|
if scrollPos == nil {
|
||||||
self.scrollPos = point
|
scrollPos = point
|
||||||
let centerPoint = CGPoint(
|
let screenSize = UIScreen.main.bounds.size
|
||||||
x: scrollingCollectionView.bounds.midX
|
var scrollOffset = point
|
||||||
+ scrollingCollectionView.contentOffset.x,
|
if screenSize.width > screenSize.height {
|
||||||
y: scrollingCollectionView.bounds.midY
|
scrollOffset.y *= (screenSize.width / screenSize.height)
|
||||||
+ scrollingCollectionView.contentOffset.y)
|
}
|
||||||
|
if let indexPath = scrollingCollectionView.indexPathForItem(at: scrollOffset) {
|
||||||
if let indexPath = scrollingCollectionView.indexPathForItem(at: centerPoint) {
|
|
||||||
let (chapter, page) = chapterAndPages[indexPath.item]
|
let (chapter, page) = chapterAndPages[indexPath.item]
|
||||||
currentChapter = chapter
|
currentChapter = chapter
|
||||||
currentPage = page
|
currentPage = page
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
self.mode = .scroll
|
self.mode = .scroll
|
||||||
}
|
}
|
||||||
@@ -338,11 +347,25 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
|||||||
|
|
||||||
override func viewDidLayoutSubviews() {
|
override func viewDidLayoutSubviews() {
|
||||||
super.viewDidLayoutSubviews()
|
super.viewDidLayoutSubviews()
|
||||||
if scrollingCollectionView.isHidden == false && mode == .scroll
|
if scrollingCollectionView.isHidden == true || mode != .scroll
|
||||||
&& !hasSetContentOffset
|
|| scrollingCollectionView.contentSize == .zero
|
||||||
{
|
{
|
||||||
scrollingCollectionView.setContentOffset(scrollPos, animated: false)
|
return
|
||||||
|
}
|
||||||
|
if !hasSetContentOffset && scrollPos != nil {
|
||||||
|
let screenSize = UIScreen.main.bounds.size
|
||||||
|
var offset = scrollPos!
|
||||||
|
if screenSize.width > screenSize.height {
|
||||||
|
offset.y *= (screenSize.width / screenSize.height)
|
||||||
|
}
|
||||||
|
scrollingCollectionView.setContentOffset(offset, animated: false)
|
||||||
hasSetContentOffset = true
|
hasSetContentOffset = true
|
||||||
|
} else if changedOrientation {
|
||||||
|
changedOrientation = false
|
||||||
|
let screenSize = UIScreen.main.bounds.size
|
||||||
|
var offset = scrollingCollectionView.contentOffset
|
||||||
|
offset.y *= (screenSize.width / screenSize.height)
|
||||||
|
scrollingCollectionView.setContentOffset(offset, animated: false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -972,7 +995,10 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setImages(path: URL, metadata: Metadata) {
|
func setImages(path: URL, metadata: Metadata) {
|
||||||
let scaling = UIView.ContentMode.scaleAspectFit
|
let scaling: UIView.ContentMode =
|
||||||
|
if mode == .scroll {
|
||||||
|
.scaleAspectFill
|
||||||
|
} else { .scaleAspectFit }
|
||||||
var directories: [URL] = []
|
var directories: [URL] = []
|
||||||
if currentPage != nil && currentChapter != nil {
|
if currentPage != nil && currentChapter != nil {
|
||||||
var vol = Int(metadata.chapters[currentChapter - 1].volume)
|
var vol = Int(metadata.chapters[currentChapter - 1].volume)
|
||||||
@@ -1051,7 +1077,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
|||||||
to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator
|
to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator
|
||||||
) {
|
) {
|
||||||
super.viewWillTransition(to: size, with: coordinator)
|
super.viewWillTransition(to: size, with: coordinator)
|
||||||
|
if mode != .scroll {
|
||||||
self.imageLoader.loadImage(
|
self.imageLoader.loadImage(
|
||||||
at: self.getImagePath(
|
at: self.getImagePath(
|
||||||
chapter: self.currentChapter,
|
chapter: self.currentChapter,
|
||||||
@@ -1062,6 +1088,10 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate {
|
|||||||
) { image in
|
) { image in
|
||||||
self.imageView.image = image
|
self.imageView.image = image
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
scrollingCollectionView.reloadData()
|
||||||
|
changedOrientation = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1328,8 +1358,10 @@ class ImageLoader {
|
|||||||
let vScaleImage = resizeImage(image, to: vScaledSize)
|
let vScaleImage = resizeImage(image, to: vScaledSize)
|
||||||
guard let cost = imageByteSize(vScaleImage) else { return }
|
guard let cost = imageByteSize(vScaleImage) else { return }
|
||||||
self.cache.setObject(vScaleImage, forKey: path.path as NSString, cost: cost)
|
self.cache.setObject(vScaleImage, forKey: path.path as NSString, cost: cost)
|
||||||
|
if rotation == .vertical {
|
||||||
image = vScaleImage
|
image = vScaleImage
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if completion == nil || rotation == .horizontal {
|
if completion == nil || rotation == .horizontal {
|
||||||
let horizontal = CGSize(
|
let horizontal = CGSize(
|
||||||
@@ -1340,8 +1372,10 @@ class ImageLoader {
|
|||||||
let hScaleImage = resizeImage(image, to: hScaledSize)
|
let hScaleImage = resizeImage(image, to: hScaledSize)
|
||||||
guard let cost = imageByteSize(hScaleImage) else { return }
|
guard let cost = imageByteSize(hScaleImage) else { return }
|
||||||
self.horCache.setObject(hScaleImage, forKey: path.path as NSString, cost: cost)
|
self.horCache.setObject(hScaleImage, forKey: path.path as NSString, cost: cost)
|
||||||
|
if rotation == .horizontal {
|
||||||
image = hScaleImage
|
image = hScaleImage
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.loadingTasks[path.path]?.forEach { $0(image) }
|
self.loadingTasks[path.path]?.forEach { $0(image) }
|
||||||
|
Reference in New Issue
Block a user