Added a detail view etc

This commit is contained in:
David Bureš 2022-07-03 17:51:12 +02:00 committed by David Bureš
parent 945746e3de
commit 7b26adb04c
10 changed files with 193 additions and 68 deletions

View File

@ -19,6 +19,8 @@
634065822871B0BE001A2178 /* Get Contents of Folder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 634065812871B0BE001A2178 /* Get Contents of Folder.swift */; };
634EB3972871C1F00009DFC2 /* Get Package Version.swift in Sources */ = {isa = PBXBuildFile; fileRef = 634EB3962871C1F00009DFC2 /* Get Package Version.swift */; };
6371EA1E2871D98100300E1B /* Package List Item.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6371EA1D2871D98100300E1B /* Package List Item.swift */; };
6371EA212871E0FE00300E1B /* Return Formatted Versions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6371EA202871E0FE00300E1B /* Return Formatted Versions.swift */; };
6371EA232871EE5A00300E1B /* Brew Interface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6371EA222871EE5A00300E1B /* Brew Interface.swift */; };
639A7D832871D08200B50280 /* Add Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = 639A7D822871D08200B50280 /* Add Package.swift */; };
639A7D862871D78C00B50280 /* Package Details.swift in Sources */ = {isa = PBXBuildFile; fileRef = 639A7D852871D78C00B50280 /* Package Details.swift */; };
/* End PBXBuildFile section */
@ -37,6 +39,8 @@
634065812871B0BE001A2178 /* Get Contents of Folder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Get Contents of Folder.swift"; sourceTree = "<group>"; };
634EB3962871C1F00009DFC2 /* Get Package Version.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Get Package Version.swift"; sourceTree = "<group>"; };
6371EA1D2871D98100300E1B /* Package List Item.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Package List Item.swift"; sourceTree = "<group>"; };
6371EA202871E0FE00300E1B /* Return Formatted Versions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Return Formatted Versions.swift"; sourceTree = "<group>"; };
6371EA222871EE5A00300E1B /* Brew Interface.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Brew Interface.swift"; sourceTree = "<group>"; };
639A7D822871D08200B50280 /* Add Package.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Add Package.swift"; sourceTree = "<group>"; };
639A7D852871D78C00B50280 /* Package Details.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Package Details.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */
@ -96,8 +100,9 @@
634065762871AB2A001A2178 /* Logic */ = {
isa = PBXGroup;
children = (
6371EA242871EE6000300E1B /* Shell */,
6371EA1F2871E0EC00300E1B /* Helpers */,
634065802871B0A3001A2178 /* File Browser */,
634065772871AB3A001A2178 /* Shell Interface.swift */,
634065792871AC27001A2178 /* Brew Data Storage.swift */,
);
path = Logic;
@ -129,6 +134,23 @@
path = Packages;
sourceTree = "<group>";
};
6371EA1F2871E0EC00300E1B /* Helpers */ = {
isa = PBXGroup;
children = (
6371EA202871E0FE00300E1B /* Return Formatted Versions.swift */,
);
path = Helpers;
sourceTree = "<group>";
};
6371EA242871EE6000300E1B /* Shell */ = {
isa = PBXGroup;
children = (
634065772871AB3A001A2178 /* Shell Interface.swift */,
6371EA222871EE5A00300E1B /* Brew Interface.swift */,
);
path = Shell;
sourceTree = "<group>";
};
639A7D812871D07800B50280 /* Views */ = {
isa = PBXGroup;
children = (
@ -223,6 +245,8 @@
6340657D2871AC65001A2178 /* Brew Package.swift in Sources */,
6371EA1E2871D98100300E1B /* Package List Item.swift in Sources */,
634EB3972871C1F00009DFC2 /* Get Package Version.swift in Sources */,
6371EA212871E0FE00300E1B /* Return Formatted Versions.swift in Sources */,
6371EA232871EE5A00300E1B /* Brew Interface.swift in Sources */,
639A7D862871D78C00B50280 /* Package Details.swift in Sources */,
634065652871AA42001A2178 /* CorkApp.swift in Sources */,
);

View File

@ -10,6 +10,7 @@ import SwiftUI
struct ContentView: View {
@StateObject var brewData = BrewDataStorage()
@StateObject var selectedPackageInfo = SelectedPackageInfo()
@State private var multiSelection = Set<UUID>()
@ -18,33 +19,42 @@ struct ContentView: View {
var body: some View {
VStack {
NavigationView {
List(selection: $multiSelection) {
Section("Installed Formulae") {
Text("Installed Formulae")
.font(.headline)
Section(header: Text("Installed Formulae").font(.headline).foregroundColor(.black)) {
if brewData.installedFormulae.count != 0 {
ForEach(brewData.installedFormulae) { package in
NavigationLink {
PackageDetailView(package: package, isCask: false, packageInfo: selectedPackageInfo)
} label: {
PackageListItem(packageItem: package)
}
}
} else {
ProgressView()
}
}
Section("Installed Casks") {
Text("Installed Casks")
.font(.headline)
Section(header: Text("Installed Casks").font(.headline).foregroundColor(.black)) {
if brewData.installedCasks.count != 0 {
ForEach(brewData.installedCasks) { package in
NavigationLink {
PackageDetailView(package: package, isCask: true, packageInfo: selectedPackageInfo)
} label: {
PackageListItem(packageItem: package)
}
}
} else {
ProgressView()
}
}
}
.listStyle(.bordered)
}
.toolbar {
VStack(alignment: .leading) {
Text("Cork")
@ -98,12 +108,9 @@ struct ContentView: View {
Task {
print("Started Command task at \(Date())")
do {
let commandResult = try await executeCommand("/opt/homebrew/bin list")
let commandResult = await shell("/opt/homebrew/bin/brew", ["search", "gimp"])
print(commandResult)
} catch let error as NSError {
print("Error executing command: \(error)")
}
}
Task { // Task that gets the contents of the Cellar folder
@ -111,8 +118,8 @@ struct ContentView: View {
let contentsOfCellarFolder = await getContentsOfFolder(targetFolder: AppConstantsLocal.brewCellarPath)
for package in contentsOfCellarFolder {
brewData.installedFormulae.append(BrewPackage(name: package))
print("Appended \(package)")
brewData.installedFormulae.append(package)
// print("Appended \(package)")
}
//print(brewData.installedFormulae!)
@ -123,8 +130,8 @@ struct ContentView: View {
let contentsOfCaskFolder = await getContentsOfFolder(targetFolder: AppConstantsLocal.brewCaskPath)
for package in contentsOfCaskFolder {
brewData.installedCasks.append(BrewPackage(name: package))
print("Appended \(package)")
brewData.installedCasks.append(package)
// print("Appended \(package)")
}
//print(brewData.installedCasks!)

View File

@ -7,14 +7,24 @@
import Foundation
func getContentsOfFolder(targetFolder: URL) async -> [String] {
var contentsOfFolder = [String]()
func getContentsOfFolder(targetFolder: URL) async -> [BrewPackage] {
var contentsOfFolder = [BrewPackage]()
do {
let items = try FileManager.default.contentsOfDirectory(atPath: targetFolder.path)
for item in items {
contentsOfFolder.append(item)
do {
let versions = try FileManager.default.contentsOfDirectory(atPath: targetFolder.appendingPathComponent(item, conformingTo: .folder).path)
contentsOfFolder.append(BrewPackage(name: item, versions: versions))
} catch let error as NSError {
print("Failed while getting package version: \(error)")
}
}
} catch let error as NSError {
print("Failed while accessing foldeR: \(error)")
}

View File

@ -0,0 +1,12 @@
//
// Return Formatted Versions.swift
// Cork
//
// Created by David Bureš on 03.07.2022.
//
import Foundation
func returnFormattedVersions(_ array: [String]) -> String {
return array.joined(separator: ", ")
}

View File

@ -1,25 +0,0 @@
//
// Shell Interface.swift
// Cork
//
// Created by David Bureš on 03.07.2022.
//
import Foundation
func executeCommand(_ commandToExecute: String) async throws -> String {
let task = Process()
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
task.arguments = ["-c", commandToExecute]
task.launchPath = "/bin/zsh"
task.standardInput = nil
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)!
return output
}

View File

@ -0,0 +1,27 @@
//
// Brew Interface.swift
// Cork
//
// Created by David Bureš on 03.07.2022.
//
import Foundation
enum BrewCommands {
case search, info, install, delete
}
/*
func executeBrewCommand(commandType: BrewCommands, argument: String? = nil) -> String {
switch commandType {
case .search:
<#code#>
case .info:
<#code#>
case .install:
<#code#>
case .delete:
<#code#>
}
}
*/

View File

@ -0,0 +1,28 @@
//
// Shell Interface.swift
// Cork
//
// Created by David Bureš on 03.07.2022.
//
import Foundation
func shell(_ launchPath: String, _ arguments: [String]) async -> String?
{
let task = Process()
task.launchPath = launchPath
task.arguments = arguments
let pipe = Pipe()
task.standardOutput = pipe
do {
try task.run()
} catch {
print(error)
}
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: String.Encoding.utf8)
return output
}

View File

@ -10,4 +10,5 @@ import Foundation
struct BrewPackage: Identifiable {
let id = UUID()
let name: String
let versions: [String]
}

View File

@ -7,10 +7,49 @@
import SwiftUI
class SelectedPackageInfo: ObservableObject {
@Published var contents: String?
}
struct PackageDetailView: View {
@State var packageName: String
@State var package: BrewPackage
@State var isCask: Bool
@StateObject var packageInfo: SelectedPackageInfo
var body: some View {
Text(packageName)
VStack {
Text(package.name)
.font(.title)
Text(returnFormattedVersions(package.versions))
.font(.subheadline)
.foregroundColor(.gray)
if packageInfo.contents == nil {
VStack {
ProgressView()
Text("Loading package info...")
}
} else {
ScrollView {
Text(packageInfo.contents!)
}
}
}
.onAppear {
Task {
if !isCask {
packageInfo.contents = await shell("/opt/homebrew/bin/brew", ["info", "--json", package.name])
} else {
packageInfo.contents = await shell("/opt/homebrew/bin/brew", ["info", "--json=v2", "--cask", package.name])
}
}
}
.onDisappear {
packageInfo.contents = nil
}
}
}

View File

@ -12,11 +12,13 @@ struct PackageListItem: View {
var body: some View {
HStack {
HStack(alignment: .firstTextBaseline) {
Text(packageItem.name)
Text(returnFormattedVersions(packageItem.versions))
.font(.subheadline)
.foregroundColor(.gray)
}
Spacer()
Image(systemName: "arrow.right")
}
}
}