load article

This commit is contained in:
Chris Li 2016-11-16 23:30:32 -05:00
parent 159c8a3159
commit 04eb423b93
11 changed files with 120 additions and 193 deletions

View File

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

View File

@ -10,6 +10,7 @@ import UIKit
class MainController: UIViewController {
@IBOutlet weak var webView: UIWebView!
let searchBar = SearchBar()
lazy var controllers = Controllers()
lazy var buttons = Buttons()
@ -17,6 +18,7 @@ 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

@ -8,9 +8,9 @@
import UIKit
extension MainController: SearchBarDelegate, ButtonDelegates, SearchContainerDelegate {
// MARK: - SearchBarDelegate
// MARK: - Search
extension MainController: SearchBarDelegate {
func didBecomeFirstResponder(searchBar: SearchBar) {
showSearch(animated: true)
@ -24,8 +24,68 @@ extension MainController: SearchBarDelegate, ButtonDelegates, SearchContainerDel
controllers.search.searchText = text
}
// MARK: - Button Delegates
private func showSearch(animated: Bool) {
let controller = controllers.search
controller.delegate = self
guard !childViewControllers.contains(controller) else {return}
// add cancel button if needed
if traitCollection.horizontalSizeClass == .compact {
navigationItem.setRightBarButton(buttons.cancel, animated: animated)
}
// manage view hierarchy
addChildViewController(controller)
controller.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(controller.view)
let views = ["view": controller.view]
view.addConstraints(NSLayoutConstraint.constraints(
withVisualFormat: "H:|[view]|", options: .alignAllCenterY, metrics: nil, views: views))
view.addConstraint(controller.view.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor))
view.addConstraint(controller.view.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor))
if animated {
controller.view.alpha = 0.5
UIView.animate(withDuration: 0.15, delay: 0.0, options: .curveEaseOut, animations: { () -> Void in
controller.view.alpha = 1.0
}, completion: nil)
} else {
controller.view.alpha = 1.0
}
controller.didMove(toParentViewController: self)
}
private func hideSearch(animated: Bool) {
guard let searchController = childViewControllers.flatMap({$0 as? SearchContainer}).first else {return}
// remove cancel button if needed
if traitCollection.horizontalSizeClass == .compact {
navigationItem.setRightBarButton(nil, animated: animated)
}
let completion = { (complete: Bool) -> Void in
guard complete else {return}
searchController.view.removeFromSuperview()
searchController.removeFromParentViewController()
guard self.traitCollection.horizontalSizeClass == .compact else {return}
self.navigationController?.setToolbarHidden(false, animated: animated)
}
searchController.willMove(toParentViewController: nil)
if animated {
UIView.animate(withDuration: 0.15, delay: 0.0, options: .beginFromCurrentState, animations: {
searchController.view.alpha = 0.0
}, completion: completion)
} else {
completion(true)
}
}
}
// MARK: - Button Delegates
extension MainController: ButtonDelegates, SearchContainerDelegate {
func didTapLibraryButton() {
present(controllers.library, animated: true, completion: nil)
}
@ -33,11 +93,33 @@ extension MainController: SearchBarDelegate, ButtonDelegates, SearchContainerDel
func didTapCancelButton() {
_ = searchBar.resignFirstResponder()
}
// MARK: - SearchContainerDelegate
}
// MARK: - SearchContainerDelegate
extension MainController {
func didTapDimView() {
_ = searchBar.resignFirstResponder()
}
}
// MARK: - Welcome
extension MainController {
func showWelcome() {
let controller = controllers.welcome
controller.view.translatesAutoresizingMaskIntoConstraints = false
addChildViewController(controller)
view.addSubview(controller.view)
let views = ["view": controller.view]
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view]|", options: .alignAllTop, metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", options: .alignAllLeft, metrics: nil, views: views))
controller.didMove(toParentViewController: self)
}
func hideWelcome() {
guard let controller = childViewControllers.flatMap({$0 as? WelcomeController}).first else {return}
controller.removeFromParentViewController()
controller.view.removeFromSuperview()
}
}

View File

