This commit is contained in:
Chris Li 2016-11-25 11:32:12 -05:00
parent 8bd87ec9cc
commit 0d4f09ec1d
9 changed files with 90 additions and 223 deletions

View File

@ -7,6 +7,7 @@
//
import UIKit
import WebKit
class MainController: UIViewController {
@ -18,7 +19,6 @@ class MainController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// webView.loadRequest(URLRequest(url: URL(string: "about:blank")!))
navigationItem.titleView = searchBar
searchBar.delegate = self
buttons.delegate = self

View File

@ -95,7 +95,6 @@ extension MainController: UIWebViewDelegate, SFSafariViewControllerDelegate {
present(controller, animated: true, completion: nil)
return false
}
controllers.navigationList.startLoading(requestURL: url)
return true
}
@ -107,8 +106,8 @@ extension MainController: UIWebViewDelegate, SFSafariViewControllerDelegate {
guard let title = JS.getTitle(from: webView) else {return}
searchBar.title = title
buttons.back.tintColor = controllers.navigationList.canGoBack ? nil : UIColor.gray
buttons.forward.tintColor = controllers.navigationList.canGoForward ? nil : UIColor.gray
buttons.back.tintColor = webView.canGoBack ? nil : UIColor.gray
buttons.forward.tintColor = webView.canGoForward ? nil : UIColor.gray
}
func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
@ -128,11 +127,11 @@ extension MainController {
extension MainController: ButtonDelegates, SearchContainerDelegate {
func didTapBackButton() {
// navigationList.goBack(webView: webView)
webView.goBack()
}
func didTapForwardButton() {
// navigationList.goForward(webView: webView)
webView.goForward()
}
func didTapTOCButton() {
@ -152,19 +151,9 @@ extension MainController: ButtonDelegates, SearchContainerDelegate {
}
func didLongPressBackButton() {
let controller = controllers.navigationList
controller.type = .back
controller.delegate = self
let nav = UINavigationController(rootViewController: controller)
present(nav, animated: true, completion: nil)
}
func didLongPressForwardButton() {
let controller = controllers.navigationList
controller.type = .forward
controller.delegate = self
let nav = UINavigationController(rootViewController: controller)
present(nav, animated: true, completion: nil)
}
func didLongPressBookmarkButton() {
@ -174,12 +163,12 @@ extension MainController: ButtonDelegates, SearchContainerDelegate {
// MARK: - NavigationListControllerDelegate
extension MainController: NavigationListControllerDelegate {
func load(url: URL) {
let request = URLRequest(url: url)
webView.loadRequest(request)
}
}
//extension MainController: NavigationListControllerDelegate {
// func load(url: URL) {
// let request = URLRequest(url: url)
// webView.loadRequest(request)
// }
//}
// MARK: - SearchContainerDelegate

View File

@ -1,75 +0,0 @@
//
// NavigationStack.swift
// Kiwix
//
// Created by Chris Li on 11/20/16.
// Copyright © 2016 Chris Li. All rights reserved.
//
import UIKit
class NavigationList {
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 currentURL != requestURL else {return}
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, backListIndex: Int = 0) {
guard let currentIndex = currentIndex else {return}
let index = currentIndex - 1 - backListIndex
guard index >= 0 else {return}
self.currentIndex = index
guard let url = currentURL else {return}
let request = URLRequest(url: url)
webView.loadRequest(request)
}
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
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 {
guard let currentIndex = currentIndex else {return false}
return currentIndex >= 1 && urls.indices.contains(currentIndex - 1)
}
var canGoForward: Bool {
guard let currentIndex = currentIndex else {return false}
return currentIndex >= 0 && urls.indices.contains(currentIndex + 1)
}
}

View File

@ -2,123 +2,94 @@
// NavigationListController.swift
// Kiwix
//
// Created by Chris Li on 11/21/16.
// Created by Chris Li on 11/25/16.
// Copyright © 2016 Chris Li. All rights reserved.
//
import UIKit
class NavigationListController: UITableViewController {
var type: NavigationListType = .back
weak var delegate: NavigationListControllerDelegate?
private var urls = [URL]()
private var currentIndex: Int?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(dismiss(sender:)))
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
}
func dismiss(sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}
// MARK: - Navigation List
func startLoading(requestURL: URL) {
if let index = currentIndex {
if index == urls.count - 1 {
urls.append(requestURL)
currentIndex = index + 1
} else {
if requestURL == urls[index] {
} else if requestURL == urls[index + 1] {
currentIndex = index + 1
} else {
urls.removeLast(urls.count - index - 1)
urls.append(requestURL)
currentIndex = index + 1
}
}
} else {
urls.append(requestURL)
currentIndex = 0
}
}
func urlMapping(indexPath: IndexPath) -> Int? {
guard let currentIndex = currentIndex else {return nil}
switch type {
case .back:
return currentIndex - indexPath.row - 1
case .forward:
return currentIndex + indexPath.row + 1
}
}
var canGoBack: Bool {
guard let index = currentIndex else {return false}
return index >= 1
}
var canGoForward: Bool {
guard let index = currentIndex else {return false}
return index <= urls.count - 2
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
// #warning Incomplete implementation, return the number of sections
return 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let currentIndex = currentIndex else {return 0}
switch type {
case .back:
return currentIndex
case .forward:
return urls.count - currentIndex - 1
}
// #warning Incomplete implementation, return the number of rows
return 0
}
/*
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
if let index = urlMapping(indexPath: indexPath) {
let url = urls[index]
cell.textLabel?.text = url.lastPathComponent
}
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
// Configure the cell...
return cell
}
// MARK: - Table view delegate
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
dismiss(animated: true, completion: nil)
if let index = urlMapping(indexPath: indexPath) {
let url = urls[index]
currentIndex = index
delegate?.load(url: url)
}
*/
/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
}
*/
protocol NavigationListControllerDelegate: class {
func load(url: URL)
}
/*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
enum NavigationListType {
case back, forward
}

View File

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

View File

@ -100,14 +100,14 @@
<color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.0" colorSpace="custom" customColorSpace="sRGB"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="H1Cell" textLabel="xPq-dt-NK6" style="IBUITableViewCellStyleDefault" id="DSn-3Y-Gs4">
<rect key="frame" x="0.0" y="28" width="667" height="44"/>
<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="DSn-3Y-Gs4" id="vq5-6x-vLY">
<rect key="frame" x="0.0" y="0.0" width="667" height="43"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Title" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="xPq-dt-NK6">
<rect key="frame" x="15" y="0.0" width="637" height="43"/>
<rect key="frame" x="15" y="0.0" width="384" height="43"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" weight="semibold" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
@ -122,14 +122,14 @@
<color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.0" colorSpace="custom" customColorSpace="sRGB"/>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="H2Cell" textLabel="DJX-Kf-59M" style="IBUITableViewCellStyleDefault" id="au4-iz-tLz">
<rect key="frame" x="0.0" y="72" width="667" height="44"/>
<rect key="frame" x="0.0" y="72" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="au4-iz-tLz" id="3UD-lZ-hs0">
<rect key="frame" x="0.0" y="0.0" width="667" height="43"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="DJX-Kf-59M">
<rect key="frame" x="15" y="0.0" width="637" height="43"/>
<rect key="frame" x="15" y="0.0" width="384" height="43"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" weight="medium" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
@ -144,14 +144,14 @@
<color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.0" colorSpace="custom" customColorSpace="sRGB"/>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Cell" textLabel="ry7-2s-QyS" style="IBUITableViewCellStyleDefault" id="cam-ik-ZIw">
<rect key="frame" x="0.0" y="116" width="667" height="44"/>
<rect key="frame" x="0.0" y="116" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="cam-ik-ZIw" id="ooG-35-3Wp">
<rect key="frame" x="0.0" y="0.0" width="667" height="43"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="ry7-2s-QyS">
<rect key="frame" x="15" y="0.0" width="637" height="43"/>
<rect key="frame" x="15" y="0.0" width="384" height="43"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>

View File

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

View File

@ -68,7 +68,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 */; };
977319841DE8998200111474 /* NavigationListController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 977319831DE8998200111474 /* 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, ); }; };
@ -191,7 +191,6 @@
973208251DD21E9C00EDD3DC /* CoreDataContainer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreDataContainer.swift; sourceTree = "<group>"; };
973208281DD223DB00EDD3DC /* RefreshLibrary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RefreshLibrary.swift; sourceTree = "<group>"; };
9734E54D1D289D060061C39B /* Welcome.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = Welcome.storyboard; path = "Kiwix-iOS/Storyboard/Welcome.storyboard"; sourceTree = SOURCE_ROOT; };
97362EE81DE1EADB004205B5 /* NavigationList.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NavigationList.swift; sourceTree = "<group>"; };
973BCD001CEB3FA500F10B44 /* Kiwix_OSXTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Kiwix_OSXTests.swift; sourceTree = "<group>"; };
973BCD021CEB3FA500F10B44 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
973BCD0B1CEB3FA500F10B44 /* Kiwix_OSXUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Kiwix_OSXUITests.swift; sourceTree = "<group>"; };
@ -226,7 +225,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>"; };
977319831DE8998200111474 /* 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>"; };
@ -481,10 +480,9 @@
97BC0FBA1DD90A34004BBAD1 /* old */,
97BC0FBE1DD90A65004BBAD1 /* MainController.swift */,
97D0E98E1DDA12B30029530E /* MainDelegates.swift */,
977319831DE8998200111474 /* NavigationListController.swift */,
97BC0FC11DD92B62004BBAD1 /* Buttons.swift */,
97BC0FBD1DD90A65004BBAD1 /* JSInjection.swift */,
97362EE81DE1EADB004205B5 /* NavigationList.swift */,
9773197F1DE3AA3200111474 /* NavigationListController.swift */,
972F81581DDC1B71008D7289 /* Controllers.swift */,
);
path = Main;
@ -1176,7 +1174,6 @@
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 */,
@ -1188,6 +1185,7 @@
97D6813F1D6F712800E5FA99 /* Article+CoreDataProperties.swift in Sources */,
97A1FD1D1D6F71D800A80EE2 /* URLResponseCache.swift in Sources */,
97A1FD441D6F728200A80EE2 /* Preference.swift in Sources */,
977319841DE8998200111474 /* NavigationListController.swift in Sources */,
97D681311D6F70EC00E5FA99 /* 1.5.xcmappingmodel in Sources */,
9732075C1DD136BB00EDD3DC /* CoreDataExtension.swift in Sources */,
973208271DD2238B00EDD3DC /* GlobalQueue.swift in Sources */,

View File

@ -123,19 +123,3 @@ extension UIDevice {
}
}
//extension Collection {
// subscript (safe index: Index) -> Iterator.Element? {
// return indices.contains(index) ? self[index] : nil
// }
//}
// Swift 3
extension IndexableBase {
/// Returns the element at the specified index iff it is within bounds, otherwise nil.
public subscript(safe index: Index) -> _Element? {
return index >= startIndex && index < endIndex
? self[index]
: nil
}
}