1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-07 20:10:08 +02:00

👌 Make PHP version manager previewable

This commit is contained in:
2023-03-19 23:48:12 +01:00
parent 4f11f3d8d3
commit 0bdbc0a056
4 changed files with 78 additions and 30 deletions

View File

@ -1472,6 +1472,13 @@
path = php; path = php;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
C45B42C329C7C67400366A14 /* Fake */ = {
isa = PBXGroup;
children = (
);
path = Fake;
sourceTree = "<group>";
};
C45B9147295607E200F4EC78 /* Services */ = { C45B9147295607E200F4EC78 /* Services */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
@ -1631,6 +1638,7 @@
C4AF9F6C275445D900D44ED0 /* Homebrew */ = { C4AF9F6C275445D900D44ED0 /* Homebrew */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
C45B42C329C7C67400366A14 /* Fake */,
C43931C929C4C03F0069165B /* Brew.swift */, C43931C929C4C03F0069165B /* Brew.swift */,
C4AFC4AD29C4F32F00BF4E0D /* BrewFormula.swift */, C4AFC4AD29C4F32F00BF4E0D /* BrewFormula.swift */,
C4F2E4362752F0870020E974 /* BrewDiagnostics.swift */, C4F2E4362752F0870020E974 /* BrewDiagnostics.swift */,

View File

@ -8,20 +8,15 @@
import Foundation import Foundation
class Brew: ObservableObject { class BrewFormulaeObservable: ObservableObject {
@Published var phpVersions: [BrewFormula] = []
}
class Brew {
static let shared = Brew() static let shared = Brew()
init() { /// Formulae that can be observed.
Task { var formulae = BrewFormulaeObservable()
// Asynchronously load available updates
let items = await loadPhpVersions(loadOutdated: false)
Task { @MainActor in
self.phpVersions = items
}
}
}
@Published var phpVersions: [BrewFormula] = []
/// The version of Homebrew that was detected. /// The version of Homebrew that was detected.
var version: VersionNumber? var version: VersionNumber?
@ -54,7 +49,23 @@ class Brew: ObservableObject {
"7.0": "shivammathur/php/php@7.0", "7.0": "shivammathur/php/php@7.0",
"5.6": "shivammathur/php/php@5.6" "5.6": "shivammathur/php/php@5.6"
] ]
}
protocol HandlesBrewFormulae {
func loadPhpVersions(loadOutdated: Bool) async -> [BrewFormula]
func refreshPhpVersions(loadOutdated: Bool) async
}
extension HandlesBrewFormulae {
public func refreshPhpVersions(loadOutdated: Bool) async {
let items = await loadPhpVersions(loadOutdated: loadOutdated)
Task { @MainActor in
Brew.shared.formulae.phpVersions = items
}
}
}
class BrewFormulaeHandler: HandlesBrewFormulae {
public func loadPhpVersions(loadOutdated: Bool) async -> [BrewFormula] { public func loadPhpVersions(loadOutdated: Bool) async -> [BrewFormula] {
var outdated: [OutdatedFormula]? var outdated: [OutdatedFormula]?
@ -74,9 +85,7 @@ class Brew: ObservableObject {
}) })
} }
print(PhpEnv.shared.cachedPhpInstallations) return Brew.phpVersionFormulae.map { (version, formula) in
return Self.phpVersionFormulae.map { (version, formula) in
let fullVersion = PhpEnv.shared.cachedPhpInstallations[version]?.versionNumber.text let fullVersion = PhpEnv.shared.cachedPhpInstallations[version]?.versionNumber.text
var upgradeVersion: String? var upgradeVersion: String?

View File

@ -9,22 +9,47 @@
import Foundation import Foundation
import SwiftUI import SwiftUI
struct PhpFormulaeView: View { class PhpFormulaeStatus: ObservableObject {
@ObservedObject var brew: Brew = Brew.shared @Published var busy: Bool
@State var busy: Bool = true @Published var title: String
@State var title: String = "Doing a thing" @Published var description: String
@State var description: String = "Preparing..."
init(busy: Bool = true, title: String = "", description: String = "") { init(busy: Bool, title: String, description: String) {
self.busy = busy self.busy = busy
self.title = title self.title = title
self.description = description self.description = description
} }
}
struct PhpFormulaeView: View {
@ObservedObject var formulae: BrewFormulaeObservable
@ObservedObject var status: PhpFormulaeStatus
var handler: HandlesBrewFormulae
init(
formulae: BrewFormulaeObservable,
handler: HandlesBrewFormulae
) {
self.formulae = formulae
self.handler = handler
self.status = PhpFormulaeStatus(
busy: true,
title: "Checking for updates",
description: "Checking if any PHP version is outdated..."
)
Task { [self] in
await self.handler.refreshPhpVersions(loadOutdated: false)
await self.handler.refreshPhpVersions(loadOutdated: true)
self.status.busy = false
}
}
var body: some View { var body: some View {
BlockingOverlayView(busy: busy, title: title, text: description) { BlockingOverlayView(busy: self.status.busy, title: self.status.title, text: self.status.description) {
VStack { VStack {
List(Array(brew.phpVersions.enumerated()), id: \.1.name) { (index, formula) in List(Array(formulae.phpVersions.enumerated()), id: \.1.name) { (index, formula) in
HStack { HStack {
Image(systemName: formula.icon) Image(systemName: formula.icon)
.resizable() .resizable()
@ -75,10 +100,18 @@ struct PhpFormulaeView: View {
} }
} }
/*
struct PhpFormulaeView_Previews: PreviewProvider { struct PhpFormulaeView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
PhpFormulaeView(formulae: [ PhpFormulaeView(
formulae: Brew.shared.formulae,
handler: FakeBrewFormulaeHandler()
).frame(width: 600, height: 500)
}
}
class FakeBrewFormulaeHandler: HandlesBrewFormulae {
public func loadPhpVersions(loadOutdated: Bool) async -> [BrewFormula] {
return [
BrewFormula( BrewFormula(
name: "php", name: "php",
displayName: "PHP 8.2", displayName: "PHP 8.2",
@ -121,10 +154,9 @@ struct PhpFormulaeView_Previews: PreviewProvider {
installedVersion: nil, installedVersion: nil,
upgradeVersion: nil upgradeVersion: nil
) )
]).frame(width: 600, height: 500) ]
} }
} }
*/
extension BrewFormula { extension BrewFormula {
var icon: String { var icon: String {

View File

@ -24,9 +24,8 @@ class PhpVersionManagerWindowController: PMWindowController {
let windowController = Self() let windowController = Self()
windowController.window = NSWindow() windowController.window = NSWindow()
windowController.view = PhpFormulaeView( windowController.view = PhpFormulaeView(
busy: true, formulae: Brew.shared.formulae,
title: "Loading PHP versions", handler: BrewFormulaeHandler()
description: "Loading available PHP versions..."
) )
guard let window = windowController.window else { return } guard let window = windowController.window else { return }