Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unfavorite event crash on schedule page #581

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading