diff --git a/Kiwix-iOS/Controller/ControllerRetainer.swift b/Kiwix-iOS/Controller/ControllerRetainer.swift index edac0e42..6927e0a3 100644 --- a/Kiwix-iOS/Controller/ControllerRetainer.swift +++ b/Kiwix-iOS/Controller/ControllerRetainer.swift @@ -19,18 +19,12 @@ class ControllerRetainer { } @objc func removeStrongReference() { - search = nil bookmark = nil - } - - // MARK: - Search - - private var search: SearchController? - - class var search: SearchController { - let controller = ControllerRetainer.shared.search ?? UIStoryboard(name: "Search", bundle: nil).instantiateInitialViewController() as! SearchController - ControllerRetainer.shared.search = controller - return controller + bookmarkStar = nil + library = nil + search = nil + setting = nil + welcome = nil } // MARK: - Bookmark @@ -56,8 +50,39 @@ class ControllerRetainer { private var library: UIViewController? class var library: UIViewController { - let controller = ControllerRetainer.shared.bookmarkStar ?? UIStoryboard(name: "Library", bundle: nil).instantiateInitialViewController() - ControllerRetainer.shared.library = controller! - return controller! + let controller = ControllerRetainer.shared.bookmarkStar ?? UIStoryboard(name: "Library", bundle: nil).instantiateInitialViewController()! + ControllerRetainer.shared.library = controller + return controller } + + // MARK: - Search + + private var search: SearchController? + + class var search: SearchController { + let controller = ControllerRetainer.shared.search ?? UIStoryboard(name: "Search", bundle: nil).instantiateInitialViewController() as! SearchController + ControllerRetainer.shared.search = controller + return controller + } + + // MARK: - Setting + + private var setting: UIViewController? + + class var setting: UIViewController { + let controller = ControllerRetainer.shared.setting ?? UIStoryboard(name: "Setting", bundle: nil).instantiateInitialViewController()! + ControllerRetainer.shared.setting = controller + return controller + } + + // MARK: - Welcome + + private var welcome: UIViewController? + + class var welcome: UIViewController { + let controller = ControllerRetainer.shared.welcome ?? UIStoryboard(name: "Welcome", bundle: nil).instantiateInitialViewController()! + ControllerRetainer.shared.welcome = controller + return controller + } + } diff --git a/Kiwix-iOS/Controller/Main/MainController.swift b/Kiwix-iOS/Controller/Main/MainController.swift index dc22bec0..6f47eefa 100644 --- a/Kiwix-iOS/Controller/Main/MainController.swift +++ b/Kiwix-iOS/Controller/Main/MainController.swift @@ -20,8 +20,6 @@ class MainController: UIViewController { @IBOutlet weak var tocLeadSpacing: NSLayoutConstraint! var tableOfContentsController: TableOfContentsController? - var settingController: UIViewController? - var welcomeController: UIViewController? let searchBar = SearchBar() var context: UnsafeMutablePointer = nil @@ -77,13 +75,6 @@ class MainController: UIViewController { } } - override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - tableOfContentsController = nil - settingController = nil - welcomeController = nil - } - override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) { super.traitCollectionDidChange(previousTraitCollection) if previousTraitCollection?.horizontalSizeClass != traitCollection.horizontalSizeClass { @@ -238,10 +229,9 @@ class MainController: UIViewController { } func showSettingButtonTapped() { - guard let viewController = settingController ?? UIStoryboard.setting.instantiateInitialViewController() else {return} - viewController.modalPresentationStyle = .FormSheet - settingController = viewController - presentViewController(viewController, animated: true, completion: nil) + let controller = ControllerRetainer.setting + controller.modalPresentationStyle = .FormSheet + presentViewController(controller, animated: true, completion: nil) } func cancelButtonTapped() { diff --git a/Kiwix-iOS/Controller/Main/MainControllerDelegates.swift b/Kiwix-iOS/Controller/Main/MainControllerDelegates.swift index 0de962b6..d3f1048b 100644 --- a/Kiwix-iOS/Controller/Main/MainControllerDelegates.swift +++ b/Kiwix-iOS/Controller/Main/MainControllerDelegates.swift @@ -187,109 +187,4 @@ extension MainController: LPTBarButtonItemDelegate, TableOfContentsDelegate, Zim controller.dismissViewControllerAnimated(true, completion: nil) } - // MARK: - UIScrollViewDelegate - - func scrollViewDidScroll(scrollView: UIScrollView) { - guard scrollView.contentSize.height >= scrollView.frame.height else {return} - guard let navigationBar = navigationController?.navigationBar else {return} - guard scrollView.contentOffset.y > 100 else {return} - - // Calculate current YOffset but without elasticity - let currentScrollViewYOffset: CGFloat = { - let topInset = scrollView.contentInset.top - let bottomInset = scrollView.contentInset.bottom - let minYOffset = -topInset - let maxYOffset = scrollView.contentSize.height + bottomInset - scrollView.frame.height - return max(minYOffset, min(scrollView.contentOffset.y, maxYOffset)) - }() - - // delta content offset y, scroll up minus, scroll down plus - let yDelta = previousScrollViewYOffset - currentScrollViewYOffset - - // Slide up nav bar - let navOriginY = max(20.0 - 44.0, min(navigationBar.frame.origin.y + yDelta, 20.0)) - let navFrame = CGRectMake(0, navOriginY, navigationBar.frame.width, navigationBar.frame.height) - navigationBar.frame = navFrame - - // Slide down tool bar - if let toolBar = navigationController?.toolbar { - let originY = max(view.frame.height - 44.0, min(toolBar.frame.origin.y - yDelta, view.frame.height)) - let frame = CGRectMake(0, originY, toolBar.frame.width, toolBar.frame.height) - toolBar.frame = frame - } - - // Shrink nav bar - //let newNavBarHeight = max(navBarMinHeight, min(navigationBar.frame.height + yDelta, 44.0)) - //let navFrame = CGRectMake(0, navigationBar.frame.origin.y, navigationBar.frame.width, newNavBarHeight) - //navigationBar.frame = navFrame - - updateNavBarItems() - configureWebViewInsets() - previousScrollViewYOffset = currentScrollViewYOffset - } - - func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) { - stoppedScrolling() - } - - func updateNavBarItems() { - guard let navigationBar = navigationController?.navigationBar else {return} - let min: CGFloat = 20.0 - 44.0 - let max: CGFloat = 20.0 - let alpha = (navigationBar.frame.origin.y - min) / (max - min) - navigationItem.titleView?.alpha = alpha - } - - func stoppedScrolling() { - guard let navigationBar = navigationController?.navigationBar else {return} - let show = ((navigationBar.frame.origin.y - (20-44)) / 44) > 0.5 - animateBar(show) - } - - func animateBar(show: Bool) { - UIView.animateWithDuration(0.2) { () -> Void in - if show { - if let navBar = self.navigationController?.navigationBar { - navBar.frame = CGRectMake(0, 20, navBar.frame.width, navBar.frame.height) - } - if let toolBar = self.navigationController?.toolbar { - toolBar.frame = CGRectMake(0, self.view.frame.height - toolBar.frame.height, toolBar.frame.width, toolBar.frame.height) - } - self.navigationItem.titleView?.alpha = 1.0 - } else { - if let navBar = self.navigationController?.navigationBar { - navBar.frame = CGRectMake(0, 20 - 44, navBar.frame.width, navBar.frame.height) - } - if let toolBar = self.navigationController?.toolbar { - toolBar.frame = CGRectMake(0, self.view.frame.height, toolBar.frame.width, toolBar.frame.height) - } - self.navigationItem.titleView?.alpha = 0.0 - } - } - } - -} - -class HTMLHeading { - let id: String - let tagName: String - let textContent: String - let level: Int - - init?(rawValue: [String: String]) { - let tagName = rawValue["tagName"] ?? "" - self.id = rawValue["id"] ?? "" - self.textContent = rawValue["textContent"] ?? "" - self.tagName = tagName - self.level = Int(tagName.stringByReplacingOccurrencesOfString("H", withString: "")) ?? -1 - - if id == "" {return nil} - if tagName == "" {return nil} - if textContent == "" {return nil} - if level == -1 {return nil} - } - - var scrollToJavaScript: String { - return "document.getElementById('\(id)').scrollIntoView();" - } } diff --git a/Kiwix-iOS/Controller/Main/MainControllerShowHide.swift b/Kiwix-iOS/Controller/Main/MainControllerShowHide.swift index 7bb25a1b..6dc8a65b 100644 --- a/Kiwix-iOS/Controller/Main/MainControllerShowHide.swift +++ b/Kiwix-iOS/Controller/Main/MainControllerShowHide.swift @@ -158,8 +158,7 @@ extension MainController { // MARK: - Show/Hide Welcome func showWelcome() { - guard let controller = welcomeController ?? UIStoryboard.welcome.instantiateInitialViewController() else {return} - welcomeController = controller + let controller = ControllerRetainer.welcome controller.view.translatesAutoresizingMaskIntoConstraints = false addChildViewController(controller) view.addSubview(controller.view) @@ -171,7 +170,7 @@ extension MainController { } func hideWelcome() { - guard let controller = welcomeController else {return} + let controller = ControllerRetainer.welcome controller.removeFromParentViewController() controller.view.removeFromSuperview() } diff --git a/Kiwix-iOS/Info.plist b/Kiwix-iOS/Info.plist index f5ab7d0d..da9bd42b 100644 --- a/Kiwix-iOS/Info.plist +++ b/Kiwix-iOS/Info.plist @@ -49,7 +49,7 @@ CFBundleVersion - 1.7.1706 + 1.7.1718 ITSAppUsesNonExemptEncryption LSRequiresIPhoneOS diff --git a/Kiwix-iOSWidgets/Bookmarks/Info.plist b/Kiwix-iOSWidgets/Bookmarks/Info.plist index 461e1b16..3a964ece 100644 --- a/Kiwix-iOSWidgets/Bookmarks/Info.plist +++ b/Kiwix-iOSWidgets/Bookmarks/Info.plist @@ -21,7 +21,7 @@ CFBundleSignature ???? CFBundleVersion - 1.7.2193 + 1.7.2213 NSExtension NSExtensionMainStoryboard diff --git a/Kiwix.xcodeproj/project.pbxproj b/Kiwix.xcodeproj/project.pbxproj index 77867d37..565cb036 100644 --- a/Kiwix.xcodeproj/project.pbxproj +++ b/Kiwix.xcodeproj/project.pbxproj @@ -111,6 +111,7 @@ 97C005D81D64B99E004352E8 /* LibrarySplitViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97C005D71D64B99E004352E8 /* LibrarySplitViewController.swift */; }; 97C005DC1D64BEFE004352E8 /* CloudBooksController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97C005DB1D64BEFE004352E8 /* CloudBooksController.swift */; }; 97C601DC1D7F15C400362D4F /* Bookmark.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C601DB1D7F15C400362D4F /* Bookmark.storyboard */; }; + 97C601DE1D7F342100362D4F /* HTMLHeading.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97C601DD1D7F342100362D4F /* HTMLHeading.swift */; }; 97D452BE1D1723FF0033666F /* CollectionViewCells.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97D452BD1D1723FF0033666F /* CollectionViewCells.swift */; }; 97D55EF61D2075180081B523 /* TableOfContentsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97D55EF51D2075180081B523 /* TableOfContentsController.swift */; }; 97D6811B1D6E2A7100E5FA99 /* DownloadTasksController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97D6811A1D6E2A7100E5FA99 /* DownloadTasksController.swift */; }; @@ -334,6 +335,7 @@ 97C005D71D64B99E004352E8 /* LibrarySplitViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = LibrarySplitViewController.swift; path = "Kiwix-iOS/Controller/Library/LibrarySplitViewController.swift"; sourceTree = SOURCE_ROOT; }; 97C005DB1D64BEFE004352E8 /* CloudBooksController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CloudBooksController.swift; sourceTree = ""; }; 97C601DB1D7F15C400362D4F /* Bookmark.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Bookmark.storyboard; sourceTree = ""; }; + 97C601DD1D7F342100362D4F /* HTMLHeading.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTMLHeading.swift; sourceTree = ""; }; 97D452BD1D1723FF0033666F /* CollectionViewCells.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionViewCells.swift; sourceTree = ""; }; 97D55EF51D2075180081B523 /* TableOfContentsController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TableOfContentsController.swift; path = "Kiwix-iOS/Controller/TableOfContentsController.swift"; sourceTree = SOURCE_ROOT; }; 97D6811A1D6E2A7100E5FA99 /* DownloadTasksController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DownloadTasksController.swift; sourceTree = ""; }; @@ -834,6 +836,7 @@ 97A461B61D74819000AC3DED /* DownloadProgress.swift */, 97A1FD3D1D6F728200A80EE2 /* Extensions.swift */, 97A1FD3E1D6F728200A80EE2 /* FileManager.swift */, + 97C601DD1D7F342100362D4F /* HTMLHeading.swift */, 97A1FD401D6F728200A80EE2 /* Preference.swift */, 97A1FD411D6F728200A80EE2 /* StringTools.swift */, ); @@ -1524,6 +1527,7 @@ 97D6811B1D6E2A7100E5FA99 /* DownloadTasksController.swift in Sources */, 97D681261D6F70AC00E5FA99 /* SearchOperation.swift in Sources */, 971A10381D022C15007FC62C /* WebViewController.swift in Sources */, + 97C601DE1D7F342100362D4F /* HTMLHeading.swift in Sources */, 97D681271D6F70AC00E5FA99 /* UIOperations.swift in Sources */, 975B90FE1CEB909100D13906 /* iOSExtensions.swift in Sources */, 971A10521D022D9D007FC62C /* AppDelegate.swift in Sources */, diff --git a/Kiwix/Tools/HTMLHeading.swift b/Kiwix/Tools/HTMLHeading.swift new file mode 100644 index 00000000..4dc05dc4 --- /dev/null +++ b/Kiwix/Tools/HTMLHeading.swift @@ -0,0 +1,31 @@ +// +// HTMLHeading.swift +// Kiwix +// +// Created by Chris Li on 9/6/16. +// Copyright © 2016 Chris. All rights reserved. +// + +class HTMLHeading { + let id: String + let tagName: String + let textContent: String + let level: Int + + init?(rawValue: [String: String]) { + let tagName = rawValue["tagName"] ?? "" + self.id = rawValue["id"] ?? "" + self.textContent = rawValue["textContent"] ?? "" + self.tagName = tagName + self.level = Int(tagName.stringByReplacingOccurrencesOfString("H", withString: "")) ?? -1 + + if id == "" {return nil} + if tagName == "" {return nil} + if textContent == "" {return nil} + if level == -1 {return nil} + } + + var scrollToJavaScript: String { + return "document.getElementById('\(id)').scrollIntoView();" + } +}