diff --git a/MyBoxOffice.xcodeproj/project.pbxproj b/MyBoxOffice.xcodeproj/project.pbxproj index 41d0585..6c40427 100644 --- a/MyBoxOffice.xcodeproj/project.pbxproj +++ b/MyBoxOffice.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + E92D950A21F4B07C00FB386B /* Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = E92D950921F4B07C00FB386B /* Extension.swift */; }; EC352FCA21BD0D1100433AAD /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC352FC921BD0D1100433AAD /* AppDelegate.swift */; }; EC352FCC21BD0D1100433AAD /* TableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC352FCB21BD0D1100433AAD /* TableViewController.swift */; }; EC352FCF21BD0D1100433AAD /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = EC352FCD21BD0D1100433AAD /* Main.storyboard */; }; @@ -22,6 +23,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + E92D950921F4B07C00FB386B /* Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extension.swift; sourceTree = ""; }; EC352FC621BD0D1100433AAD /* MyBoxOffice.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MyBoxOffice.app; sourceTree = BUILT_PRODUCTS_DIR; }; EC352FC921BD0D1100433AAD /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; EC352FCB21BD0D1100433AAD /* TableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewController.swift; sourceTree = ""; }; @@ -81,6 +83,7 @@ EC352FDB21BD100400433AAD /* CollectionViewController.swift */, EC352FD021BD0D1100433AAD /* Assets.xcassets */, EC352FD521BD0D1100433AAD /* Info.plist */, + E92D950921F4B07C00FB386B /* Extension.swift */, ); path = MyBoxOffice; sourceTree = ""; @@ -160,6 +163,7 @@ EC352FCC21BD0D1100433AAD /* TableViewController.swift in Sources */, EC352FE021BD1F3E00433AAD /* Request.swift in Sources */, ECFBFF6C21C0CE0200B6CD11 /* MoviesCollectionViewCell.swift in Sources */, + E92D950A21F4B07C00FB386B /* Extension.swift in Sources */, ECFC304721C6169B006E8586 /* OrderType.swift in Sources */, EC352FE221BD1F4B00433AAD /* Model.swift in Sources */, EC352FDE21BD105000433AAD /* InfoViewController.swift in Sources */, diff --git a/MyBoxOffice/CollectionViewController.swift b/MyBoxOffice/CollectionViewController.swift index 4574d66..ba478b9 100644 --- a/MyBoxOffice/CollectionViewController.swift +++ b/MyBoxOffice/CollectionViewController.swift @@ -94,12 +94,7 @@ class CollectionViewController: UIViewController, UICollectionViewDataSource, UI NotificationCenter.default.addObserver(self, selector: #selector(self.didRecieveMovieNotification(_:)), name: DidReceiveMoviesNotification, object: nil) - requestMovies(){ - (error) in - if let error = error { - self.AlertFailMessage(errorMessage: error.localizedDescription) - } - } + requestMoviesWithEscaping() } @objc func didRecieveMovieNotification(_ noti: Notification) { @@ -148,88 +143,11 @@ class CollectionViewController: UIViewController, UICollectionViewDataSource, UI } @objc func refresh() { - requestMovies(){ - (error) in - if let error = error { - self.AlertFailMessage(errorMessage: error.localizedDescription) - } - } + requestMoviesWithEscaping() self.collectionView.reloadData() self.refreshControl.endRefreshing() } - // MARK: - Action sheet - func showActionSheetController() { - let actionSheetController: UIAlertController - actionSheetController = UIAlertController(title: "정렬방식 선택", message: "영화를 어떤 순서로 정렬할까요?", preferredStyle: UIAlertControllerStyle.actionSheet) - - let reservationRateAction: UIAlertAction - reservationRateAction = UIAlertAction(title: "예매율", style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) in - OrderType.orderTypeProperty = 0 - requestMovies(){ - (error) in - if let error = error { - self.AlertFailMessage(errorMessage: error.localizedDescription) - } - } - }) - - let curationAction: UIAlertAction - curationAction = UIAlertAction(title: "큐레이션", style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) in - OrderType.orderTypeProperty = 1 - requestMovies(){ - (error) in - if let error = error { - self.AlertFailMessage(errorMessage: error.localizedDescription) - } - } - }) - - let releaseDateAction: UIAlertAction - releaseDateAction = UIAlertAction(title: "개봉일", style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) in - OrderType.orderTypeProperty = 2 - requestMovies(){ - (error) in - if let error = error { - self.AlertFailMessage(errorMessage: error.localizedDescription) - } - } - }) - - let cancelAction: UIAlertAction - cancelAction = UIAlertAction(title: "취소", style: UIAlertActionStyle.cancel, handler: nil) - - actionSheetController.addAction(reservationRateAction) - actionSheetController.addAction(curationAction) - actionSheetController.addAction(releaseDateAction) - actionSheetController.addAction(cancelAction) - - if UIDevice.current.userInterfaceIdiom == .pad { - if let popoverController = actionSheetController.popoverPresentationController { - popoverController.sourceView = self.view - popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0) - popoverController.permittedArrowDirections = [] - self.present(actionSheetController, animated: true, completion: nil) - } - } - else { - self.present(actionSheetController, animated: true, completion: nil) - } - } - - //Alert `errorMessage` in `viewController`. - func AlertFailMessage(errorMessage: String) { - let actionSheetController: UIAlertController - actionSheetController = UIAlertController(title: "ERROR!", message: errorMessage, preferredStyle: UIAlertControllerStyle.alert) - - let cancelAction: UIAlertAction - cancelAction = UIAlertAction(title: "닫기", style: UIAlertActionStyle.cancel, handler: nil) - - actionSheetController.addAction(cancelAction) - - self.present(actionSheetController, animated: true, completion: nil) - } - // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation diff --git a/MyBoxOffice/Extension.swift b/MyBoxOffice/Extension.swift new file mode 100644 index 0000000..b8b6c82 --- /dev/null +++ b/MyBoxOffice/Extension.swift @@ -0,0 +1,70 @@ +// +// Extension.swift +// MyBoxOffice +// +// Created by 정화 on 20/01/2019. +// Copyright © 2019 Wongeun Song. All rights reserved. +// + +import UIKit + +extension UIViewController { + func requestMoviesWithEscaping() { + requestMovies(){ + (error) in + if let error = error { + self.alertFailMessage(errorMessage: error.localizedDescription) + } + } + } + + //Alert `errorMessage` in `viewController`. + func alertFailMessage(errorMessage: String) { + let actionSheetController = UIAlertController(title: "ERROR!", message: errorMessage, preferredStyle: .alert) + let cancelAction = UIAlertAction(title: "닫기", style: .cancel, handler: nil) + actionSheetController.addAction(cancelAction) + self.present(actionSheetController, animated: true, completion: nil) + } + + // MARK: - Action sheet + func showActionSheetController() { + let actionSheetController = UIAlertController(title: "정렬방식 선택", message: "영화를 어떤 순서로 정렬할까요?", preferredStyle: UIAlertControllerStyle.actionSheet) + + let reservationRateAction = UIAlertAction(title: "예매율", style: .default, handler: { (action: UIAlertAction) in + OrderType.orderTypeProperty = 0 + self.requestMoviesWithEscaping() + }) + + let curationAction = UIAlertAction(title: "큐레이션", style: .default, handler: { (action: UIAlertAction) in + OrderType.orderTypeProperty = 1 + self.requestMoviesWithEscaping() + + }) + + let releaseDateAction = UIAlertAction(title: "개봉일", style: .default, handler: { (action: UIAlertAction) in + OrderType.orderTypeProperty = 2 + self.requestMoviesWithEscaping() + }) + + let cancelAction = UIAlertAction(title: "취소", style: .cancel, handler: nil) + + actionSheetController.addAction(reservationRateAction) + actionSheetController.addAction(curationAction) + actionSheetController.addAction(releaseDateAction) + actionSheetController.addAction(cancelAction) + + if UIDevice.current.userInterfaceIdiom == .pad { + if let popoverController = actionSheetController.popoverPresentationController { + popoverController.sourceView = self.view + popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0) + popoverController.permittedArrowDirections = [] + self.present(actionSheetController, animated: true, completion: nil) + } + } + else { + self.present(actionSheetController, animated: true, completion: nil) + } + } + + +} diff --git a/MyBoxOffice/MoviesTableViewCell.swift b/MyBoxOffice/MoviesTableViewCell.swift index 30b0282..80e68cb 100644 --- a/MyBoxOffice/MoviesTableViewCell.swift +++ b/MyBoxOffice/MoviesTableViewCell.swift @@ -18,4 +18,11 @@ class MoviesTableViewCell: UITableViewCell { var movieId: String? + override func prepareForReuse() { + titleLabel.text = nil + infoLabel.text = nil + dateLabel.text = nil + thumbImageView.image = nil + gradeImageView.image = nil + } } diff --git a/MyBoxOffice/Request.swift b/MyBoxOffice/Request.swift index d732053..86f1fab 100644 --- a/MyBoxOffice/Request.swift +++ b/MyBoxOffice/Request.swift @@ -131,3 +131,6 @@ func requestMovieComments(id: String, completion: @escaping(_ error: Error?) -> dataTask.resume() } + + + diff --git a/MyBoxOffice/TableViewController.swift b/MyBoxOffice/TableViewController.swift index a13a3fa..8be0c77 100644 --- a/MyBoxOffice/TableViewController.swift +++ b/MyBoxOffice/TableViewController.swift @@ -28,6 +28,7 @@ class TableViewController: UIViewController, UITableViewDataSource, UITableViewD return movies.count } + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: MoviesTableViewCell = tableView.dequeueReusableCell(withIdentifier: self.movieCellIdentifier, for: indexPath) as? MoviesTableViewCell ?? MoviesTableViewCell() @@ -94,17 +95,13 @@ class TableViewController: UIViewController, UITableViewDataSource, UITableViewD super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. - AddPullToRefresh() + addPullToRefresh() //Add Observer for movies data NotificationCenter.default.addObserver(self, selector: #selector(self.didRecieveMoviesNotification(_:)), name: DidReceiveMoviesNotification, object: nil) - requestMovies(){ - (error) in - if let error = error { - self.AlertFailMessage(errorMessage: error.localizedDescription) - } - } + self.requestMoviesWithEscaping() + } @objc func didRecieveMoviesNotification(_ noti: Notification) { @@ -126,99 +123,21 @@ class TableViewController: UIViewController, UITableViewDataSource, UITableViewD } // MARK: - Refresh - func AddPullToRefresh() { + func addPullToRefresh() { if #available(iOS 10.0, *) { tableView.refreshControl = refreshControl } else { tableView.addSubview(refreshControl) } - self.refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged) } @objc func refresh() { - requestMovies(){ - (error) in - if let error = error { - self.AlertFailMessage(errorMessage: error.localizedDescription) - } - } + self.requestMoviesWithEscaping() self.tableView.reloadData() self.refreshControl.endRefreshing() } - // MARK: - Action sheet - func showActionSheetController() { - let actionSheetController: UIAlertController - actionSheetController = UIAlertController(title: "정렬방식 선택", message: "영화를 어떤 순서로 정렬할까요?", preferredStyle: UIAlertControllerStyle.actionSheet) - - let reservationRateAction: UIAlertAction - reservationRateAction = UIAlertAction(title: "예매율", style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) in - OrderType.orderTypeProperty = 0 - requestMovies(){ - (error) in - if let error = error { - self.AlertFailMessage(errorMessage: error.localizedDescription) - } - } - }) - - let curationAction: UIAlertAction - curationAction = UIAlertAction(title: "큐레이션", style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) in - OrderType.orderTypeProperty = 1 - requestMovies(){ - (error) in - if let error = error { - self.AlertFailMessage(errorMessage: error.localizedDescription) - } - } - }) - - let releaseDateAction: UIAlertAction - releaseDateAction = UIAlertAction(title: "개봉일", style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) in - OrderType.orderTypeProperty = 2 - requestMovies(){ - (error) in - if let error = error { - self.AlertFailMessage(errorMessage: error.localizedDescription) - } - } - }) - - let cancelAction: UIAlertAction - cancelAction = UIAlertAction(title: "취소", style: UIAlertActionStyle.cancel, handler: nil) - - actionSheetController.addAction(reservationRateAction) - actionSheetController.addAction(curationAction) - actionSheetController.addAction(releaseDateAction) - actionSheetController.addAction(cancelAction) - - if UIDevice.current.userInterfaceIdiom == .pad { - if let popoverController = actionSheetController.popoverPresentationController { - popoverController.sourceView = self.view - popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0) - popoverController.permittedArrowDirections = [] - self.present(actionSheetController, animated: true, completion: nil) - } - } - else { - self.present(actionSheetController, animated: true, completion: nil) - } - } - - //Alert `errorMessage` in `viewController`. - func AlertFailMessage(errorMessage: String) { - let actionSheetController: UIAlertController - actionSheetController = UIAlertController(title: "ERROR!", message: errorMessage, preferredStyle: UIAlertControllerStyle.alert) - - let cancelAction: UIAlertAction - cancelAction = UIAlertAction(title: "닫기", style: UIAlertActionStyle.cancel, handler: nil) - - actionSheetController.addAction(cancelAction) - - self.present(actionSheetController, animated: true, completion: nil) - } - // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation