This commit is contained in:
Chris Li 2016-09-06 14:00:04 -04:00
parent 7e2cb6d302
commit 92094df4c6
8 changed files with 81 additions and 137 deletions

View File

@ -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
}
}

View File

@ -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<Void> = 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() {

View File

@ -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();"
}
}

View File

@ -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()
}

View File

@ -49,7 +49,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>1.7.1706</string>
<string>1.7.1718</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSRequiresIPhoneOS</key>

View File

@ -21,7 +21,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.7.2193</string>
<string>1.7.2213</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionMainStoryboard</key>

View File

@ -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 = "<group>"; };
97C601DB1D7F15C400362D4F /* Bookmark.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Bookmark.storyboard; sourceTree = "<group>"; };
97C601DD1D7F342100362D4F /* HTMLHeading.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTMLHeading.swift; sourceTree = "<group>"; };
97D452BD1D1723FF0033666F /* CollectionViewCells.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionViewCells.swift; sourceTree = "<group>"; };
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 = "<group>"; };
@ -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 */,

View File

@ -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();"
}
}