Skip to content

Commit 0bfb6d2

Browse files
committed
Add swiftlint & Minor fixes
1 parent 8855e0e commit 0bfb6d2

29 files changed

+375
-189
lines changed

.swiftlint.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
excluded:
2+
- Frameworks
3+
- Libbox.xcframework
4+
- build
5+
6+
disabled_rules:
7+
- identifier_name
8+
- type_body_length
9+
- file_length
10+
- function_body_length
11+
- cyclomatic_complexity
12+
- large_tuple
13+
- nesting
14+
- line_length
15+
- todo
16+
- trailing_comma
17+
- function_parameter_count
18+
- type_name
19+
- force_cast
20+
- force_try
21+
- opening_brace

ApplicationLibrary/Views/Abstract/NavigationButtonsView.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,30 @@ public struct NavigationButtonsView: View {
2727

2828
public var body: some View {
2929
HStack(spacing: 12) {
30-
if showGroupsButton {
30+
if showConnectionsButton {
3131
Divider()
32-
Text(verbatim: "\(groupsCount)")
32+
Text(verbatim: "\(connectionsCount)")
3333
.font(.subheadline)
3434
.foregroundStyle(.secondary)
3535
.fixedSize()
3636
Button {
37-
onGroupsTap()
37+
onConnectionsTap()
3838
} label: {
39-
Label("Groups", systemImage: "rectangle.3.group.fill")
39+
Label("Connections", systemImage: "list.bullet.rectangle.portrait.fill")
4040
}
4141
.labelStyle(.iconOnly)
4242
.foregroundStyle(.primary)
4343
}
44-
if showConnectionsButton {
44+
if showGroupsButton {
4545
Divider()
46-
Text(verbatim: "\(connectionsCount)")
46+
Text(verbatim: "\(groupsCount)")
4747
.font(.subheadline)
4848
.foregroundStyle(.secondary)
4949
.fixedSize()
5050
Button {
51-
onConnectionsTap()
51+
onGroupsTap()
5252
} label: {
53-
Label("Connections", systemImage: "list.bullet.rectangle.portrait.fill")
53+
Label("Groups", systemImage: "rectangle.3.group.fill")
5454
}
5555
.labelStyle(.iconOnly)
5656
.foregroundStyle(.primary)

ApplicationLibrary/Views/Connections/Connection.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ public struct Connection: Codable {
2424
public let outboundType: String
2525
public let chain: [String]
2626

27-
var hashValue: Int {
28-
var value = id.hashValue
29-
(value, _) = value.addingReportingOverflow(upload.hashValue)
30-
(value, _) = value.addingReportingOverflow(download.hashValue)
31-
(value, _) = value.addingReportingOverflow(uploadTotal.hashValue)
32-
(value, _) = value.addingReportingOverflow(downloadTotal.hashValue)
33-
return value
27+
func hash(into hasher: inout Hasher) {
28+
hasher.combine(id)
29+
hasher.combine(upload)
30+
hasher.combine(download)
31+
hasher.combine(uploadTotal)
32+
hasher.combine(downloadTotal)
3433
}
3534

3635
func performSearch(_ content: String) -> Bool {

ApplicationLibrary/Views/Connections/ConnectionListView.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public struct ConnectionListView: View {
1717
} else {
1818
ScrollView {
1919
LazyVGrid(columns: [GridItem(.flexible())], alignment: .leading) {
20-
ForEach(viewModel.filteredConnections(), id: \.hashValue) { it in
20+
ForEach(viewModel.filteredConnections(), id: \.id) { it in
2121
ConnectionView(it)
2222
}
2323
}
@@ -56,9 +56,19 @@ public struct ConnectionListView: View {
5656
#endif
5757
.alertBinding($viewModel.alert)
5858
.onAppear {
59-
viewModel.setCommandClient(environments.commandClient)
6059
viewModel.connect()
6160
}
61+
.onReceive(environments.commandClient.$connections) { connections in
62+
viewModel.setConnections(connections)
63+
}
64+
.onChangeCompat(of: viewModel.connectionStateFilter) { filter in
65+
environments.commandClient.connectionStateFilter = filter
66+
environments.commandClient.filterConnectionsNow()
67+
}
68+
.onChangeCompat(of: viewModel.connectionSort) { sort in
69+
environments.commandClient.connectionSort = sort
70+
environments.commandClient.filterConnectionsNow()
71+
}
6272
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
6373
#if os(iOS)
6474
.background(Color(uiColor: .systemGroupedBackground))

ApplicationLibrary/Views/Connections/ConnectionListViewModel.swift

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Combine
21
import Libbox
32
import Library
43
import SwiftUI
@@ -11,8 +10,6 @@ public class ConnectionListViewModel: ObservableObject {
1110
@Published public var alert: Alert?
1211
@Published public var connectionStateFilter: ConnectionStateFilter {
1312
didSet {
14-
commandClient?.connectionStateFilter = connectionStateFilter
15-
commandClient?.filterConnectionsNow()
1613
saveStateFilterTask?.cancel()
1714
saveStateFilterTask = Task {
1815
await SharedPreferences.connectionStateFilter.set(connectionStateFilter.rawValue)
@@ -22,17 +19,13 @@ public class ConnectionListViewModel: ObservableObject {
2219

2320
@Published public var connectionSort: ConnectionSort {
2421
didSet {
25-
commandClient?.connectionSort = connectionSort
26-
commandClient?.filterConnectionsNow()
2722
saveSortTask?.cancel()
2823
saveSortTask = Task {
2924
await SharedPreferences.connectionSort.set(connectionSort.rawValue)
3025
}
3126
}
3227
}
3328

34-
private var commandClient: CommandClient?
35-
private var cancellables = Set<AnyCancellable>()
3629
private var connectTask: Task<Void, Never>?
3730
private var saveStateFilterTask: Task<Void, Never>?
3831
private var saveSortTask: Task<Void, Never>?
@@ -42,16 +35,6 @@ public class ConnectionListViewModel: ObservableObject {
4235
connectionSort = .byDate
4336
}
4437

45-
public func setCommandClient(_ client: CommandClient) {
46-
commandClient = client
47-
client.$connections
48-
.compactMap { $0 }
49-
.sink { [weak self] goConnections in
50-
self?.setConnections(goConnections)
51-
}
52-
.store(in: &cancellables)
53-
}
54-
5538
public func connect() {
5639
if ApplicationLibrary.inPreview {
5740
isLoading = false
@@ -97,7 +80,8 @@ public class ConnectionListViewModel: ObservableObject {
9780
}
9881
}
9982

100-
private func setConnections(_ goConnections: [LibboxConnection]) {
83+
public func setConnections(_ goConnections: [LibboxConnection]?) {
84+
guard let goConnections else { return }
10185
connections = convertConnections(goConnections)
10286
isLoading = false
10387
}

ApplicationLibrary/Views/Dashboard/ActiveDashboardView.swift

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -111,22 +111,25 @@ public struct ActiveDashboardView: View {
111111

112112
#if os(iOS) || os(tvOS)
113113
private func updateButtonVisibility() {
114-
buttonState.update(profile: profile, commandClient: environments.commandClient)
114+
buttonState.update(profile: profile, commandClient: environments.commandClient, requireAnyConnection: true)
115115
}
116116

117+
#if os(iOS)
118+
private var isTabViewBottomAccessoryAvailable: Bool {
119+
if #available(iOS 26.0, *), !Variant.debugNoIOS26 {
120+
return true
121+
}
122+
return false
123+
}
124+
#endif
125+
117126
@ToolbarContentBuilder
118127
private var toolbar: some ToolbarContent {
119-
ToolbarItem(placement: .topBarLeading) {
120-
#if os(iOS)
121-
if #available(iOS 26.0, *), !Variant.debugNoIOS26 {
122-
EmptyView()
123-
} else {
124-
navigationButtons
125-
}
126-
#else
128+
#if os(tvOS)
129+
ToolbarItem(placement: .topBarLeading) {
127130
navigationButtons
128-
#endif
129-
}
131+
}
132+
#endif
130133
ToolbarItem(placement: .topBarTrailing) {
131134
if #available(iOS 16.0, tvOS 17.0, *) {
132135
cardManagementButton
@@ -137,10 +140,7 @@ public struct ActiveDashboardView: View {
137140
if #available(iOS 26.0, *), !Variant.debugNoIOS26 {
138141
EmptyView()
139142
} else {
140-
HStack(spacing: 12) {
141-
Divider()
142-
StartStopButton()
143-
}
143+
StartStopButton()
144144
}
145145
#else
146146
HStack(spacing: 12) {
@@ -151,16 +151,18 @@ public struct ActiveDashboardView: View {
151151
}
152152
}
153153

154-
private var navigationButtons: some View {
155-
NavigationButtonsView(
156-
showGroupsButton: buttonState.showGroupsButton,
157-
showConnectionsButton: buttonState.showConnectionsButton,
158-
groupsCount: buttonState.groupsCount,
159-
connectionsCount: buttonState.connectionsCount,
160-
onGroupsTap: { showGroups = true },
161-
onConnectionsTap: { showConnections = true }
162-
)
163-
}
154+
#if os(tvOS)
155+
private var navigationButtons: some View {
156+
NavigationButtonsView(
157+
showGroupsButton: buttonState.showGroupsButton,
158+
showConnectionsButton: buttonState.showConnectionsButton,
159+
groupsCount: buttonState.groupsCount,
160+
connectionsCount: buttonState.connectionsCount,
161+
onGroupsTap: { showGroups = true },
162+
onConnectionsTap: { showConnections = true }
163+
)
164+
}
165+
#endif
164166
#endif
165167

166168
#if os(iOS) || os(tvOS)
@@ -176,19 +178,42 @@ public struct ActiveDashboardView: View {
176178
@ViewBuilder
177179
private var cardManagementButton: some View {
178180
Menu {
181+
#if os(iOS)
182+
if !isTabViewBottomAccessoryAvailable {
183+
if buttonState.showGroupsButton {
184+
Button {
185+
showGroups = true
186+
} label: {
187+
Label("Groups (\(buttonState.groupsCount))", systemImage: "rectangle.3.group.fill")
188+
}
189+
}
190+
if buttonState.showConnectionsButton {
191+
Button {
192+
showConnections = true
193+
} label: {
194+
Label("Connections (\(buttonState.connectionsCount))", systemImage: "list.bullet.rectangle.portrait.fill")
195+
}
196+
}
197+
if buttonState.showGroupsButton || buttonState.showConnectionsButton {
198+
Divider()
199+
}
200+
}
201+
#endif
179202
Button {
180203
showCardManagement = true
181204
} label: {
182205
Label("Dashboard Items", systemImage: "square.grid.2x2")
183206
}
184207
} label: {
185-
Label("Others", systemImage: "ellipsis.circle")
208+
Label("Others", systemImage: "line.3.horizontal.circle")
186209
}
187-
.sheet(isPresented: $showCardManagement) {
188-
CardManagementSheet(configurationVersion: $cardConfigurationVersion)
210+
.sheet(isPresented: $showCardManagement, onDismiss: {
211+
cardConfigurationVersion += 1
212+
}, content: {
213+
CardManagementSheet()
189214
.presentationDetents([.large])
190215
.presentationDragIndicator(.visible)
191-
}
216+
})
192217
}
193218
#endif
194219
}

ApplicationLibrary/Views/Dashboard/Cards/CardManagementSheet.swift

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ import SwiftUI
44
@MainActor public struct CardManagementSheet: View {
55
@Environment(\.dismiss) private var dismiss
66
@StateObject private var configuration = DashboardCardConfiguration()
7-
@Binding private var configurationVersion: Int
87

9-
public init(configurationVersion: Binding<Int>) {
10-
_configurationVersion = configurationVersion
11-
}
8+
public init() {}
129

1310
public var body: some View {
1411
#if os(macOS)
@@ -41,19 +38,16 @@ import SwiftUI
4138
Button("Reset", role: .destructive) {
4239
Task {
4340
await configuration.resetToDefault()
44-
configurationVersion += 1
4541
}
4642
}
4743
}
4844
ToolbarItem(placement: .confirmationAction) {
4945
Button("Done") {
5046
dismiss()
5147
}
48+
.keyboardShortcut(.escape, modifiers: [])
5249
}
5350
}
54-
.onExitCommand {
55-
dismiss()
56-
}
5751
}
5852
#else
5953
private var iOSBody: some View {
@@ -74,7 +68,6 @@ import SwiftUI
7468
Button("Reset", role: .destructive) {
7569
Task {
7670
await configuration.resetToDefault()
77-
configurationVersion += 1
7871
}
7972
}
8073
}
@@ -91,13 +84,13 @@ import SwiftUI
9184
isEnabled: configuration.isEnabled(card),
9285
onToggle: {
9386
configuration.toggleCard(card)
94-
configurationVersion += 1
9587
}
9688
)
9789
}
9890
.onMove { source, destination in
99-
configuration.moveCard(from: source, to: destination)
100-
configurationVersion += 1
91+
Task {
92+
await configuration.moveCard(from: source, to: destination)
93+
}
10194
}
10295
}
10396
.applyContentMargins()

ApplicationLibrary/Views/Dashboard/Cards/DashboardCardConfiguration.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,9 @@ public final class DashboardCardConfiguration: ObservableObject {
4040
}
4141
}
4242

43-
public func moveCard(from source: IndexSet, to destination: Int) {
43+
public func moveCard(from source: IndexSet, to destination: Int) async {
4444
cardOrder.move(fromOffsets: source, toOffset: destination)
45-
46-
// Save asynchronously in background
47-
Task {
48-
await saveCardOrder()
49-
}
45+
await saveCardOrder()
5046
}
5147

5248
public func resetToDefault() async {

ApplicationLibrary/Views/Dashboard/Cards/HTTPProxyCard.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ public struct HTTPProxyCard: View {
1919

2020
public var body: some View {
2121
DashboardCardView(title: "", isHalfWidth: false) {
22-
Toggle("System HTTP Proxy", isOn: $systemProxyEnabled)
23-
.onChangeCompat(of: systemProxyEnabled) { newValue in
24-
Task {
25-
await onToggle(newValue)
22+
HStack {
23+
Text("System HTTP Proxy")
24+
Spacer()
25+
Toggle("", isOn: $systemProxyEnabled)
26+
.labelsHidden()
27+
.onChangeCompat(of: systemProxyEnabled) { newValue in
28+
Task {
29+
await onToggle(newValue)
30+
}
2631
}
27-
}
32+
}
2833
}
2934
}
3035
}

0 commit comments

Comments
 (0)