-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathFavoritesScheduleViewController.swift
128 lines (106 loc) · 5.24 KB
/
FavoritesScheduleViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// FavoritesScheduleViewController.swift
// FOSSAsia
//
// Created by Jurvis Tan on 13/2/16.
// Copyright © 2016 FossAsia. All rights reserved.
//
import UIKit
import MGSwipeTableCell
import DZNEmptyDataSet
typealias FavoritesEmptyState = protocol<DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>
class FavoritesScheduleViewController: EventsBaseViewController, SwipeToFavoriteCellPresentable, FavoritesEmptyState {
struct StoryboardConstants {
static let storyboardName = "ScheduleVC"
static let viewControllerId = String(FavoritesScheduleViewController)
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.emptyDataSetSource = self
self.tableView.emptyDataSetDelegate = self
}
class func scheduleViewControllerFor(schedule: ScheduleViewModel) -> FavoritesScheduleViewController {
let storyboard = UIStoryboard(name: FavoritesScheduleViewController.StoryboardConstants.storyboardName, bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier(FavoritesScheduleViewController.StoryboardConstants.viewControllerId) as! FavoritesScheduleViewController
viewController.viewModel = schedule
return viewController
}
}
// MARK:- DZNEmptyDataSetSource Conformance
extension FavoritesScheduleViewController {
internal func titleForEmptyDataSet(scrollView: UIScrollView!) -> NSAttributedString! {
let text = "No Favorites Yet!"
let attributes = [NSFontAttributeName: UIFont.boldSystemFontOfSize(18.0),
NSForegroundColorAttributeName: UIColor.darkGrayColor()]
return NSAttributedString.init(string: text, attributes: attributes)
}
internal func buttonImageForEmptyDataSet(scrollView: UIScrollView!, forState state: UIControlState) -> UIImage! {
switch state {
case UIControlState.Highlighted:
return UIImage(named: "browse_events_btn_selected")
default:
return UIImage(named: "browse_events_btn")
}
}
}
// MARK:- DZNEmptyDataSetDelegate Conformance
extension FavoritesScheduleViewController {
internal func emptyDataSet(scrollView: UIScrollView!, didTapButton button: UIButton!) {
self.tabBarController?.selectedIndex = 0
}
}
// MARK:- UITableViewDelegate Conformance
extension FavoritesScheduleViewController {
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(EventsBaseViewController.kEventCellReuseIdentifier, forIndexPath: indexPath) as! EventCell
let eventViewModel = allEvents[indexPath.row]
cell.configure(withPresenter: eventViewModel)
cell.delegate = self
return cell
}
}
// MARK:- SwipeToFavoriteCellPresentable Conformance
extension FavoritesScheduleViewController {
func favoriteEvent(indexPath: NSIndexPath) {
let eventViewModel = self.eventViewModelForIndexPath(indexPath)
eventViewModel.favoriteEvent { [weak self] (viewModel, error) -> Void in
if error == nil {
self?.viewModel?.refresh()
self?.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left)
self?.tableView.reloadData() // refresh empty state layout
}
}
}
func swipeTableCell(cell: MGSwipeTableCell!, swipeButtonsForDirection direction: MGSwipeDirection, swipeSettings: MGSwipeSettings!, expansionSettings: MGSwipeExpansionSettings!) -> [AnyObject]! {
swipeSettings.transition = .Border
expansionSettings.buttonIndex = 0
weak var me = self
let eventViewModel = me!.eventViewModelForIndexPath(me!.tableView.indexPathForCell(cell)!)
if direction == .LeftToRight {
expansionSettings.fillOnTrigger = false
expansionSettings.threshold = 2
let faveButton = MGSwipeButton(title: "", icon: (eventViewModel.isFavorite ? UIImage(named: "cell_favorite_selected") : UIImage(named: "cell_favorite")), backgroundColor: Colors.favoriteOrangeColor!) { (sender: MGSwipeTableCell!) -> Bool in
if let sessionCell = sender as? EventCell {
UIView.animateWithDuration(0.3, animations: { () -> Void in
if (eventViewModel.isFavorite) {
sessionCell.favoriteImage.transform = CGAffineTransformMakeScale(0.1, 0.1)
sessionCell.favoriteImage.alpha = 0.0
} else {
sessionCell.favoriteImage.transform = CGAffineTransformIdentity
sessionCell.favoriteImage.alpha = 1.0
}
}, completion: { (done) -> Void in
if done {
self.favoriteEvent(me!.tableView.indexPathForCell(sender)!)
}
})
}
return false
}
faveButton.setPadding(CGFloat(20))
cell.leftButtons = [faveButton]
return [faveButton]
}
return nil
}
}