Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion NodePass/Server/ServerCardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ struct ServerCardView: View {
VStack(alignment: .leading, spacing: 10) {
VStack(alignment: .leading) {
HStack(spacing: 10) {
Circle()
.fill(server.isEnabled ? Color.green : Color.gray)
.frame(width: 8, height: 8)
Text(server.name)
if let uptime = metadata?.uptime {
HStack(spacing: 5) {
Expand Down Expand Up @@ -115,7 +118,13 @@ struct ServerCardView: View {
.foregroundStyle(.secondary)
}
else {
if let metadataResult {
if !server.isEnabled {
Text("Disabled")
.bold()
.foregroundStyle(.secondary)
.frame(maxWidth: .infinity)
}
else if let metadataResult {
switch metadataResult {
case .success:
Text("Metadata Unavailable")
Expand Down
6 changes: 6 additions & 0 deletions NodePass/Server/ServerListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ struct ServerListView: View {
}
.contextMenu {
ControlGroup {
Button {
server.isEnabled.toggle()
try? context.save()
} label: {
Label(server.isEnabled ? "Disable" : "Enable", systemImage: server.isEnabled ? "pause.circle" : "play.circle")
}
Button {
state.editServerSheetMode = .editing
state.editServerSheetServer = server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ struct AddDirectForwardServiceView: View {
@Environment(\.modelContext) private var context
@Query(sort: \Server.timestamp) private var servers: [Server]

private var enabledServers: [Server] {
servers.filter { $0.isEnabled }
}

private var isAdvancedModeEnabled: Bool = NPCore.isAdvancedModeEnabled

@State private var name: String = ""
Expand Down Expand Up @@ -42,7 +46,7 @@ struct AddDirectForwardServiceView: View {
Picker("Server", selection: $client) {
Text("Select")
.tag(nil as Server?)
ForEach(servers) { server in
ForEach(enabledServers) { server in
Text(server.name)
.tag(server)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ struct AddNATPassthroughServiceView: View {
@Environment(\.modelContext) private var context
@Query(sort: \Server.timestamp) private var servers: [Server]

private var enabledServers: [Server] {
servers.filter { $0.isEnabled }
}

private var isAdvancedModeEnabled: Bool = NPCore.isAdvancedModeEnabled

@State private var name: String = ""
Expand Down Expand Up @@ -54,7 +58,7 @@ struct AddNATPassthroughServiceView: View {
Picker("Server", selection: $remoteServer) {
Text("Select")
.tag(nil as Server?)
ForEach(servers) { server in
ForEach(enabledServers) { server in
Text(server.name)
.tag(server)
}
Expand Down Expand Up @@ -94,7 +98,7 @@ struct AddNATPassthroughServiceView: View {
Picker("Server", selection: $localServer) {
Text("Select")
.tag(nil as Server?)
ForEach(servers) { server in
ForEach(enabledServers) { server in
Text(server.name)
.tag(server)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ struct AddTunnelForwardServiceView: View {
@Environment(\.modelContext) private var context
@Query(sort: \Server.timestamp) private var servers: [Server]

private var enabledServers: [Server] {
servers.filter { $0.isEnabled }
}

@State private var isAdvancedModeEnabled: Bool = NPCore.isAdvancedModeEnabled

@State private var name: String = ""
Expand Down Expand Up @@ -74,7 +78,7 @@ struct AddTunnelForwardServiceView: View {
Picker("Server", selection: $relayServer) {
Text("Select")
.tag(nil as Server?)
ForEach(servers) { server in
ForEach(enabledServers) { server in
Text(server.name)
.tag(server)
}
Expand Down Expand Up @@ -112,7 +116,7 @@ struct AddTunnelForwardServiceView: View {
Picker("Server", selection: $destinationServer) {
Text("Select")
.tag(nil as Server?)
ForEach(servers) { server in
ForEach(enabledServers) { server in
Text(server.name)
.tag(server)
}
Expand Down
52 changes: 41 additions & 11 deletions NodePass/Service/Detail/DirectForwardDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ struct DirectForwardDetailView: View {
@Query private var servers: [Server]

@State private var instance: Instance?
@State private var instanceLoadingState: LoadingState = .loading
@State private var isShowEditInstanceSheet: Bool = false

@State private var isShowErrorAlert: Bool = false
@State private var errorMessage: String = ""

enum LoadingState {
case loading
case loaded
case notFound
}

var body: some View {
if service.type == .directForward {
Form {
Expand All @@ -42,17 +49,26 @@ struct DirectForwardDetailView: View {
}
.frame(maxWidth: .infinity)

if let instance {
VStack {
InstanceCardView(instance: instance)
.onTapGesture {
isShowEditInstanceSheet = true
}
}
.frame(maxWidth: .infinity)
}
else {
switch instanceLoadingState {
case .loading:
ProgressView()
.listRowSeparator(.hidden)
.frame(maxWidth: .infinity)
case .loaded:
if let instance {
VStack {
InstanceCardView(instance: instance)
.onTapGesture {
isShowEditInstanceSheet = true
}
}
.listRowSeparator(.hidden)
.frame(maxWidth: .infinity)
}
case .notFound:
Label("Instance Not Found", systemImage: "exclamationmark.triangle.fill")
.foregroundStyle(.red)
.listRowSeparator(.hidden)
.frame(maxWidth: .infinity)
}
}
Expand Down Expand Up @@ -89,16 +105,30 @@ struct DirectForwardDetailView: View {

private func fetchInstance() {
guard let server else { return }
instanceLoadingState = .loading
Task {
let instanceService = InstanceService()
do {
let instances = try await instanceService.listInstances(baseURLString: server.url, apiKey: server.key)
self.instance = instances.first(where: { $0.id == implementation.instanceID })
if let foundInstance = instances.first(where: { $0.id == implementation.instanceID }) {
await MainActor.run {
self.instance = foundInstance
self.instanceLoadingState = .loaded
}
} else {
await MainActor.run {
self.instance = nil
self.instanceLoadingState = .notFound
}
}
}
catch {
#if DEBUG
print("Error Fetching Instance: \(error.localizedDescription)")
#endif
await MainActor.run {
self.instanceLoadingState = .notFound
}
}
}
}
Expand Down
98 changes: 76 additions & 22 deletions NodePass/Service/Detail/NATPassthroughDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@ struct NATPassthroughDetailView: View {

@State private var instance0: Instance?
@State private var instance1: Instance?
@State private var instance0LoadingState: LoadingState = .loading
@State private var instance1LoadingState: LoadingState = .loading
@State private var isShowEditInstance0Sheet: Bool = false
@State private var isShowEditInstance1Sheet: Bool = false

@State private var isShowErrorAlert: Bool = false
@State private var errorMessage: String = ""

enum LoadingState {
case loading
case loaded
case notFound
}

var body: some View {
if service.type == .natPassthrough {
Form {
Expand All @@ -52,17 +60,26 @@ struct NATPassthroughDetailView: View {
}
.frame(maxWidth: .infinity)

if let instance0 {
VStack {
InstanceCardView(instance: instance0)
.onTapGesture {
isShowEditInstance0Sheet = true
}
}
.frame(maxWidth: .infinity)
}
else {
switch instance0LoadingState {
case .loading:
ProgressView()
.listRowSeparator(.hidden)
.frame(maxWidth: .infinity)
case .loaded:
if let instance0 {
VStack {
InstanceCardView(instance: instance0)
.onTapGesture {
isShowEditInstance0Sheet = true
}
}
.listRowSeparator(.hidden)
.frame(maxWidth: .infinity)
}
case .notFound:
Label("Instance Not Found", systemImage: "exclamationmark.triangle.fill")
.foregroundStyle(.red)
.listRowSeparator(.hidden)
.frame(maxWidth: .infinity)
}
}
Expand All @@ -86,17 +103,26 @@ struct NATPassthroughDetailView: View {
}
.frame(maxWidth: .infinity)

if let instance1 {
VStack {
InstanceCardView(instance: instance1)
.onTapGesture {
isShowEditInstance1Sheet = true
}
}
.frame(maxWidth: .infinity)
}
else {
switch instance1LoadingState {
case .loading:
ProgressView()
.listRowSeparator(.hidden)
.frame(maxWidth: .infinity)
case .loaded:
if let instance1 {
VStack {
InstanceCardView(instance: instance1)
.onTapGesture {
isShowEditInstance1Sheet = true
}
}
.listRowSeparator(.hidden)
.frame(maxWidth: .infinity)
}
case .notFound:
Label("Instance Not Found", systemImage: "exclamationmark.triangle.fill")
.foregroundStyle(.red)
.listRowSeparator(.hidden)
.frame(maxWidth: .infinity)
}
}
Expand Down Expand Up @@ -140,30 +166,58 @@ struct NATPassthroughDetailView: View {

private func fetchInstances() {
if let server0 {
instance0LoadingState = .loading
Task {
let instanceService = InstanceService()
do {
let instances = try await instanceService.listInstances(baseURLString: server0.url, apiKey: server0.key)
self.instance0 = instances.first(where: { $0.id == implementation0.instanceID })
if let foundInstance = instances.first(where: { $0.id == implementation0.instanceID }) {
await MainActor.run {
self.instance0 = foundInstance
self.instance0LoadingState = .loaded
}
} else {
await MainActor.run {
self.instance0 = nil
self.instance0LoadingState = .notFound
}
}
}
catch {
#if DEBUG
print("Error Fetching Instance 0: \(error.localizedDescription)")
#endif
await MainActor.run {
self.instance0LoadingState = .notFound
}
}
}
}
if let server1 {
instance1LoadingState = .loading
Task {
let instanceService = InstanceService()
do {
let instances = try await instanceService.listInstances(baseURLString: server1.url, apiKey: server1.key)
self.instance1 = instances.first(where: { $0.id == implementation1.instanceID })
if let foundInstance = instances.first(where: { $0.id == implementation1.instanceID }) {
await MainActor.run {
self.instance1 = foundInstance
self.instance1LoadingState = .loaded
}
} else {
await MainActor.run {
self.instance1 = nil
self.instance1LoadingState = .notFound
}
}
}
catch {
#if DEBUG
print("Error Fetching Instance 1: \(error.localizedDescription)")
#endif
await MainActor.run {
self.instance1LoadingState = .notFound
}
}
}
}
Expand Down
Loading