This commit is contained in:
Chris Li 2016-11-22 13:53:01 -05:00
parent 3496d1e95d
commit 13b33e2e3a
9 changed files with 220 additions and 34 deletions

View File

@ -75,9 +75,9 @@ class Buttons {
guard let view = recognizer.view, recognizer.state == .began else {return}
switch view.tag {
case 0:
print("left long tapped")
delegate?.didLongPressBackButton()
case 1:
print("right long tapped")
delegate?.didLongPressForwardButton()
case 3:
delegate?.didLongPressBookmarkButton()
default:
@ -93,17 +93,38 @@ protocol ButtonDelegates {
func didTapLibraryButton()
func didTapCancelButton()
func didLongPressBackButton()
func didLongPressForwardButton()
func didLongPressBookmarkButton()
}
class GrayBarButtonItem: UIBarButtonItem {
override init() {
super.init()
print(value(forKey: "view") as? UIView)
tintColor = UIColor.gray
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
print(value(forKey: "view") as? UIView)
tintColor = UIColor.gray
}
}
class BarButton: UIBarButtonItem {
private(set) var type = BarButtonType.blank
convenience init(type: BarButtonType) {
let button = UIButton()
button.setImage(<#T##image: UIImage?##UIImage?#>, for: <#T##UIControlState#>)
self.init(customView: button)
self.type = type
}
}
enum BarButtonType {
case back, forward
case tableOfContent, bookmark, library, setting
case blank
}

View File

@ -25,8 +25,13 @@ class Controllers {
return (UIApplication.appDelegate.window?.rootViewController as! UINavigationController).topViewController as! MainController
}
// // MARK: - Bookmark
//
// MARK: - NavigationList
private(set) lazy var navigationList = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NavigationListController") as! NavigationListController
// MARK: - Bookmark
//
// private var bookmark: UINavigationController?
//
// class var bookmark: UINavigationController {

View File

@ -107,8 +107,8 @@ extension MainController: UIWebViewDelegate, SFSafariViewControllerDelegate {
guard let title = JS.getTitle(from: webView) else {return}
searchBar.title = title
buttons.back.tintColor = navigationList.canGoBack ? nil : UIColor.gray
buttons.forward.tintColor = navigationList.canGoForward ? nil : UIColor.gray
// buttons.back.tintColor = navigationList.canGoBack ? nil : UIColor.gray
// buttons.forward.tintColor = navigationList.canGoForward ? nil : UIColor.gray
}
func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
@ -147,6 +147,20 @@ extension MainController: ButtonDelegates, SearchContainerDelegate {
_ = searchBar.resignFirstResponder()
}
func didLongPressBackButton() {
let controller = controllers.navigationList
controller.urls = navigationList.backList
let nav = UINavigationController(rootViewController: controller)
present(nav, animated: true, completion: nil)
}
func didLongPressForwardButton() {
let controller = controllers.navigationList
controller.urls = navigationList.forwardList
let nav = UINavigationController(rootViewController: controller)
present(nav, animated: true, completion: nil)
}
func didLongPressBookmarkButton() {
showBookmarkHUD()
}

View File

@ -9,48 +9,70 @@
import UIKit
class NavigationList {
var backList = [URL]()
var forwardList = [URL]()
var currentURL: URL?
private var urls = [URL]()
private var currentIndex: Int?
var currentURL: URL? {
guard let currentIndex = currentIndex else {return nil}
return urls.indices.contains(currentIndex) ? urls[currentIndex] : nil
}
func webViewStartLoading(requestURL: URL) {
guard let currentURL = currentURL else {
self.currentURL = requestURL
return
}
guard currentURL != requestURL else {return}
backList.append(currentURL)
self.currentURL = requestURL
forwardList.removeAll()
if let index = currentIndex {
urls.removeLast(urls.count - index - 1)
urls.append(requestURL)
self.currentIndex = index + 1
} else {
urls.append(requestURL)
self.currentIndex = 0
}
}
func goBack(webView: UIWebView) {
guard let lastURL = backList.last, let currentURL = currentURL else {return}
backList.removeLast()
self.currentURL = lastURL
forwardList.insert(currentURL, at: 0)
func goBack(webView: UIWebView, backListIndex: Int = 0) {
guard let currentIndex = currentIndex else {return}
let index = currentIndex - 1 - backListIndex
guard index >= 0 else {return}
self.currentIndex = index
let request = URLRequest(url: lastURL)
guard let url = currentURL else {return}
let request = URLRequest(url: url)
webView.loadRequest(request)
}
func goForward(webView: UIWebView) {
guard let nextURL = forwardList.first, let currentURL = currentURL else {return}
backList.append(currentURL)
self.currentURL = nextURL
forwardList.removeFirst()
func goForward(webView: UIWebView, forwardListIndex: Int = 0) {
guard let currentIndex = currentIndex else {return}
let index = currentIndex + 1 + forwardListIndex
guard index <= urls.count - 1 else {return}
self.currentIndex = index
let request = URLRequest(url: nextURL)
guard let url = currentURL else {return}
let request = URLRequest(url: url)
webView.loadRequest(request)
}
var backList: [URL] {
guard let currentIndex = currentIndex else {return [URL]()}
return Array(urls.prefix(currentIndex))
}
var forwardList: [URL] {
guard let currentIndex = currentIndex else {return [URL]()}
return Array(urls.suffix(urls.count - currentIndex - 1))
}
var canGoBack: Bool {
return backList.count > 0
guard let currentIndex = currentIndex else {return false}
return currentIndex >= 1 && urls.indices.contains(currentIndex - 1)
}
var canGoForward: Bool {
return forwardList.count > 0
guard let currentIndex = currentIndex else {return false}
return currentIndex >= 0 && urls.indices.contains(currentIndex + 1)
}
}
enum NavigationListType {
case Back, Forward
}

View File

@ -0,0 +1,47 @@
//
// NavigationListController.swift
// Kiwix
//
// Created by Chris Li on 11/21/16.
// Copyright © 2016 Chris Li. All rights reserved.
//
import UIKit
class NavigationListController: UITableViewController {
var type: NavigationListType?
var urls = [URL]() {
didSet {
tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(dismiss(sender:)))
}
func dismiss(sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return urls.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let url = urls[indexPath.row]
cell.textLabel?.text = url.lastPathComponent
return cell
}
}

View File

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

View File

@ -10,6 +10,79 @@
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Navigation List Controller-->
<scene sceneID="jZE-Dn-ydL">
<objects>
<tableViewController storyboardIdentifier="NavigationListController" id="mzx-cq-adE" customClass="NavigationListController" customModule="Kiwix" customModuleProvider="target" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" id="eVE-sA-vdZ">
<rect key="frame" x="0.0" y="0.0" width="414" height="736"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Cell" id="r7E-6T-nCU">
<rect key="frame" x="0.0" y="28" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="r7E-6T-nCU" id="MHm-bs-fau">
<rect key="frame" x="0.0" y="0.0" width="414" height="43"/>
<autoresizingMask key="autoresizingMask"/>
</tableViewCellContentView>
</tableViewCell>
</prototypes>
<connections>
<outlet property="dataSource" destination="mzx-cq-adE" id="ryr-XA-qfn"/>
<outlet property="delegate" destination="mzx-cq-adE" id="cBk-E2-CcQ"/>
</connections>
</tableView>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="AUI-Vv-fbI" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="3084" y="-674"/>
</scene>
<!--Root View Controller-->
<scene sceneID="BNz-yk-AfC">
<objects>
<tableViewController id="WhC-HH-hZh" customClass="NavigationListController" customModule="Kiwix" customModuleProvider="target" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" id="03l-xW-El2">
<rect key="frame" x="0.0" y="0.0" width="414" height="736"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Cell" id="dzE-1z-TGl">
<rect key="frame" x="0.0" y="28" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="dzE-1z-TGl" id="MUh-zX-0U1">
<rect key="frame" x="0.0" y="0.0" width="414" height="43"/>
<autoresizingMask key="autoresizingMask"/>
</tableViewCellContentView>
</tableViewCell>
</prototypes>
<connections>
<outlet property="dataSource" destination="WhC-HH-hZh" id="4PA-es-DCw"/>
<outlet property="delegate" destination="WhC-HH-hZh" id="Uy3-JF-5AO"/>
</connections>
</tableView>
<navigationItem key="navigationItem" title="Root View Controller" id="30n-oT-cxd"/>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="PJq-HV-PHx" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="3416" y="-348"/>
</scene>
<!--Navigation List Nav-->
<scene sceneID="VeW-EH-F1h">
<objects>
<navigationController id="R7u-Q9-uy7" customClass="NavigationListNav" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" id="yDq-ho-5C5">
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<connections>
<segue destination="WhC-HH-hZh" kind="relationship" relationship="rootViewController" id="wFp-ul-1Fy"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="mqo-ts-Ds4" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="2509" y="-350"/>
</scene>
<!--Table Of Contents Controller-->
<scene sceneID="sFA-xU-ByH">
<objects>

View File

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

View File

@ -70,6 +70,7 @@
9764F5991D833F2B00E0B1C4 /* KiwixURL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9764F5981D833F2B00E0B1C4 /* KiwixURL.swift */; };
976B86D81DDA0C7E00FA7FD1 /* SearchContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 976B86D71DDA0C7E00FA7FD1 /* SearchContainer.swift */; };
9771A5BD1DD269BD005F1795 /* Book+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97D6813C1D6F712800E5FA99 /* Book+CoreDataProperties.swift */; };
977319801DE3AA3200111474 /* NavigationListController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9773197F1DE3AA3200111474 /* NavigationListController.swift */; };
9779C3141D4575AD0064CC8E /* NotificationCenter.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 97E609F01D103DED00EBCB9D /* NotificationCenter.framework */; };
9779C3171D4575AE0064CC8E /* TodayViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9779C3161D4575AE0064CC8E /* TodayViewController.swift */; };
9779C31E1D4575AE0064CC8E /* Bookmarks.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 9779C3131D4575AD0064CC8E /* Bookmarks.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
@ -228,6 +229,7 @@
9764F5981D833F2B00E0B1C4 /* KiwixURL.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KiwixURL.swift; sourceTree = "<group>"; };
976A0C801D41619C0006A742 /* DZNEmptyDataSet.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = DZNEmptyDataSet.framework; path = "../../../../Users/chrisli/Library/Developer/Xcode/DerivedData/Kiwix-ayxrfhaqnfxzendihdolvkklkmhk/Build/Products/Debug-iphoneos/DZNEmptyDataSet/DZNEmptyDataSet.framework"; sourceTree = "<group>"; };
976B86D71DDA0C7E00FA7FD1 /* SearchContainer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SearchContainer.swift; sourceTree = "<group>"; };
9773197F1DE3AA3200111474 /* NavigationListController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NavigationListController.swift; sourceTree = "<group>"; };
9779C3131D4575AD0064CC8E /* Bookmarks.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = Bookmarks.appex; sourceTree = BUILT_PRODUCTS_DIR; };
9779C3161D4575AE0064CC8E /* TodayViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TodayViewController.swift; sourceTree = "<group>"; };
9779C31B1D4575AE0064CC8E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@ -486,6 +488,7 @@
97BC0FC11DD92B62004BBAD1 /* Buttons.swift */,
97BC0FBD1DD90A65004BBAD1 /* JSInjection.swift */,
97362EE81DE1EADB004205B5 /* NavigationList.swift */,
9773197F1DE3AA3200111474 /* NavigationListController.swift */,
972F81581DDC1B71008D7289 /* Controllers.swift */,
);
path = Main;
@ -1179,6 +1182,7 @@
9764F5991D833F2B00E0B1C4 /* KiwixURL.swift in Sources */,
97A127CC1D777CF100FB204D /* SearchResultController.swift in Sources */,
9732079F1DD197F400EDD3DC /* CloudBooksController.swift in Sources */,
977319801DE3AA3200111474 /* NavigationListController.swift in Sources */,
973208241DD217B600EDD3DC /* BookmarkHUD.swift in Sources */,
971A102F1D022AD5007FC62C /* Logo.swift in Sources */,
970E7F821DA0305000741290 /* TableOfContentsController.swift in Sources */,