Skip to content

Commit

Permalink
#1 Display network error message on label, background color for refre…
Browse files Browse the repository at this point in the history
…sh control.
  • Loading branch information
Nhan Nguyen committed Jul 11, 2016
1 parent d3b871e commit 6e7575d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
Binary file not shown.
12 changes: 11 additions & 1 deletion Flicks/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@
<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">
<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"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" id="5YW-ik-Zwx">
<rect key="frame" x="0.0" y="91" width="320" height="413"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
Expand Down Expand Up @@ -129,6 +137,7 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES" flexibleMaxY="YES"/>
</imageView>
</subviews>
<color key="backgroundColor" red="1" green="0.68619643529999996" blue="0.046901713880000001" alpha="1" colorSpace="calibratedRGB"/>
</tableViewCellContentView>
<color key="backgroundColor" red="1" green="0.68619643529999996" blue="0.046901713880000001" alpha="1" colorSpace="calibratedRGB"/>
<connections>
Expand Down Expand Up @@ -163,14 +172,15 @@
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
</subviews>
<color key="backgroundColor" red="1" green="0.6861964353374983" blue="0.046901713876730367" alpha="1" colorSpace="calibratedRGB"/>
<color key="backgroundColor" red="1" green="0.68619643529999996" blue="0.046901713880000001" alpha="1" colorSpace="calibratedRGB"/>
</view>
<extendedEdge key="edgesForExtendedLayout" bottom="YES"/>
<tabBarItem key="tabBarItem" title="Item" id="FdD-cw-YFX"/>
<navigationItem key="navigationItem" id="WJd-Xq-xmn"/>
<connections>
<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
4 changes: 1 addition & 3 deletions Flicks/ImageHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ class ImageHandler {
loadImageFrom(smallImageURL, forImageView: posterImageView) { (smallImage) -> Void in
// load large image
dispatch_async(dispatch_get_main_queue(), { () -> Void in
loadImageFrom(largeImageURL, forImageView: posterImageView) { (largeImage) -> Void in
posterImageView.image = largeImage
}
loadImageFrom(largeImageURL, forImageView: posterImageView)
})

}
Expand Down
35 changes: 23 additions & 12 deletions Flicks/MoviesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import MBProgressHUD

class MoviesViewController: UIViewController {

@IBOutlet weak var networkErrorMessage: UILabel!

@IBOutlet var searchBar:UISearchBar!

@IBOutlet weak var moviesTable: UITableView!
Expand Down Expand Up @@ -50,7 +52,7 @@ class MoviesViewController: UIViewController {
override func viewDidLoad() {
showListMovies()
customizeNavigationBar()

self.moviesTable.dataSource = self
self.moviesTable.delegate = self

Expand All @@ -59,6 +61,9 @@ class MoviesViewController: UIViewController {

self.searchBar.delegate = self

refreshControl.backgroundColor = view.backgroundColor
refreshControlOnGridView.backgroundColor = view.backgroundColor

refreshControl.addTarget(self, action: #selector(loadMoviesData), forControlEvents: UIControlEvents.ValueChanged)

refreshControlOnGridView.addTarget(self, action: #selector(loadMoviesData), forControlEvents: UIControlEvents.ValueChanged)
Expand Down Expand Up @@ -97,6 +102,18 @@ class MoviesViewController: UIViewController {
}
}

func showNetworkErrorMessage() {
self.networkErrorMessage.hidden = false
self.moviesTable.hidden = true
self.moviesCollection.hidden = true
}

func hideNetworkErrorMessage() {
self.networkErrorMessage.hidden = true
self.moviesTable.hidden = false
self.moviesCollection.hidden = false
}

func showListMovies() {
self.moviesTable.hidden = false
self.moviesCollection.hidden = true
Expand Down Expand Up @@ -142,19 +159,13 @@ class MoviesViewController: UIViewController {
let task: NSURLSessionDataTask = session.dataTaskWithRequest(request,
completionHandler: { (dataOrNil, response, error) in

if (error != nil) {
// create the alert
let alert = UIAlertController(title: "Nework error!", message: error!.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)

// add an action (button)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

// show the alert
self.presentViewController(alert, animated: true, completion: nil)
self.view.hidden = true

if error != nil {
self.networkErrorMessage.text = error!.localizedDescription
self.showNetworkErrorMessage()
MBProgressHUD.hideHUDForView(self.view, animated: true)
} else {
let data = dataOrNil
self.hideNetworkErrorMessage()
if let responseDictionary = try! NSJSONSerialization.JSONObjectWithData(
data!, options:[]) as? NSDictionary {
self.movies = responseDictionary["results"] as! [NSDictionary]
Expand Down

0 comments on commit 6e7575d

Please sign in to comment.