diff --git a/MartHoliday/MartHoliday/Base.lproj/Main.storyboard b/MartHoliday/MartHoliday/Base.lproj/Main.storyboard index 50f603a..8f1293a 100644 --- a/MartHoliday/MartHoliday/Base.lproj/Main.storyboard +++ b/MartHoliday/MartHoliday/Base.lproj/Main.storyboard @@ -403,8 +403,6 @@ - - diff --git a/MartHoliday/MartHoliday/Model/FavoriteBranch.swift b/MartHoliday/MartHoliday/Model/FavoriteBranch.swift index eed589d..94c53a4 100644 --- a/MartHoliday/MartHoliday/Model/FavoriteBranch.swift +++ b/MartHoliday/MartHoliday/Model/FavoriteBranch.swift @@ -11,12 +11,18 @@ import Foundation protocol ExpandCollapseTogglable { var isExpanded: Bool { get set } func toggleExpand() + func firstHoliday() -> String + func allHolidays() -> [String] + func branchName() -> String } class FavoriteBranch: ExpandCollapseTogglable { var isExpanded: Bool var branch: Branch private var holidays: [String] + lazy var isEmpty: Bool = { + return holidays.isEmpty + }() init(branch: Branch) { self.branch = branch diff --git a/MartHoliday/MartHoliday/View/HolidayHeaderView.swift b/MartHoliday/MartHoliday/View/HolidayHeaderView.swift index 5c72b75..8742211 100644 --- a/MartHoliday/MartHoliday/View/HolidayHeaderView.swift +++ b/MartHoliday/MartHoliday/View/HolidayHeaderView.swift @@ -13,7 +13,7 @@ class HolidayHeaderView: UITableViewHeaderFooterView { @IBOutlet weak var containerView: UIView! @IBOutlet weak var button: UIButton! @IBOutlet weak var dateLabel: UILabel! - var delegate: DetailHeaderDelegate? + var delegate: HeaderDelegate? func set(holiday: String?) { dateLabel.text = holiday ?? "정보가 없습니다 :(" @@ -26,7 +26,7 @@ class HolidayHeaderView: UITableViewHeaderFooterView { } @IBAction func tapButton(_ sender: Any) { - delegate?.toggleHeader() + delegate?.selectHeader(index: 0) } func setExpand(state: Bool) { diff --git a/MartHoliday/MartHoliday/View/HolidayTableViewCell.swift b/MartHoliday/MartHoliday/View/HolidayTableViewCell.swift index b5a69b6..13744e4 100644 --- a/MartHoliday/MartHoliday/View/HolidayTableViewCell.swift +++ b/MartHoliday/MartHoliday/View/HolidayTableViewCell.swift @@ -8,10 +8,6 @@ import UIKit -protocol DetailHeaderDelegate { - func toggleHeader() -} - class HolidayTableViewCell: UITableViewCell { @IBOutlet weak var dateLabel: UILabel! diff --git a/MartHoliday/MartHoliday/ViewController/DetailViewController.swift b/MartHoliday/MartHoliday/ViewController/DetailViewController.swift index d92b2ba..2face30 100644 --- a/MartHoliday/MartHoliday/ViewController/DetailViewController.swift +++ b/MartHoliday/MartHoliday/ViewController/DetailViewController.swift @@ -12,21 +12,23 @@ import SafariServices class DetailViewController: UIViewController, SFSafariViewControllerDelegate, MartMapViewHolder { @IBOutlet weak var tableViewHeight: NSLayoutConstraint! - - @IBOutlet weak var detailScrollView: UIScrollView! - @IBOutlet weak var detailContentView: UIView! - @IBOutlet weak var mockUpMapview: UIView! @IBOutlet weak var businessHour: UILabel! @IBOutlet weak var phoneNumberLabel: UILabel! @IBOutlet weak var address: UILabel! @IBOutlet weak var tableView: UITableView! var starButton: StarButton! - var isExpanded = false - var branchData: Branch? var mapView : NMapView? var mapViewDelegate: MartMapDelegate! + var branchData: Branch? { + didSet { + guard let branchData = branchData else { return } + holidayDatum = FavoriteBranch(branch: branchData) + } + } + var holidayDatum: ExpandCollapseTogglable? + override func viewDidLoad() { super.viewDidLoad() mapViewDelegate = MartMapDelegate(address: branchData!.address) @@ -196,11 +198,11 @@ extension DetailViewController: FavoriteTogglable { } -extension DetailViewController: UITableViewDelegate, UITableViewDataSource, DetailHeaderDelegate { +extension DetailViewController: UITableViewDelegate, UITableViewDataSource, HeaderDelegate { - func toggleHeader() { - self.isExpanded = !isExpanded - tableView.reloadSections([0], with: .automatic) + func selectHeader(index: Int) { + holidayDatum?.toggleExpand() + tableView.reloadSections([index], with: .automatic) tableViewHeight.constant = tableView.contentSize.height } @@ -210,8 +212,8 @@ extension DetailViewController: UITableViewDelegate, UITableViewDataSource, Deta func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "dateCell", for: indexPath) as! HolidayTableViewCell - guard let branchData = branchData else { return UITableViewCell() } - cell.setData(holiday:branchData.holidays[indexPath.row+1]) + guard let holidayDatum = holidayDatum else { return UITableViewCell() } + cell.setData(holiday: holidayDatum.allHolidays()[indexPath.row + 1]) return cell } @@ -220,9 +222,9 @@ extension DetailViewController: UITableViewDelegate, UITableViewDataSource, Deta } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - guard let branchData = branchData else { return 0 } - if isExpanded { - return branchData.holidays.count - 1 + guard let holidayDatum = holidayDatum else { return 0 } + if holidayDatum.isExpanded { + return holidayDatum.allHolidays().count - 1 } else { return 0 } @@ -233,20 +235,14 @@ extension DetailViewController: UITableViewDelegate, UITableViewDataSource, Deta } func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { - guard let branchData = branchData else { return nil } + guard let holidayDatum = holidayDatum else { return nil } guard let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: "detailHeader") as? HolidayHeaderView else { return nil } view.delegate = self - view.set(holiday: branchData.holidays.isEmpty ? nil:branchData.holidays[0]) - - let state = self.isExpanded - view.setExpand(state: state) + view.set(holiday: holidayDatum.firstHoliday()) + view.setExpand(state: holidayDatum.isExpanded) return view } - @objc func tapHeader() { - toggleHeader() - } - } diff --git a/MartHoliday/MartHoliday/ViewController/MainViewController.swift b/MartHoliday/MartHoliday/ViewController/MainViewController.swift index dc14c5c..a2fbf70 100644 --- a/MartHoliday/MartHoliday/ViewController/MainViewController.swift +++ b/MartHoliday/MartHoliday/ViewController/MainViewController.swift @@ -9,22 +9,20 @@ import UIKit protocol FavoriteConvertible { - var favoriteList: BranchList { get set } + var holidayData: [ExpandCollapseTogglable] { get set } func fetchFavoriteBranch(handler: @escaping(()->Void)) } protocol HeaderDelegate { - var favoriteData: [ExpandCollapseTogglable] { get set } func selectHeader(index: Int) - } protocol FooterDelegate { - var favoriteData: [ExpandCollapseTogglable] { get set } func toggleFooter(index: Int) } class MainViewController: UIViewController, FavoriteConvertible, HeaderDelegate, FooterDelegate { + typealias HolidayData = [ExpandCollapseTogglable] @IBOutlet weak var tableView: UITableView! let slideMenuManager = SlideMenuManager() @@ -32,8 +30,7 @@ class MainViewController: UIViewController, FavoriteConvertible, HeaderDelegate, var slidetopView: SlideTopView! var slideMenu: SlideMenu! var slideOpenFlag: Bool? - var favoriteList = BranchList() - var favoriteData = [ExpandCollapseTogglable]() + var holidayData = [ExpandCollapseTogglable]() var noDataView: NoDataView? // MARK: override functions @@ -115,12 +112,12 @@ class MainViewController: UIViewController, FavoriteConvertible, HeaderDelegate, } func toggleFooter(index: Int) { - favoriteData[index].toggleExpand() + holidayData[index].toggleExpand() tableView.reloadSections([index], with: .automatic) } func selectHeader(index: Int) { - guard let favorite = favoriteData[index] as? FavoriteBranch else { return } + guard let favorite = holidayData[index] as? FavoriteBranch else { return } let branch = favorite.branch guard let nextVC = storyboard?.instantiateViewController(withIdentifier: "detailVC") as? DetailViewController else { return } nextVC.branchData = branch @@ -144,14 +141,15 @@ class MainViewController: UIViewController, FavoriteConvertible, HeaderDelegate, URLSession.shared.dataTask(with: url) { (data, response, error) in if let response = response as? HTTPURLResponse, 200...299 ~= response.statusCode, let data = data { var branches = [BranchRawData]() + var favoriteList = BranchList() var mainFavorites = [FavoriteBranch]() do { branches = try JSONDecoder().decode([BranchRawData].self, from: data) - self.favoriteList = BranchList(branches: branches) + favoriteList = BranchList(branches: branches) - for fav in self.favoriteList.branches { + for fav in favoriteList.branches { mainFavorites.append(FavoriteBranch(branch: fav)) - self.favoriteData = mainFavorites + self.holidayData = mainFavorites } handler() } catch let error { @@ -178,10 +176,9 @@ extension MainViewController: UITableViewDelegate, UITableViewDataSource { } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - guard let branch = favoriteData[section] as? FavoriteBranch else { return 1 } - let countOfHoliday = branch.allHolidays().count + let countOfHoliday = holidayData[section].allHolidays().count guard countOfHoliday != 0 else { return 1 } - if favoriteData[section].isExpanded { + if holidayData[section].isExpanded { return countOfHoliday } else { return 1 @@ -189,26 +186,24 @@ extension MainViewController: UITableViewDelegate, UITableViewDataSource { } func numberOfSections(in tableView: UITableView) -> Int { - return favoriteData.count + return holidayData.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "dateCell", for: indexPath) as! MainTableViewCell - if favoriteData.count == 0 { + if holidayData.isEmpty { return UITableViewCell() } else { - guard let branch = favoriteData[indexPath.section] as? FavoriteBranch else { return UITableViewCell() } - cell.setData(text: branch.allHolidays()[indexPath.row]) + cell.setData(text: holidayData[indexPath.section].allHolidays()[indexPath.row]) return cell } } func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { guard let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "mainHeader") as? MainTableViewHeader else { return nil } - guard let branch = favoriteData[section] as? FavoriteBranch else { return nil } headerView.sectionIndex = section headerView.delegate = self - headerView.name = branch.branchName() + headerView.name = holidayData[section].branchName() return headerView } @@ -216,7 +211,7 @@ extension MainViewController: UITableViewDelegate, UITableViewDataSource { let view = MainTableViewFooter(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 40)) view.delegate = self view.sectionindex = section - let state = favoriteData[section].isExpanded + let state = holidayData[section].isExpanded view.setExpand(state: state) return view }