Skip to content

Commit 9038255

Browse files
committed
~ More refactoring…
1 parent dd9d7b0 commit 9038255

File tree

17 files changed

+186
-131
lines changed

17 files changed

+186
-131
lines changed

Cork/AppDelegate.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class AppDelegate: NSObject, NSApplicationDelegate
2020
@ObservableDefault(.showInMenuBar) @ObservationIgnored var showInMenuBar: Bool
2121
@ObservableDefault(.startWithoutWindow) @ObservationIgnored var startWithoutWindow: Bool
2222

23-
@MainActor let appState: AppState = .init()
24-
2523
func applicationWillFinishLaunching(_: Notification)
2624
{
2725
if startWithoutWindow

Cork/ContentView.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,25 @@ struct ContentView: View, Sendable
9696
{
9797
NavigationSplitView(columnVisibility: self.$columnVisibility)
9898
{
99+
let _ = print("Parent appState: \(ObjectIdentifier(appState))")
100+
99101
SidebarView()
100102
} detail: {
101-
if let openedScreen = appState.navigationManager.openedScreen
103+
NavigationStack(path: Bindable(appState.navigationManager).path)
102104
{
103-
switch openedScreen
105+
StartPage()
106+
.frame(minWidth: 600, minHeight: 500)
107+
}
108+
.navigationDestination(for: AppState.NavigationManager.DetailDestination.self)
109+
{ destination in
110+
switch destination
104111
{
105112
case .package(let package):
106113
PackageDetailView(package: package)
107114
case .tap(let tap):
108115
TapDetailView(tap: tap)
109116
}
110117
}
111-
else
112-
{
113-
NavigationStack
114-
{
115-
StartPage()
116-
.frame(minWidth: 600, minHeight: 500)
117-
}
118-
}
119118
}
120119
.navigationTitle("app-name")
121120
.navigationSubtitle("navigation.installed-packages.count-\(self.brewPackagesTracker.numberOfInstalledPackages)")
@@ -623,7 +622,7 @@ private extension View
623622

624623
case .tapLoadingFailedDueToTapItself:
625624
EmptyView()
626-
case .couldNotDeleteCachedDownloads:
625+
case .couldNotDeleteCachedDownloads(error: let error):
627626
EmptyView()
628627
}
629628
} message: { error in
@@ -705,3 +704,4 @@ private extension ContentView
705704
await self.topPackagesTracker.loadTopPackages(numberOfDays: self.discoverabilityDaySpan.rawValue, appState: self.appState)
706705
}
707706
}
707+

Cork/CorkApp.swift

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ struct CorkApp: App
2525
{
2626
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate: AppDelegate
2727

28+
@InjectedObservable(\.appState) var appState: AppState
2829
@InjectedObservable(\.brewfileManager) var brewfileManager: BrewfileManager
2930

31+
3032
@State var brewPackagesTracker: BrewPackagesTracker = .init()
3133
@State var tapTracker: TapTracker = .init()
3234

@@ -88,7 +90,6 @@ struct CorkApp: App
8890
LicensingView()
8991
.interactiveDismissDisabled()
9092
})
91-
.environment(appDelegate.appState)
9293
.environment(brewPackagesTracker)
9394
.environment(tapTracker)
9495
.environment(cachedDownloadsTracker)
@@ -105,7 +106,7 @@ struct CorkApp: App
105106

