Skip to content

Commit

Permalink
fix: crash when no repos (oops!)
Browse files Browse the repository at this point in the history
  • Loading branch information
khcrysalis committed Nov 1, 2024
1 parent cca6b9a commit bcc9ac4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
4 changes: 2 additions & 2 deletions feather.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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 = "";
Expand Down
37 changes: 23 additions & 14 deletions iOS/Views/Sources/SourcesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import SwiftUI

class SourcesViewController: UITableViewController {

var sources: [Source]?
var sources: [Source] = []
public var searchController: UISearchController!
let searchResultsTableViewController = SearchResultsTableViewController()

Expand Down Expand Up @@ -63,7 +63,7 @@ extension SourcesViewController {
case 0:
return 1
case 1:
return sources?.count ?? 0
return sources.count
default:
return 0
}
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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: [
Expand All @@ -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)
Expand All @@ -180,32 +181,40 @@ 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
}

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)
}
Expand All @@ -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
Expand Down

0 comments on commit bcc9ac4

Please sign in to comment.