Skip to content
This repository was archived by the owner on Nov 11, 2024. It is now read-only.

Commit 23838e0

Browse files
committed
Move FetchResultControllerDelegate to it's own class
1 parent c31f7a0 commit 23838e0

File tree

4 files changed

+70
-43
lines changed

4 files changed

+70
-43
lines changed

wutComicReader.xcodeproj/project.pbxproj

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@
3939
E45A878B25FCB8410011CF9F /* UIKitPreview.swift in Sources */ = {isa = PBXBuildFile; fileRef = E45A878A25FCB8410011CF9F /* UIKitPreview.swift */; };
4040
E45C842A238DACD700669155 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = E45C8429238DACD700669155 /* Colors.swift */; };
4141
E462BD05245581940089AD5F /* VCAlert.swift in Sources */ = {isa = PBXBuildFile; fileRef = E462BD04245581940089AD5F /* VCAlert.swift */; };
42-
E462BD07245583D90089AD5F /* Library+FetchResultController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E462BD06245583D90089AD5F /* Library+FetchResultController.swift */; };
42+
E462BD07245583D90089AD5F /* LibraryFetchResultControllerHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = E462BD06245583D90089AD5F /* LibraryFetchResultControllerHandler.swift */; };
4343
E462BD092455FA6D0089AD5F /* LibraryVCTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E462BD082455FA6D0089AD5F /* LibraryVCTests.swift */; };
4444
E463B29822AC2FCE0094265C /* Consts.swift in Sources */ = {isa = PBXBuildFile; fileRef = E463B29722AC2FCE0094265C /* Consts.swift */; };
4545
E465B11A2413FBAD00A6003D /* InfoVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = E465B1192413FBAC00A6003D /* InfoVC.swift */; };
4646
E475D0AE245751D10005CF8F /* ComicKhanUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E475D0AD245751D10005CF8F /* ComicKhanUITests.swift */; };
4747
E48253EA22D631B1001A0E85 /* Helper.swift in Sources */ = {isa = PBXBuildFile; fileRef = E48253E922D631B1001A0E85 /* Helper.swift */; };
4848
E4850C1D2832A43E00AA0F85 /* ThemesStackView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4850C1C2832A43E00AA0F85 /* ThemesStackView.swift */; };
49+
E499A0BC2840C20F002F8351 /* Library+FetchRsultHandlerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E499A0BB2840C20F002F8351 /* Library+FetchRsultHandlerDelegate.swift */; };
4950
E49C7C132417EBED004DE82A /* ImageResizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = E49C7C122417EBED004DE82A /* ImageResizer.swift */; };
5051
E49C7C152418FEF4004DE82A /* ComicImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = E49C7C142418FEF4004DE82A /* ComicImage.swift */; };
5152
E49F2F602833C73300BE90FE /* +CollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E49F2F5F2833C73300BE90FE /* +CollectionView.swift */; };
@@ -63,7 +64,7 @@
6364
E4E8FCED22C28E4700A8B32D /* BookReaderVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4E8FCEC22C28E4700A8B32D /* BookReaderVC.swift */; };
6465
E4EB1E392569762800C56632 /* IndexSelectionManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4EB1E382569762800C56632 /* IndexSelectionManager.swift */; };
6566
E4EBC304241E886600F558AF /* EmptyGroupView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4EBC303241E886600F558AF /* EmptyGroupView.swift */; };
66-
E4EBC306241E8D4C00F558AF /* EmptyViewDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4EBC305241E8D4C00F558AF /* EmptyViewDelegate.swift */; };
67+
E4EBC306241E8D4C00F558AF /* Library+EmptyViewDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4EBC305241E8D4C00F558AF /* Library+EmptyViewDelegate.swift */; };
6768
E4EC58B225A47150004D6190 /* ArrayOfStringTransformer.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4EC58B125A47150004D6190 /* ArrayOfStringTransformer.swift */; };
6869
E4FD092924542439004D0A53 /* FileManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4FD092824542439004D0A53 /* FileManagerTests.swift */; };
6970
E4FD092B245426E7004D0A53 /* ExtractorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4FD092A245426E7004D0A53 /* ExtractorTests.swift */; };
@@ -138,7 +139,7 @@
138139
E45A878A25FCB8410011CF9F /* UIKitPreview.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIKitPreview.swift; sourceTree = "<group>"; };
139140
E45C8429238DACD700669155 /* Colors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Colors.swift; sourceTree = "<group>"; };
140141
E462BD04245581940089AD5F /* VCAlert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VCAlert.swift; sourceTree = "<group>"; };
141-
E462BD06245583D90089AD5F /* Library+FetchResultController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Library+FetchResultController.swift"; sourceTree = "<group>"; };
142+
E462BD06245583D90089AD5F /* LibraryFetchResultControllerHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LibraryFetchResultControllerHandler.swift; sourceTree = "<group>"; };
142143
E462BD082455FA6D0089AD5F /* LibraryVCTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LibraryVCTests.swift; sourceTree = "<group>"; };
143144
E463B29722AC2FCE0094265C /* Consts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Consts.swift; sourceTree = "<group>"; };
144145
E465B1192413FBAC00A6003D /* InfoVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoVC.swift; sourceTree = "<group>"; };
@@ -148,6 +149,7 @@
148149
E48253E922D631B1001A0E85 /* Helper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Helper.swift; sourceTree = "<group>"; };
149150
E4850C1C2832A43E00AA0F85 /* ThemesStackView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemesStackView.swift; sourceTree = "<group>"; };
150151
E48B2009883E73D5F63B7D37 /* Pods-wutComicReader.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-wutComicReader.debug.xcconfig"; path = "Target Support Files/Pods-wutComicReader/Pods-wutComicReader.debug.xcconfig"; sourceTree = "<group>"; };
152+
E499A0BB2840C20F002F8351 /* Library+FetchRsultHandlerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Library+FetchRsultHandlerDelegate.swift"; sourceTree = "<group>"; };
151153
E49C7C122417EBED004DE82A /* ImageResizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageResizer.swift; sourceTree = "<group>"; };
152154
E49C7C142418FEF4004DE82A /* ComicImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComicImage.swift; sourceTree = "<group>"; };
153155
E49F2F5F2833C73300BE90FE /* +CollectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "+CollectionView.swift"; sourceTree = "<group>"; };
@@ -167,7 +169,7 @@
167169
E4E8FCEC22C28E4700A8B32D /* BookReaderVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookReaderVC.swift; sourceTree = "<group>"; };
168170
E4EB1E382569762800C56632 /* IndexSelectionManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IndexSelectionManager.swift; sourceTree = "<group>"; };
169171
E4EBC303241E886600F558AF /* EmptyGroupView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyGroupView.swift; sourceTree = "<group>"; };
170-
E4EBC305241E8D4C00F558AF /* EmptyViewDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyViewDelegate.swift; sourceTree = "<group>"; };
172+
E4EBC305241E8D4C00F558AF /* Library+EmptyViewDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Library+EmptyViewDelegate.swift"; sourceTree = "<group>"; };
171173
E4EC58B025A45631004D6190 /* coredata v2.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "coredata v2.xcdatamodel"; sourceTree = "<group>"; };
172174
E4EC58B125A47150004D6190 /* ArrayOfStringTransformer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrayOfStringTransformer.swift; sourceTree = "<group>"; };
173175
E4FD092824542439004D0A53 /* FileManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileManagerTests.swift; sourceTree = "<group>"; };
@@ -376,6 +378,7 @@
376378
isa = PBXGroup;
377379
children = (
378380
E40C7A9E22A1168C00D7CD17 /* LibraryVC.swift */,
381+
E462BD06245583D90089AD5F /* LibraryFetchResultControllerHandler.swift */,
379382
E4CE3781240D6C5800429C30 /* Views */,
380383
E4CE3780240D6C2800429C30 /* Extensions */,
381384
);
@@ -415,10 +418,10 @@
415418
children = (
416419
E40A353223F97C83006FA758 /* Library+ProgressContainer.swift */,
417420
E4CE3782240D6CFB00429C30 /* Library+CollectionView.swift */,
418-
E4EBC305241E8D4C00F558AF /* EmptyViewDelegate.swift */,
421+
E4EBC305241E8D4C00F558AF /* Library+EmptyViewDelegate.swift */,
419422
E4AE1A2325E28E6000AEF86A /* Library+ExtractionErrorHandeling.swift */,
420-
E462BD06245583D90089AD5F /* Library+FetchResultController.swift */,
421423
E455AFDE245EA30D003F403D /* Library+DocumentBrowser.swift */,
424+
E499A0BB2840C20F002F8351 /* Library+FetchRsultHandlerDelegate.swift */,
422425
);
423426
path = Extensions;
424427
sourceTree = "<group>";
@@ -723,13 +726,14 @@
723726
E463B29822AC2FCE0094265C /* Consts.swift in Sources */,
724727
E4A20D6822D879450067BDF8 /* LibraryCell.swift in Sources */,
725728
E455AFDF245EA30D003F403D /* Library+DocumentBrowser.swift in Sources */,
729+
E499A0BC2840C20F002F8351 /* Library+FetchRsultHandlerDelegate.swift in Sources */,
726730
E49F2F602833C73300BE90FE /* +CollectionView.swift in Sources */,
727731
E4596A07240803A60030DB3E /* ReaderTopBar.swift in Sources */,
728732
E407CB8323FD3D780056F64D /* RoundedProgressView.swift in Sources */,
729733
E45213A922D4927D00216217 /* ComicExteractor.swift in Sources */,
730734
E4D3920123EEA23800E8C873 /* NewGroupVC.swift in Sources */,
731735
E4C51EFE245EF82700B8AC04 /* LoadingView.swift in Sources */,
732-
E462BD07245583D90089AD5F /* Library+FetchResultController.swift in Sources */,
736+
E462BD07245583D90089AD5F /* LibraryFetchResultControllerHandler.swift in Sources */,
733737
E4CE377F240D578600429C30 /* ProgressContainerView.swift in Sources */,
734738
E45A877D25FC99280011CF9F /* Cores.swift in Sources */,
735739
E48253EA22D631B1001A0E85 /* Helper.swift in Sources */,
@@ -745,7 +749,7 @@
745749
E4EBC304241E886600F558AF /* EmptyGroupView.swift in Sources */,
746750
E4C596F7283BA25E004C71D9 /* AppFont.swift in Sources */,
747751
E45A878B25FCB8410011CF9F /* UIKitPreview.swift in Sources */,
748-
E4EBC306241E8D4C00F558AF /* EmptyViewDelegate.swift in Sources */,
752+
E4EBC306241E8D4C00F558AF /* Library+EmptyViewDelegate.swift in Sources */,
749753
E42B9EA024506CF3007B3803 /* DataService.swift in Sources */,
750754
E4288B4E23DB30E000182E0B /* thumbnailCollectionView.swift in Sources */,
751755
E4C3D175261E09D7007CC0E1 /* AppState.swift in Sources */,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// Library+FetchRsultHandlerDelegate.swift
3+
// wutComicReader
4+
//
5+
// Created by Sha Yan on 3/6/1401 AP.
6+
// Copyright © 1401 AP wutup. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension LibraryVC: LibraryFetchResultControllerHandlerDelegate {
12+
func libraryBecame(empty: Bool) {
13+
UIView.animate(withDuration: 0.2) { [weak self] in
14+
self?.emptyGroupsView.alpha = empty ? 1 : 0
15+
self?.bookCollectionView.alpha = empty ? 0 : 1
16+
} completion: { [weak self] _ in
17+
self?.emptyGroupsView.isHidden = !empty
18+
self?.bookCollectionView.isHidden = empty
19+
}
20+
}
21+
}

