mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-11-08 05:30:05 +01:00
👌 Toggle service via button
This commit is contained in:
@@ -64,6 +64,29 @@ class Actions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Third Party Services
|
||||||
|
public static func stopService(name: String, completion: @escaping () -> Void) {
|
||||||
|
DispatchQueue.global(qos: .userInitiated).async {
|
||||||
|
brew("services stop \(name)", sudo: ServicesManager.shared.rootServices.contains { $0.value.name == name })
|
||||||
|
ServicesManager.loadHomebrewServices(completed: {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
completion()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func startService(name: String, completion: @escaping () -> Void) {
|
||||||
|
DispatchQueue.global(qos: .userInitiated).async {
|
||||||
|
brew("services start \(name)", sudo: ServicesManager.shared.rootServices.contains { $0.value.name == name })
|
||||||
|
ServicesManager.loadHomebrewServices(completed: {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
completion()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Finding Config Files
|
// MARK: - Finding Config Files
|
||||||
|
|
||||||
public static func openGenericPhpConfigFolder() {
|
public static func openGenericPhpConfigFolder() {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class ServicesManager: ObservableObject {
|
|||||||
@Published var rootServices: [String: HomebrewService] = [:]
|
@Published var rootServices: [String: HomebrewService] = [:]
|
||||||
@Published var userServices: [String: HomebrewService] = [:]
|
@Published var userServices: [String: HomebrewService] = [:]
|
||||||
|
|
||||||
public static func loadHomebrewServices() {
|
public static func loadHomebrewServices(completed: (() -> Void)? = nil) {
|
||||||
let rootServiceNames = [
|
let rootServiceNames = [
|
||||||
PhpEnv.phpInstall.formula,
|
PhpEnv.phpInstall.formula,
|
||||||
"nginx",
|
"nginx",
|
||||||
@@ -56,6 +56,7 @@ class ServicesManager: ObservableObject {
|
|||||||
ServicesManager.shared.userServices = Dictionary(
|
ServicesManager.shared.userServices = Dictionary(
|
||||||
uniqueKeysWithValues: services.map { ($0.name, $0) }
|
uniqueKeysWithValues: services.map { ($0.name, $0) }
|
||||||
)
|
)
|
||||||
|
completed?()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ struct ServicesView: View {
|
|||||||
|
|
||||||
struct CheckmarkView: View {
|
struct CheckmarkView: View {
|
||||||
@State var serviceName: String
|
@State var serviceName: String
|
||||||
|
@State var busy: Bool = false
|
||||||
@EnvironmentObject var manager: ServicesManager
|
@EnvironmentObject var manager: ServicesManager
|
||||||
|
|
||||||
public func hasAnyServices() -> Bool {
|
public func hasAnyServices() -> Bool {
|
||||||
@@ -88,6 +89,18 @@ struct CheckmarkView: View {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func toggleService() {
|
||||||
|
if active()! {
|
||||||
|
Actions.stopService(name: serviceName, completion: {
|
||||||
|
busy = false
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Actions.startService(name: serviceName, completion: {
|
||||||
|
busy = false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
if !hasAnyServices() {
|
if !hasAnyServices() {
|
||||||
Image(systemName: "hourglass.circle")
|
Image(systemName: "hourglass.circle")
|
||||||
@@ -95,16 +108,24 @@ struct CheckmarkView: View {
|
|||||||
.frame(width: 16.0, height: 16.0)
|
.frame(width: 16.0, height: 16.0)
|
||||||
.foregroundColor(.secondary)
|
.foregroundColor(.secondary)
|
||||||
} else {
|
} else {
|
||||||
if active() == nil {
|
if busy {
|
||||||
Image(systemName: "questionmark.square.dashed")
|
ProgressView()
|
||||||
.resizable()
|
.scaleEffect(x: 0.5, y: 0.5, anchor: .center)
|
||||||
.frame(width: 16.0, height: 16.0)
|
.frame(width: 16.0, height: 20.0)
|
||||||
.foregroundColor(Color("IconColorRed"))
|
} else if active() == nil {
|
||||||
|
Button { } label: {
|
||||||
|
Text("?")
|
||||||
|
}.disabled(true)
|
||||||
} else {
|
} else {
|
||||||
Image(systemName: active()! ? "checkmark.circle" : "xmark.circle")
|
Button {
|
||||||
.resizable()
|
busy = true
|
||||||
.frame(width: 16.0, height: 16.0)
|
toggleService()
|
||||||
.foregroundColor(active()! ? Color.primary : Color("IconColorRed"))
|
} label: {
|
||||||
|
Image(systemName: active()! ? "checkmark" : "xmark")
|
||||||
|
.resizable()
|
||||||
|
.frame(width: 12.0, height: 12.0)
|
||||||
|
.foregroundColor(active()! ? Color.primary : Color("IconColorRed"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -142,7 +163,7 @@ struct ServicesView_Previews: PreviewProvider {
|
|||||||
]),
|
]),
|
||||||
servicesToDisplay: ["php", "nginx", "dnsmasq",
|
servicesToDisplay: ["php", "nginx", "dnsmasq",
|
||||||
"mysql", "redis", "php@7.4"],
|
"mysql", "redis", "php@7.4"],
|
||||||
perRow: 4
|
perRow: 3
|
||||||
)
|
)
|
||||||
.frame(width: 330.0)
|
.frame(width: 330.0)
|
||||||
.previewDisplayName("Dark Mode")
|
.previewDisplayName("Dark Mode")
|
||||||
|
|||||||
Reference in New Issue
Block a user