mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-08-07 03:50:08 +02:00
👌 Make PHP version manager previewable
This commit is contained in:
@ -1472,6 +1472,13 @@
|
||||
path = php;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
C45B42C329C7C67400366A14 /* Fake */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
);
|
||||
path = Fake;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
C45B9147295607E200F4EC78 /* Services */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
@ -1631,6 +1638,7 @@
|
||||
C4AF9F6C275445D900D44ED0 /* Homebrew */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
C45B42C329C7C67400366A14 /* Fake */,
|
||||
C43931C929C4C03F0069165B /* Brew.swift */,
|
||||
C4AFC4AD29C4F32F00BF4E0D /* BrewFormula.swift */,
|
||||
C4F2E4362752F0870020E974 /* BrewDiagnostics.swift */,
|
||||
|
@ -8,20 +8,15 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
class Brew: ObservableObject {
|
||||
class BrewFormulaeObservable: ObservableObject {
|
||||
@Published var phpVersions: [BrewFormula] = []
|
||||
}
|
||||
|
||||
class Brew {
|
||||
static let shared = Brew()
|
||||
|
||||
init() {
|
||||
Task {
|
||||
// Asynchronously load available updates
|
||||
let items = await loadPhpVersions(loadOutdated: false)
|
||||
Task { @MainActor in
|
||||
self.phpVersions = items
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Published var phpVersions: [BrewFormula] = []
|
||||
/// Formulae that can be observed.
|
||||
var formulae = BrewFormulaeObservable()
|
||||
|
||||
/// The version of Homebrew that was detected.
|
||||
var version: VersionNumber?
|
||||
@ -54,7 +49,23 @@ class Brew: ObservableObject {
|
||||
"7.0": "shivammathur/php/php@7.0",
|
||||
"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] {
|
||||
var outdated: [OutdatedFormula]?
|
||||
|
||||
@ -74,9 +85,7 @@ class Brew: ObservableObject {
|
||||
})
|
||||
}
|
||||
|
||||
print(PhpEnv.shared.cachedPhpInstallations)
|
||||
|
||||
return Self.phpVersionFormulae.map { (version, formula) in
|
||||
return Brew.phpVersionFormulae.map { (version, formula) in
|
||||
let fullVersion = PhpEnv.shared.cachedPhpInstallations[version]?.versionNumber.text
|
||||
var upgradeVersion: String?
|
||||
|
||||
|
@ -9,22 +9,47 @@
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
struct PhpFormulaeView: View {
|
||||
@ObservedObject var brew: Brew = Brew.shared
|
||||
@State var busy: Bool = true
|
||||
@State var title: String = "Doing a thing"
|
||||
@State var description: String = "Preparing..."
|
||||
class PhpFormulaeStatus: ObservableObject {
|
||||
@Published var busy: Bool
|
||||
@Published var title: String
|
||||
@Published var description: String
|
||||
|
||||
init(busy: Bool = true, title: String = "", description: String = "") {
|
||||
init(busy: Bool, title: String, description: String) {
|
||||
self.busy = busy
|
||||
self.title = title
|
||||
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 {
|
||||
BlockingOverlayView(busy: busy, title: title, text: description) {
|
||||
BlockingOverlayView(busy: self.status.busy, title: self.status.title, text: self.status.description) {
|
||||
VStack {
|
||||
List(Array(brew.phpVersions.enumerated()), id: \.1.name) { (index, formula) in
|
||||
List(Array(formulae.phpVersions.enumerated()), id: \.1.name) { (index, formula) in
|
||||
HStack {
|
||||
Image(systemName: formula.icon)
|
||||
.resizable()
|
||||
@ -75,10 +100,18 @@ struct PhpFormulaeView: View {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
struct PhpFormulaeView_Previews: PreviewProvider {
|
||||
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(
|
||||
name: "php",
|
||||
displayName: "PHP 8.2",
|
||||
@ -121,10 +154,9 @@ struct PhpFormulaeView_Previews: PreviewProvider {
|
||||
installedVersion: nil,
|
||||
upgradeVersion: nil
|
||||
)
|
||||
]).frame(width: 600, height: 500)
|
||||
]
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
extension BrewFormula {
|
||||
var icon: String {
|
||||
|
@ -24,9 +24,8 @@ class PhpVersionManagerWindowController: PMWindowController {
|
||||
let windowController = Self()
|
||||
windowController.window = NSWindow()
|
||||
windowController.view = PhpFormulaeView(
|
||||
busy: true,
|
||||
title: "Loading PHP versions",
|
||||
description: "Loading available PHP versions..."
|
||||
formulae: Brew.shared.formulae,
|
||||
handler: BrewFormulaeHandler()
|
||||
)
|
||||
|
||||
guard let window = windowController.window else { return }
|
||||
|
Reference in New Issue
Block a user