Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add search bar #9

Merged
merged 1 commit into from Jul 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions TechMuzzJsonParser/Base.lproj/Main.storyboard
Expand Up @@ -35,9 +35,17 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<searchBar key="tableHeaderView" contentMode="redraw" id="U2v-5R-dn0">
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="P9y-gP-SO7" id="WYV-GJ-axL"/>
</connections>
</searchBar>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="TrackCell" rowHeight="100" id="WbV-oM-gLv">
<rect key="frame" x="0.0" y="28" width="375" height="100"/>
<rect key="frame" x="0.0" y="72" width="375" height="100"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="WbV-oM-gLv" id="P6A-Tr-Wli">
<rect key="frame" x="0.0" y="0.0" width="375" height="99"/>
Expand All @@ -50,7 +58,7 @@
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" tag="1" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="psH-wc-Lrg">
<rect key="frame" x="107" y="39" width="260" height="21"/>
<rect key="frame" x="107.5" y="39" width="259.5" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
Expand All @@ -76,6 +84,9 @@
</connections>
</tableView>
<navigationItem key="navigationItem" id="LlH-xO-TpV"/>
<connections>
<outlet property="searchBar" destination="U2v-5R-dn0" id="43s-BQ-l3R"/>
</connections>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="DJz-Uf-f9H" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
Expand Down
17 changes: 14 additions & 3 deletions TechMuzzJsonParser/ViewController.swift
Expand Up @@ -18,16 +18,25 @@ struct Post {
let previewUrl : String!
}

class TableViewController: UITableViewController {

class TableViewController: UITableViewController, UISearchBarDelegate {
var posts = [Post]()
var searchUrl = "https://api.spotify.com/v1/search?q=Imagine+Dragons&type=track"
let headers = [
"Authorization": "Bearer BQBNmIhnTrdid89TX4bZD0IVzcyk5IqDDYyisxp01J3Yvci8ZDbULQI9ljR_Lp2vD6_UArYTAk5ssswuBwn7Qt54Qz0Wny7ZEg1cHLjF2zM9UjCQ57k9HZ0wbG1srDzEHjB1uIelvgbeMALNUxBMFjVoq2xQf6SY1lxuLigiCoyF1i8Dky02V5T60N_udoCE_TPgixgEb2UND4lYtLBOWei-DkpD6JbZS4bRUKEWYij8zreWJEvDfmNxiKQC0xtgAtu8jxPWMXfZma3Gxd5a1TDi-3LPShiA6m0XOjQJ3Itu2LC6XyZdvIjkbSwVqWfbg3SeccXrdr4cCbWykh87wg",
"Authorization": "Bearer BQCzZMUotB9oYP6orcmeje79ly6ql1P6UOuM5oIHtiMPqe4lfRR96wbiU57ASbyPzZlwQ2sItzCZtHwvj5XDi7aebPRUhRZk0XS_HHEFWxkKAacFYAPfE3RfnxKxqrFISCchESIka_Ia0gZKsLT_HppltHI94qZNCn5Hh4B4nwaqO8H9lr6vHuGWjFmAUfPU-DYKRonnPbV-xJOkarG28BKh46Sdo_cOzvi1yDyaNXT3oRXX5NtErN3Ot-VXK9OvkfWzJGnOviCb508yxYFCTAwb7WFLwxwWiGeWvo341ejEFG8h8NiH2CWcKxLsbZjKeZVDgVInfIm-9iqLRgz9ZQ",
"Accept": "application/json"
]

@IBOutlet var searchBar: UISearchBar!
typealias JSONStandard = [String: AnyObject]

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
let keywords = searchBar.text
let finalKeywords = keywords?.replacingOccurrences(of: " ", with: "+")
searchUrl = "https://api.spotify.com/v1/search?q=\(finalKeywords!)&type=track"
callAlamo(url: searchUrl)
self.view.endEditing(true)
}

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -38,6 +47,8 @@ class TableViewController: UITableViewController {
func callAlamo(url: String) {
Alamofire.request(url, headers: headers).responseJSON(completionHandler: {
response in
self.posts.removeAll()
self.tableView.reloadData()
self.parseData(JSONData: response.data!)
})
}
Expand Down