106107
if areNotificationsEnabled
107108
{
108-
await appDelegate.appState.setupNotifications()
109+
await appState.setupNotifications()
109110
}
110111
}
111112
.task
@@ -160,12 +161,12 @@ struct CorkApp: App
160161
{ _, newValue in // Remove the badge from the app icon if the user turns off notifications, put it back when they turn them back on
161162
Task
162163
{
163-
await appDelegate.appState.requestNotificationAuthorization()
164+
await appState.requestNotificationAuthorization()
164165

165-
if appDelegate.appState.notificationEnabledInSystemSettings == true
166+
if appState.notificationEnabledInSystemSettings == true
166167
{
167-
await appDelegate.appState.requestNotificationAuthorization()
168-
if appDelegate.appState.notificationAuthStatus == .denied
168+
await appState.requestNotificationAuthorization()
169+
if appState.notificationAuthStatus == .denied
169170
{
170171
areNotificationsEnabled = false
171172
}
@@ -299,7 +300,7 @@ struct CorkApp: App
299300

300301
PackagePreview(packageToPreview: convertedMinimalPackage)
301302
.navigationTitle(packageToPreview?.name ?? "")
302-
.environment(appDelegate.appState)
303+
.environment(appState)
303304
.environment(brewPackagesTracker)
304305
.environment(outdatedPackagesTracker)
305306
}
@@ -318,7 +319,7 @@ struct CorkApp: App
318319
Settings
319320
{
320321
SettingsView()
321-
.environment(appDelegate.appState)
322+
.environment(appState)
322323
}
323324
.windowResizability(.contentSize)
324325

@@ -327,7 +328,7 @@ struct CorkApp: App
327328
MenuBarExtra("app-name", systemImage: outdatedPackagesTracker.allDisplayableOutdatedPackages.isEmpty ? "mug" : "mug.fill", isInserted: $showInMenuBar)
328329
{
329330
MenuBarItem()
330-
.environment(appDelegate.appState)
331+
.environment(appState)
331332
.environment(brewPackagesTracker)
332333
.environment(tapTracker)
333334
.environment(cachedDownloadsTracker)
@@ -417,11 +418,11 @@ struct CorkApp: App
417418
{
418419
Button
419420
{
420-
appDelegate.appState.navigationManager.dismissScreen()
421+
appState.navigationManager.dismissScreen()
421422
} label: {
422423
Label("action.go-to-status-page.menu-bar", systemImage: "house")
423424
}
424-
.disabled(!appDelegate.appState.navigationManager.isAnyScreenOpened)
425+
.disabled(!appState.navigationManager.isAnyScreenOpened)
425426
Divider()
426427
}
427428

@@ -432,7 +433,9 @@ struct CorkApp: App
432433
{
433434
do throws(BrewfileManager.BrewfileDumpingError)
434435
{
435-
brewfileContents = try await brewfileManager.exportBrewfile(appState: appDelegate.appState)
436+
brewfileContents = try await brewfileManager.exportBrewfile(
437+
appState: appState
438+
)
436439

437440
isShowingBrewfileExporter = true
438441
}
@@ -443,13 +446,13 @@ struct CorkApp: App
443446
switch brewfileExportError
444447
{
445448
case .couldNotDetermineWorkingDirectory:
446-
appDelegate.appState.showAlert(errorToShow: .couldNotGetWorkingDirectory)
449+
appState.showAlert(errorToShow: .couldNotGetWorkingDirectory)
447450

448451
case .errorWhileDumpingBrewfile(let error):
449-
appDelegate.appState.showAlert(errorToShow: .couldNotDumpBrewfile(error: error))
452+
appState.showAlert(errorToShow: .couldNotDumpBrewfile(error: error))
450453

451454
case .couldNotReadBrewfile:
452-
appDelegate.appState.showAlert(errorToShow: .couldNotReadBrewfile(error: brewfileExportError.localizedDescription))
455+
appState.showAlert(errorToShow: .couldNotReadBrewfile(error: brewfileExportError.localizedDescription))
453456
}
454457
}
455458
} label: {
@@ -480,7 +483,7 @@ struct CorkApp: App
480483
{
481484
try await brewfileManager.importBrewfile(
482485
from: brewfileURL,
483-
appState: appDelegate.appState,
486+
appState: appState,
484487
brewPackagesTracker: brewPackagesTracker,
485488
cachedDownloadsTracker: cachedDownloadsTracker
486489
)
@@ -489,9 +492,9 @@ struct CorkApp: App
489492
{
490493
AppConstants.shared.logger.error("\(brewfileImportingError.localizedDescription, privacy: .public)")
491494

492-
appDelegate.appState.showAlert(errorToShow: .malformedBrewfile)
495+
appState.showAlert(errorToShow: .malformedBrewfile)
493496

494-
appDelegate.appState.showSheet(ofType: .brewfileImport)
497+
appState.showSheet(ofType: .brewfileImport)
495498
}
496499
}
497500
}
@@ -500,10 +503,10 @@ struct CorkApp: App
500503
switch error
501504
{
502505
case .couldNotGetBrewfileLocation:
503-
appDelegate.appState.showAlert(errorToShow: .couldNotGetBrewfileLocation)
506+
appState.showAlert(errorToShow: .couldNotGetBrewfileLocation)
504507

505508
case .couldNotImportFile:
506-
appDelegate.appState.showAlert(errorToShow: .couldNotImportBrewfile)
509+
appState.showAlert(errorToShow: .couldNotImportBrewfile)
507510
}
508511
}
509512
} label: {
@@ -519,7 +522,7 @@ struct CorkApp: App
519522

520523
Button
521524
{
522-
appDelegate.appState.isSearchFieldFocused = true
525+
appState.isSearchFieldFocused = true
523526
} label: {
524527
Label("navigation.menu.search", systemImage: "magnifyingglass")
525528
}
@@ -529,20 +532,20 @@ struct CorkApp: App
529532
@ViewBuilder
530533
var packagesMenuBarSection: some View
531534
{
532-
InstallPackageButton(appState: appDelegate.appState)
535+
InstallPackageButton(appState: appState)
533536
.keyboardShortcut("n")
534537

535-
AddTapButton(appState: appDelegate.appState)
538+
AddTapButton(appState: appState)
536539
.keyboardShortcut("n", modifiers: [.command, .option])
537540

538541
Divider()
539542

540543
CheckForOutdatedPackagesButton()
541544
.keyboardShortcut("r")
542-
.environment(appDelegate.appState)
545+
.environment(appState)
543546
.environment(outdatedPackagesTracker)
544547

545-
UpgradePackagesButton(appState: appDelegate.appState)
548+
UpgradePackagesButton(appState: appState)
546549
.keyboardShortcut("r", modifiers: [.control, .command])
547550
}
548551

@@ -561,10 +564,10 @@ struct CorkApp: App
561564
@ViewBuilder
562565
var maintenanceMenuBarSection: some View
563566
{
564-
OpenMaintenanceSheetButton(appState: appDelegate.appState, labelType: .performMaintenance)
567+
OpenMaintenanceSheetButton(appState: appState, labelType: .performMaintenance)
565568
.keyboardShortcut("m", modifiers: [.command, .shift])
566569

567-
DeleteCachedDownloadsButton(appState: appDelegate.appState)
570+
DeleteCachedDownloadsButton(appState: appState)
568571
.keyboardShortcut("m", modifiers: [.command, .option])
569572
.disabled(cachedDownloadsTracker.cachedDownloadsSize == 0)
570573
}
@@ -578,7 +581,7 @@ struct CorkApp: App
578581
{
579582
demoActivatedAt = nil
580583
hasValidatedEmail = false
581-
appDelegate.appState.licensingState = .notBoughtOrHasNotActivatedDemo
584+
appState.licensingState = .notBoughtOrHasNotActivatedDemo
582585
hasFinishedLicensingWorkflow = false
583586
} label: {
584587
Text("debug.action.reset-license-state")
@@ -588,7 +591,7 @@ struct CorkApp: App
588591
{
589592
demoActivatedAt = nil
590593
hasValidatedEmail = false
591-
appDelegate.appState.licensingState = .demo
594+
appState.licensingState = .demo
592595
hasFinishedLicensingWorkflow = false
593596
} label: {
594597
Text("debug.action.activate-demo")
@@ -742,15 +745,15 @@ struct CorkApp: App
742745
// MARK: - Licensing
743746
func handleLicensing()
744747
{
745-
print("Licensing state: \(appDelegate.appState.licensingState)")
748+
print("Licensing state: \(appState.licensingState)")
746749

747750
#if SELF_COMPILED
748751
AppConstants.shared.logger.debug("Will set licensing state to Self Compiled")
749-
appDelegate.appState.licensingState = .selfCompiled
752+
appState.licensingState = .selfCompiled
750753
#else
751754
if !hasValidatedEmail
752755
{
753-
if appDelegate.appState.licensingState != .selfCompiled
756+
if appState.licensingState != .selfCompiled
754757
{
755758
if let demoActivatedAt
756759
{

Cork/Localizable.xcstrings

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16417,6 +16417,10 @@
1641716417
}
1641816418
}
1641916419
},
16420+
"add-package.search.failed" : {
16421+
"comment" : "Label for an inline content unavailable view that indicates that a search for packages failed.",
16422+
"isCommentAutoGenerated" : true
16423+
},
1642016424
"add-package.search.prompt" : {
1642116425
"localizations" : {
1642216426
"cs" : {

Cork/Logic/Search/Search for Package.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import CorkShared
1010
import CorkTerminalFunctions
1111
import Foundation
1212

13-
func searchForPackage(packageName: String, packageType: BrewPackage.PackageType) async -> [String]
13+
func searchForPackage(packageName: String, packageType: BrewPackage.PackageType) async -> [String]?
1414
{
1515
var finalCommandOutputs: [TerminalOutput]
1616

@@ -23,7 +23,12 @@ func searchForPackage(packageName: String, packageType: BrewPackage.PackageType)
2323
finalCommandOutputs = await shell(AppConstants.shared.brewExecutablePath, ["search", "--casks", packageName])
2424
}
2525

26-
AppConstants.shared.logger.info("Search found these packages: \(finalCommandOutputs, privacy: .auto)")
26+
AppConstants.shared.logger.info("Search found \(finalCommandOutputs.count) packages: \(finalCommandOutputs, privacy: .auto)")
2727

28-
return finalCommandOutputs.standardOutputs
28+
guard let unsplitListOfFoundPackages: String = finalCommandOutputs.standardOutputs.first else
29+
{
30+
return nil
31+
}
32+
33+
return unsplitListOfFoundPackages.components(separatedBy: .newlines)
2934
}

Cork/Models/Search Result Tracker.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class SearchResultTracker
1313
{
1414
/// These two have to be arrays because the order matters
1515
/// When searching, Homebrew returns the best result at the top
16-
var foundFormulae: [BrewPackage] = .init()
17-
var foundCasks: [BrewPackage] = .init()
16+
var foundFormulae: [BrewPackage]? = .init()
17+
var foundCasks: [BrewPackage]? = .init()
1818
var selectedPackagesForInstallation: [String] = .init()
1919
}

Cork/Protocols/View/Dismissable Pane.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import Foundation
99
import SwiftUI
1010
import CorkModels
11+
import FactoryKit
1112

1213
protocol DismissablePane: View
1314
{

0 commit comments

Comments
 (0)