Skip to content

Commit 224540b

Browse files
committed
♻️ refactor commands
1 parent a8dfec2 commit 224540b

File tree

4 files changed

+134
-139
lines changed

4 files changed

+134
-139
lines changed

ui/iPortForwarder.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
967344E82A1DE8DF00141A9E /* ForwardedItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 967344E72A1DE8DF00141A9E /* ForwardedItem.swift */; };
1616
967344EB2A1DEAFD00141A9E /* ForwardedItemRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 967344EA2A1DEAFD00141A9E /* ForwardedItemRow.swift */; };
1717
9673452F2A1F8FF900141A9E /* Libipf in Frameworks */ = {isa = PBXBuildFile; productRef = 9673452E2A1F8FF900141A9E /* Libipf */; };
18+
96E31ACA2A68EF1A004DAF36 /* iPortForwarderCommands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96E31AC92A68EF1A004DAF36 /* iPortForwarderCommands.swift */; };
19+
96E31ACC2A68F0D5004DAF36 /* iPortForwarderState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96E31ACB2A68F0D5004DAF36 /* iPortForwarderState.swift */; };
1820
/* End PBXBuildFile section */
1921

2022
/* Begin PBXFileReference section */
@@ -29,6 +31,8 @@
2931
967344EA2A1DEAFD00141A9E /* ForwardedItemRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForwardedItemRow.swift; sourceTree = "<group>"; };
3032
9673452C2A1F8FE700141A9E /* Libipf */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = Libipf; path = iPortForwarder/Libipf; sourceTree = "<group>"; };
3133
9673452D2A1F8FED00141A9E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
34+
96E31AC92A68EF1A004DAF36 /* iPortForwarderCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iPortForwarderCommands.swift; sourceTree = "<group>"; };
35+
96E31ACB2A68F0D5004DAF36 /* iPortForwarderState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iPortForwarderState.swift; sourceTree = "<group>"; };
3236
/* End PBXFileReference section */
3337

3438
/* Begin PBXFrameworksBuildPhase section */
@@ -67,6 +71,8 @@
6771
9673452D2A1F8FED00141A9E /* Info.plist */,
6872
967344E92A1DEA1300141A9E /* Views */,
6973
967344D82A1DE03400141A9E /* iPortForwarderApp.swift */,
74+
96E31AC92A68EF1A004DAF36 /* iPortForwarderCommands.swift */,
75+
96E31ACB2A68F0D5004DAF36 /* iPortForwarderState.swift */,
7076
967344DC2A1DE03800141A9E /* Assets.xcassets */,
7177
967344E12A1DE03800141A9E /* iPortForwarder.entitlements */,
7278
967344DE2A1DE03800141A9E /* Preview Content */,
@@ -183,7 +189,9 @@
183189
files = (
184190
960312BC2A52E69000B79937 /* ShowErrorDialog.swift in Sources */,
185191
967344DB2A1DE03400141A9E /* ContentView.swift in Sources */,
192+
96E31ACC2A68F0D5004DAF36 /* iPortForwarderState.swift in Sources */,
186193
967344D92A1DE03400141A9E /* iPortForwarderApp.swift in Sources */,
194+
96E31ACA2A68EF1A004DAF36 /* iPortForwarderCommands.swift in Sources */,
187195
967344EB2A1DEAFD00141A9E /* ForwardedItemRow.swift in Sources */,
188196
967344E82A1DE8DF00141A9E /* ForwardedItem.swift in Sources */,
189197
);
Lines changed: 2 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,23 @@
11
import SwiftUI
2-
import AppKit
32

43
import Libipf
54

