Skip to content

Commit

Permalink
~ Final implementation of improved package loader
Browse files Browse the repository at this point in the history
  • Loading branch information
buresdv committed Nov 13, 2024
1 parent 8e78277 commit dad7b5c
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 15 deletions.
13 changes: 4 additions & 9 deletions Cork/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,6 @@ struct ContentView: View, Sendable
}
}
.task(priority: .high)
{
await brewData.loadInstalledPackages(packageTypeToLoad: .formula, appState: appState)
await brewData.loadInstalledPackages(packageTypeToLoad: .cask, appState: appState)
}
.task(priority: .high)
{
AppConstants.shared.logger.info("Started Package Load startup action at \(Date())")

Expand All @@ -268,13 +263,13 @@ struct ContentView: View, Sendable
appState.isLoadingCasks = false
}

async let availableFormulae: Set<BrewPackage> = await loadUpPackages(whatToLoad: .formula, appState: appState)
async let availableCasks: Set<BrewPackage> = await loadUpPackages(whatToLoad: .cask, appState: appState)
async let availableFormulae: Set<BrewPackage>? = await brewData.loadInstalledPackages(packageTypeToLoad: .formula, appState: appState)
async let availableCasks: Set<BrewPackage>? = await brewData.loadInstalledPackages(packageTypeToLoad: .cask, appState: appState)

async let availableTaps: [BrewTap] = await loadUpTappedTaps()

brewData.installedFormulae = await availableFormulae
brewData.installedCasks = await availableCasks
brewData.installedFormulae = await availableFormulae ?? .init()
brewData.installedCasks = await availableCasks ?? .init()

tapData.addedTaps = await availableTaps

Expand Down
21 changes: 21 additions & 0 deletions Cork/Errors/Intent Error.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Intent Error.swift
// Cork
//
// Created by David Bureš on 13.11.2024.
//

import Foundation

enum IntentError: LocalizedError
{
case failedWhilePerformingIntent

var errorDescription: String?
{
switch self {
case .failedWhilePerformingIntent:
return String(localized: "error.intents.general-failure")
}
}
}
12 changes: 12 additions & 0 deletions Cork/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -12132,6 +12132,7 @@
}
},
"alert.fatal.could-not-filter-invalid-packages" : {
"extractionState" : "stale",
"localizations" : {
"cs" : {
"stringUnit" : {
Expand Down Expand Up @@ -15547,6 +15548,9 @@
}
}
}
},
"error.intents.general-failure" : {

},
"error.json-parsing.could-not-convert-string-to-data.%@" : {
"localizations" : {
Expand Down Expand Up @@ -16279,8 +16283,12 @@
}
}
}
},
"error.package-loading.could-not-convert-contents-of-install-receipt-to-data" : {

},
"error.package-loading.could-not-convert-contents-of-install-receipt-to-data-%@" : {
"extractionState" : "stale",
"localizations" : {
"cs" : {
"stringUnit" : {
Expand Down Expand Up @@ -16319,8 +16327,12 @@
}
}
}
},
"error.package-loading.could-not-decode-installa-receipt" : {

},
"error.package-loading.could-not-decode-installa-receipt-%@" : {
"extractionState" : "stale",
"localizations" : {
"cs" : {
"stringUnit" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ struct GetInstalledCasksIntent: AppIntent

if allowAccessToFile
{
let installedFormulae: Set<BrewPackage> = await loadUpPackages(whatToLoad: .cask, appState: AppState())
let dummyBrewData: BrewDataStorage = await .init()

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

AppConstants.shared.brewCaskPath.stopAccessingSecurityScopedResource()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ struct GetInstalledFormulaeIntent: AppIntent

if allowAccessToFile
{
let installedFormulae: Set<BrewPackage> = await loadUpPackages(whatToLoad: .formula, appState: AppState())
let dummyBrewData: BrewDataStorage = await .init()

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

AppConstants.shared.brewCellarPath.stopAccessingSecurityScopedResource()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation
import SwiftUI
import CorkShared

/*
func getContentsOfFolder(targetFolder: URL) async throws -> Set<BrewPackage>
{
do
Expand Down Expand Up @@ -85,7 +86,8 @@ func getContentsOfFolder(targetFolder: URL) async throws -> Set<BrewPackage>
throw error
}
}

*/

// MARK: - Sub-functions

private extension URL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ func synchronizeInstalledPackages(brewData: BrewDataStorage) async
let dummyAppState: AppState = .init()
dummyAppState.isLoadingFormulae = false
dummyAppState.isLoadingCasks = false

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 loadUpPackages(whatToLoad: .formula, appState: dummyAppState)
let newCasks: Set<BrewPackage> = await loadUpPackages(whatToLoad: .cask, appState: dummyAppState)
let newFormulae: Set<BrewPackage> = await dummyBrewData.loadInstalledPackages(packageTypeToLoad: .formula, appState: dummyAppState) ?? .init()
let newCasks: Set<BrewPackage> = await dummyBrewData.loadInstalledPackages(packageTypeToLoad: .cask, appState: dummyAppState) ?? .init()

if newFormulae.count != brewData.installedFormulae.count
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private extension BrewDataStorage

AppConstants.shared.logger.debug("Loaded contents of folder: \(urlsInParentFolder)")

let packageLoader = await withTaskGroup(of: BrewPackage?.self, returning: Set<BrewPackage>.self)
let packageLoader: Set<BrewPackage> = await withTaskGroup(of: BrewPackage?.self, returning: Set<BrewPackage>.self)
{ taskGroup in
for packageURL in urlsInParentFolder
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import CorkShared
import Foundation

/*
@MainActor
func loadUpPackages(whatToLoad: PackageType, appState: AppState) async -> Set<BrewPackage>
{
Expand Down Expand Up @@ -50,3 +51,4 @@ func loadUpPackages(whatToLoad: PackageType, appState: AppState) async -> Set<Br

return contentsOfFolder
}
*/

0 comments on commit dad7b5c

Please sign in to comment.