Skip to content

Commit

Permalink
#1 Display "No results found" for not found movie search.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nhan Nguyen committed Jul 11, 2016
1 parent 6e7575d commit d1ff038
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
Binary file not shown.
4 changes: 2 additions & 2 deletions Flicks/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<rect key="frame" x="0.0" y="64" width="320" height="504"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="networkError" textAlignment="center" lineBreakMode="tailTruncation" adjustsFontSizeToFit="NO" id="guh-2K-q9W">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="errorMessage" textAlignment="center" lineBreakMode="tailTruncation" adjustsFontSizeToFit="NO" id="guh-2K-q9W">
<rect key="frame" x="0.0" y="91" width="320" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="calibratedRGB"/>
Expand Down Expand Up @@ -178,9 +178,9 @@
<tabBarItem key="tabBarItem" title="Item" id="FdD-cw-YFX"/>
<navigationItem key="navigationItem" id="WJd-Xq-xmn"/>
<connections>
<outlet property="errorMessage" destination="guh-2K-q9W" id="ScC-fh-GXf"/>
<outlet property="moviesCollection" destination="5YW-ik-Zwx" id="ccs-aO-f7A"/>
<outlet property="moviesTable" destination="62r-IC-hUf" id="asW-dx-DAb"/>
<outlet property="networkErrorMessage" destination="guh-2K-q9W" id="q27-6P-Dxu"/>
<outlet property="searchBar" destination="I1c-tr-UdV" id="8xj-U8-MPU"/>
</connections>
</viewController>
Expand Down
19 changes: 10 additions & 9 deletions Flicks/MoviesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import MBProgressHUD

class MoviesViewController: UIViewController {

@IBOutlet weak var networkErrorMessage: UILabel!
@IBOutlet weak var errorMessage: UILabel!

@IBOutlet var searchBar:UISearchBar!

@IBOutlet weak var moviesTable: UITableView!
Expand Down Expand Up @@ -102,16 +102,18 @@ class MoviesViewController: UIViewController {
}
}

func showNetworkErrorMessage() {
self.networkErrorMessage.hidden = false
func showErrorMessage(message: String) {
self.errorMessage.hidden = false
self.moviesTable.hidden = true
self.moviesCollection.hidden = true
self.errorMessage.text = message
}

func hideNetworkErrorMessage() {
self.networkErrorMessage.hidden = true
func hideErrorMessage() {
self.errorMessage.hidden = true
self.moviesTable.hidden = false
self.moviesCollection.hidden = false
self.errorMessage.text = ""
}

func showListMovies() {
Expand Down Expand Up @@ -160,12 +162,11 @@ class MoviesViewController: UIViewController {
completionHandler: { (dataOrNil, response, error) in

if error != nil {
self.networkErrorMessage.text = error!.localizedDescription
self.showNetworkErrorMessage()
self.showErrorMessage(error!.localizedDescription)
MBProgressHUD.hideHUDForView(self.view, animated: true)
} else {
let data = dataOrNil
self.hideNetworkErrorMessage()
self.hideErrorMessage()
if let responseDictionary = try! NSJSONSerialization.JSONObjectWithData(
data!, options:[]) as? NSDictionary {
self.movies = responseDictionary["results"] as! [NSDictionary]
Expand Down
8 changes: 7 additions & 1 deletion Flicks/UISearchBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ extension MoviesViewController: UISearchBarDelegate {
let title = $0["original_title"] as! String
return title.lowercaseString.containsString(searchText.lowercaseString)
})
self.filteredMovies = filteredMovies
if filteredMovies.count > 0 {
hideErrorMessage()
self.filteredMovies = filteredMovies
} else {
showErrorMessage("No results found")
}

} else {
self.filteredMovies = NSArray.init(array: self.movies, copyItems: true) as! [NSDictionary]
}
Expand Down

0 comments on commit d1ff038

Please sign in to comment.