Skip to content

Commit

Permalink
~ Fuck this
Browse files Browse the repository at this point in the history
  • Loading branch information
buresdv committed Nov 19, 2024
1 parent 56b98bd commit da85c9e
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,25 @@ struct GetInstalledCasksIntent: AppIntent
{
let dummyBrewData: BrewDataStorage = await .init()

guard let installedFormulae: Set<BrewPackage> = await dummyBrewData.loadInstalledPackages(packageTypeToLoad: .cask, appState: AppState()) else
guard let installedCasks: BrewPackages = await dummyBrewData.loadInstalledPackages(packageTypeToLoad: .cask, appState: AppState()) else
{
throw IntentError.failedWhilePerformingIntent
}

/// Filter out all packages that gave an error
let validInstalledCasks: Set<BrewPackage> = Set(installedCasks.compactMap({ rawResult in
if case let .success(success) = rawResult {
return success
}
else
{
return nil
}
}))

AppConstants.shared.brewCaskPath.stopAccessingSecurityScopedResource()

let minimalPackages: [MinimalHomebrewPackage] = installedFormulae.map
let minimalPackages: [MinimalHomebrewPackage] = validInstalledCasks.map
{ package in
.init(name: package.name, type: .cask, installDate: package.installedOn, installedIntentionally: true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,25 @@ struct GetInstalledFormulaeIntent: AppIntent
{
let dummyBrewData: BrewDataStorage = await .init()

guard let installedFormulae: Set<BrewPackage> = await dummyBrewData.loadInstalledPackages(packageTypeToLoad: .formula, appState: AppState()) else
guard let installedFormulae: BrewPackages = await dummyBrewData.loadInstalledPackages(packageTypeToLoad: .formula, appState: AppState()) else
{
throw IntentError.failedWhilePerformingIntent
}

/// Filter out all packages that gave an error
let validInstalledFormulae: Set<BrewPackage> = Set(installedFormulae.compactMap { rawResult in
if case let .success(success) = rawResult {
return success
}
else
{
return nil
}
})

AppConstants.shared.brewCellarPath.stopAccessingSecurityScopedResource()

var minimalPackages: [MinimalHomebrewPackage] = installedFormulae.map
var minimalPackages: [MinimalHomebrewPackage] = validInstalledFormulae.map
{ package in
.init(name: package.name, type: .formula, installedIntentionally: package.installedIntentionally)
}
Expand Down
23 changes: 7 additions & 16 deletions Cork/Logic/Installation & Removal/Removal/Uninstall Packages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extension BrewDataStorage
/// Store the old navigation selection to see if it got updated in the middle of switching
let oldNavigationSelectionId: UUID? = appState.navigationTargetId

/*
if shouldApplyUninstallSpinnerToRelevantItemInSidebar
{
if package.type == .formula
Expand Down Expand Up @@ -54,6 +55,7 @@ extension BrewDataStorage
{
appState.isShowingUninstallationProgressView = true
}
*/

AppConstants.shared.logger.info("Will try to remove package \(package.name, privacy: .auto)")
var uninstallCommandOutput: TerminalOutput
Expand All @@ -74,7 +76,7 @@ extension BrewDataStorage
AppConstants.shared.logger.warning("Could not uninstall this package because it's a dependency")

/// If the uninstallation failed, change the status back to "not being modified"
resetPackageState(package: package)
//resetPackageState(package: package)

do
{
Expand All @@ -99,26 +101,13 @@ extension BrewDataStorage
appState.packageTryingToBeUninstalledWithSudo = package
appState.isShowingSudoRequiredForUninstallSheet = true

resetPackageState(package: package)
//resetPackageState(package: package)
}
else
{
AppConstants.shared.logger.info("Uninstalling can proceed")

switch package.type
{
case .formula:
withAnimation
{
self.removeFormulaFromTracker(withName: package.name)
}

case .cask:
withAnimation
{
self.removeCaskFromTracker(withName: package.name)
}
}
await synchronizeInstalledPackages(brewData: self)

if appState.navigationTargetId != nil
{
Expand All @@ -144,6 +133,7 @@ extension BrewDataStorage
}
}

/*
@MainActor
private func resetPackageState(package: BrewPackage)
{
Expand Down Expand Up @@ -172,4 +162,5 @@ extension BrewDataStorage
})
}
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func synchronizeInstalledPackages(brewData: BrewDataStorage) async
let dummyBrewData: BrewDataStorage = .init()

/// These have to use this dummy AppState, which forces them to not activate the "loading" animation. We don't want the entire thing to re-draw
let newFormulae: Set<BrewPackage> = await dummyBrewData.loadInstalledPackages(packageTypeToLoad: .formula, appState: dummyAppState) ?? .init()
let newCasks: Set<BrewPackage> = await dummyBrewData.loadInstalledPackages(packageTypeToLoad: .cask, appState: dummyAppState) ?? .init()
let newFormulae: BrewPackages = await dummyBrewData.loadInstalledPackages(packageTypeToLoad: .formula, appState: dummyAppState) ?? .init()
let newCasks: BrewPackages = await dummyBrewData.loadInstalledPackages(packageTypeToLoad: .cask, appState: dummyAppState) ?? .init()

if newFormulae.count != brewData.installedFormulae.count
{
Expand Down
57 changes: 35 additions & 22 deletions Cork/Logic/Tagging/Apply Tags to Package Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,43 @@
import Foundation
import CorkShared

@MainActor
func applyTagsToPackageTrackingArray(appState: AppState, brewData: BrewDataStorage) async throws
extension BrewDataStorage
{
for taggedName in appState.taggedPackageNames
@MainActor
func applyTags(appState: AppState) async throws
{
AppConstants.shared.logger.log("Will attempt to place package name \(taggedName, privacy: .public)")
brewData.installedFormulae = Set(brewData.installedFormulae.map
{ formula in
var copyFormula: BrewPackage = formula
if copyFormula.name == taggedName
{
copyFormula.changeTaggedStatus()
}
return copyFormula
})
for taggedName in appState.taggedPackageNames
{
AppConstants.shared.logger.log("Will attempt to place package name \(taggedName, privacy: .public)")
self.installedFormulae = Set(self.installedFormulae.map
{ formula in
switch formula
{
case .success(var brewPackage):
if brewPackage.name == taggedName
{
brewPackage.changeTaggedStatus()
}
return .success(brewPackage)
case .failure(let error):
return .failure(error)
}
})

brewData.installedCasks = Set(brewData.installedCasks.map
{ cask in
var copyCask: BrewPackage = cask
if copyCask.name == taggedName
{
copyCask.changeTaggedStatus()
}
return copyCask
})
self.installedCasks = Set(self.installedCasks.map
{ cask in
switch cask
{
case .success(var brewPackage):
if brewPackage.name == taggedName
{
brewPackage.changeTaggedStatus()
}
return .success(brewPackage)
case .failure(let error):
return .failure(error)
}
})
}
}
}
13 changes: 13 additions & 0 deletions Cork/Logic/Tagging/Change Package Tag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@
import Foundation
import CorkShared

/*
@MainActor
func changePackageTagStatus(package: BrewPackage, brewData: BrewDataStorage, appState: AppState) async
{
if package.type == .formula
{
brewData.installedFormulae = Set(brewData.installedFormulae.map
{ formula in
switch formula
{
case .success(let brewPackage):
if brewPackage.name == package.name
{
brewPackage.changeTaggedStatus()
}
return brewPackage
case .failure(let error):
return .failure(error)
}
var copyFormula: BrewPackage = formula
if copyFormula.name == package.name
{
Expand Down Expand Up @@ -49,3 +61,4 @@ func changePackageTagStatus(package: BrewPackage, brewData: BrewDataStorage, app

AppConstants.shared.logger.debug("Tagged packages: \(appState.taggedPackageNames, privacy: .public)")
}
*/
42 changes: 38 additions & 4 deletions Cork/Models/Brew Data Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,54 @@ import SwiftUI
@MainActor
class BrewDataStorage: ObservableObject
{
@Published var installedFormulae: Set<BrewPackage> = .init()
@Published var installedCasks: Set<BrewPackage> = .init()
@Published var installedFormulae: BrewPackages = .init()
@Published var installedCasks: BrewPackages = .init()

/// Formulae that were successfuly loaded from disk
var successfullyLoadedFormulae: Set<BrewPackage>
{
return Set(installedFormulae.compactMap
{ rawResult in
if case .success(let success) = rawResult
{
return success
}
else
{
return nil
}
})
}

/// Casks that were successfuly loaded from disk
var successfullyLoadedCasks: Set<BrewPackage>
{
return Set(installedCasks.compactMap
{ rawResult in
if case .success(let success) = rawResult
{
return success
}
else
{
return nil
}
})
}

func insertPackageIntoTracker(_ package: BrewPackage)
{
if package.type == .formula
{
installedFormulae.insert(package)
installedFormulae.insert(.success(package))
}
else
{
installedCasks.insert(package)
installedCasks.insert(.success(package))
}
}

/*
func removeFormulaFromTracker(withName name: String)
{
removePackageFromTracker(withName: name, tracker: .formula)
Expand All @@ -52,6 +85,7 @@ class BrewDataStorage: ObservableObject
}
}
}
*/
}

@MainActor
Expand Down
4 changes: 2 additions & 2 deletions Cork/Views/Sidebar/Components/Casks Section.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ struct CasksSection: View
{
if searchText.isEmpty || searchText.contains("#")
{
return brewData.installedCasks
return brewData.successfullyLoadedCasks
}
else
{
return brewData.installedCasks.filter { $0.name.contains(searchText) }
return brewData.successfullyLoadedCasks.filter { $0.name.contains(searchText) }
}
}
}
2 changes: 1 addition & 1 deletion Cork/Views/Sidebar/Components/Formulae Section.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ struct FormulaeSection: View
}
}

return brewData.installedFormulae.filter(filter)
return brewData.successfullyLoadedFormulae.filter(filter)
}
}
2 changes: 2 additions & 0 deletions Cork/Views/Sidebar/Components/Sidebar Package Row.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ struct SidebarPackageRow: View
{
Task(priority: .userInitiated)
{
/*
await changePackageTagStatus(
package: package,
brewData: brewData,
appState: appState
)
*/
}
} label: {
Label(package.isTagged ? "sidebar.section.all.contextmenu.untag-\(package.name)" : "sidebar.section.all.contextmenu.tag-\(package.name)", systemImage: package.isTagged ? "tag.slash" : "tag")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct PackageAndTapOverviewBox: View
{
GroupBoxHeadlineGroup(
image: "terminal",
title: LocalizedStringKey("start-page.installed-formulae.count-\(displayOnlyIntentionallyInstalledPackagesByDefault ? brewData.installedFormulae.filter(\.installedIntentionally).count : brewData.installedFormulae.count)"),
title: LocalizedStringKey("start-page.installed-formulae.count-\(displayOnlyIntentionallyInstalledPackagesByDefault ? brewData.successfullyLoadedFormulae.filter(\.installedIntentionally).count : brewData.installedFormulae.count)"),
mainText: "start-page.installed-formulae.description",
animateNumberChanges: true
)
Expand Down

0 comments on commit da85c9e

Please sign in to comment.