From bcc9ac4c26b24c6daad207f858b2a4991e3cb604 Mon Sep 17 00:00:00 2001 From: khcrysalis <97859147+khcrysalis@users.noreply.github.com> Date: Thu, 31 Oct 2024 23:11:37 -0700 Subject: [PATCH] fix: crash when no repos (oops!) --- feather.xcodeproj/project.pbxproj | 4 +- iOS/Views/Sources/SourcesViewController.swift | 37 ++++++++++++------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/feather.xcodeproj/project.pbxproj b/feather.xcodeproj/project.pbxproj index 6e40d96..9f457a7 100644 --- a/feather.xcodeproj/project.pbxproj +++ b/feather.xcodeproj/project.pbxproj @@ -1244,7 +1244,7 @@ "$(PROJECT_DIR)/Shared/Signing", "$(PROJECT_DIR)/Shared/Resources", ); - MARKETING_VERSION = 1.1.1; + MARKETING_VERSION = 1.1.2; PRODUCT_BUNDLE_IDENTIFIER = kh.crysalis.feather; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -1291,7 +1291,7 @@ "$(PROJECT_DIR)/Shared/Signing", "$(PROJECT_DIR)/Shared/Resources", ); - MARKETING_VERSION = 1.1.1; + MARKETING_VERSION = 1.1.2; PRODUCT_BUNDLE_IDENTIFIER = kh.crysalis.feather; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; diff --git a/iOS/Views/Sources/SourcesViewController.swift b/iOS/Views/Sources/SourcesViewController.swift index 7436bea..4a431e2 100644 --- a/iOS/Views/Sources/SourcesViewController.swift +++ b/iOS/Views/Sources/SourcesViewController.swift @@ -13,7 +13,7 @@ import SwiftUI class SourcesViewController: UITableViewController { - var sources: [Source]? + var sources: [Source] = [] public var searchController: UISearchController! let searchResultsTableViewController = SearchResultsTableViewController() @@ -63,7 +63,7 @@ extension SourcesViewController { case 0: return 1 case 1: - return sources?.count ?? 0 + return sources.count default: return 0 } @@ -76,7 +76,7 @@ extension SourcesViewController { if section == 1 { let headerWithButton = GroupedSectionHeader( title: String.localized("SOURCES_VIEW_CONTROLLER_REPOSITORIES"), - subtitle: String.localized(sources?.count ?? 0 > 1 ? "SOURCES_VIEW_CONTROLLER_NUMBER_OF_SOURCES_PLURAL" : "SOURCES_VIEW_CONTROLLER_NUMBER_OF_SOURCES", arguments: "\(sources?.count ?? 0)"), + subtitle: String.localized(sources.count > 1 ? "SOURCES_VIEW_CONTROLLER_NUMBER_OF_SOURCES_PLURAL" : "SOURCES_VIEW_CONTROLLER_NUMBER_OF_SOURCES", arguments: "\(sources.count)"), buttonTitle: String.localized("SOURCES_VIEW_CONTROLLER_ADD_SOURCES"), buttonAction: { let transferPreview = RepoViewController(sources: self.sources) @@ -100,8 +100,6 @@ extension SourcesViewController { override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "Cell") - - let source = sources![indexPath.row] cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 17) cell.detailTextLabel?.font = UIFont.systemFont(ofSize: 13) @@ -120,6 +118,9 @@ extension SourcesViewController { SectionIcons.sectionIcon(to: cell, with: repoIcon, backgroundColor: Preferences.appTintColor.uiColor.withAlphaComponent(0.7)) return cell case 1: + if sources.isEmpty { return cell } + let source = sources[indexPath.row] + cell.textLabel?.text = source.name ?? String.localized("UNKNOWN") cell.detailTextLabel?.text = source.sourceURL?.absoluteString @@ -138,7 +139,7 @@ extension SourcesViewController { override func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? { if indexPath.section == 1 { - let source = sources![indexPath.row] + let source = sources[indexPath.row] let configuration = UIContextMenuConfiguration(identifier: nil, actionProvider: { _ in return UIMenu(title: "", image: nil, identifier: nil, options: [], children: [ @@ -156,12 +157,12 @@ extension SourcesViewController { override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { if indexPath.section == 1 { let deleteAction = UIContextualAction(style: .destructive, title: String.localized("DELETE")) { (action, view, completionHandler) in - let sourceToRm = self.sources![indexPath.row] + let sourceToRm = self.sources[indexPath.row] CoreDataManager.shared.context.delete(sourceToRm) do { try CoreDataManager.shared.context.save() - self.sources?.remove(at: indexPath.row) - self.searchResultsTableViewController.sources = self.sources ?? [] + self.sources.remove(at: indexPath.row) + self.searchResultsTableViewController.sources = self.sources self.tableView.reloadSections(IndexSet(integer: 1), with: .automatic) } catch { Debug.shared.log(message: "trailingSwipeActionsConfigurationForRowAt.deleteAction", type: .error) @@ -180,18 +181,25 @@ extension SourcesViewController { } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - guard let sourcerow = sources?[indexPath.row] else { return } + if sources.isEmpty { + tableView.deselectRow(at: indexPath, animated: true) + return + } + + let sourcerow = sources[indexPath.row] switch indexPath.section { case 0: let savc = SourceAppViewController() savc.name = "All Repositories" - savc.uri = sources!.compactMap { $0.sourceURL } + savc.uri = sources.compactMap { $0.sourceURL } navigationController?.pushViewController(savc, animated: true) case 1: let savc = SourceAppViewController() savc.name = sourcerow.name - savc.uri = [sourcerow.sourceURL!] + if let sourceURL = sourcerow.sourceURL { + savc.uri = [sourceURL] + } navigationController?.pushViewController(savc, animated: true) default: break @@ -199,13 +207,14 @@ extension SourcesViewController { tableView.deselectRow(at: indexPath, animated: true) } + } extension SourcesViewController { @objc func fetch() {self.fetchSources()} func fetchSources() { sources = CoreDataManager.shared.getAZSources() - searchResultsTableViewController.sources = sources ?? [] + searchResultsTableViewController.sources = sources DispatchQueue.main.async { self.tableView.reloadSections(IndexSet(integer: 1), with: .automatic) } @@ -220,7 +229,7 @@ extension SourcesViewController: UISearchControllerDelegate, UISearchBarDelegate self.searchController.delegate = self self.searchController.searchBar.placeholder = String.localized("SOURCES_VIEW_CONTROLLER_SEARCH_SOURCES") self.searchController.searchResultsUpdater = searchResultsTableViewController - searchResultsTableViewController.sources = sources ?? [] + searchResultsTableViewController.sources = sources self.navigationItem.searchController = searchController self.definesPresentationContext = true self.navigationItem.hidesSearchBarWhenScrolling = false