Skip to content

Commit

Permalink
fixed issue where the schedule page may crash after favoriting > 1 ev…
Browse files Browse the repository at this point in the history
…ent (#581)
  • Loading branch information
s0phialiu authored Feb 21, 2024
1 parent 07ccc47 commit 5dad79e
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions HackIllinois/ViewControllers/HIEventListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,20 @@ extension HIEventListViewController {

// MARK: - HIEventCellDelegate
extension HIEventListViewController: HIEventCellDelegate {
// This method is necessary to refresh the indecies and sections in the table view controller
func fetchAndReloadData() {
try? _fetchedResultsController?.performFetch()
tableView?.reloadData()
}

func eventCellDidSelectFavoriteButton(_ eventCell: HIEventCell) {
guard let indexPath = eventCell.indexPath,
let event = _fetchedResultsController?.object(at: indexPath) as? Event else { return }

guard let indexPath = eventCell.indexPath else { return }
// Ensure the section and item indices are within bounds
guard indexPath.section < _fetchedResultsController?.sections?.count ?? 0,
indexPath.item < _fetchedResultsController?.sections?[indexPath.section].numberOfObjects ?? 0 else {
return
}
guard let event = _fetchedResultsController?.object(at: indexPath) as? Event else { return }
guard let user = HIApplicationStateController.shared.user else { return }

let changeFavoriteStatusRequest: APIRequest<FollowStatus> =
Expand All @@ -93,6 +103,8 @@ extension HIEventListViewController: HIEventCellDelegate {
} else {
HILocalNotificationController.shared.unscheduleNotification(for: event)
}
// Call the fetchAndReloadData method to update the fetched results controller and reload the table view, otherwise index and section of next events will be incorrect
self.fetchAndReloadData()
}
case .failure(let error):
print(error, error.localizedDescription)
Expand Down

0 comments on commit 5dad79e

Please sign in to comment.