Skip to content

Commit

Permalink
+ Tap loading adjustments ~ Modifier Hell
Browse files Browse the repository at this point in the history
  • Loading branch information
buresdv committed Jan 4, 2025
1 parent 051c1d7 commit 8771533
Show file tree
Hide file tree
Showing 15 changed files with 431 additions and 287 deletions.
504 changes: 296 additions & 208 deletions Cork/ContentView.swift

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cork/CorkApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct CorkApp: App
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate: AppDelegate

@StateObject var brewData: BrewDataStorage = .init()
@StateObject var availableTaps: AvailableTaps = .init()
@StateObject var availableTaps: TapTracker = .init()

@StateObject var topPackagesTracker: TopPackagesTracker = .init()

Expand Down
27 changes: 27 additions & 0 deletions Cork/Errors/Tap Loading Error.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Tap Loading Error.swift
// Cork
//
// Created by David Bureš - P on 04.01.2025.
//

import Foundation

enum TapLoadingError: LocalizedError, Hashable
{
/// Could not read the folder that includes the taps
case couldNotAccessParentTapFolder(errorDetails: String)

/// Could not read a tap itself
case couldNotReadTapFolderContents(errorDetails: String)

var errorDescription: String?
{
switch self {
case .couldNotAccessParentTapFolder(let errorDetails):
return errorDetails
case .couldNotReadTapFolderContents(let errorDetails):
return errorDetails
}
}
}
162 changes: 94 additions & 68 deletions Cork/Logic/Load up Tapped Taps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,104 +8,130 @@
import Foundation
import CorkShared

