Better reactivity and functionality

This commit is contained in:
cdricms
2024-07-09 20:29:20 +02:00
parent bd9c351b7c
commit c675a98309
9 changed files with 541 additions and 361 deletions

View File

@@ -9,6 +9,9 @@
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
9E6C73072C3D5E570056ADDC /* SearchView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E6C73062C3D5E570056ADDC /* SearchView.swift */; }; 9E6C73072C3D5E570056ADDC /* SearchView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E6C73062C3D5E570056ADDC /* SearchView.swift */; };
9E6C73092C3D5E950056ADDC /* InstalledView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E6C73082C3D5E950056ADDC /* InstalledView.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 */; }; 9E8CE5362C3C545600A39146 /* BrewerApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E8CE5352C3C545600A39146 /* BrewerApp.swift */; };
9E8CE5382C3C545600A39146 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E8CE5372C3C545600A39146 /* ContentView.swift */; }; 9E8CE5382C3C545600A39146 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E8CE5372C3C545600A39146 /* ContentView.swift */; };
9E8CE53A2C3C545700A39146 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9E8CE5392C3C545700A39146 /* Assets.xcassets */; }; 9E8CE53A2C3C545700A39146 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9E8CE5392C3C545700A39146 /* Assets.xcassets */; };
@@ -39,6 +42,9 @@
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
9E6C73062C3D5E570056ADDC /* SearchView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchView.swift; sourceTree = "<group>"; }; 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>"; }; 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; }; 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>"; }; 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>"; }; 9E8CE5372C3C545600A39146 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
@@ -78,6 +84,23 @@
/* End PBXFrameworksBuildPhase section */ /* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup 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 = { 9E8CE5292C3C545600A39146 = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
@@ -101,9 +124,11 @@
9E8CE5342C3C545600A39146 /* Brewer */ = { 9E8CE5342C3C545600A39146 /* Brewer */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
9E6C730A2C3D795D0056ADDC /* Components */,
9E8CE5602C3C5A5000A39146 /* Model */, 9E8CE5602C3C5A5000A39146 /* Model */,
9E8CE5352C3C545600A39146 /* BrewerApp.swift */, 9E8CE5352C3C545600A39146 /* BrewerApp.swift */,
9E6C73062C3D5E570056ADDC /* SearchView.swift */, 9E6C73062C3D5E570056ADDC /* SearchView.swift */,
9E6C730F2C3DB5850056ADDC /* Details */,
9E6C73082C3D5E950056ADDC /* InstalledView.swift */, 9E6C73082C3D5E950056ADDC /* InstalledView.swift */,
9E8CE5372C3C545600A39146 /* ContentView.swift */, 9E8CE5372C3C545600A39146 /* ContentView.swift */,
9E8CE5392C3C545700A39146 /* Assets.xcassets */, 9E8CE5392C3C545700A39146 /* Assets.xcassets */,
@@ -277,10 +302,13 @@
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
9E6C73072C3D5E570056ADDC /* SearchView.swift in Sources */, 9E6C73072C3D5E570056ADDC /* SearchView.swift in Sources */,
9E6C730C2C3D796D0056ADDC /* DownloadButton.swift in Sources */,
9E8CE5382C3C545600A39146 /* ContentView.swift in Sources */, 9E8CE5382C3C545600A39146 /* ContentView.swift in Sources */,
9E6C730E2C3DB16F0056ADDC /* UninstallButton.swift in Sources */,
9E8CE5622C3C5A6A00A39146 /* Homebrew.swift in Sources */, 9E8CE5622C3C5A6A00A39146 /* Homebrew.swift in Sources */,
9E6C73092C3D5E950056ADDC /* InstalledView.swift in Sources */, 9E6C73092C3D5E950056ADDC /* InstalledView.swift in Sources */,
9E8CE5362C3C545600A39146 /* BrewerApp.swift in Sources */, 9E8CE5362C3C545600A39146 /* BrewerApp.swift in Sources */,
9E6C73112C3DB5940056ADDC /* CaskDetailView.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@@ -9,12 +9,34 @@ import SwiftUI
@main @main
struct BrewerApp: App { 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 { var body: some Scene {
WindowGroup { WindowGroup {
ContentView() EmptyView()
} }
MenuBarExtra("Brewer", systemImage: "sparkle.magnifyingglass") { MenuBarExtra {
ContentView().padding() ContentView().padding()
}.menuBarExtraStyle(.window) } label: {
if isUpToDate {
Label("Brewer", systemImage: "sparkle.magnifyingglass")
} else {
Label("Brewer", systemImage: "sparkle.magnifyingglass")
.symbolRenderingMode(.palette)
.foregroundStyle(.orange)
} }
} }
.menuBarExtraStyle(.window)
}
}

View 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)
}
}
}
}
}
}

View 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")
}

View 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)
}
}
}
}

View File

@@ -16,7 +16,6 @@ struct ContentView: View {
@State private var segmentedSelection: SegementedSelection = .search @State private var segmentedSelection: SegementedSelection = .search
var body: some View { var body: some View {
NavigationStack { NavigationStack {
Picker("", selection: $segmentedSelection) { Picker("", selection: $segmentedSelection) {
ForEach(SegementedSelection.allCases, id: \.self) { sel in ForEach(SegementedSelection.allCases, id: \.self) { sel in
Text(sel.rawValue).tag(sel) Text(sel.rawValue).tag(sel)

View File

@@ -19,15 +19,8 @@ struct InstalledView: View {
Section { Section {
ForEach(data.casks, id: \.fullToken) { cask in ForEach(data.casks, id: \.fullToken) { cask in
NavigationLink { NavigationLink {
NavigationStack { CaskDetailView(cask: cask, brewListing: brew)
VStack {
Spacer()
}
.navigationTitle(cask.name.first ?? cask.fullToken)
}
} label: { } label: {
HStack { HStack {
Text(cask.name.first ?? cask.fullToken) Text(cask.name.first ?? cask.fullToken)
Spacer() Spacer()
@@ -44,12 +37,7 @@ struct InstalledView: View {
} }
} }
.contextMenu { .contextMenu {
Button(role: .destructive) { UninstallButton(name: cask.fullToken, brewListing: brew)
// brew.uninstall(app.0)
// brew.getInstalled()
} label: {
Label("Uninstall", systemImage: "trash")
}
} }
} }
} }
@@ -81,12 +69,7 @@ struct InstalledView: View {
} }
} }
.contextMenu { .contextMenu {
Button(role: .destructive) { UninstallButton(name: formulae.fullName, brewListing: brew)
// brew.uninstall(app.0)
// brew.getInstalled()
} label: {
Label("Uninstall", systemImage: "trash")
}
} }
} }
} header: { } header: {
@@ -99,13 +82,13 @@ struct InstalledView: View {
} }
} }
.listStyle(.inset(alternatesRowBackgrounds: true)) .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 { Button {
} label: { } label: {
Label("Update all", systemImage: "arrow.counterclockwise.circle.fill") Label("Update all", systemImage: "arrow.counterclockwise.circle.fill")
// .symbolRenderingMode(.palette)
// .foregroundStyle(.white, .orange)
} }
.buttonStyle(.borderedProminent) .buttonStyle(.borderedProminent)
.tint(.orange) .tint(.orange)

View File

@@ -21,9 +21,10 @@ func shell(_ command: String) throws -> String {
task.standardError = pipe task.standardError = pipe
task.standardOutput = pipe task.standardOutput = pipe
try task.run() 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 return output
} }
@@ -129,7 +130,6 @@ class Homebrew {
Task { [weak self] in Task { [weak self] in
do { do {
let res = try shell("/opt/homebrew/bin/brew info --json=v2 --installed") let res = try shell("/opt/homebrew/bin/brew info --json=v2 --installed")
print(res)
if let data = res.data(using: .utf8) { if let data = res.data(using: .utf8) {
let output = try JSONDecoder().decode(InfoResponse.self, from: data) let output = try JSONDecoder().decode(InfoResponse.self, from: data)
self?.data = output self?.data = output
@@ -142,51 +142,73 @@ class Homebrew {
} }
// func install(_ fullToken: String) where T == Bool { func isDownloaded(_ name: String) async -> Bool {
// self.isLoading = true self.isLoading = true
// self.data = false self.errorMessage = nil
// Task { [weak self] in self.data = nil
// do { let task = Task { [weak self] in
// try shell("/opt/homebrew/bin/brew install --cask \(fullToken)") do {
// } catch { let res = try shell("/opt/homebrew/bin/brew list -1 \(name) >/dev/null 2>&1; echo $?")
// self?.errorMessage = error.localizedDescription .trimmingCharacters(in: .whitespacesAndNewlines)
// } self?.isLoading = false
// self?.isLoading = false return res == "0"
// do { } catch {
// var isInstalled = try shell("/opt/homebrew/bin/brew list \(fullToken)>/dev/null 2>&1 && echo true || false") self?.errorMessage = error.localizedDescription
// let _ = isInstalled.popLast()
// let installed = isInstalled == "true"
// DispatchQueue.main.async { [weak self] in
// self?.data = installed
// }
// } 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
}
}
}

View File

@@ -23,6 +23,7 @@ struct SearchView: View {
ProgressView("Loading...") ProgressView("Loading...")
} else if let data = brew.data { } else if let data = brew.data {
List { List {
if !data.casks.isEmpty {
Section("Casks") { Section("Casks") {
ForEach(data.casks, id: \.fullToken) { cask in ForEach(data.casks, id: \.fullToken) { cask in
HStack { HStack {
@@ -33,33 +34,21 @@ struct SearchView: View {
.symbolRenderingMode(.palette) .symbolRenderingMode(.palette)
.foregroundStyle(.white, .green) .foregroundStyle(.white, .green)
} else { } else {
if !brew.isLoading && brew.errorMessage == nil { DownloadButton(name: cask.fullToken, isCask: true)
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)
} }
} }
} }
} }
} }
if !data.formulae.isEmpty {
Section("Formulaes") { 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 { } else if let errorMessage = brew.errorMessage {
@@ -68,6 +57,7 @@ struct SearchView: View {
} }
} }
} }
} }
#Preview { #Preview {