Skip to content

Commit cc43dcc

Browse files
author
Jurvis Tan
committed
refactor event favoriting to EventViewModel
1 parent d1ac18b commit cc43dcc

5 files changed

+34
-17
lines changed

FOSSAsia/EventViewModel.swift

+20-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ struct EventViewModel: EventTypePresentable, EventDetailsPresentable, EventDescr
4545
let endDateTime: Observable<NSDate>
4646
let favorite: Observable<Bool>
4747

48+
// MARK: - Services
49+
private var eventsService: EventsServiceProtocol
50+
4851
init (_ event: Event) {
4952
sessionId = Observable(event.id)
5053
track = Observable(event.trackCode.getTrackColor())
@@ -55,9 +58,25 @@ struct EventViewModel: EventTypePresentable, EventDetailsPresentable, EventDescr
5558
startDateTime = Observable(event.startDateTime)
5659
endDateTime = Observable(event.endDateTime)
5760
favorite = Observable(event.favorite)
61+
62+
// Dependency Injections
63+
eventsService = FossAsiaEventsService()
5864
}
5965

60-
func updateFavorite() {
66+
func favoriteEvent(completionHandler: (EventViewModel?, Error?) -> Void) {
67+
eventsService.toggleFavorite(self.sessionId.value) { (error) -> Void in
68+
guard error == nil else {
69+
completionHandler(nil, error)
70+
return
71+
}
72+
73+
self.updateFavorite()
74+
completionHandler(self, nil)
75+
}
76+
}
77+
78+
79+
private func updateFavorite() {
6180
favorite.value = !favorite.value
6281
}
6382
}

FOSSAsia/FavoritesScheduleViewController.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ extension FavoritesScheduleViewController {
4545
extension FavoritesScheduleViewController {
4646
func favoriteEvent(indexPath: NSIndexPath) {
4747
let eventViewModel = self.eventViewModelForIndexPath(indexPath)
48-
self.viewModel?.favoriteEvent(eventViewModel.sessionId.value)
49-
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left)
48+
eventViewModel.favoriteEvent { (viewModel, error) -> Void in
49+
if error == nil {
50+
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left)
51+
}
52+
}
5053
}
5154

5255
func swipeTableCell(cell: MGSwipeTableCell!, swipeButtonsForDirection direction: MGSwipeDirection, swipeSettings: MGSwipeSettings!, expansionSettings: MGSwipeExpansionSettings!) -> [AnyObject]! {

FOSSAsia/IndividualEvent.storyboard

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<view contentMode="scaleToFill" verticalHuggingPriority="251" verticalCompressionResistancePriority="749" placeholderIntrinsicWidth="600" placeholderIntrinsicHeight="488" translatesAutoresizingMaskIntoConstraints="NO" id="ov1-VO-pkC" userLabel="Content View">
2525
<rect key="frame" x="0.0" y="0.0" width="600" height="488"/>
2626
<subviews>
27-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="UQs-dw-Qdl" userLabel="Talk Description View" customClass="EventInfoView" customModule="FOSSAsia" customModuleProvider="target">
27+
<view contentMode="scaleToFill" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="UQs-dw-Qdl" userLabel="Talk Description View" customClass="EventInfoView" customModule="FOSSAsia" customModuleProvider="target">
2828
<rect key="frame" x="8" y="8" width="584" height="95"/>
2929
<subviews>
3030
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Santosh Viswanatham" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="b3B-gf-smR">
@@ -33,7 +33,7 @@
3333
<color key="textColor" red="0.3995272815" green="0.41182690859999999" blue="0.46382170919999999" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
3434
<nil key="highlightedColor"/>
3535
</label>
36-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Mozilla — JFDI Blk71" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="0XL-9Z-ufs">
36+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" misplaced="YES" text="Mozilla — JFDI Blk71" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="0XL-9Z-ufs">
3737
<rect key="frame" x="8" y="66" width="568" height="21"/>
3838
<fontDescription key="fontDescription" type="system" pointSize="17"/>
3939
<color key="textColor" red="0.3995272815" green="0.41182690859999999" blue="0.46382170919999999" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
@@ -67,7 +67,7 @@
6767
<outlet property="speakerLabel" destination="b3B-gf-smR" id="HsG-Ge-cJD"/>
6868
</connections>
6969
</view>
70-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="l1P-zW-uzf">
70+
<view contentMode="scaleToFill" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="l1P-zW-uzf">
7171
<rect key="frame" x="0.0" y="111" width="600" height="1"/>
7272
<color key="backgroundColor" red="0.89365547899999997" green="0.8943349719" blue="0.89376074080000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
7373
<constraints>
@@ -80,7 +80,7 @@
8080
</mask>
8181
</variation>
8282
</view>
83-
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" scrollEnabled="NO" editable="NO" translatesAutoresizingMaskIntoConstraints="NO" id="hnB-J0-qpW">
83+
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" misplaced="YES" scrollEnabled="NO" editable="NO" translatesAutoresizingMaskIntoConstraints="NO" id="hnB-J0-qpW">
8484
<rect key="frame" x="8" y="120" width="584" height="263"/>
8585
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
8686
<string key="text">Collaboration is an amazing option which adds beauty to work,reduces work burden,helps in Innovation and many more by enhancing teamwork option and enabling people to work collectively. TogetherJS is an Open source javascript library which adds collaborative features to a website.Thimble is an amazing Webmaker Tool which helps you to create Webpages and instantly preview your work.You can even publish the created webpages with a Single click.Awesomeness is when these two gets combine "Thimble" and "TogetherJS" where more than one person can work and code on a single webpage.Two or more people can work on a single webpage and instantly view the output.They can even discuss with the help of chatbox.

FOSSAsia/ScheduleViewController.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ extension ScheduleViewController {
7676
func favoriteEvent(indexPath: NSIndexPath) {
7777
weak var me = self
7878
let eventViewModel = me!.eventViewModelForIndexPath(indexPath)
79-
me!.viewModel?.favoriteEvent(eventViewModel.sessionId.value)
80-
self.tableView.reloadData()
79+
eventViewModel.favoriteEvent { (eventViewModel, error) -> Void in
80+
if error == nil {
81+
self.tableView.reloadData()
82+
}
83+
}
8184
}
8285

8386
func swipeTableCell(cell: MGSwipeTableCell!, swipeButtonsForDirection direction: MGSwipeDirection, swipeSettings: MGSwipeSettings!, expansionSettings: MGSwipeExpansionSettings!) -> [AnyObject]! {

FOSSAsia/ScheduleViewModel.swift

-8
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,6 @@ struct ScheduleViewModel: ScheduleCountPresentable {
5555
}
5656
}
5757

58-
func favoriteEvent(sessionId: Int) {
59-
eventsService.toggleFavorite(sessionId) { (error) -> Void in
60-
if error == nil {
61-
self.refresh()
62-
}
63-
}
64-
}
65-
6658
private func update(schedule: Schedule) {
6759
let tempEvents = schedule.events.map { event in
6860
return EventViewModel(event)

0 commit comments

Comments
 (0)