Skip to content

Commit 5271a7b

Browse files
committed
Proper favorite toggling, updates (local) model
1 parent 46cee5c commit 5271a7b

2 files changed

Lines changed: 31 additions & 14 deletions

File tree

BrickHack-Mobile/Controllers/ScheduleTableViewController.swift

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,18 @@ class ScheduleTableViewController: UITableViewController {
184184
}
185185

186186
// Configure favorite accessory
187-
188-
// @TODO: Set toggle functionality
189-
// if isFavorite {
190-
// cell.accessoryView = UIImageView(image: UIImage(named: "filledStar"))
191-
192187
let favButton = FavoriteButton(type: .custom)
193-
favButton.setImage(UIImage(named: "filledStar"), for: .normal)
188+
// Set images
194189
favButton.addTarget(self, action: #selector(favoriteTapped(sender:)), for: .touchUpInside)
195-
favButton.tag = indexPath.row
196190
cell.accessoryView = favButton
197191
// Set custom properties
198192
favButton.section = indexPath.section
199193
favButton.row = indexPath.row
200194
favButton.sizeToFit()
201195

196+
// Now, toggle the stars that are favorited
197+
favButton.isSelected = isFavorite
198+
202199
// Confgure bubble
203200
cell.bubbleColor = UIColor.clear
204201
cell.bubbleWidth = 20.0
@@ -221,19 +218,25 @@ class ScheduleTableViewController: UITableViewController {
221218
// we use a subclassed UIButton, FavoriteButton, so we know what exact
222219
// event was pressed. (row is section-dependent)
223220
@objc func favoriteTapped(sender: UIButton) {
221+
222+
// Reject if improper call
224223
guard let favButton = sender as? FavoriteButton else {
225224
MessageHandler.showInvalidFavoriteButtonError()
226225
return
227226
}
228-
print("Tapped star at section \(favButton.section!), \(favButton.row!)")
229227

230-
// @TODO: Obvs read/write to/from server, but also:
231-
// @TODO: Change local data model, look for a table view delegate
232-
// @TODO: Check if margin updates when using forked TimelineTableViewCell eventually
233-
}
228+
// Update view
229+
// (FavoriteButton subclass handles this condition)
230+
favButton.isSelected = !favButton.isSelected
231+
232+
// Update model
233+
// (We handle this condition!)
234+
sampleData[favButton.section!]![favButton.row!].isFavorite = favButton.isSelected
235+
236+
// @TODO: Handle updating favorite with server
237+
// @TODO: Handle notifying users on their favorited events
234238

235-
override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
236-
print("defualt accButtonTapped: \(indexPath)")
239+
print("User did something to \(sampleData[favButton.section!]![favButton.row!].title)")
237240
}
238241

239242

BrickHack-Mobile/Views/FavoriteButton.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ class FavoriteButton: UIButton {
1212

1313
var section: Int?
1414
var row: Int?
15+
override var isSelected: Bool {
16+
didSet {
17+
self.toggle()
18+
}
19+
}
20+
21+
// Selected state things are not working out, so this is the best solution for now.
22+
func toggle() {
23+
if isSelected {
24+
self.setImage(UIImage(named: "filledStar"), for: .normal)
25+
} else {
26+
self.setImage(UIImage(named: "emptyStar"), for: .normal)
27+
}
28+
}
1529

1630
/*
1731
// Only override draw() if you perform custom drawing.

0 commit comments

Comments
 (0)