Skip to content

Commit

Permalink
Refactor: Header,Footer delegate and ExpandCollapseTogglable #43
Browse files Browse the repository at this point in the history
- Modify protocol implement to abstract expand/collapse holiday list feature for MainVC, DetailVC
- Delete DetailHeaderDelegate
  • Loading branch information
jinios committed Oct 1, 2018
1 parent 3808be2 commit f01b4bc
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 53 deletions.
2 changes: 0 additions & 2 deletions MartHoliday/MartHoliday/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,6 @@
<connections>
<outlet property="address" destination="nFR-G1-Xtd" id="vOW-4w-DJJ"/>
<outlet property="businessHour" destination="wf3-kj-MA8" id="7ET-Pm-dWm"/>
<outlet property="detailContentView" destination="ctw-dV-lwK" id="YrN-zc-Td6"/>
<outlet property="detailScrollView" destination="ltX-Hb-Yau" id="fPq-qq-ULv"/>
<outlet property="mockUpMapview" destination="EPP-nl-lMa" id="bYw-Ff-2OO"/>
<outlet property="phoneNumberLabel" destination="Cdv-Ua-B8c" id="5zc-7Z-MTg"/>
<outlet property="tableView" destination="970-Km-ZJW" id="hhl-ld-Lvq"/>
Expand Down
6 changes: 6 additions & 0 deletions MartHoliday/MartHoliday/Model/FavoriteBranch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions MartHoliday/MartHoliday/View/HolidayHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? "정보가 없습니다 :("
Expand All @@ -26,7 +26,7 @@ class HolidayHeaderView: UITableViewHeaderFooterView {
}

@IBAction func tapButton(_ sender: Any) {
delegate?.toggleHeader()
delegate?.selectHeader(index: 0)
}

func setExpand(state: Bool) {
Expand Down
4 changes: 0 additions & 4 deletions MartHoliday/MartHoliday/View/HolidayTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

import UIKit

protocol DetailHeaderDelegate {
func toggleHeader()
}

class HolidayTableViewCell: UITableViewCell {

@IBOutlet weak var dateLabel: UILabel!
Expand Down
44 changes: 20 additions & 24 deletions MartHoliday/MartHoliday/ViewController/DetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}
Expand All @@ -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()
}

}

37 changes: 16 additions & 21 deletions MartHoliday/MartHoliday/ViewController/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,28 @@
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()
var backgroundView: SlideBackgroundView!
var slidetopView: SlideTopView!
var slideMenu: SlideMenu!
var slideOpenFlag: Bool?
var favoriteList = BranchList()
var favoriteData = [ExpandCollapseTogglable]()
var holidayData = [ExpandCollapseTogglable]()
var noDataView: NoDataView?

// MARK: override functions
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -178,45 +176,42 @@ 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
}
}

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
}

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
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
}
Expand Down

0 comments on commit f01b4bc

Please sign in to comment.