@MainActor
func loadUpTappedTaps() async -> [BrewTap]
extension TapTracker
{
var finalAvailableTaps: [BrewTap] = .init()
@MainActor
func loadUpTappedTaps() async throws(TapLoadingError) -> [BrewTap]
{
var finalAvailableTaps: [BrewTap] = .init()

let contentsOfTapFolder: [URL] = try! getContentsOfFolder(targetFolder: AppConstants.shared.tapPath, options: .skipsHiddenFiles)
do
{
let contentsOfTapFolder: [URL] = try getContentsOfFolder(targetFolder: AppConstants.shared.tapPath, options: .skipsHiddenFiles)

AppConstants.shared.logger.debug("Contents of tap folder: \(contentsOfTapFolder)")
AppConstants.shared.logger.debug("Contents of tap folder: \(contentsOfTapFolder)")

for tapRepoParentURL in contentsOfTapFolder
{
AppConstants.shared.logger.debug("Tap repo: \(tapRepoParentURL)")
for tapRepoParentURL in contentsOfTapFolder
{
AppConstants.shared.logger.debug("Tap repo: \(tapRepoParentURL)")

let contentsOfTapRepoParent: [URL] = try! getContentsOfFolder(targetFolder: tapRepoParentURL, options: .skipsHiddenFiles)
do
{
let contentsOfTapRepoParent: [URL] = try getContentsOfFolder(targetFolder: tapRepoParentURL, options: .skipsHiddenFiles)

for repoURL in contentsOfTapRepoParent
{
let repoParentComponents: [String] = repoURL.pathComponents
for repoURL in contentsOfTapRepoParent
{
let repoParentComponents: [String] = repoURL.pathComponents

let repoParentName: String = repoParentComponents.penultimate()!
let repoParentName: String = repoParentComponents.penultimate()!

let repoNameRaw: String = repoParentComponents.last!
let repoName: String = .init(repoNameRaw.dropFirst(9))
let repoNameRaw: String = repoParentComponents.last!
let repoName: String = .init(repoNameRaw.dropFirst(9))

let fullTapName: String = "\(repoParentName)/\(repoName)"
let fullTapName: String = "\(repoParentName)/\(repoName)"

AppConstants.shared.logger.info("Full tap name: \(fullTapName)")
AppConstants.shared.logger.info("Full tap name: \(fullTapName)")

finalAvailableTaps.append(BrewTap(name: fullTapName))
}
}
finalAvailableTaps.append(BrewTap(name: fullTapName))
}
}
catch let tapFolderReadingError
{
throw TapLoadingError.couldNotReadTapFolderContents(errorDetails: tapFolderReadingError.localizedDescription)
}
}

let nonLocalBasicTaps: [BrewTap] = await withTaskGroup(of: BrewTap?.self)
{ taskGroup in
if finalAvailableTaps.filter({ $0.name == "homebrew/core" }).isEmpty
{
AppConstants.shared.logger.warning("Couldn't find homebrew/core in local taps")
taskGroup.addTask
{
let isCoreAdded: Bool = await checkIfTapIsAdded(tapToCheck: "homebrew/core")
if isCoreAdded
let nonLocalBasicTaps: [BrewTap] = await withTaskGroup(of: BrewTap?.self)
{ taskGroup in
if finalAvailableTaps.filter({ $0.name == "homebrew/core" }).isEmpty
{
AppConstants.shared.logger.info("homebrew/core is added, but not in local taps")
return BrewTap(name: "homebrew/core")
AppConstants.shared.logger.warning("Couldn't find homebrew/core in local taps")
taskGroup.addTask
{
let isCoreAdded: Bool = await self.checkIfTapIsAdded(tapToCheck: "homebrew/core")
if isCoreAdded
{
AppConstants.shared.logger.info("homebrew/core is added, but not in local taps")
return BrewTap(name: "homebrew/core")
}
else
{
AppConstants.shared.logger.warning("homebrew/core is not added and not in local taps")
return nil
}
}
}
else
{
AppConstants.shared.logger.warning("homebrew/core is not added and not in local taps")
return nil
AppConstants.shared.logger.info("Found homebrew/core in local taps")
}
}
}
else
{
AppConstants.shared.logger.info("Found homebrew/core in local taps")
}

if finalAvailableTaps.filter({ $0.name == "homebrew/cask" }).isEmpty
{
AppConstants.shared.logger.warning("Couldn't find homebrew/cask in local taps")
taskGroup.addTask
{
let isCaskAdded: Bool = await checkIfTapIsAdded(tapToCheck: "homebrew/cask")
if isCaskAdded
if finalAvailableTaps.filter({ $0.name == "homebrew/cask" }).isEmpty
{
return BrewTap(name: "homebrew/cask")
AppConstants.shared.logger.warning("Couldn't find homebrew/cask in local taps")
taskGroup.addTask
{
let isCaskAdded: Bool = await self.checkIfTapIsAdded(tapToCheck: "homebrew/cask")
if isCaskAdded
{
return BrewTap(name: "homebrew/cask")
}
else
{
AppConstants.shared.logger.warning("homebrew/cask is not added and not in local taps")
return nil
}
}
}
else
{
AppConstants.shared.logger.warning("homebrew/cask is not added and not in local taps")
return nil
AppConstants.shared.logger.info("Found homebrew/cask in local taps")
}

var nonLocalBasicTapsInternal: [BrewTap] = .init()

for await tap in taskGroup
{
if let tap = tap
{
nonLocalBasicTapsInternal.append(tap)
}
}

return nonLocalBasicTapsInternal
}
}
else
{
AppConstants.shared.logger.info("Found homebrew/cask in local taps")
}

var nonLocalBasicTapsInternal: [BrewTap] = .init()
finalAvailableTaps.append(contentsOf: nonLocalBasicTaps)

for await tap in taskGroup
return finalAvailableTaps
}
catch let tapFolderReadingError
{
if let tap = tap
let shouldStrictlyCheckForHomebrewErrors: Bool = UserDefaults.standard.bool(forKey: "strictlyCheckForHomebrewErrors")

if shouldStrictlyCheckForHomebrewErrors
{
nonLocalBasicTapsInternal.append(tap)
throw TapLoadingError.couldNotAccessParentTapFolder(errorDetails: tapFolderReadingError.localizedDescription)
}
else
{
return [.init(name: "homebrew/core"), .init(name: "homebrew/cask")]
}
}

return nonLocalBasicTapsInternal
}

finalAvailableTaps.append(contentsOf: nonLocalBasicTaps)

return finalAvailableTaps
}

private func checkIfTapIsAdded(tapToCheck _: String) async -> Bool
{
return true
private func checkIfTapIsAdded(tapToCheck _: String) async -> Bool
{
return true
}
}
2 changes: 2 additions & 0 deletions Cork/Logic/Package Loading/Load Up Installed Packages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import CorkShared
import Foundation

/// A representation of the loaded ``BrewPackage``s
/// Includes packages that were loaded properly, along those whose loading failed
typealias BrewPackages = Set<Result<BrewPackage, PackageLoadingError>>

extension BrewDataStorage
Expand Down
2 changes: 1 addition & 1 deletion Cork/Logic/Taps/Remove Tap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ enum UntapError: LocalizedError
}

@MainActor
func removeTap(name: String, availableTaps: AvailableTaps, appState: AppState, shouldApplyUninstallSpinnerToRelevantItemInSidebar: Bool = false) async throws
func removeTap(name: String, availableTaps: TapTracker, appState: AppState, shouldApplyUninstallSpinnerToRelevantItemInSidebar: Bool = false) async throws
{
var indexToReplaceGlobal: Int?

Expand Down
3 changes: 2 additions & 1 deletion Cork/Models/Brew Data Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class BrewDataStorage: ObservableObject
@Published var installedFormulae: BrewPackages = .init()
@Published var installedCasks: BrewPackages = .init()

// MARK: - Successfully loaded packages
/// Formulae that were successfuly loaded from disk
var successfullyLoadedFormulae: Set<BrewPackage>
{
Expand Down Expand Up @@ -106,7 +107,7 @@ extension BrewDataStorage
}

@MainActor
class AvailableTaps: ObservableObject
class TapTracker: ObservableObject
{
@Published var addedTaps: [BrewTap] = .init()
}
2 changes: 1 addition & 1 deletion Cork/Views/Menu Bar/Menu Bar Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct MenuBarItem: View
@EnvironmentObject var appState: AppState

@EnvironmentObject var brewData: BrewDataStorage
@EnvironmentObject var availableTaps: AvailableTaps
@EnvironmentObject var availableTaps: TapTracker

@EnvironmentObject var outdatedPackageTracker: OutdatedPackageTracker

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI
struct MenuBar_PackageOverview: View
{
@EnvironmentObject var brewData: BrewDataStorage
@EnvironmentObject var availableTaps: AvailableTaps
@EnvironmentObject var availableTaps: TapTracker

var body: some View
{
Expand Down
2 changes: 1 addition & 1 deletion Cork/Views/Sidebar/Components/Taps Section.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import CorkShared
struct TapsSection: View
{
@EnvironmentObject var appState: AppState
@EnvironmentObject var availableTaps: AvailableTaps
@EnvironmentObject var availableTaps: TapTracker

let searchText: String

Expand Down
2 changes: 1 addition & 1 deletion Cork/Views/Start Page/Start Page.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI
struct StartPage: View
{
@EnvironmentObject var brewData: BrewDataStorage
@EnvironmentObject var availableTaps: AvailableTaps
@EnvironmentObject var availableTaps: TapTracker

@EnvironmentObject var appState: AppState

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct PackageAndTapOverviewBox: View
@AppStorage("displayOnlyIntentionallyInstalledPackagesByDefault") var displayOnlyIntentionallyInstalledPackagesByDefault: Bool = true

@EnvironmentObject var brewData: BrewDataStorage
@EnvironmentObject var availableTaps: AvailableTaps
@EnvironmentObject var availableTaps: TapTracker

var body: some View
{
Expand Down
2 changes: 1 addition & 1 deletion Cork/Views/Taps/Add Tap View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct AddTapView: View

@State private var tappingError: TappingError = .other

@EnvironmentObject var availableTaps: AvailableTaps
@EnvironmentObject var availableTaps: TapTracker
@EnvironmentObject var outdatedPackageTracker: OutdatedPackageTracker

var body: some View
Expand Down
2 changes: 1 addition & 1 deletion Cork/Views/Taps/Sub-Views/Tap - Finished.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import CorkShared

struct AddTapFinishedView: View
{
@EnvironmentObject var availableTaps: AvailableTaps
@EnvironmentObject var availableTaps: TapTracker

let requestedTap: String

Expand Down
2 changes: 1 addition & 1 deletion Cork/Views/Taps/Tap Details/Tap Details.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct TapDetailView: View, Sendable
let tap: BrewTap

@EnvironmentObject var appState: AppState
@EnvironmentObject var availableTaps: AvailableTaps
@EnvironmentObject var availableTaps: TapTracker

@State private var isLoadingTapInfo: Bool = true

Expand Down

0 comments on commit 8771533

Please sign in to comment.