mirror of
https://github.com/kiwix/kiwix-apple.git
synced 2025-09-28 06:25:04 -04:00
preserve search text
This commit is contained in:
parent
eff2d2203a
commit
8bede3734f
@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// ControllerRetainer.swift
|
// Controllers.swift
|
||||||
// Kiwix
|
// Kiwix
|
||||||
//
|
//
|
||||||
// Created by Chris Li on 8/31/16.
|
// Created by Chris Li on 8/31/16.
|
||||||
@ -8,10 +8,10 @@
|
|||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
class ControllerRetainer {
|
class Controllers {
|
||||||
static let shared = ControllerRetainer()
|
static let shared = Controllers()
|
||||||
private init() {
|
private init() {
|
||||||
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ControllerRetainer.removeStrongReference), name: UIApplicationDidReceiveMemoryWarningNotification, object: nil)
|
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(Controllers.removeStrongReference), name: UIApplicationDidReceiveMemoryWarningNotification, object: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
@ -27,21 +27,27 @@ class ControllerRetainer {
|
|||||||
welcome = nil
|
welcome = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Main
|
||||||
|
|
||||||
|
var main: MainController {
|
||||||
|
return (UIApplication.appDelegate.window?.rootViewController as! UINavigationController).topViewController as! MainController
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Bookmark
|
// MARK: - Bookmark
|
||||||
|
|
||||||
private var bookmark: UINavigationController?
|
private var bookmark: UINavigationController?
|
||||||
|
|
||||||
class var bookmark: UINavigationController {
|
class var bookmark: UINavigationController {
|
||||||
let controller = ControllerRetainer.shared.bookmark ?? UIStoryboard(name: "Bookmark", bundle: nil).instantiateInitialViewController() as! UINavigationController
|
let controller = Controllers.shared.bookmark ?? UIStoryboard(name: "Bookmark", bundle: nil).instantiateInitialViewController() as! UINavigationController
|
||||||
ControllerRetainer.shared.bookmark = controller
|
Controllers.shared.bookmark = controller
|
||||||
return controller
|
return controller
|
||||||
}
|
}
|
||||||
|
|
||||||
private var bookmarkStar: BookmarkHUD?
|
private var bookmarkStar: BookmarkHUD?
|
||||||
|
|
||||||
class var bookmarkStar: BookmarkHUD {
|
class var bookmarkStar: BookmarkHUD {
|
||||||
let controller = ControllerRetainer.shared.bookmarkStar ?? UIStoryboard(name: "Bookmark", bundle: nil).instantiateViewControllerWithIdentifier("BookmarkHUD") as! BookmarkHUD
|
let controller = Controllers.shared.bookmarkStar ?? UIStoryboard(name: "Bookmark", bundle: nil).instantiateViewControllerWithIdentifier("BookmarkHUD") as! BookmarkHUD
|
||||||
ControllerRetainer.shared.bookmarkStar = controller
|
Controllers.shared.bookmarkStar = controller
|
||||||
return controller
|
return controller
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,8 +56,8 @@ class ControllerRetainer {
|
|||||||
private var library: UIViewController?
|
private var library: UIViewController?
|
||||||
|
|
||||||
class var library: UIViewController {
|
class var library: UIViewController {
|
||||||
let controller = ControllerRetainer.shared.library ?? UIStoryboard(name: "Library", bundle: nil).instantiateInitialViewController()!
|
let controller = Controllers.shared.library ?? UIStoryboard(name: "Library", bundle: nil).instantiateInitialViewController()!
|
||||||
ControllerRetainer.shared.library = controller
|
Controllers.shared.library = controller
|
||||||
return controller
|
return controller
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,8 +66,8 @@ class ControllerRetainer {
|
|||||||
private var search: SearchController?
|
private var search: SearchController?
|
||||||
|
|
||||||
class var search: SearchController {
|
class var search: SearchController {
|
||||||
let controller = ControllerRetainer.shared.search ?? UIStoryboard(name: "Search", bundle: nil).instantiateInitialViewController() as! SearchController
|
let controller = Controllers.shared.search ?? UIStoryboard(name: "Search", bundle: nil).instantiateInitialViewController() as! SearchController
|
||||||
ControllerRetainer.shared.search = controller
|
Controllers.shared.search = controller
|
||||||
return controller
|
return controller
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,8 +76,8 @@ class ControllerRetainer {
|
|||||||
private var setting: UIViewController?
|
private var setting: UIViewController?
|
||||||
|
|
||||||
class var setting: UIViewController {
|
class var setting: UIViewController {
|
||||||
let controller = ControllerRetainer.shared.setting ?? UIStoryboard(name: "Setting", bundle: nil).instantiateInitialViewController()!
|
let controller = Controllers.shared.setting ?? UIStoryboard(name: "Setting", bundle: nil).instantiateInitialViewController()!
|
||||||
ControllerRetainer.shared.setting = controller
|
Controllers.shared.setting = controller
|
||||||
return controller
|
return controller
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,8 +86,8 @@ class ControllerRetainer {
|
|||||||
private var welcome: WelcomeController?
|
private var welcome: WelcomeController?
|
||||||
|
|
||||||
class var welcome: WelcomeController {
|
class var welcome: WelcomeController {
|
||||||
let controller = ControllerRetainer.shared.welcome ?? UIStoryboard(name: "Welcome", bundle: nil).instantiateInitialViewController() as! WelcomeController
|
let controller = Controllers.shared.welcome ?? UIStoryboard(name: "Welcome", bundle: nil).instantiateInitialViewController() as! WelcomeController
|
||||||
ControllerRetainer.shared.welcome = controller
|
Controllers.shared.welcome = controller
|
||||||
return controller
|
return controller
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,7 +49,6 @@ class MainController: UIViewController {
|
|||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
|
|
||||||
webView.delegate = self
|
webView.delegate = self
|
||||||
searchBar.delegate = self
|
|
||||||
ZimMultiReader.shared.delegate = self
|
ZimMultiReader.shared.delegate = self
|
||||||
navigationItem.titleView = searchBar
|
navigationItem.titleView = searchBar
|
||||||
|
|
||||||
@ -241,13 +240,13 @@ class MainController: UIViewController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func showLibraryButtonTapped() {
|
func showLibraryButtonTapped() {
|
||||||
let controller = ControllerRetainer.library
|
let controller = Controllers.library
|
||||||
controller.modalPresentationStyle = .FullScreen
|
controller.modalPresentationStyle = .FullScreen
|
||||||
presentViewController(controller, animated: true, completion: nil)
|
presentViewController(controller, animated: true, completion: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func showSettingButtonTapped() {
|
func showSettingButtonTapped() {
|
||||||
let controller = ControllerRetainer.setting
|
let controller = Controllers.setting
|
||||||
controller.modalPresentationStyle = .FormSheet
|
controller.modalPresentationStyle = .FormSheet
|
||||||
presentViewController(controller, animated: true, completion: nil)
|
presentViewController(controller, animated: true, completion: nil)
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,7 @@ extension MainController: UIWebViewDelegate, SFSafariViewControllerDelegate, LPT
|
|||||||
let operation = UpdateWidgetDataSourceOperation()
|
let operation = UpdateWidgetDataSourceOperation()
|
||||||
GlobalQueue.shared.addOperation(operation)
|
GlobalQueue.shared.addOperation(operation)
|
||||||
|
|
||||||
let controller = ControllerRetainer.bookmarkStar
|
let controller = Controllers.bookmarkStar
|
||||||
controller.bookmarkAdded = article.isBookmarked
|
controller.bookmarkAdded = article.isBookmarked
|
||||||
controller.transitioningDelegate = self
|
controller.transitioningDelegate = self
|
||||||
controller.modalPresentationStyle = .OverFullScreen
|
controller.modalPresentationStyle = .OverFullScreen
|
||||||
@ -120,24 +120,7 @@ extension MainController: UIWebViewDelegate, SFSafariViewControllerDelegate, LPT
|
|||||||
GlobalQueue.shared.add(load: operation)
|
GlobalQueue.shared.add(load: operation)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - UISearchBarDelegate
|
|
||||||
|
|
||||||
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
|
|
||||||
showSearch(animated: true)
|
|
||||||
}
|
|
||||||
|
|
||||||
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
|
|
||||||
hideSearch(animated: true)
|
|
||||||
configureSearchBarPlaceHolder()
|
|
||||||
}
|
|
||||||
|
|
||||||
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
|
|
||||||
ControllerRetainer.search.startSearch(searchText, delayed: true)
|
|
||||||
}
|
|
||||||
|
|
||||||
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
|
|
||||||
ControllerRetainer.search.searchResultController?.selectFirstResultIfPossible()
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: - UIPopoverPresentationControllerDelegate
|
// MARK: - UIPopoverPresentationControllerDelegate
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ extension MainController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func showSearchResultController(animated animated: Bool) {
|
private func showSearchResultController(animated animated: Bool) {
|
||||||
let controller = ControllerRetainer.search
|
let controller = Controllers.search
|
||||||
guard !childViewControllers.contains(controller) else {return}
|
guard !childViewControllers.contains(controller) else {return}
|
||||||
addChildViewController(controller)
|
addChildViewController(controller)
|
||||||
controller.view.translatesAutoresizingMaskIntoConstraints = false
|
controller.view.translatesAutoresizingMaskIntoConstraints = false
|
||||||
@ -161,7 +161,7 @@ extension MainController {
|
|||||||
// MARK: - Show Bookmark
|
// MARK: - Show Bookmark
|
||||||
|
|
||||||
func showBookmarkTBVC() {
|
func showBookmarkTBVC() {
|
||||||
let controller = ControllerRetainer.bookmark
|
let controller = Controllers.bookmark
|
||||||
controller.modalPresentationStyle = .FormSheet
|
controller.modalPresentationStyle = .FormSheet
|
||||||
presentViewController(controller, animated: true, completion: nil)
|
presentViewController(controller, animated: true, completion: nil)
|
||||||
}
|
}
|
||||||
@ -169,7 +169,7 @@ extension MainController {
|
|||||||
// MARK: - Show/Hide Welcome
|
// MARK: - Show/Hide Welcome
|
||||||
|
|
||||||
func showWelcome() {
|
func showWelcome() {
|
||||||
let controller = ControllerRetainer.welcome
|
let controller = Controllers.welcome
|
||||||
controller.view.translatesAutoresizingMaskIntoConstraints = false
|
controller.view.translatesAutoresizingMaskIntoConstraints = false
|
||||||
addChildViewController(controller)
|
addChildViewController(controller)
|
||||||
view.addSubview(controller.view)
|
view.addSubview(controller.view)
|
||||||
|
@ -16,11 +16,7 @@ class SearchController: UIViewController, UISearchBarDelegate, UIGestureRecogniz
|
|||||||
@IBOutlet var tapGestureRecognizer: UITapGestureRecognizer!
|
@IBOutlet var tapGestureRecognizer: UITapGestureRecognizer!
|
||||||
var searchResultController: SearchResultController?
|
var searchResultController: SearchResultController?
|
||||||
|
|
||||||
private var searchText = "" {
|
private var searchTerm = "" // last searchTerm
|
||||||
didSet {
|
|
||||||
configureViewVisibility()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
@ -30,14 +26,7 @@ class SearchController: UIViewController, UISearchBarDelegate, UIGestureRecogniz
|
|||||||
|
|
||||||
override func viewWillAppear(animated: Bool) {
|
override func viewWillAppear(animated: Bool) {
|
||||||
super.viewWillAppear(animated)
|
super.viewWillAppear(animated)
|
||||||
configureViewVisibility()
|
configureViewVisibility(searchTerm)
|
||||||
}
|
|
||||||
|
|
||||||
override func viewWillDisappear(animated: Bool) {
|
|
||||||
super.viewWillDisappear(animated)
|
|
||||||
guard searchText != "" else {return}
|
|
||||||
Preference.recentSearchTerms.insert(searchText, atIndex: 0)
|
|
||||||
searchText = ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
|
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
|
||||||
@ -47,8 +36,8 @@ class SearchController: UIViewController, UISearchBarDelegate, UIGestureRecogniz
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func configureViewVisibility() {
|
func configureViewVisibility(searchTerm: String) {
|
||||||
if searchText == "" {
|
if searchTerm == "" {
|
||||||
searchResultTBVCContainer.hidden = true
|
searchResultTBVCContainer.hidden = true
|
||||||
tabControllerContainer.hidden = false
|
tabControllerContainer.hidden = false
|
||||||
} else {
|
} else {
|
||||||
@ -59,16 +48,17 @@ class SearchController: UIViewController, UISearchBarDelegate, UIGestureRecogniz
|
|||||||
|
|
||||||
// MARK: - Search
|
// MARK: - Search
|
||||||
|
|
||||||
func startSearch(searchText: String, delayed: Bool) {
|
func startSearch(searchTerm: String, delayed: Bool) {
|
||||||
self.searchText = searchText
|
guard self.searchTerm != searchTerm else {return}
|
||||||
|
self.searchTerm = searchTerm
|
||||||
|
configureViewVisibility(searchTerm)
|
||||||
if delayed {
|
if delayed {
|
||||||
let previousSearchText = searchText
|
|
||||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(275 * USEC_PER_SEC)), dispatch_get_main_queue()) {
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(275 * USEC_PER_SEC)), dispatch_get_main_queue()) {
|
||||||
guard previousSearchText == self.searchText else {return}
|
guard searchTerm == self.searchTerm else {return}
|
||||||
self.searchResultController?.startSearch(self.searchText)
|
self.searchResultController?.startSearch(self.searchTerm)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
searchResultController?.startSearch(searchText)
|
searchResultController?.startSearch(searchTerm)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,7 +49,7 @@
|
|||||||
</dict>
|
</dict>
|
||||||
</array>
|
</array>
|
||||||
<key>CFBundleVersion</key>
|
<key>CFBundleVersion</key>
|
||||||
<string>1.8.1343</string>
|
<string>1.8.1363</string>
|
||||||
<key>ITSAppUsesNonExemptEncryption</key>
|
<key>ITSAppUsesNonExemptEncryption</key>
|
||||||
<false/>
|
<false/>
|
||||||
<key>LSRequiresIPhoneOS</key>
|
<key>LSRequiresIPhoneOS</key>
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
// not used
|
||||||
class CustomSearchBar: UISearchBar, UITextFieldDelegate {
|
class CustomSearchBar: UISearchBar, UITextFieldDelegate {
|
||||||
|
|
||||||
override init(frame: CGRect) {
|
override init(frame: CGRect) {
|
||||||
@ -91,7 +91,13 @@ class CustomSearchBar: UISearchBar, UITextFieldDelegate {
|
|||||||
|
|
||||||
|
|
||||||
// Used in v1.4
|
// Used in v1.4
|
||||||
class SearchBar: UISearchBar {
|
class SearchBar: UISearchBar, UISearchBarDelegate {
|
||||||
|
var searchTerm: String?
|
||||||
|
|
||||||
|
private var textField: UITextField {
|
||||||
|
return valueForKey("searchField") as! UITextField
|
||||||
|
}
|
||||||
|
|
||||||
override init(frame: CGRect) {
|
override init(frame: CGRect) {
|
||||||
super.init(frame: frame)
|
super.init(frame: frame)
|
||||||
}
|
}
|
||||||
@ -106,5 +112,30 @@ class SearchBar: UISearchBar {
|
|||||||
self.autocapitalizationType = .None
|
self.autocapitalizationType = .None
|
||||||
self.placeholder = LocalizedStrings.search
|
self.placeholder = LocalizedStrings.search
|
||||||
self.returnKeyType = .Go
|
self.returnKeyType = .Go
|
||||||
|
self.delegate = self
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - UISearchBarDelegate
|
||||||
|
|
||||||
|
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
|
||||||
|
text = searchTerm
|
||||||
|
Controllers.shared.main.showSearch(animated: true)
|
||||||
|
let dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.05 * Double(NSEC_PER_SEC)))
|
||||||
|
dispatch_after(dispatchTime, dispatch_get_main_queue(), { [unowned self] in
|
||||||
|
self.textField.selectAll(nil)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
|
||||||
|
Controllers.shared.main.hideSearch(animated: true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
|
||||||
|
searchTerm = searchText
|
||||||
|
Controllers.search.startSearch(searchText, delayed: true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
|
||||||
|
Controllers.search.searchResultController?.selectFirstResultIfPossible()
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -21,7 +21,7 @@
|
|||||||
<key>CFBundleSignature</key>
|
<key>CFBundleSignature</key>
|
||||||
<string>????</string>
|
<string>????</string>
|
||||||
<key>CFBundleVersion</key>
|
<key>CFBundleVersion</key>
|
||||||
<string>1.8.1347</string>
|
<string>1.8.1367</string>
|
||||||
<key>NSExtension</key>
|
<key>NSExtension</key>
|
||||||
<dict>
|
<dict>
|
||||||
<key>NSExtensionMainStoryboard</key>
|
<key>NSExtensionMainStoryboard</key>
|
||||||
|
@ -56,7 +56,7 @@
|
|||||||
moduleName = "Kiwix"
|
moduleName = "Kiwix"
|
||||||
usesParentBreakpointCondition = "Yes"
|
usesParentBreakpointCondition = "Yes"
|
||||||
urlString = "file:///Volumes/Data/Developer/Kiwix/Kiwix/Operations/BookmarkMigrationOperation.swift"
|
urlString = "file:///Volumes/Data/Developer/Kiwix/Kiwix/Operations/BookmarkMigrationOperation.swift"
|
||||||
timestampString = "496789129.532637"
|
timestampString = "496791025.863771"
|
||||||
startingColumnNumber = "9223372036854775807"
|
startingColumnNumber = "9223372036854775807"
|
||||||
endingColumnNumber = "9223372036854775807"
|
endingColumnNumber = "9223372036854775807"
|
||||||
startingLineNumber = "27"
|
startingLineNumber = "27"
|
||||||
@ -71,7 +71,7 @@
|
|||||||
moduleName = "Kiwix"
|
moduleName = "Kiwix"
|
||||||
usesParentBreakpointCondition = "Yes"
|
usesParentBreakpointCondition = "Yes"
|
||||||
urlString = "file:///Volumes/Data/Developer/Kiwix/Kiwix/Operations/BookmarkMigrationOperation.swift"
|
urlString = "file:///Volumes/Data/Developer/Kiwix/Kiwix/Operations/BookmarkMigrationOperation.swift"
|
||||||
timestampString = "496789129.534098"
|
timestampString = "496791025.865063"
|
||||||
startingColumnNumber = "9223372036854775807"
|
startingColumnNumber = "9223372036854775807"
|
||||||
endingColumnNumber = "9223372036854775807"
|
endingColumnNumber = "9223372036854775807"
|
||||||
startingLineNumber = "27"
|
startingLineNumber = "27"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user