Skip to content

Commit

Permalink
fixed issue where the schedule page may crash after favoriting > 1 event
Browse files Browse the repository at this point in the history
  • Loading branch information
s0phialiu committed Feb 21, 2024
1 parent 07ccc47 commit 4cad6e1
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 4cad6e1

Please sign in to comment.