Skip to content

Commit

Permalink
GUI Enhancements and Code Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
TechUnRestricted committed May 25, 2022
1 parent f41d93c commit 6c93d68
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 122 deletions.
17 changes: 9 additions & 8 deletions PSDirectSender/ConnectionDetails.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@

import Foundation

class LogsCollector: ObservableObject {
@Published var logLines: [String] = []
func addLog(_ text: String) {
DispatchQueue.main.async {
self.logLines.append("[\(getStringDate())] \(text)")
}
}
}

class ConnectionDetails: ObservableObject {
@Published var serverIP: String = ""
@Published var serverPort: String = ""
Expand All @@ -15,8 +24,6 @@ class ConnectionDetails: ObservableObject {
@Published var connectionStatus: ServerStatus = .stopped
@Published var networkingIPs: [String] = []

@Published var logLines: [String] = []

func generateServerDetails() {
serverPort = String(networking.findFreePort())

Expand All @@ -25,10 +32,4 @@ class ConnectionDetails: ObservableObject {
serverIP = ip
}
}

func addLog(_ text: String) {
DispatchQueue.main.async {
self.logLines.append("[\(getStringDate())] \(text)")
}
}
}
20 changes: 12 additions & 8 deletions PSDirectSender/PSDirectSenderApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ let message: MessageCollection = .init()

@main
struct PSDirectSenderApp: App {
@StateObject var currentTheme = ConnectionDetails()

@StateObject var connectionDetails = ConnectionDetails()
@StateObject var logsCollector = LogsCollector()

var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(currentTheme)
.environmentObject(connectionDetails)
.environmentObject(logsCollector)
.frame(minWidth: 650,
idealWidth: 750,
maxWidth: .infinity,
Expand Down Expand Up @@ -181,12 +183,14 @@ func swiftStartServer(serverIP: String, serverPort: String) {
}

@discardableResult
func sendPackagesToConsole(packageFilename: String, consoleIP: String, consolePort: Int, serverIP: String, serverPort: Int) -> Any? {
func sendPackagesToConsole(packageFilename: String, connection: ConnectionDetails) -> Any? {

let dataStructure = PackageSenderData(type: "direct", packages: ["http://\(serverIP):\(serverPort)/\(packageFilename)"])
let dataStructure = PackageSenderData(type: "direct",
packages: ["http://\(connection.serverIP):\(connection.serverPort)/\(packageFilename)"]
)
let jsonData = try? JSONEncoder().encode(dataStructure)

let builtURL = URL(string: "http://\(consoleIP):\(consolePort)/api/install")
let builtURL = URL(string: "http://\(connection.consoleIP):\(connection.consolePort)/api/install")

var request = URLRequest(
url: (builtURL)!,
Expand Down Expand Up @@ -214,8 +218,8 @@ func sendPackagesToConsole(packageFilename: String, consoleIP: String, consolePo
return nil
}

func checkIfServerIsWorking(serverIP: String, serverPort: String) -> ServerStatus {
guard let url = URL(string: "http://\(serverIP):\(serverPort)") else {
func checkIfServerIsWorking(connection: ConnectionDetails) -> ServerStatus {
guard let url = URL(string: "http://\(connection.serverIP):\(connection.serverPort)") else {
return .fail
}
var status: ServerStatus = .stopped
Expand Down
25 changes: 11 additions & 14 deletions PSDirectSender/SFOExplorer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,23 @@ class SFOExplorer {
return nil
}

func getParamSFOData(url: URL) -> [String: String]? {
do {
let fileHandler: FileHandle = try .init(forReadingFrom: url)
return getParamSFOData(fileHandler: fileHandler)
} catch { }
return nil
}

func getParamSFOData(fileHandler: FileHandle) -> [String: String] {
/// MAGIC
/*let magic: Int32*/ _ = loadFileDataBlocks(
/*
let magic: Int32 = loadFileDataBlocks(
from: 0,
bytesCount: 4,
fileHandler: fileHandler
).integerRepresentation
*/

/// PARAM.SFO
guard let offset32: UInt32 = getParamSFOOffset(fileHandler: fileHandler) else {
Expand Down Expand Up @@ -171,16 +181,3 @@ class SFOExplorer {
return paramSFOData
}
}
/*printTimeElapsedWhenRunningCode(title: "Main", operation: {
let pkgPath: String = "/Users/macintosh/0000000000000000000.pkg"

guard let fileHandler: FileHandle = .init(forReadingAtPath: pkgPath) else {
return
}

let data = getParamSFOData(fileHandler: fileHandler)
for (key, value) in data {
print("\(key)=\(value)")
}
try? fileHandler.close()
})*/
6 changes: 4 additions & 2 deletions PSDirectSender/Screens/ConfigurationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ fileprivate class HelpShow: ObservableObject {

struct ConfigurationView: View {
@EnvironmentObject var connection: ConnectionDetails
@EnvironmentObject var logsCollector: LogsCollector

@StateObject var inputConnectionData: ConnectionDetails = ConnectionDetails()

@StateObject fileprivate var helpShow: HelpShow = HelpShow()
Expand Down Expand Up @@ -139,7 +141,7 @@ struct ConfigurationView: View {
connection.consoleIP = inputConnectionData.consoleIP
connection.consolePort = inputConnectionData.consolePort

connection.addLog("""
logsCollector.addLog("""
Staring web server:
[SERVER] IP: \(connection.serverIP) Port: \(connection.serverPort)
[CONSOLE] IP: \(connection.consoleIP) Port: \(connection.consolePort)
Expand Down Expand Up @@ -173,7 +175,7 @@ Staring web server:
return
}

let status = checkIfServerIsWorking(serverIP: connection.serverIP, serverPort: connection.serverPort)
let status = checkIfServerIsWorking(connection: connection)
DispatchQueue.main.async {
connection.connectionStatus = status
connectionStatusLoaded = true
Expand Down
22 changes: 11 additions & 11 deletions PSDirectSender/Screens/LogsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ import SwiftUI

struct LogsView: View {
@EnvironmentObject var connection: ConnectionDetails

@EnvironmentObject var logsCollector: LogsCollector

var body: some View {
VStack {
HStack(spacing: 25) {
ColorButton(text: "Copy logs", color: .purple, image: Image(systemName: "doc.on.doc"), action: {
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
pasteboard.setString(connection.logLines.joined(separator: "\n"), forType: .string)
pasteboard.setString(logsCollector.logLines.joined(separator: "\n"), forType: .string)
})

ColorButton(text: "Clear logs", color: .red, image: Image(systemName: "trash"), action: {
connection.logLines.removeAll()
logsCollector.logLines.removeAll()
})
}.padding()
List {
ForEach(connection.logLines, id: \.self) { logLine in
ForEach(logsCollector.logLines, id: \.self) { logLine in
Text(LocalizedStringKey(logLine))
.frame(maxWidth: .infinity, alignment: .leading)
.lineLimit(3)
Expand All @@ -42,13 +43,12 @@ struct LogsView_Previews: PreviewProvider {

static var previews: some View {
let view = LogsView()
.environmentObject({ () -> ConnectionDetails in
let vm = ConnectionDetails()
vm.addLog("Can't get server configuration.")
vm.addLog("Can't get console configuration.")
vm.addLog("Creating package alias \(#""/Volumes/Macintosh HD/game.pkg""#) -> \"\(tempDirectory.path)\(UUID().uuidString).pkg\"")

return vm
.environmentObject({ () -> LogsCollector in
let logsCollector = LogsCollector()
logsCollector.addLog("Can't get server configuration.")
logsCollector.addLog("Can't get console configuration.")
logsCollector.addLog("Creating package alias \(#""/Volumes/Macintosh HD/game.pkg""#) -> \"\(tempDirectory.path)\(UUID().uuidString).pkg\"")
return logsCollector
}())
view
view
Expand Down
Loading

0 comments on commit 6c93d68

Please sign in to comment.