From be5e513cbc2fb78c37d14ffa0db08b00970719b0 Mon Sep 17 00:00:00 2001 From: Vegard Bieker Matthey Date: Thu, 9 Jul 2026 22:56:57 +0200 Subject: [PATCH] feat: reintroduce scrolling --- ImageViewer/ViewController.swift | 55 ++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/ImageViewer/ViewController.swift b/ImageViewer/ViewController.swift index 11e8912..5ae9b84 100644 --- a/ImageViewer/ViewController.swift +++ b/ImageViewer/ViewController.swift @@ -59,8 +59,6 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { var scrollPos: CGPoint! var hasSetContentOffset = false var pageCount = 0 - var pagesAvailable = 0 - var chapterAndPages: [(Int, Int)] = [] var imageView = UIImageView() var mode = PageTurnMode.leftToRight @@ -136,9 +134,9 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { readerView.bottomAnchor.constraint(equalTo: view.bottomAnchor), readerView.widthAnchor.constraint(equalTo: view.widthAnchor), ]) + setupScrollingCollectionView() loadComics() setupImageView() - setupScrollingCollectionView() setupGestures() setupBar() setupHomeView() @@ -594,7 +592,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { } if let indexPath = scrollingCollectionView.indexPathForItem(at: scrollOffset) { var theProgress = ProgressIndices(v: 0, c: 0, i: 0) - for _ in 0 ... indexPath.item { + for _ in 0 ..< indexPath.item { theProgress = getProgressIndicesFromTurn( turn: .next, progress: theProgress ) @@ -632,9 +630,11 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { ) if let indexPath = scrollingCollectionView.indexPathForItem(at: centerPoint) { - let (chapter, page) = chapterAndPages[indexPath.item] - progress.c = chapter - progress.i = page + var newProgress = ProgressIndices(v: 0, c: 0, i: 0) + for _ in 0 ..< indexPath.item { + newProgress = getProgressIndicesFromTurn(turn: .next, progress: newProgress) + } + progress = newProgress } if scrollingCollectionView.isHidden == false { saveLocalState() @@ -694,7 +694,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { leftView.isHidden = true rightView.isHidden = true scrollingCollectionView.isHidden = false - // TODO: Fix scrolling again + scrollingCollectionView.reloadData() } func countFiles() -> Int { @@ -1028,6 +1028,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { scrollingCollectionView.isHidden = mode != .scroll leftView.isHidden = mode == .scroll rightView.isHidden = mode == .scroll + imageView.isHidden = mode == .scroll togglePageTurnDropdown() saveLocalState() } @@ -1161,7 +1162,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { self?.updateInfo() } - for _ in 0 ... preloadCount { + for _ in 0 ..< preloadCount { newProgress = getProgressIndicesFromTurn(turn: .next, progress: newProgress) let path = getImagePath(progress: newProgress) imageLoader.preloadImage(at: path, scaling: scaling, screenSize: UIScreen.main.bounds.size) @@ -1302,7 +1303,7 @@ class ViewController: UIViewController, UIGestureRecognizerDelegate { } var newProgress = progress - for _ in 0 ... preloadCount { + for _ in 0 ..< preloadCount { newProgress = getProgressIndicesFromTurn(turn: .next, progress: newProgress) imageLoader.preloadImage(at: getImagePath(progress: newProgress), scaling: scaling, screenSize: UIScreen.main.bounds.size) } @@ -1375,7 +1376,13 @@ extension ViewController: UICollectionViewDataSource, UICollectionViewDelegateFl if collectionView == comicCollectionView { return comics.count } else { - return pagesAvailable + var sum = 0 + for volume in metadata.volumes { + for chapter in volume.chapters { + sum += chapter.images.count + } + } + return sum } } @@ -1391,16 +1398,25 @@ extension ViewController: UICollectionViewDataSource, UICollectionViewDelegateFl cell.imageView.image = comics[indexPath.item].cover return cell } else if collectionView == scrollingCollectionView { - // let scaling = UIView.ContentMode.scaleAspectFill + let scaling = UIView.ContentMode.scaleAspectFill let cell = collectionView.dequeueReusableCell( withReuseIdentifier: "ScrollingImageCell", for: indexPath ) as! ScrollingImageCell if metadata == nil { + print("metadata is nil, should probably not be the case") return cell } - // TODO: Fix scrolling again + var newProgress = ProgressIndices(v: 0, c: 0, i: 0) + if indexPath.item != 0 { + for _ in 0 ..< indexPath.item { + newProgress = getProgressIndicesFromTurn(turn: .next, progress: newProgress) + } + } + imageLoader.loadImage(at: getImagePath(progress: newProgress), scaling: scaling, screenSize: UIScreen.main.bounds.size) { image in + cell.imageView.image = image + } return cell } else { assertionFailure() @@ -1417,7 +1433,7 @@ extension ViewController: UICollectionViewDataSource, UICollectionViewDelegateFl func collectionView( _ collectionView: UICollectionView, layout _: UICollectionViewLayout, - sizeForItemAt _: IndexPath + sizeForItemAt indexPath: IndexPath ) -> CGSize { if collectionView == comicCollectionView { let spacing: CGFloat = 8 @@ -1429,8 +1445,15 @@ extension ViewController: UICollectionViewDataSource, UICollectionViewDelegateFl if metadata == nil { return CGSize() } - // TODO: Fix scrolling again - return CGSize() + var newProgress = ProgressIndices(v: 0, c: 0, i: 0) + for _ in 0 ..< indexPath.item { + newProgress = getProgressIndicesFromTurn(turn: .next, progress: newProgress) + } + let imageMetadata = metadata.volumes[newProgress.v].chapters[newProgress.c].images[newProgress.i] + return CGSize( + width: readerView.bounds.width, + height: CGFloat(imageMetadata.size!.height) * readerView.bounds.width / CGFloat(imageMetadata.size!.width) + ) } else { assertionFailure() }