wutComicReader/ViewControllers/Library/Extensions/Library+FetchResultController.swift renamed to wutComicReader/ViewControllers/Library/LibraryFetchResultControllerHandler.swift

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,57 @@
99
import UIKit
1010
import CoreData
1111

12-
extension LibraryVC: NSFetchedResultsControllerDelegate {
12+
class LibraryFetchResultControllerHandler: NSObject, NSFetchedResultsControllerDelegate {
13+
14+
let collectionView: UICollectionView
15+
let fetchResultController: NSFetchedResultsController<Comic>
16+
private var blockOperations = [BlockOperation]()
17+
weak var delegate: LibraryFetchResultControllerHandlerDelegate?
18+
19+
init(fetchResultController: NSFetchedResultsController<Comic> ,collectionView: UICollectionView) {
20+
self.collectionView = collectionView
21+
self.fetchResultController = fetchResultController
22+
super.init()
23+
fetchResultController.delegate = self
24+
}
1325

1426
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
1527

1628
switch type {
1729
case .insert:
1830
if let index = newIndexPath {
1931
blockOperations.append(BlockOperation(block: { [weak self] in
20-
self?.bookCollectionView.insertItems(at: [index])
32+
self?.collectionView.insertItems(at: [index])
2133
}))
2234
}
2335
case .delete:
2436
if let index = indexPath {
2537
blockOperations.append(BlockOperation(block: { [weak self] in
26-
self?.bookCollectionView.deleteItems(at: [index])
38+
self?.collectionView.deleteItems(at: [index])
2739
}))
2840
}
2941
case .update:
3042
if let updatatdComic = anObject as? Comic,
3143
let index = indexPath {
3244
blockOperations.append(BlockOperation(block: { [weak self] in
33-
let cell = self?.bookCollectionView.cellForItem(at: index) as? LibraryCell
45+
let cell = self?.collectionView.cellForItem(at: index) as? LibraryCell
3446
cell?.book = updatatdComic
3547
}))
3648
}
3749
case .move:
3850
if let index = indexPath,
39-
let newIndex = newIndexPath {
51+
let newIndex = newIndexPath {
4052
blockOperations.append(BlockOperation(block: { [weak self] in
41-
self?.bookCollectionView.deleteItems(at: [index])
42-
self?.bookCollectionView.insertItems(at: [newIndex])
53+
self?.collectionView.deleteItems(at: [index])
54+
self?.collectionView.insertItems(at: [newIndex])
4355
}))
4456
}
4557
default:
4658
break
4759
}
4860

4961

50-
UIView.animate(withDuration: 0.2) {
51-
self.emptyGroupsView.isHidden = !(controller.fetchedObjects?.isEmpty ?? true)
52-
}
62+
delegate?.libraryBecame(empty: controller.fetchedObjects?.isEmpty ?? true)
5363

5464

5565
}
@@ -67,22 +77,22 @@ extension LibraryVC: NSFetchedResultsControllerDelegate {
6777
case .insert:
6878

6979
blockOperations.append(BlockOperation(block: { [weak self] in
70-
self?.bookCollectionView.insertSections(indexSet)
80+
self?.collectionView.insertSections(indexSet)
7181
}))
7282
print("------FRC Section Insert")
7383
case .delete:
7484
blockOperations.append(BlockOperation(block: { [weak self] in
75-
self?.bookCollectionView.deleteSections(indexSet)
85+
self?.collectionView.deleteSections(indexSet)
7686
}))
7787
print("------FRC Section Delete")
7888
case .move:
7989
blockOperations.append(BlockOperation(block: { [weak self] in
80-
self?.bookCollectionView.reloadData()
90+
self?.collectionView.reloadData()
8191
}))
8292
print("------FRC Section Move")
8393
case .update:
8494
blockOperations.append(BlockOperation(block: { [weak self] in
85-
self?.bookCollectionView.reloadSections(indexSet)
95+
self?.collectionView.reloadSections(indexSet)
8696
}))
8797
print("------FRC Section Update")
8898

@@ -95,13 +105,17 @@ extension LibraryVC: NSFetchedResultsControllerDelegate {
95105

96106
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
97107

98-
bookCollectionView.performBatchUpdates({
108+
collectionView.performBatchUpdates({
99109
for operation in blockOperations {
100110
operation.start()
101111
}
102112
}) { (_) in
103113
self.blockOperations.removeAll()
104114
}
105115
}
106-
116+
}
117+
118+
119+
protocol LibraryFetchResultControllerHandlerDelegate: NSObject {
120+
func libraryBecame(empty: Bool)
107121
}

wutComicReader/ViewControllers/Library/LibraryVC.swift

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class LibraryVC: UIViewController {
2323
private(set) var dataService: DataService
2424

2525
let fetchResultController: NSFetchedResultsController<Comic>
26-
var blockOperations = [BlockOperation]()
26+
private var fetchResultHandler: LibraryFetchResultControllerHandler!
2727

2828
private(set) var collectionViewCellSize: CGSize!
2929

@@ -33,6 +33,7 @@ class LibraryVC: UIViewController {
3333

3434
let indexSelectionManager = IndexSelectionManager()
3535
var indexSelectionCancelabels = [Cancellable]()
36+
private var fetchResultControllerHandler: NSFetchedResultsControllerDelegate!
3637

3738
//MARK:- UI Variables
3839

@@ -98,20 +99,22 @@ class LibraryVC: UIViewController {
9899
self.appfileManager = Cores.main.appfileManager
99100
self.comicExtractor = Cores.main.extractor
100101
self.dataService = Cores.main.dataService
101-
fetchResultController = try! dataService.configureFetchResultController()
102+
self.fetchResultController = try! dataService.configureFetchResultController()
102103

103104
super.init(coder: coder)
104105
}
105106

106107
override func viewDidLoad() {
107108
super.viewDidLoad()
108109

110+
fetchResultHandler = LibraryFetchResultControllerHandler(fetchResultController: fetchResultController,
111+
collectionView: bookCollectionView)
112+
109113
setupDiractoryWatcher()
110114
didUserAddNewFilesWhileAppWasDeactive()
111115

112116
configureCellSize(basedOn: UIScreen.main.traitCollection)
113117
setUpDesigns()
114-
115118
setupDelegates()
116119

117120
setUpNavigationButtons()
@@ -204,7 +207,7 @@ class LibraryVC: UIViewController {
204207
}
205208

206209
private func setupDelegates() {
207-
fetchResultController.delegate = self
210+
fetchResultHandler.delegate = self
208211
comicExtractor.delegate = self
209212
comicExtractor.errorDelegate = self
210213
appfileManager.progressDelegate = self
@@ -319,8 +322,6 @@ class LibraryVC: UIViewController {
319322

320323
func updateNavBarWhenEditingChanged() {
321324
if editingMode {
322-
// navigationItem.setRightBarButtonItems([deleteBarButton , groupBarButton , infoButton], animated: true)
323-
// navigationItem.setLeftBarButtonItems([editBarButton], animated: true)
324325
bottomToolbar.isHidden = false
325326
bottomToolbar.alpha = 0
326327

@@ -329,33 +330,20 @@ class LibraryVC: UIViewController {
329330
} completion: { (_) in
330331

331332
}
332-
333-
334-
335333
bottomToolbar.isHidden = false
336-
// infoButton.isEnabled = false
337334
addComicsButton.isEnabled = false
338-
// infoButton.tintColor = .clear
339335
editBarButton.title = "Done"
340336
editBarButton.style = .done
341337
}else{
342-
// navigationItem.setRightBarButtonItems([editBarButton], animated: true)
343-
// navigationItem.setLeftBarButtonItems([infoButton], animated: true)
344338

345339
UIView.animate(withDuration: 0.2) {
346340
self.bottomToolbar.alpha = 0
347341
} completion: { (_) in
348342
self.bottomToolbar.isHidden = true
349343
}
350-
351-
352-
// infoButton.isEnabled = true
353344
addComicsButton.isEnabled = true
354-
// infoButton.tintColor = addComicsButton.tintColor
355345
editBarButton.title = "Edit"
356346
editBarButton.style = .plain
357-
// deleteBarButton.isEnabled = false
358-
// groupBarButton.isEnabled = false
359347

360348
}
361349
}

0 commit comments

Comments
 (0)