6-
class GlobalState: ObservableObject {
7-
@Published var items: [ForwardedItem] = []
8-
@Published var errors: [Int8: [IpfError]] = [:]
9-
@Published var isAddingNewItem: Bool = false
10-
11-
public func clear() {
12-
items = []
13-
errors = [:]
14-
isAddingNewItem = false
15-
}
16-
}
17-
18-
var globalState = GlobalState()
19-
20-
// Disable tab
21-
final class AppDelegate: NSObject, NSApplicationDelegate {
22-
func applicationDidFinishLaunching(_ notification: Notification) {
23-
NSWindow.allowsAutomaticWindowTabbing = false
24-
}
25-
}
26-
27-
struct WindowAccessor: NSViewRepresentable {
28-
@Binding var window: NSWindow?
29-
30-
func makeNSView(context: Context) -> NSView {
31-
let view = NSView()
32-
DispatchQueue.main.async {
33-
self.window = view.window
34-
}
35-
return view
36-
}
37-
38-
func updateNSView(_ nsView: NSView, context: Context) {}
39-
}
40-
415
@main
426
struct iPortForwarderApp: App {
43-
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
44-
45-
@State private var window: NSWindow?
46-
@State private var isMainWindowOpened = false
47-
487
init() {
49-
try! Libipf.registerErrorHandler { id, ipfError in
50-
DispatchQueue.main.async {
51-
if var errors = globalState.errors[id] {
52-
errors.append(ipfError)
53-
} else {
54-
globalState.errors[id] = [ipfError]
55-
}
56-
}
57-
}
8+
initLibipfErrorHandler()
589
}
5910

6011
var body: some Scene {
6112
WindowGroup {
6213
ContentView()
63-
.background(WindowAccessor(window: $window))
6414
.environmentObject(globalState)
65-
.onAppear {
66-
isMainWindowOpened = true
67-
}
6815
.onDisappear {
69-
isMainWindowOpened = false
7016
globalState.clear()
7117
}
7218
}
7319
.commands {
74-
CommandGroup(replacing: .newItem) {
75-
if isMainWindowOpened {
76-
Button("New Forward Item") {
77-
withAnimation {
78-
globalState.isAddingNewItem = true
79-
}
80-
}
81-
.keyboardShortcut("n", modifiers: .command)
82-
}
83-
}
84-
85-
CommandGroup(replacing: .saveItem) {
86-
if isMainWindowOpened {
87-
Button("Save Current Forwarding List") {
88-
let listOfItemInfo = globalState.items.map {
89-
ForwardedItemInfo(item: $0)
90-
}
91-
let jsonData = try! JSONEncoder().encode(listOfItemInfo)
92-
let jsonString = String(data: jsonData, encoding: .utf8)!
93-
94-
let savePanel = NSSavePanel()
95-
savePanel.allowedContentTypes = [.json]
96-
savePanel.canCreateDirectories = true
97-
savePanel.isExtensionHidden = false
98-
savePanel.title = "Save current forwarding list"
99-
savePanel.message = "Choose a folder and a name to store the list."
100-
savePanel.nameFieldLabel = "List name:"
101-
savePanel.beginSheetModal(for: window!) {
102-
if $0 == .OK {
103-
if let saveUrl = savePanel.url {
104-
do {
105-
try jsonString.write(to: saveUrl, atomically: false, encoding: .utf8)
106-
} catch {
107-
showErrorDialog(error)
108-
}
109-
}
110-
}
111-
}
112-
}
113-
.keyboardShortcut("s", modifiers: .command)
114-
}
115-
}
116-
117-
CommandGroup(after: .saveItem) {
118-
if isMainWindowOpened {
119-
Button("Import Forwarding List") {
120-
let openPanel = NSOpenPanel()
121-
openPanel.allowsMultipleSelection = false
122-
openPanel.allowedContentTypes = [.json]
123-
openPanel.allowsOtherFileTypes = false
124-
openPanel.canChooseFiles = true
125-
openPanel.canChooseDirectories = false
126-
openPanel.canCreateDirectories = false
127-
openPanel.isExtensionHidden = false
128-
openPanel.title = "Import a forwarding list"
129-
openPanel.message = "Choose a forwarding list to import."
130-
openPanel.beginSheetModal(for: window!) {
131-
if $0 == .OK {
132-
if let openUrl = openPanel.url {
133-
do {
134-
var jsonString: String
135-
if #available(macOS 13, *) {
136-
jsonString = try String(contentsOfFile: openUrl.path(percentEncoded: false))
137-
} else {
138-
jsonString = try String(contentsOfFile: openUrl.path)
139-
}
140-
141-
let list = try JSONDecoder().decode([ForwardedItemInfo].self, from: jsonString.data(using: .utf8)!)
142-
143-
try withAnimation {
144-
for item in list {
145-
globalState.items.append(try ForwardedItem(item: item))
146-
}
147-
}
148-
} catch {
149-
showErrorDialog(error)
150-
}
151-
}
152-
}
153-
}
154-
}
155-
.keyboardShortcut("o", modifiers: .command)
156-
}
157-
}
20+
iPortForwarderCommands()
15821
}
15922
}
16023
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import Foundation
2+
import SwiftUI
3+
4+
struct iPortForwarderCommands: Commands {
5+
var window: NSWindow? {
6+
get {
7+
return NSApplication.shared.windows.first
8+
}
9+
}
10+
11+
var body: some Commands {
12+
CommandGroup(replacing: .newItem) {
13+
Button("New Forward Item") {
14+
withAnimation {
15+
globalState.isAddingNewItem = true
16+
}
17+
}
18+
.keyboardShortcut("n", modifiers: .command)
19+
}
20+
21+
CommandGroup(replacing: .saveItem) {
22+
Button("Save Current Forwarding List") {
23+
if let window {
24+
let listOfItemInfo = globalState.items.map {
25+
ForwardedItemInfo(item: $0)
26+
}
27+
let jsonData = try! JSONEncoder().encode(listOfItemInfo)
28+
let jsonString = String(data: jsonData, encoding: .utf8)!
29+
30+
let savePanel = NSSavePanel()
31+
savePanel.allowedContentTypes = [.json]
32+
savePanel.canCreateDirectories = true
33+
savePanel.isExtensionHidden = false
34+
savePanel.title = "Save current forwarding list"
35+
savePanel.message = "Choose a folder and a name to store the list."
36+
savePanel.nameFieldLabel = "List name:"
37+
savePanel.beginSheetModal(for: window) {
38+
if $0 == .OK {
39+
if let saveUrl = savePanel.url {
40+
do {
41+
try jsonString.write(to: saveUrl, atomically: false, encoding: .utf8)
42+
} catch {
43+
showErrorDialog(error)
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}
50+
.keyboardShortcut("s", modifiers: .command)
51+
}
52+
53+
CommandGroup(after: .saveItem) {
54+
Button("Import Forwarding List") {
55+
if let window {
56+
let openPanel = NSOpenPanel()
57+
openPanel.allowsMultipleSelection = false
58+
openPanel.allowedContentTypes = [.json]
59+
openPanel.allowsOtherFileTypes = false
60+
openPanel.canChooseFiles = true
61+
openPanel.canChooseDirectories = false
62+
openPanel.canCreateDirectories = false
63+
openPanel.isExtensionHidden = false
64+
openPanel.title = "Import a forwarding list"
65+
openPanel.message = "Choose a forwarding list to import."
66+
openPanel.beginSheetModal(for: window) {
67+
if $0 == .OK {
68+
if let openUrl = openPanel.url {
69+
do {
70+
var jsonString: String
71+
if #available(macOS 13, *) {
72+
jsonString = try String(contentsOfFile: openUrl.path(percentEncoded: false))
73+
} else {
74+
jsonString = try String(contentsOfFile: openUrl.path)
75+
}
76+
77+
let list = try JSONDecoder().decode([ForwardedItemInfo].self, from: jsonString.data(using: .utf8)!)
78+
79+
try withAnimation {
80+
for item in list {
81+
globalState.items.append(try ForwardedItem(item: item))
82+
}
83+
}
84+
} catch {
85+
showErrorDialog(error)
86+
}
87+
}
88+
}
89+
}
90+
}
91+
}
92+
.keyboardShortcut("o", modifiers: .command)
93+
}
94+
}
95+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Foundation
2+
3+
import Libipf
4+
5+
class GlobalState: ObservableObject {
6+
@Published var items: [ForwardedItem] = []
7+
@Published var errors: [Int8: [IpfError]] = [:]
8+
@Published var isAddingNewItem: Bool = false
9+
10+
public func clear() {
11+
items = []
12+
errors = [:]
13+
isAddingNewItem = false
14+
}
15+
}
16+
17+
var globalState = GlobalState()
18+
19+
func initLibipfErrorHandler() {
20+
try! Libipf.registerErrorHandler { id, ipfError in
21+
DispatchQueue.main.async {
22+
if var errors = globalState.errors[id] {
23+
errors.append(ipfError)
24+
} else {
25+
globalState.errors[id] = [ipfError]
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)