Better reactivity and functionality
This commit is contained in:
@@ -9,6 +9,9 @@
|
||||
/* Begin PBXBuildFile section */
|
||||
9E6C73072C3D5E570056ADDC /* SearchView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E6C73062C3D5E570056ADDC /* SearchView.swift */; };
|
||||
9E6C73092C3D5E950056ADDC /* InstalledView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E6C73082C3D5E950056ADDC /* InstalledView.swift */; };
|
||||
9E6C730C2C3D796D0056ADDC /* DownloadButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E6C730B2C3D796D0056ADDC /* DownloadButton.swift */; };
|
||||
9E6C730E2C3DB16F0056ADDC /* UninstallButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E6C730D2C3DB16F0056ADDC /* UninstallButton.swift */; };
|
||||
9E6C73112C3DB5940056ADDC /* CaskDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E6C73102C3DB5940056ADDC /* CaskDetailView.swift */; };
|
||||
9E8CE5362C3C545600A39146 /* BrewerApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E8CE5352C3C545600A39146 /* BrewerApp.swift */; };
|
||||
9E8CE5382C3C545600A39146 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E8CE5372C3C545600A39146 /* ContentView.swift */; };
|
||||
9E8CE53A2C3C545700A39146 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9E8CE5392C3C545700A39146 /* Assets.xcassets */; };
|
||||
@@ -39,6 +42,9 @@
|
||||
/* Begin PBXFileReference section */
|
||||
9E6C73062C3D5E570056ADDC /* SearchView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchView.swift; sourceTree = "<group>"; };
|
||||
9E6C73082C3D5E950056ADDC /* InstalledView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstalledView.swift; sourceTree = "<group>"; };
|
||||
9E6C730B2C3D796D0056ADDC /* DownloadButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DownloadButton.swift; sourceTree = "<group>"; };
|
||||
9E6C730D2C3DB16F0056ADDC /* UninstallButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UninstallButton.swift; sourceTree = "<group>"; };
|
||||
9E6C73102C3DB5940056ADDC /* CaskDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CaskDetailView.swift; sourceTree = "<group>"; };
|
||||
9E8CE5322C3C545600A39146 /* Brewer.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Brewer.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
9E8CE5352C3C545600A39146 /* BrewerApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrewerApp.swift; sourceTree = "<group>"; };
|
||||
9E8CE5372C3C545600A39146 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
|
||||
@@ -78,6 +84,23 @@
|
||||
/* End PBXFrameworksBuildPhase section */
|
||||
|
||||
/* Begin PBXGroup section */
|
||||
9E6C730A2C3D795D0056ADDC /* Components */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
9E6C730B2C3D796D0056ADDC /* DownloadButton.swift */,
|
||||
9E6C730D2C3DB16F0056ADDC /* UninstallButton.swift */,
|
||||
);
|
||||
path = Components;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
9E6C730F2C3DB5850056ADDC /* Details */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
9E6C73102C3DB5940056ADDC /* CaskDetailView.swift */,
|
||||
);
|
||||
name = Details;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
9E8CE5292C3C545600A39146 = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
@@ -101,9 +124,11 @@
|
||||
9E8CE5342C3C545600A39146 /* Brewer */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
9E6C730A2C3D795D0056ADDC /* Components */,
|
||||
9E8CE5602C3C5A5000A39146 /* Model */,
|
||||
9E8CE5352C3C545600A39146 /* BrewerApp.swift */,
|
||||
9E6C73062C3D5E570056ADDC /* SearchView.swift */,
|
||||
9E6C730F2C3DB5850056ADDC /* Details */,
|
||||
9E6C73082C3D5E950056ADDC /* InstalledView.swift */,
|
||||
9E8CE5372C3C545600A39146 /* ContentView.swift */,
|
||||
9E8CE5392C3C545700A39146 /* Assets.xcassets */,
|
||||
@@ -277,10 +302,13 @@
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
9E6C73072C3D5E570056ADDC /* SearchView.swift in Sources */,
|
||||
9E6C730C2C3D796D0056ADDC /* DownloadButton.swift in Sources */,
|
||||
9E8CE5382C3C545600A39146 /* ContentView.swift in Sources */,
|
||||
9E6C730E2C3DB16F0056ADDC /* UninstallButton.swift in Sources */,
|
||||
9E8CE5622C3C5A6A00A39146 /* Homebrew.swift in Sources */,
|
||||
9E6C73092C3D5E950056ADDC /* InstalledView.swift in Sources */,
|
||||
9E8CE5362C3C545600A39146 /* BrewerApp.swift in Sources */,
|
||||
9E6C73112C3DB5940056ADDC /* CaskDetailView.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
||||
@@ -9,12 +9,34 @@ import SwiftUI
|
||||
|
||||
@main
|
||||
struct BrewerApp: App {
|
||||
@AppStorage("isUpToDate") var isUpToDate: Bool = true
|
||||
init() {
|
||||
do {
|
||||
let res = try shell("/opt/homebrew/bin/brew outdated --greedy-latest -g | wc -l")
|
||||
if let number = Int(res.trimmingCharacters(in: .whitespacesAndNewlines)), number > 0 {
|
||||
isUpToDate = false
|
||||
}
|
||||
} catch {
|
||||
print(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
EmptyView()
|
||||
}
|
||||
MenuBarExtra("Brewer", systemImage: "sparkle.magnifyingglass") {
|
||||
MenuBarExtra {
|
||||
ContentView().padding()
|
||||
}.menuBarExtraStyle(.window)
|
||||
} label: {
|
||||
if isUpToDate {
|
||||
Label("Brewer", systemImage: "sparkle.magnifyingglass")
|
||||
} else {
|
||||
Label("Brewer", systemImage: "sparkle.magnifyingglass")
|
||||
.symbolRenderingMode(.palette)
|
||||
.foregroundStyle(.orange)
|
||||
}
|
||||
}
|
||||
.menuBarExtraStyle(.window)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
38
Brewer/CaskDetailView.swift
Normal file
38
Brewer/CaskDetailView.swift
Normal file
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// CaskDetailView.swift
|
||||
// Brewer
|
||||
//
|
||||
// Created by Cédric MAS on 09/07/2024.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct CaskDetailView: View {
|
||||
var cask: Cask
|
||||
@Bindable var brewListing: Homebrew
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
VStack {
|
||||
Text("Identifier: \(cask.fullToken)")
|
||||
Text("Version: \(cask.version)")
|
||||
}
|
||||
.navigationTitle(cask.name.first ?? cask.fullToken)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
if cask.installed == nil {
|
||||
DownloadButton(name: cask.fullToken, isCask: true)
|
||||
} else if cask.outdated {
|
||||
Button("Update") {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if cask.installed != nil {
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
UninstallButton(name: cask.fullToken, brewListing: brewListing)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
53
Brewer/Components/DownloadButton.swift
Normal file
53
Brewer/Components/DownloadButton.swift
Normal file
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// DownloadButton.swift
|
||||
// Brewer
|
||||
//
|
||||
// Created by Cédric MAS on 09/07/2024.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct DownloadButton: View {
|
||||
let name: String
|
||||
var isCask: Bool = false
|
||||
@State private var brew = Homebrew()
|
||||
@State private var downloaded = false
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
if downloaded {
|
||||
Image(systemName: "checkmark")
|
||||
.symbolRenderingMode(.palette)
|
||||
.symbolVariant(.circle)
|
||||
.symbolVariant(.fill)
|
||||
.foregroundStyle(.white, .green)
|
||||
} else if brew.isLoading {
|
||||
ProgressView()
|
||||
.controlSize(.small)
|
||||
} else if brew.errorMessage != nil {
|
||||
Image(systemName: "x")
|
||||
.symbolRenderingMode(.palette)
|
||||
.symbolVariant(.circle)
|
||||
.symbolVariant(.fill)
|
||||
.foregroundStyle(.white, .red)
|
||||
} else {
|
||||
Button("Get") {
|
||||
Task {
|
||||
downloaded = await brew.install(name, isCask: isCask)
|
||||
}
|
||||
}
|
||||
.buttonBorderShape(.capsule)
|
||||
.buttonStyle(.borderedProminent)
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
Task {
|
||||
downloaded = await brew.isDownloaded(name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
DownloadButton(name: "firefox")
|
||||
}
|
||||
45
Brewer/Components/UninstallButton.swift
Normal file
45
Brewer/Components/UninstallButton.swift
Normal file
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// UninstallButton.swift
|
||||
// Brewer
|
||||
//
|
||||
// Created by Cédric MAS on 09/07/2024.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct UninstallButton: View {
|
||||
let name: String
|
||||
@State private var brew = Homebrew()
|
||||
@State private var uninstalled = false
|
||||
@Bindable var brewListing: Homebrew
|
||||
var body: some View {
|
||||
VStack {
|
||||
if uninstalled {
|
||||
EmptyView()
|
||||
} else if brew.isLoading {
|
||||
ProgressView()
|
||||
.controlSize(.small)
|
||||
} else if brew.errorMessage != nil {
|
||||
Image(systemName: "x")
|
||||
.symbolRenderingMode(.palette)
|
||||
.symbolVariant(.circle)
|
||||
.symbolVariant(.fill)
|
||||
.foregroundStyle(.white, .red)
|
||||
} else {
|
||||
Button("Uninstall", role: .destructive) {
|
||||
Task {
|
||||
uninstalled = await brew.uninstall(name)
|
||||
brewListing.getInstalled()
|
||||
}
|
||||
}
|
||||
.buttonBorderShape(.capsule)
|
||||
.buttonStyle(.borderedProminent)
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
Task {
|
||||
uninstalled = await brew.isDownloaded(name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,6 @@ struct ContentView: View {
|
||||
@State private var segmentedSelection: SegementedSelection = .search
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
|
||||
Picker("", selection: $segmentedSelection) {
|
||||
ForEach(SegementedSelection.allCases, id: \.self) { sel in
|
||||
Text(sel.rawValue).tag(sel)
|
||||
|
||||
@@ -19,15 +19,8 @@ struct InstalledView: View {
|
||||
Section {
|
||||
ForEach(data.casks, id: \.fullToken) { cask in
|
||||
NavigationLink {
|
||||
NavigationStack {
|
||||
VStack {
|
||||
|
||||
Spacer()
|
||||
}
|
||||
.navigationTitle(cask.name.first ?? cask.fullToken)
|
||||
}
|
||||
CaskDetailView(cask: cask, brewListing: brew)
|
||||
} label: {
|
||||
|
||||
HStack {
|
||||
Text(cask.name.first ?? cask.fullToken)
|
||||
Spacer()
|
||||
@@ -44,12 +37,7 @@ struct InstalledView: View {
|
||||
}
|
||||
}
|
||||
.contextMenu {
|
||||
Button(role: .destructive) {
|
||||
// brew.uninstall(app.0)
|
||||
// brew.getInstalled()
|
||||
} label: {
|
||||
Label("Uninstall", systemImage: "trash")
|
||||
}
|
||||
UninstallButton(name: cask.fullToken, brewListing: brew)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -81,12 +69,7 @@ struct InstalledView: View {
|
||||
}
|
||||
}
|
||||
.contextMenu {
|
||||
Button(role: .destructive) {
|
||||
// brew.uninstall(app.0)
|
||||
// brew.getInstalled()
|
||||
} label: {
|
||||
Label("Uninstall", systemImage: "trash")
|
||||
}
|
||||
UninstallButton(name: formulae.fullName, brewListing: brew)
|
||||
}
|
||||
}
|
||||
} header: {
|
||||
@@ -99,13 +82,13 @@ struct InstalledView: View {
|
||||
}
|
||||
}
|
||||
.listStyle(.inset(alternatesRowBackgrounds: true))
|
||||
if data.casks.count + data.formulae.count > 0 {
|
||||
let areOutdated = data.casks.map(\.outdated).count + data.formulae.map(\.outdated).count
|
||||
let _ = UserDefaults.standard.set(!(areOutdated > 0), forKey: "isUpToDate")
|
||||
if areOutdated > 0 {
|
||||
Button {
|
||||
|
||||
} label: {
|
||||
Label("Update all", systemImage: "arrow.counterclockwise.circle.fill")
|
||||
// .symbolRenderingMode(.palette)
|
||||
// .foregroundStyle(.white, .orange)
|
||||
}
|
||||
.buttonStyle(.borderedProminent)
|
||||
.tint(.orange)
|
||||
|
||||
@@ -21,9 +21,10 @@ func shell(_ command: String) throws -> String {
|
||||
task.standardError = pipe
|
||||
task.standardOutput = pipe
|
||||
|
||||
|
||||
try task.run()
|
||||
if let data = try pipe.fileHandleForReading.readToEnd(), let output = String(data: data, encoding: .utf8) {
|
||||
if let data = try pipe.fileHandleForReading.readToEnd(),
|
||||
let output = String(data: data, encoding: .utf8)
|
||||
{
|
||||
return output
|
||||
}
|
||||
|
||||
@@ -129,7 +130,6 @@ class Homebrew {
|
||||
Task { [weak self] in
|
||||
do {
|
||||
let res = try shell("/opt/homebrew/bin/brew info --json=v2 --installed")
|
||||
print(res)
|
||||
if let data = res.data(using: .utf8) {
|
||||
let output = try JSONDecoder().decode(InfoResponse.self, from: data)
|
||||
self?.data = output
|
||||
@@ -142,51 +142,73 @@ class Homebrew {
|
||||
|
||||
}
|
||||
|
||||
// func install(_ fullToken: String) where T == Bool {
|
||||
// self.isLoading = true
|
||||
// self.data = false
|
||||
// Task { [weak self] in
|
||||
// do {
|
||||
// try shell("/opt/homebrew/bin/brew install --cask \(fullToken)")
|
||||
// } catch {
|
||||
// self?.errorMessage = error.localizedDescription
|
||||
// }
|
||||
// self?.isLoading = false
|
||||
// do {
|
||||
// var isInstalled = try shell("/opt/homebrew/bin/brew list \(fullToken)>/dev/null 2>&1 && echo true || false")
|
||||
// let _ = isInstalled.popLast()
|
||||
// let installed = isInstalled == "true"
|
||||
// DispatchQueue.main.async { [weak self] in
|
||||
// self?.data = installed
|
||||
// }
|
||||
// } catch {
|
||||
// self?.errorMessage = error.localizedDescription
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// }
|
||||
func isDownloaded(_ name: String) async -> Bool {
|
||||
self.isLoading = true
|
||||
self.errorMessage = nil
|
||||
self.data = nil
|
||||
let task = Task { [weak self] in
|
||||
do {
|
||||
let res = try shell("/opt/homebrew/bin/brew list -1 \(name) >/dev/null 2>&1; echo $?")
|
||||
.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
self?.isLoading = false
|
||||
return res == "0"
|
||||
} catch {
|
||||
self?.errorMessage = error.localizedDescription
|
||||
}
|
||||
|
||||
// func uninstall(_ fullToken: String) {
|
||||
// self.isLoading = true
|
||||
// self.data = false
|
||||
// Task { [weak self] in
|
||||
// do {
|
||||
// try shell("/opt/homebrew/bin/brew uninstall \(fullToken)")
|
||||
// } catch {
|
||||
// self?.errorMessage = error.localizedDescription
|
||||
// }
|
||||
// self?.isLoading = false
|
||||
// do {
|
||||
// var isInstalled = try shell("/opt/homebrew/bin/brew list \(fullToken)>/dev/null 2>&1 && echo true || false")
|
||||
// let _ = isInstalled.popLast()
|
||||
// let installed = isInstalled != "true"
|
||||
// DispatchQueue.main.async { [weak self] in
|
||||
// self?.data = installed
|
||||
// }
|
||||
// } catch {
|
||||
// self?.errorMessage = error.localizedDescription
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
return false
|
||||
}
|
||||
|
||||
switch await task.result {
|
||||
case .success(let success):
|
||||
return success
|
||||
case .failure(let fail):
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func install(_ fullToken: String, isCask: Bool = false) async -> Bool {
|
||||
self.isLoading = true
|
||||
self.data = nil
|
||||
let task = Task { [weak self] in
|
||||
do {
|
||||
try shell("/opt/homebrew/bin/brew install \(isCask ? "--cask" : "") \(fullToken)")
|
||||
} catch {
|
||||
self?.errorMessage = error.localizedDescription
|
||||
}
|
||||
self?.isLoading = false
|
||||
return await self?.isDownloaded(fullToken) ?? false
|
||||
}
|
||||
|
||||
return switch await task.result {
|
||||
case .success(let success):
|
||||
success
|
||||
case .failure(let fail):
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
func uninstall(_ fullToken: String) async -> Bool {
|
||||
self.isLoading = true
|
||||
self.data = nil
|
||||
let task = Task { [weak self] in
|
||||
do {
|
||||
let res =
|
||||
try shell("/opt/homebrew/bin/brew uninstall \(fullToken); echo $?")
|
||||
.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
} catch {
|
||||
self?.errorMessage = error.localizedDescription
|
||||
}
|
||||
self?.isLoading = false
|
||||
return await !(self?.isDownloaded(fullToken) ?? true)
|
||||
}
|
||||
|
||||
return switch await task.result {
|
||||
case .success(let success):
|
||||
success
|
||||
case .failure(let failure):
|
||||
false
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ struct SearchView: View {
|
||||
ProgressView("Loading...")
|
||||
} else if let data = brew.data {
|
||||
List {
|
||||
if !data.casks.isEmpty {
|
||||
Section("Casks") {
|
||||
ForEach(data.casks, id: \.fullToken) { cask in
|
||||
HStack {
|
||||
@@ -33,33 +34,21 @@ struct SearchView: View {
|
||||
.symbolRenderingMode(.palette)
|
||||
.foregroundStyle(.white, .green)
|
||||
} else {
|
||||
if !brew.isLoading && brew.errorMessage == nil {
|
||||
Button("Get") {
|
||||
// brew.install(cask.fullToken)
|
||||
// #warning("Doesn't work, doesn't know why; concurency issue")
|
||||
// if let isInstalled = brew.data, isInstalled {
|
||||
// brew.getInfo(on: cask.fullToken)
|
||||
// }
|
||||
}
|
||||
.buttonStyle(.borderedProminent)
|
||||
.buttonBorderShape(.capsule)
|
||||
.tint(.accentColor)
|
||||
} else if brew.isLoading {
|
||||
ProgressView()
|
||||
.frame(width: 10, height: 10)
|
||||
} else if let error = brew.errorMessage {
|
||||
Image(systemName: "x.circle.fill")
|
||||
.symbolRenderingMode(.palette)
|
||||
.foregroundStyle(.white, .red)
|
||||
DownloadButton(name: cask.fullToken, isCask: true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !data.formulae.isEmpty {
|
||||
|
||||
Section("Formulaes") {
|
||||
// ForEach(data.formulae, id: \.self) {formulae in
|
||||
//
|
||||
// }
|
||||
ForEach(data.formulae, id: \.fullName) { formulae in
|
||||
HStack {
|
||||
Text(formulae.fullName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if let errorMessage = brew.errorMessage {
|
||||
@@ -68,6 +57,7 @@ struct SearchView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#Preview {
|
||||
|
||||
Reference in New Issue
Block a user