From 2f7e0e9a6cf1cfc3c3426ac6b1cca4dc7b6addb9 Mon Sep 17 00:00:00 2001 From: Chris Li Date: Sat, 26 Nov 2016 17:09:22 -0500 Subject: [PATCH] separate CoreDataTableBaseController --- .../Library/CloudBooksController.swift | 2 +- .../Library/DownloadTasksController.swift | 2 +- .../Library/LibrarySplitViewController.swift | 49 +--------------- .../Library/LocalBooksController.swift | 2 +- .../Others/CoreDataTableBaseController.swift | 56 +++++++++++++++++++ Kiwix-iOS/Info.plist | 2 +- Kiwix-iOSWidgets/Bookmarks/Info.plist | 2 +- Kiwix.xcodeproj/project.pbxproj | 4 ++ 8 files changed, 67 insertions(+), 52 deletions(-) create mode 100644 Kiwix-iOS/Controller/Others/CoreDataTableBaseController.swift diff --git a/Kiwix-iOS/Controller/Library/CloudBooksController.swift b/Kiwix-iOS/Controller/Library/CloudBooksController.swift index ea41d1a5..30072101 100644 --- a/Kiwix-iOS/Controller/Library/CloudBooksController.swift +++ b/Kiwix-iOS/Controller/Library/CloudBooksController.swift @@ -12,7 +12,7 @@ import ProcedureKit import MBProgressHUD import DZNEmptyDataSet -class CloudBooksController: LibraryBaseController, UITableViewDelegate, UITableViewDataSource, LanguageFilterUpdating, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate { +class CloudBooksController: CoreDataTableBaseController, UITableViewDelegate, UITableViewDataSource, LanguageFilterUpdating, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate { private(set) var isRefreshing = false // used to control text on empty table view private(set) var isOnScreen = false // used to determine if should delay showing lang filter alert private(set) var langFilterAlertPending = false diff --git a/Kiwix-iOS/Controller/Library/DownloadTasksController.swift b/Kiwix-iOS/Controller/Library/DownloadTasksController.swift index 25665932..02ba8cb9 100644 --- a/Kiwix-iOS/Controller/Library/DownloadTasksController.swift +++ b/Kiwix-iOS/Controller/Library/DownloadTasksController.swift @@ -10,7 +10,7 @@ import UIKit import CoreData import DZNEmptyDataSet -class DownloadTasksController: LibraryBaseController, UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate { +class DownloadTasksController: CoreDataTableBaseController, UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate { var timer: Timer? // MARK: - Override diff --git a/Kiwix-iOS/Controller/Library/LibrarySplitViewController.swift b/Kiwix-iOS/Controller/Library/LibrarySplitViewController.swift index adaae338..c638df3f 100644 --- a/Kiwix-iOS/Controller/Library/LibrarySplitViewController.swift +++ b/Kiwix-iOS/Controller/Library/LibrarySplitViewController.swift @@ -22,10 +22,10 @@ class LibrarySplitViewController: UISplitViewController, UISplitViewControllerDe override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { guard traitCollection != previousTraitCollection else {return} - let controller: LibraryBaseController? = { + let controller: CoreDataTableBaseController? = { let nav = viewControllers.first as? UINavigationController let tab = nav?.topViewController as? UITabBarController - return tab?.selectedViewController as? LibraryBaseController + return tab?.selectedViewController as? CoreDataTableBaseController }() controller?.tableView.indexPathsForVisibleRows?.forEach({ (indexPath) in guard let cell = controller?.tableView.cellForRow(at: indexPath) else {return} @@ -60,48 +60,3 @@ class LibrarySplitViewController: UISplitViewController, UISplitViewControllerDe return ((viewControllers[safe: 1] as? UINavigationController)?.topViewController is LanguageFilterController) } } - -class LibraryBaseController: UIViewController, NSFetchedResultsControllerDelegate { - @IBOutlet weak var tableView: UITableView! - - func configureCell(_ cell: UITableViewCell, atIndexPath indexPath: IndexPath) { - - } - - func controllerWillChangeContent(_ controller: NSFetchedResultsController) { - tableView.beginUpdates() - } - - func controller(_ controller: NSFetchedResultsController, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) { - switch type { - case .insert: - tableView.insertSections(IndexSet(integer: sectionIndex), with: .fade) - case .delete: - tableView.deleteSections(IndexSet(integer: sectionIndex), with: .fade) - default: - return - } - } - - func controller(_ controller: NSFetchedResultsController, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) { - switch type { - case .insert: - guard let newIndexPath = newIndexPath else {return} - tableView.insertRows(at: [newIndexPath], with: .fade) - case .delete: - guard let indexPath = indexPath else {return} - tableView.deleteRows(at: [indexPath], with: .fade) - case .update: - guard let indexPath = indexPath, let cell = tableView.cellForRow(at: indexPath) else {return} - configureCell(cell, atIndexPath: indexPath) - case .move: - guard let indexPath = indexPath, let newIndexPath = newIndexPath else {return} - tableView.deleteRows(at: [indexPath], with: .fade) - tableView.insertRows(at: [newIndexPath], with: .fade) - } - } - - func controllerDidChangeContent(_ controller: NSFetchedResultsController) { - tableView.endUpdates() - } -} diff --git a/Kiwix-iOS/Controller/Library/LocalBooksController.swift b/Kiwix-iOS/Controller/Library/LocalBooksController.swift index 177faf49..6726c575 100644 --- a/Kiwix-iOS/Controller/Library/LocalBooksController.swift +++ b/Kiwix-iOS/Controller/Library/LocalBooksController.swift @@ -11,7 +11,7 @@ import CoreData import ProcedureKit import DZNEmptyDataSet -class LocalBooksController: LibraryBaseController, UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate { +class LocalBooksController: CoreDataTableBaseController, UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate { // MARK: - Override diff --git a/Kiwix-iOS/Controller/Others/CoreDataTableBaseController.swift b/Kiwix-iOS/Controller/Others/CoreDataTableBaseController.swift new file mode 100644 index 00000000..8a6c7d37 --- /dev/null +++ b/Kiwix-iOS/Controller/Others/CoreDataTableBaseController.swift @@ -0,0 +1,56 @@ +// +// CoreDataTableBaseController.swift +// Kiwix +// +// Created by Chris Li on 11/26/16. +// Copyright © 2016 Chris Li. All rights reserved. +// + +import UIKit +import CoreData + + +class CoreDataTableBaseController: UIViewController, NSFetchedResultsControllerDelegate { + @IBOutlet weak var tableView: UITableView! + + func configureCell(_ cell: UITableViewCell, atIndexPath indexPath: IndexPath) { + + } + + func controllerWillChangeContent(_ controller: NSFetchedResultsController) { + tableView.beginUpdates() + } + + func controller(_ controller: NSFetchedResultsController, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) { + switch type { + case .insert: + tableView.insertSections(IndexSet(integer: sectionIndex), with: .fade) + case .delete: + tableView.deleteSections(IndexSet(integer: sectionIndex), with: .fade) + default: + return + } + } + + func controller(_ controller: NSFetchedResultsController, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) { + switch type { + case .insert: + guard let newIndexPath = newIndexPath else {return} + tableView.insertRows(at: [newIndexPath], with: .fade) + case .delete: + guard let indexPath = indexPath else {return} + tableView.deleteRows(at: [indexPath], with: .fade) + case .update: + guard let indexPath = indexPath, let cell = tableView.cellForRow(at: indexPath) else {return} + configureCell(cell, atIndexPath: indexPath) + case .move: + guard let indexPath = indexPath, let newIndexPath = newIndexPath else {return} + tableView.deleteRows(at: [indexPath], with: .fade) + tableView.insertRows(at: [newIndexPath], with: .fade) + } + } + + func controllerDidChangeContent(_ controller: NSFetchedResultsController) { + tableView.endUpdates() + } +} diff --git a/Kiwix-iOS/Info.plist b/Kiwix-iOS/Info.plist index 1a47be22..90c43c1a 100644 --- a/Kiwix-iOS/Info.plist +++ b/Kiwix-iOS/Info.plist @@ -49,7 +49,7 @@ CFBundleVersion - 1.8.3342 + 1.8.3345 ITSAppUsesNonExemptEncryption LSRequiresIPhoneOS diff --git a/Kiwix-iOSWidgets/Bookmarks/Info.plist b/Kiwix-iOSWidgets/Bookmarks/Info.plist index bd0106e7..90c727e8 100644 --- a/Kiwix-iOSWidgets/Bookmarks/Info.plist +++ b/Kiwix-iOSWidgets/Bookmarks/Info.plist @@ -21,7 +21,7 @@ CFBundleSignature ???? CFBundleVersion - 1.8.3342 + 1.8.3345 NSExtension NSExtensionMainStoryboard diff --git a/Kiwix.xcodeproj/project.pbxproj b/Kiwix.xcodeproj/project.pbxproj index d8a3441c..8524983e 100644 --- a/Kiwix.xcodeproj/project.pbxproj +++ b/Kiwix.xcodeproj/project.pbxproj @@ -42,6 +42,7 @@ 973208271DD2238B00EDD3DC /* GlobalQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97D6811C1D6F70AC00E5FA99 /* GlobalQueue.swift */; }; 973208291DD223DB00EDD3DC /* RefreshLibrary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 973208281DD223DB00EDD3DC /* RefreshLibrary.swift */; }; 9734E54E1D289D060061C39B /* Welcome.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9734E54D1D289D060061C39B /* Welcome.storyboard */; }; + 973A5C921DEA3F5600C7804C /* CoreDataTableBaseController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 973A5C911DEA3F5600C7804C /* CoreDataTableBaseController.swift */; }; 973BCD1A1CEB402900F10B44 /* KiwixTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 973BCD181CEB402900F10B44 /* KiwixTests.swift */; }; 973DD40F1D343F2F009D45DB /* libicudata.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 973DD4041D343F2F009D45DB /* libicudata.a */; }; 973DD4101D343F2F009D45DB /* libicui18n.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 973DD4051D343F2F009D45DB /* libicui18n.a */; }; @@ -189,6 +190,7 @@ 973208251DD21E9C00EDD3DC /* CoreDataContainer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreDataContainer.swift; sourceTree = ""; }; 973208281DD223DB00EDD3DC /* RefreshLibrary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RefreshLibrary.swift; sourceTree = ""; }; 9734E54D1D289D060061C39B /* Welcome.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = Welcome.storyboard; path = "Kiwix-iOS/Storyboard/Welcome.storyboard"; sourceTree = SOURCE_ROOT; }; + 973A5C911DEA3F5600C7804C /* CoreDataTableBaseController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreDataTableBaseController.swift; sourceTree = ""; }; 973BCD001CEB3FA500F10B44 /* Kiwix_OSXTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Kiwix_OSXTests.swift; sourceTree = ""; }; 973BCD021CEB3FA500F10B44 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 973BCD0B1CEB3FA500F10B44 /* Kiwix_OSXUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Kiwix_OSXUITests.swift; sourceTree = ""; }; @@ -625,6 +627,7 @@ children = ( 970E7F7C1DA0305000741290 /* Alerts.swift */, 970E7F7E1DA0305000741290 /* TableOfContentsController.swift */, + 973A5C911DEA3F5600C7804C /* CoreDataTableBaseController.swift */, 970E7F7F1DA0305000741290 /* WelcomeController.swift */, ); path = Others; @@ -1183,6 +1186,7 @@ 97D681311D6F70EC00E5FA99 /* 1.5.xcmappingmodel in Sources */, 9732075C1DD136BB00EDD3DC /* CoreDataExtension.swift in Sources */, 973208271DD2238B00EDD3DC /* GlobalQueue.swift in Sources */, + 973A5C921DEA3F5600C7804C /* CoreDataTableBaseController.swift in Sources */, 97A1FD181D6F71CE00A80EE2 /* SearchResult.swift in Sources */, 973207A11DD1983D00EDD3DC /* LocalBooksController.swift in Sources */, 977AE7F91DD8F22400F1E581 /* SearchBar.swift in Sources */,