@ -1,110 +0,0 @@
//
// MainShowHide.swift
// Kiwix
//
// Created by Chris Li on 11/16/16.
// Copyright © 2016 Chris Li. All rights reserved.
//
import UIKit
extension MainController {
// MARK: - Welcome
func showWelcome() {
let controller = controllers.welcome
controller.view.translatesAutoresizingMaskIntoConstraints = false
addChildViewController(controller)
view.addSubview(controller.view)
let views = ["view": controller.view]
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view]|", options: .alignAllTop, metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|", options: .alignAllLeft, metrics: nil, views: views))
controller.didMove(toParentViewController: self)
}
func hideWelcome() {
guard let controller = childViewControllers.flatMap({$0 as? WelcomeController}).first else {return}
controller.removeFromParentViewController()
controller.view.removeFromSuperview()
}
// MARK: - Search
func showSearch(animated: Bool) {
let controller = controllers.search
controller.delegate = self
guard !childViewControllers.contains(controller) else {return}
// add cancel button if needed
if traitCollection.horizontalSizeClass == .compact {
navigationItem.setRightBarButton(buttons.cancel, animated: animated)
}
// manage view hierarchy
addChildViewController(controller)
controller.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(controller.view)
let views = ["view": controller.view]
view.addConstraints(NSLayoutConstraint.constraints(
withVisualFormat: "H:|[view]|", options: .alignAllCenterY, metrics: nil, views: views))
view.addConstraint(controller.view.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor))
view.addConstraint(controller.view.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor))
if animated {
controller.view.alpha = 0.5
UIView.animate(withDuration: 0.15, delay: 0.0, options: .curveEaseOut, animations: { () -> Void in
controller.view.alpha = 1.0
}, completion: nil)
} else {
controller.view.alpha = 1.0
}
controller.didMove(toParentViewController: self)
}
func hideSearch(animated: Bool) {
guard let searchController = childViewControllers.flatMap({$0 as? SearchContainer}).first else {return}
// remove cancel button if needed
if traitCollection.horizontalSizeClass == .compact {
navigationItem.setRightBarButton(nil, animated: animated)
}
let completion = { (complete: Bool) -> Void in
guard complete else {return}
searchController.view.removeFromSuperview()
searchController.removeFromParentViewController()
guard self.traitCollection.horizontalSizeClass == .compact else {return}
self.navigationController?.setToolbarHidden(false, animated: animated)
}
searchController.willMove(toParentViewController: nil)
if animated {
UIView.animate(withDuration: 0.15, delay: 0.0, options: .beginFromCurrentState, animations: {
searchController.view.alpha = 0.0
}, completion: completion)
} else {
completion(true)
}
}
// MARK: - Web
func showWeb() {
let controller = controllers.web
guard !childViewControllers.contains(controller) else {return}
addChildViewController(controller)
controller.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(controller.view)
let views = ["view": controller.view]
view.addConstraints(NSLayoutConstraint.constraints(
withVisualFormat: "H:|[view]|", options: .alignAllCenterY, metrics: nil, views: views))
view.addConstraint(controller.view.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor))
view.addConstraint(controller.view.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor))
controller.didMove(toParentViewController: self)
}
}

View File

@ -1,20 +0,0 @@
//
// WebViewController.swift
// Kiwix
//
// Created by Chris Li on 11/16/16.
// Copyright © 2016 Chris Li. All rights reserved.
//
import UIKit
class WebViewController: UIViewController {
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
}
}

View File

@ -87,6 +87,8 @@ class SearchResultController: SearchBaseTableController, UITableViewDataSource,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let result = searchResults[indexPath.row]
let operation = ArticleLoadOperation(bookID: result.bookID, articlePath: result.path)
GlobalQueue.shared.add(articleLoadOperation: operation)
}
// MARK: - DZNEmptyDataSet

View File

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

View File

@ -10,40 +10,6 @@
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Web View Controller-->
<scene sceneID="Plq-9G-fRT">
<objects>
<viewController storyboardIdentifier="WebViewController" id="hhM-UO-XZK" customClass="WebViewController" customModule="Kiwix" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="gaV-4e-09V"/>
<viewControllerLayoutGuide type="bottom" id="7Tg-lD-cCY"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="h0X-vc-qIf">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<webView contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="MjM-In-ehJ">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</webView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="MjM-In-ehJ" firstAttribute="top" secondItem="gaV-4e-09V" secondAttribute="bottom" constant="-64" id="2DB-FF-mbL"/>
<constraint firstAttribute="trailing" secondItem="MjM-In-ehJ" secondAttribute="trailing" id="2On-HI-gaZ"/>
<constraint firstItem="MjM-In-ehJ" firstAttribute="leading" secondItem="h0X-vc-qIf" secondAttribute="leading" id="qz0-jI-wLz"/>
<constraint firstItem="7Tg-lD-cCY" firstAttribute="top" secondItem="MjM-In-ehJ" secondAttribute="bottom" id="rmz-qH-LFY"/>
</constraints>
</view>
<simulatedNavigationBarMetrics key="simulatedTopBarMetrics" prompted="NO"/>
<connections>
<outlet property="webView" destination="MjM-In-ehJ" id="tud-dZ-b7q"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="3pg-HH-V5d" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="2474.4000000000001" y="-348.57571214392806"/>
</scene>
<!--Table Of Contents Controller-->
<scene sceneID="sFA-xU-ByH">
<objects>
@ -242,13 +208,29 @@
<view key="view" contentMode="scaleToFill" id="hRW-ET-fRA">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<webView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="MjM-In-ehJ">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</webView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="MjM-In-ehJ" firstAttribute="top" secondItem="KVH-WC-JGt" secondAttribute="bottom" constant="-64" id="1Om-9P-m57"/>
<constraint firstItem="MjM-In-ehJ" firstAttribute="leading" secondItem="hRW-ET-fRA" secondAttribute="leading" id="Kf8-Rn-ROp"/>
<constraint firstItem="UIX-e1-SUg" firstAttribute="top" secondItem="MjM-In-ehJ" secondAttribute="bottom" constant="-44" id="MRq-VY-WQT"/>
<constraint firstAttribute="trailing" secondItem="MjM-In-ehJ" secondAttribute="trailing" id="tIZ-J4-u59"/>
</constraints>
</view>
<navigationItem key="navigationItem" id="fuj-b7-iB1"/>
<simulatedToolbarMetrics key="simulatedBottomBarMetrics"/>
<connections>
<outlet property="webView" destination="MjM-In-ehJ" id="bOM-dN-Gc6"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Yrv-8u-IwG" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1657" y="-349"/>
<point key="canvasLocation" x="1656.8" y="-349.47526236881561"/>
</scene>
<!--Main Controller-->
<scene sceneID="nEL-IB-dp3">

