Skip to content

Commit ac123e1

Browse files
author
Jurvis Tan
committed
favoriting functionality done
1 parent b85aeb0 commit ac123e1

34 files changed

+257
-84
lines changed

FOSSAsia.xcodeproj/project.pbxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
28091E901C64B0B900E789CD /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
5959
28091E921C64B61C00E789CD /* RangeExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RangeExtension.swift; sourceTree = "<group>"; };
6060
28091E9A1C64CC1A00E789CD /* TrackViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TrackViewModel.swift; sourceTree = "<group>"; };
61-
280DE5281C62FCB200E64020 /* Podfile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Podfile; sourceTree = "<group>"; };
61+
280DE5281C62FCB200E64020 /* Podfile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Podfile; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.ruby; };
6262
2859B0FD1C5DF8D7009656FC /* EventViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EventViewController.swift; sourceTree = "<group>"; };
6363
2859B0FF1C5DFD09009656FC /* Colors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Colors.swift; sourceTree = "<group>"; };
6464
2859B1011C5E29DC009656FC /* EventInfoView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EventInfoView.swift; sourceTree = "<group>"; };
Binary file not shown.
Binary file not shown.
Binary file not shown.

FOSSAsia/Assets.xcassets/cal_selected.imageset/Contents.json FOSSAsia/Assets.xcassets/cell_favorite.imageset/Contents.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
"images" : [
33
{
44
"idiom" : "universal",
5-
"filename" : "cal_selected.png",
5+
"filename" : "cell_favorite.png",
66
"scale" : "1x"
77
},
88
{
99
"idiom" : "universal",
10-
"filename" : "cal_selected@2x.png",
10+
"filename" : "cell_favorite@2x.png",
1111
"scale" : "2x"
1212
},
1313
{
1414
"idiom" : "universal",
15-
"filename" : "cal_selected@3x.png",
15+
"filename" : "cell_favorite@3x.png",
1616
"scale" : "3x"
1717
}
1818
],
Loading
Loading
Loading

FOSSAsia/Assets.xcassets/more_selected.imageset/Contents.json FOSSAsia/Assets.xcassets/cell_favorite_selected.imageset/Contents.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
"images" : [
33
{
44
"idiom" : "universal",
5-
"filename" : "more_selected.png",
5+
"filename" : "cell_favorite_selected.png",
66
"scale" : "1x"
77
},
88
{
99
"idiom" : "universal",
10-
"filename" : "more_selected@2x.png",
10+
"filename" : "cell_favorite_selected@2x.png",
1111
"scale" : "2x"
1212
},
1313
{
1414
"idiom" : "universal",
15-
"filename" : "more_selected@3x.png",
15+
"filename" : "cell_favorite_selected@3x.png",
1616
"scale" : "3x"
1717
}
1818
],
Loading
Loading
Loading
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

FOSSAsia/Assets.xcassets/favorites_selected.imageset/Contents.json FOSSAsia/Assets.xcassets/session_cell_favorite.imageset/Contents.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
"images" : [
33
{
44
"idiom" : "universal",
5-
"filename" : "favorites_selected.png",
5+
"filename" : "session_cell_favorite.png",
66
"scale" : "1x"
77
},
88
{
99
"idiom" : "universal",
10-
"filename" : "favorites_selected@2x.png",
10+
"filename" : "session_cell_favorite@2x.png",
1111
"scale" : "2x"
1212
},
1313
{
1414
"idiom" : "universal",
15-
"filename" : "favorites_selected@3x.png",
15+
"filename" : "session_cell_favorite@3x.png",
1616
"scale" : "3x"
1717
}
1818
],
Loading
Loading
Loading

FOSSAsia/Colors.swift

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ import Foundation
1111
struct Colors {
1212
static let mainRedColor = UIColor(hexString: "D90300")
1313
static let creamTintColor = UIColor(hexString: "FEFAD5")
14+
static let favoriteOrangeColor = UIColor(hexString: "F5A623")
1415
}

FOSSAsia/Event.swift

+2-16
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,13 @@ struct Event {
4747
}
4848
}
4949

50-
50+
let id: Int
5151
let trackCode: Track
5252
let title: String
5353
let shortDescription: String
5454
let speaker: Speaker?
5555
let location: String
5656
let startDateTime: NSDate
5757
let endDateTime: NSDate
58-
var favorite = false
59-
60-
init (trackCode: Track, title: String, shortDescription: String, speaker: Speaker?, location: String, startDateTime: NSDate, endDateTime: NSDate) {
61-
self.trackCode = trackCode
62-
self.title = title
63-
self.shortDescription = shortDescription
64-
self.speaker = speaker
65-
self.location = location
66-
self.startDateTime = startDateTime
67-
self.endDateTime = endDateTime
68-
}
69-
70-
mutating func setFavorite() {
71-
favorite = !favorite
72-
}
58+
var favorite: Bool
7359
}

FOSSAsia/EventCell.swift

+16-6
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,30 @@
77
//
88

99
import UIKit
10+
import MGSwipeTableCell
1011

1112
typealias EventCellWithTypePresentable = protocol<EventTypePresentable, EventDetailsPresentable>
1213

13-
class EventCell: UITableViewCell {
14+
class EventCell: MGSwipeTableCell {
15+
@IBOutlet weak var favoriteImage: UIImageView!
1416
@IBOutlet weak var typeView: UIView!
1517
@IBOutlet weak var titleLabel: UILabel!
1618
@IBOutlet weak var timingLabel: UILabel!
17-
private var delegate: EventCellWithTypePresentable?
19+
20+
private var viewModel: EventCellWithTypePresentable?
1821

1922
func configure(withPresenter presenter: EventCellWithTypePresentable) {
20-
delegate = presenter
21-
titleLabel.text = delegate!.eventName
22-
typeView.backgroundColor = delegate!.typeColor
23-
timingLabel.text = delegate!.timing
23+
viewModel = presenter
24+
titleLabel.text = viewModel!.eventName
25+
typeView.backgroundColor = viewModel!.typeColor
26+
timingLabel.text = viewModel!.timing
27+
if (viewModel!.isFavorite) {
28+
favoriteImage.transform = CGAffineTransformIdentity
29+
favoriteImage.alpha = 1.0
30+
} else {
31+
favoriteImage.transform = CGAffineTransformMakeScale(0.1, 0.1)
32+
favoriteImage.alpha = 0.0
33+
}
2434
}
2535

2636
}

FOSSAsia/EventViewModel.swift

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ protocol EventDetailsPresentable {
1717
var eventName: String {get}
1818
var speakerName: String {get}
1919
var timing: String {get}
20+
var isFavorite: Bool {get}
2021
}
2122

2223
protocol EventDescriptionPresentable {
2324
var eventDescription: String {get }
2425
}
2526

2627
struct EventViewModel: EventTypePresentable, EventDetailsPresentable, EventDescriptionPresentable {
28+
let sessionId: Observable<Int>
2729
let track: Observable<UIColor>
2830
let title: Observable<String>
2931
let shortDescription: Observable<String>
@@ -34,6 +36,7 @@ struct EventViewModel: EventTypePresentable, EventDetailsPresentable, EventDescr
3436
let favorite: Observable<Bool>
3537

3638
init (_ event: Event) {
39+
sessionId = Observable(event.id)
3740
track = Observable(event.trackCode.getTrackColor())
3841
title = Observable(event.title)
3942
shortDescription = Observable(event.shortDescription)
@@ -43,6 +46,10 @@ struct EventViewModel: EventTypePresentable, EventDetailsPresentable, EventDescr
4346
endDateTime = Observable(event.endDateTime)
4447
favorite = Observable(event.favorite)
4548
}
49+
50+
func updateFavorite() {
51+
favorite.value = !favorite.value
52+
}
4653
}
4754

4855
// MARK: - EventDescriptionPresentable Conformance
@@ -59,10 +66,10 @@ extension EventViewModel {
5966
extension EventViewModel {
6067
var eventName: String { return self.title.value }
6168
var speakerName: String { return (self.speaker.value?.name)! }
62-
6369
var timing: String {
6470
let startTime = self.startDateTime.value.formattedDateWithFormat("HH:mm")
6571
let endTime = self.endDateTime.value.formattedDateWithFormat("HH:mm")
6672
return "\(startTime) - \(endTime) - \(self.location.value)"
6773
}
74+
var isFavorite: Bool { return self.favorite.value }
6875
}

FOSSAsia/EventsServiceProtocol.swift

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ typealias EventCompletionHandler = ([Event]?, Error?) -> Void
1212

1313
protocol EventsServiceProtocol {
1414
func retrieveEventsInfo(trackIds: [Int]?, completionHandler: EventCompletionHandler)
15+
func toggleFavorite(sessionId: Int, completionHandler: EventsServiceCommitmentCompletionHandler)
1516
}

FOSSAsia/EventsViewController.swift

+49-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
import UIKit
10+
import MGSwipeTableCell
1011

1112
class EventsViewController: UIViewController {
1213
private let kEventCellReuseIdentifier = "EventCell"
@@ -48,6 +49,10 @@ class EventsViewController: UIViewController {
4849
}
4950
}
5051
}
52+
53+
func eventViewModelForIndexPath(path: NSIndexPath) -> EventViewModel {
54+
return eventsArray[path.row]
55+
}
5156

5257
}
5358

@@ -58,8 +63,9 @@ extension EventsViewController: UITableViewDelegate {
5863
extension EventsViewController: UITableViewDataSource {
5964
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
6065
let cell = tableView.dequeueReusableCellWithIdentifier(kEventCellReuseIdentifier, forIndexPath: indexPath) as! EventCell
61-
let viewModel = eventsArray[indexPath.row]
62-
cell.configure(withPresenter: viewModel)
66+
let eventViewModel = eventsArray[indexPath.row]
67+
cell.configure(withPresenter: eventViewModel)
68+
cell.delegate = self
6369

6470
return cell
6571
}
@@ -76,4 +82,45 @@ extension EventsViewController: UITableViewDataSource {
7682
// have to patch in code because IB wasn't listening to me
7783
return 70
7884
}
85+
}
86+
87+
extension EventsViewController: MGSwipeTableCellDelegate {
88+
func swipeTableCell(cell: MGSwipeTableCell!, swipeButtonsForDirection direction: MGSwipeDirection, swipeSettings: MGSwipeSettings!, expansionSettings: MGSwipeExpansionSettings!) -> [AnyObject]! {
89+
swipeSettings.transition = .Border
90+
expansionSettings.buttonIndex = 0
91+
92+
weak var me = self
93+
let eventViewModel = me!.eventViewModelForIndexPath(me!.tableView.indexPathForCell(cell)!)
94+
95+
if direction == .LeftToRight {
96+
expansionSettings.fillOnTrigger = false
97+
expansionSettings.threshold = 2
98+
99+
100+
let faveButton = MGSwipeButton(title: "", icon: (eventViewModel.isFavorite ? UIImage(named: "cell_favorite_selected") : UIImage(named: "cell_favorite")), backgroundColor: Colors.favoriteOrangeColor!) { (sender: MGSwipeTableCell!) -> Bool in
101+
if let sessionCell = sender as? EventCell {
102+
UIView.animateWithDuration(0.3, animations: { () -> Void in
103+
if (eventViewModel.isFavorite) {
104+
sessionCell.favoriteImage.transform = CGAffineTransformMakeScale(0.1, 0.1)
105+
sessionCell.favoriteImage.alpha = 0.0
106+
} else {
107+
sessionCell.favoriteImage.transform = CGAffineTransformIdentity
108+
sessionCell.favoriteImage.alpha = 1.0
109+
}
110+
}, completion: { (done) -> Void in
111+
if done {
112+
me!.viewModel?.favoriteEvent(eventViewModel.sessionId.value)
113+
}
114+
})
115+
}
116+
return true
117+
}
118+
119+
faveButton.setPadding(CGFloat(20))
120+
cell.leftButtons = [faveButton]
121+
122+
return [faveButton]
123+
}
124+
return nil
125+
}
79126
}

0 commit comments

Comments
 (0)