Skip to content

Commit 04e908b

Browse files
authored
Merge pull request #57 from opendocument-app/feature/search-ui
Fixed constraints in Main.storyboard. Call search functions.
2 parents f4e40e5 + f8c3227 commit 04e908b

2 files changed

Lines changed: 94 additions & 8 deletions

File tree

OpenDocumentReader/DocumentViewController.swift

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Firebase
1313
import GoogleMobileAds
1414

1515
// taken from: https://developer.apple.com/documentation/uikit/view_controllers/building_a_document_browser-based_app
16-
class DocumentViewController: UIViewController, DocumentDelegate, GADBannerViewDelegate {
16+
class DocumentViewController: UIViewController, DocumentDelegate, GADBannerViewDelegate, UISearchBarDelegate {
1717

1818
private var browserTransition: DocumentBrowserTransitioningDelegate?
1919
public var transitionController: UIDocumentBrowserTransitionController? {
@@ -33,6 +33,10 @@ class DocumentViewController: UIViewController, DocumentDelegate, GADBannerViewD
3333

3434
private var EXTENSION_WHITELIST = ["pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "rtf", "rtfd.zip", "csv", "txt", "jpg", "jpeg", "png", "gif", "svg", "pages", "pages.zip", "numbers", "numbers.zip", "key", "key.zip", "mp3", "mp4", "flv", "mkv", "3gp", "aac", "bmp", "css", "htm", "html", "js", "json", "mpeg", "oga", "ogv", "sh", "tif", "tiff", "weba", "webm", "webp", "xhtml", "xml"]
3535

36+
@IBOutlet weak var toolBar: UIToolbar!
37+
@IBOutlet weak var searchBarTop: NSLayoutConstraint!
38+
@IBOutlet weak var searchBarHeight: NSLayoutConstraint!
39+
@IBOutlet weak var searchBar: UISearchBar!
3640
@IBOutlet weak var segmentedControl: ScrollableSegmentedControl!
3741
private var initialSelect = false
3842

@@ -55,6 +59,11 @@ class DocumentViewController: UIViewController, DocumentDelegate, GADBannerViewD
5559

5660
override func viewWillAppear(_ animated: Bool) {
5761
super.viewWillAppear(animated)
62+
63+
searchBar.delegate = self
64+
searchBar.showsCancelButton = true
65+
66+
hideSearchBar()
5867

5968
barButtonItem.title = NSLocalizedString("back_to_documents", comment: "")
6069

@@ -145,6 +154,62 @@ class DocumentViewController: UIViewController, DocumentDelegate, GADBannerViewD
145154
return isFullscreen
146155
}
147156

157+
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
158+
hideSearchBar()
159+
}
160+
161+
func searchBarResultsListButtonClicked(_ searchBar: UISearchBar) {
162+
if let searchText = searchBar.text {
163+
findNext(searchText: searchText)
164+
}
165+
}
166+
167+
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
168+
findAll(searchText: searchText)
169+
}
170+
171+
@IBAction func searchButton(_ sender: UIBarButtonItem) {
172+
showSearchBar()
173+
}
174+
175+
private func showSearchBar() {
176+
searchBar.becomeFirstResponder()
177+
searchBar.isHidden = false
178+
searchBarHeight.constant = 56.0
179+
searchBarTop.constant = 0.0
180+
181+
toolBar.isHidden = true
182+
}
183+
184+
private func hideSearchBar() {
185+
searchBar.text = ""
186+
searchBar.isHidden = true
187+
searchBarHeight.constant = 0.0
188+
searchBarTop.constant = 40.0
189+
190+
toolBar.isHidden = false
191+
192+
self.view.endEditing(true)
193+
}
194+
195+
private func findNext(searchText: String) {
196+
webview?.evaluateJavaScript("odr.searchNext(\"" + searchText + "\")", completionHandler: { (value: Any!, error: Error!) -> Void in
197+
if error != nil {
198+
Crashlytics.crashlytics().record(error: error)
199+
fatalError("search failed")
200+
}
201+
})
202+
}
203+
204+
private func findAll(searchText: String) {
205+
webview?.evaluateJavaScript("odr.search(\"" + searchText + "\")", completionHandler: { (value: Any!, error: Error!) -> Void in
206+
if error != nil {
207+
Crashlytics.crashlytics().record(error: error)
208+
fatalError("search failed")
209+
}
210+
})
211+
}
212+
148213
@IBAction func returnToDocuments(_ sender: Any) {
149214
guard let doc = document else {
150215
fatalError("document is null")

OpenDocumentReader/Main.storyboard

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="15705" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16097" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
33
<device id="retina6_5" orientation="portrait" appearance="light"/>
44
<dependencies>
55
<deployment identifier="iOS"/>
6-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15706"/>
6+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
77
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
88
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
99
</dependencies>
@@ -50,6 +50,12 @@
5050
</connections>
5151
</barButtonItem>
5252
<barButtonItem style="plain" systemItem="flexibleSpace" id="lgr-dX-fUO"/>
53+
<barButtonItem title="Search Button" image="magnifyingglass" catalog="system" id="fGi-i7-iOR">
54+
<connections>
55+
<action selector="searchButton:" destination="M2k-L8-Olo" id="6fS-of-qcB"/>
56+
</connections>
57+
</barButtonItem>
58+
<barButtonItem width="10" style="plain" systemItem="fixedSpace" id="Di6-gg-JT6"/>
5359
<barButtonItem image="ellipsis" catalog="system" id="Vsd-IL-ujz">
5460
<inset key="imageInsets" minX="0.0" minY="8" maxX="0.0" maxY="0.0"/>
5561
<connections>
@@ -71,16 +77,23 @@
7177
</subviews>
7278
</stackView>
7379
<wkWebView contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="VF5-Do-xh9">
74-
<rect key="frame" x="0.0" y="138" width="414" height="758"/>
80+
<rect key="frame" x="0.0" y="190" width="414" height="706"/>
7581
<color key="backgroundColor" red="0.36078431370000003" green="0.38823529410000002" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
7682
<wkWebViewConfiguration key="configuration">
7783
<audiovisualMediaTypes key="mediaTypesRequiringUserActionForPlayback" none="YES"/>
7884
<wkPreferences key="preferences"/>
7985
</wkWebViewConfiguration>
8086
</wkWebView>
87+
<searchBar contentMode="redraw" showsSearchResultsButton="YES" translatesAutoresizingMaskIntoConstraints="NO" id="A5V-8n-dvk">
88+
<rect key="frame" x="0.0" y="84" width="414" height="56"/>
89+
<constraints>
90+
<constraint firstAttribute="height" constant="56" id="a6J-0N-RfG"/>
91+
</constraints>
92+
<textInputTraits key="textInputTraits"/>
93+
</searchBar>
8194
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="yut-lR-gq5" customClass="GADBannerView">
82-
<rect key="frame" x="0.0" y="88" width="414" height="50"/>
83-
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
95+
<rect key="frame" x="0.0" y="140" width="414" height="50"/>
96+
<color key="backgroundColor" systemColor="systemBrownColor" red="0.63529411759999999" green="0.51764705879999995" blue="0.36862745099999999" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
8497
<constraints>
8598
<constraint firstAttribute="height" constant="50" id="dMZ-rD-IOK"/>
8699
</constraints>
@@ -90,18 +103,21 @@
90103
<constraints>
91104
<constraint firstItem="VF5-Do-xh9" firstAttribute="top" secondItem="yut-lR-gq5" secondAttribute="bottom" id="2UY-wY-5ad"/>
92105
<constraint firstItem="9mi-yf-jXf" firstAttribute="leading" secondItem="HAk-oU-gAF" secondAttribute="leading" id="5Hh-GZ-aPw"/>
106+
<constraint firstItem="A5V-8n-dvk" firstAttribute="top" secondItem="HAk-oU-gAF" secondAttribute="top" constant="40" id="DuI-QF-5bJ"/>
93107
<constraint firstItem="sKS-nk-f6M" firstAttribute="top" secondItem="SIE-Uh-Zny" secondAttribute="top" id="HjQ-CN-3cF"/>
108+
<constraint firstItem="A5V-8n-dvk" firstAttribute="leading" secondItem="HAk-oU-gAF" secondAttribute="leading" id="L8H-vT-LAL"/>
94109
<constraint firstItem="HAk-oU-gAF" firstAttribute="trailing" secondItem="yut-lR-gq5" secondAttribute="trailing" id="NMY-4I-DYB"/>
95110
<constraint firstItem="VF5-Do-xh9" firstAttribute="leading" secondItem="HAk-oU-gAF" secondAttribute="leading" id="OvL-SW-INd"/>
96111
<constraint firstItem="VF5-Do-xh9" firstAttribute="trailing" secondItem="HAk-oU-gAF" secondAttribute="trailing" id="Qyj-ei-4NG"/>
97112
<constraint firstItem="yut-lR-gq5" firstAttribute="leading" secondItem="HAk-oU-gAF" secondAttribute="leading" id="WhK-2N-j4f"/>
98113
<constraint firstItem="N1x-QM-Qks" firstAttribute="top" secondItem="9mi-yf-jXf" secondAttribute="top" id="Yiq-pe-hl5"/>
114+
<constraint firstItem="HAk-oU-gAF" firstAttribute="trailing" secondItem="A5V-8n-dvk" secondAttribute="trailing" id="bci-XP-0gq"/>
99115
<constraint firstItem="N1x-QM-Qks" firstAttribute="bottom" secondItem="sKS-nk-f6M" secondAttribute="bottom" constant="44" id="fDF-Mx-Nxy"/>
100116
<constraint firstItem="VF5-Do-xh9" firstAttribute="bottom" secondItem="SIE-Uh-Zny" secondAttribute="bottom" id="ftB-8D-F6n"/>
101117
<constraint firstItem="sKS-nk-f6M" firstAttribute="bottom" secondItem="HAk-oU-gAF" secondAttribute="top" id="gWh-EO-FK9"/>
118+
<constraint firstItem="yut-lR-gq5" firstAttribute="top" secondItem="A5V-8n-dvk" secondAttribute="bottom" id="hWn-sT-gEW"/>
102119
<constraint firstItem="9mi-yf-jXf" firstAttribute="top" secondItem="SIE-Uh-Zny" secondAttribute="top" id="rED-wA-6gv"/>
103120
<constraint firstItem="9mi-yf-jXf" firstAttribute="trailing" secondItem="HAk-oU-gAF" secondAttribute="trailing" id="tPc-vX-Egd"/>
104-
<constraint firstItem="yut-lR-gq5" firstAttribute="top" secondItem="9mi-yf-jXf" secondAttribute="bottom" id="yhh-1s-AmR"/>
105121
</constraints>
106122
<viewLayoutGuide key="safeArea" id="HAk-oU-gAF"/>
107123
</view>
@@ -111,7 +127,11 @@
111127
<outlet property="barButtonItem" destination="yhO-7V-lbn" id="ach-z7-H9P"/>
112128
<outlet property="menuButton" destination="Vsd-IL-ujz" id="GoN-tp-egn"/>
113129
<outlet property="progressBar" destination="ePk-bQ-fV8" id="nz6-qw-ouf"/>
130+
<outlet property="searchBar" destination="A5V-8n-dvk" id="AIb-1s-JO6"/>
131+
<outlet property="searchBarHeight" destination="a6J-0N-RfG" id="TaC-Vz-Dkq"/>
132+
<outlet property="searchBarTop" destination="DuI-QF-5bJ" id="ces-q7-PG4"/>
114133
<outlet property="segmentedControl" destination="sIx-zo-9kG" id="pCn-x0-Z01"/>
134+
<outlet property="toolBar" destination="N1x-QM-Qks" id="db4-aA-GH6"/>
115135
<outlet property="webview" destination="VF5-Do-xh9" id="25W-hb-tHy"/>
116136
</connections>
117137
</viewController>
@@ -217,6 +237,7 @@
217237
</scene>
218238
</scenes>
219239
<resources>
220-
<image name="ellipsis" catalog="system" width="64" height="18"/>
240+
<image name="ellipsis" catalog="system" width="128" height="37"/>
241+
<image name="magnifyingglass" catalog="system" width="128" height="115"/>
221242
</resources>
222243
</document>

0 commit comments

Comments
 (0)