Skip to content

Commit

Permalink
Fix index out-of-range crash and ensure info table refresh on view di…
Browse files Browse the repository at this point in the history
…smissal
  • Loading branch information
bjorkert committed Aug 7, 2024
1 parent 4510923 commit c5c3980
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
3 changes: 3 additions & 0 deletions LoopFollow/InfoDisplaySettings/InfoDisplaySettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ struct InfoDisplaySettingsView: View {
.navigationBarItems(trailing: Button("Done") {
presentationMode.wrappedValue.dismiss()
})
.onDisappear {
NotificationCenter.default.post(name: NSNotification.Name("refresh"), object: nil)
}
}
}
}
12 changes: 11 additions & 1 deletion LoopFollow/InfoTable/InfoManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,19 @@ class InfoManager {
return UserDefaultsRepository.infoSort.value.filter { UserDefaultsRepository.infoVisible.value[$0] }.count
}

func dataForIndexPath(_ indexPath: IndexPath) -> InfoData {
func dataForIndexPath(_ indexPath: IndexPath) -> InfoData? {
let sortedAndVisibleIndexes = UserDefaultsRepository.infoSort.value.filter { UserDefaultsRepository.infoVisible.value[$0] }

guard indexPath.row < sortedAndVisibleIndexes.count else {
return nil
}

let infoIndex = sortedAndVisibleIndexes[indexPath.row]

guard infoIndex < tableData.count else {
return nil
}

return tableData[infoIndex]
}
}
14 changes: 10 additions & 4 deletions LoopFollow/ViewControllers/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,18 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)
let values = infoManager.dataForIndexPath(indexPath)
cell.textLabel?.text = values.name
cell.detailTextLabel?.text = values.value

if let values = infoManager.dataForIndexPath(indexPath) {
cell.textLabel?.text = values.name
cell.detailTextLabel?.text = values.value
} else {
cell.textLabel?.text = ""
cell.detailTextLabel?.text = ""
}

return cell
}

@objc func appMovedToBackground() {
// Allow screen to turn off
UIApplication.shared.isIdleTimerDisabled = false;
Expand Down

0 comments on commit c5c3980

Please sign in to comment.