View File

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

View File

@ -29,8 +29,6 @@
9726591D1D90A64600D1DFFB /* Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9726591C1D90A64500D1DFFB /* Notifications.swift */; };
972F81571DDBFC79008D7289 /* Search.swift in Sources */ = {isa = PBXBuildFile; fileRef = 972F81561DDBFC79008D7289 /* Search.swift */; };
972F81591DDC1B71008D7289 /* Controllers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 972F81581DDC1B71008D7289 /* Controllers.swift */; };
972F815B1DDCBF71008D7289 /* MainShowHide.swift in Sources */ = {isa = PBXBuildFile; fileRef = 972F815A1DDCBF71008D7289 /* MainShowHide.swift */; };
972F815D1DDCBFF9008D7289 /* WebViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 972F815C1DDCBFF9008D7289 /* WebViewController.swift */; };
9732075C1DD136BB00EDD3DC /* CoreDataExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9732075B1DD136BB00EDD3DC /* CoreDataExtension.swift */; };
9732079E1DD197EA00EDD3DC /* LibrarySplitViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97C005D71D64B99E004352E8 /* LibrarySplitViewController.swift */; };
9732079F1DD197F400EDD3DC /* CloudBooksController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97C005DB1D64BEFE004352E8 /* CloudBooksController.swift */; };
@ -190,8 +188,6 @@
9726591C1D90A64500D1DFFB /* Notifications.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Notifications.swift; sourceTree = "<group>"; };
972F81561DDBFC79008D7289 /* Search.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Search.swift; sourceTree = "<group>"; };
972F81581DDC1B71008D7289 /* Controllers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Controllers.swift; sourceTree = "<group>"; };
972F815A1DDCBF71008D7289 /* MainShowHide.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainShowHide.swift; sourceTree = "<group>"; };
972F815C1DDCBFF9008D7289 /* WebViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WebViewController.swift; sourceTree = "<group>"; };
9732075B1DD136BB00EDD3DC /* CoreDataExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreDataExtension.swift; sourceTree = "<group>"; };
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>"; };
@ -485,8 +481,6 @@
97BC0FBA1DD90A34004BBAD1 /* old */,
97BC0FBE1DD90A65004BBAD1 /* MainController.swift */,
97D0E98E1DDA12B30029530E /* MainDelegates.swift */,
972F815A1DDCBF71008D7289 /* MainShowHide.swift */,
972F815C1DDCBFF9008D7289 /* WebViewController.swift */,
97BC0FC11DD92B62004BBAD1 /* Buttons.swift */,
97BC0FBD1DD90A65004BBAD1 /* JSInjection.swift */,
972F81581DDC1B71008D7289 /* Controllers.swift */,
@ -1189,9 +1183,7 @@
97A1FD1C1D6F71D800A80EE2 /* KiwixURLProtocol.swift in Sources */,
97C2C26A1DDCC58500A9CC64 /* ArticleOperation.swift in Sources */,
973208261DD21E9C00EDD3DC /* CoreDataContainer.swift in Sources */,
972F815B1DDCBF71008D7289 /* MainShowHide.swift in Sources */,
97D6813F1D6F712800E5FA99 /* Article+CoreDataProperties.swift in Sources */,
972F815D1DDCBFF9008D7289 /* WebViewController.swift in Sources */,
97A1FD1D1D6F71D800A80EE2 /* URLResponseCache.swift in Sources */,
97A1FD441D6F728200A80EE2 /* Preference.swift in Sources */,
97D681311D6F70EC00E5FA99 /* 1.5.xcmappingmodel in Sources */,

View File

@ -75,18 +75,19 @@ class ArticleLoadOperation: Procedure {
let request = URLRequest(url: url)
OperationQueue.main.addOperation {
main.hideWelcome()
main.showWeb()
main.hideSearch(animated: self.animated)
_ = main.searchBar.resignFirstResponder()
main.presentingViewController?.dismiss(animated: self.animated, completion: nil)
//if main.traitCollection.horizontalSizeClass == .compact {main.hideTableOfContentsController()}
main.hideWelcome()
let webView = main.controllers.web.webView
if webView.request?.url != url {
let webView = main.webView
if webView?.request?.url != url {
webView?.loadRequest(request)
}
self.finish()
// //if main.traitCollection.horizontalSizeClass == .compact {main.hideTableOfContentsController()}
}
}
}