Add support for tvOS
This commit is contained in:
@@ -2,7 +2,7 @@ import BackgroundTasks
|
||||
import Foundation
|
||||
import Library
|
||||
|
||||
#if os(iOS)
|
||||
#if os(iOS) || os(tvOS)
|
||||
public class UIProfileUpdateTask: BGAppRefreshTask {
|
||||
public static let taskSchedulerPermittedIdentifier = "\(FilePath.packageName).update_profiles"
|
||||
|
||||
|
||||
@@ -17,19 +17,21 @@ public func FormTextItem(_ name: String, _ value: String) -> some View {
|
||||
Text(value)
|
||||
.multilineTextAlignment(.trailing)
|
||||
.font(Font.system(.caption, design: .monospaced))
|
||||
#if os(iOS) || os(macOS)
|
||||
.textSelection(.enabled)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
public func FormItem(_ title: String, @ViewBuilder content: () -> some View) -> some View {
|
||||
#if os(iOS)
|
||||
#if os(iOS) || os(tvOS)
|
||||
HStack {
|
||||
Text(title)
|
||||
Spacer()
|
||||
Spacer()
|
||||
content()
|
||||
}
|
||||
#else
|
||||
#elseif os(macOS)
|
||||
content()
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -33,11 +33,13 @@ public struct ActiveDashboardView: View {
|
||||
Text("Empty profiles")
|
||||
} else {
|
||||
VStack {
|
||||
#if os(iOS)
|
||||
#if os(iOS) || os(tvOS)
|
||||
if ApplicationLibrary.inPreview || profile.status.isConnected {
|
||||
ExtensionStatusView()
|
||||
.listStyle(.automatic)
|
||||
#if os(iOS)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
#endif
|
||||
}
|
||||
FormView {
|
||||
StartStopButton()
|
||||
@@ -50,7 +52,7 @@ public struct ActiveDashboardView: View {
|
||||
.pickerStyle(.inline)
|
||||
}
|
||||
}
|
||||
#else
|
||||
#elseif os(macOS)
|
||||
if ApplicationLibrary.inPreview || profile.status.isConnected {
|
||||
ExtensionStatusView()
|
||||
}
|
||||
@@ -77,36 +79,48 @@ public struct ActiveDashboardView: View {
|
||||
}
|
||||
}
|
||||
.alertBinding($alert)
|
||||
#if os(iOS)
|
||||
.onChange(of: scenePhase, perform: { newValue in
|
||||
if newValue == .active {
|
||||
Task.detached {
|
||||
await doReload()
|
||||
}
|
||||
}
|
||||
})
|
||||
.onChange(of: selection.wrappedValue, perform: { newValue in
|
||||
if newValue == .dashboard {
|
||||
Task.detached {
|
||||
await doReload()
|
||||
}
|
||||
}
|
||||
})
|
||||
#elseif os(macOS)
|
||||
.onAppear {
|
||||
if observer == nil {
|
||||
observer = NotificationCenter.default.addObserver(forName: ActiveDashboardView.NotificationUpdateSelectedProfile, object: nil, queue: nil, using: { _ in
|
||||
Task.detached {
|
||||
await doReload()
|
||||
.onChange(of: profile.status, perform: { newValue in
|
||||
if newValue == .disconnecting || newValue == .connected {
|
||||
Task.detached {
|
||||
if let serviceError = try? String(contentsOf: ExtensionProvider.errorFile) {
|
||||
DispatchQueue.main.async {
|
||||
alert = Alert(errorMessage: serviceError)
|
||||
}
|
||||
})
|
||||
try? FileManager.default.removeItem(at: ExtensionProvider.errorFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
.onDisappear {
|
||||
if let observer {
|
||||
NotificationCenter.default.removeObserver(observer)
|
||||
})
|
||||
#if os(iOS) || os(tvOS)
|
||||
.onChange(of: scenePhase, perform: { newValue in
|
||||
if newValue == .active {
|
||||
Task.detached {
|
||||
await doReload()
|
||||
}
|
||||
}
|
||||
})
|
||||
.onChange(of: selection.wrappedValue, perform: { newValue in
|
||||
if newValue == .dashboard {
|
||||
Task.detached {
|
||||
await doReload()
|
||||
}
|
||||
}
|
||||
})
|
||||
#elseif os(macOS)
|
||||
.onAppear {
|
||||
if observer == nil {
|
||||
observer = NotificationCenter.default.addObserver(forName: ActiveDashboardView.NotificationUpdateSelectedProfile, object: nil, queue: nil, using: { _ in
|
||||
Task.detached {
|
||||
await doReload()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
.onDisappear {
|
||||
if let observer {
|
||||
NotificationCenter.default.removeObserver(observer)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -147,7 +161,7 @@ public struct ActiveDashboardView: View {
|
||||
NotificationCenter.default.post(name: ActiveDashboardView.NotificationUpdateSelectedProfile, object: nil)
|
||||
if profile.status.isConnected {
|
||||
do {
|
||||
try LibboxNewStandaloneCommandClient(FilePath.sharedDirectory.relativePath)?.serviceReload()
|
||||
try LibboxNewStandaloneCommandClient()!.serviceReload()
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ public struct ExtensionStatusView: View {
|
||||
|
||||
private func closeConnections() {
|
||||
do {
|
||||
try LibboxNewStandaloneCommandClient(FilePath.sharedDirectory.relativePath)?.closeConnections()
|
||||
try LibboxNewStandaloneCommandClient()!.closeConnections()
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
}
|
||||
@@ -162,9 +162,13 @@ public struct ExtensionStatusView: View {
|
||||
.font(.system(size: 16))
|
||||
}
|
||||
.frame(minWidth: 125)
|
||||
.padding(EdgeInsets(top: 10, leading: 13, bottom: 10, trailing: 13))
|
||||
.background(backgroundColor)
|
||||
.cornerRadius(10)
|
||||
#if os(tvOS)
|
||||
.padding(EdgeInsets(top: 20, leading: 26, bottom: 20, trailing: 26))
|
||||
#else
|
||||
.padding(EdgeInsets(top: 10, leading: 13, bottom: 10, trailing: 13))
|
||||
#endif
|
||||
.background(backgroundColor)
|
||||
.cornerRadius(10)
|
||||
}
|
||||
|
||||
private var backgroundColor: Color {
|
||||
@@ -172,6 +176,8 @@ public struct ExtensionStatusView: View {
|
||||
return Color(uiColor: .secondarySystemGroupedBackground)
|
||||
#elseif os(macOS)
|
||||
return Color(nsColor: .textBackgroundColor)
|
||||
#elseif os(tvOS)
|
||||
return Color(uiColor: .black)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ public struct StartStopButton: View {
|
||||
public var body: some View {
|
||||
viewBuilder {
|
||||
if ApplicationLibrary.inPreview {
|
||||
#if os(iOS)
|
||||
#if os(iOS) || os(tvOS)
|
||||
Toggle(isOn: .constant(true)) {
|
||||
Text("Enabled")
|
||||
}
|
||||
@@ -23,7 +23,7 @@ public struct StartStopButton: View {
|
||||
} else if let profile = extensionProfile.wrappedValue {
|
||||
Button0(profile)
|
||||
} else {
|
||||
#if os(iOS)
|
||||
#if os(iOS) || os(tvOS)
|
||||
Toggle(isOn: .constant(false)) {
|
||||
Text("Enabled")
|
||||
}
|
||||
@@ -49,7 +49,7 @@ public struct StartStopButton: View {
|
||||
|
||||
var body: some View {
|
||||
viewBuilder {
|
||||
#if os(iOS)
|
||||
#if os(iOS) || os(tvOS)
|
||||
Toggle(isOn: Binding(get: {
|
||||
profile.status.isConnected
|
||||
}, set: { newValue, _ in
|
||||
|
||||
@@ -14,8 +14,7 @@ public struct GroupItemView: View {
|
||||
self.item = item
|
||||
}
|
||||
|
||||
@State private var errorPresented = false
|
||||
@State private var errorMessage = ""
|
||||
@State private var alert: Alert?
|
||||
|
||||
public var body: some View {
|
||||
HStack {
|
||||
@@ -60,24 +59,17 @@ public struct GroupItemView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
.alert(isPresented: $errorPresented) {
|
||||
Alert(
|
||||
title: Text("Error"),
|
||||
message: Text(errorMessage),
|
||||
dismissButton: .default(Text("Ok"))
|
||||
)
|
||||
}
|
||||
.alertBinding($alert)
|
||||
}
|
||||
|
||||
private func selectOutbound() {
|
||||
do {
|
||||
try LibboxNewStandaloneCommandClient(FilePath.sharedDirectory.relativePath)!.selectOutbound(group.tag, outboundTag: item.tag)
|
||||
try LibboxNewStandaloneCommandClient()!.selectOutbound(group.tag, outboundTag: item.tag)
|
||||
var newGroup = group
|
||||
newGroup.selected = item.tag
|
||||
_group.wrappedValue = newGroup
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
errorPresented = true
|
||||
alert = Alert(error)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -87,6 +79,8 @@ public struct GroupItemView: View {
|
||||
return Color(uiColor: .secondarySystemGroupedBackground)
|
||||
#elseif os(macOS)
|
||||
return Color(nsColor: .textBackgroundColor)
|
||||
#elseif os(tvOS)
|
||||
return Color.black
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ public struct GroupView: View {
|
||||
}
|
||||
|
||||
private func doURLTest() {
|
||||
try? LibboxNewStandaloneCommandClient(FilePath.sharedDirectory.relativePath)!.urlTest(group.tag)
|
||||
try? LibboxNewStandaloneCommandClient()!.urlTest(group.tag)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,9 @@ public struct LogView: View {
|
||||
ForEach(Array(logClient.logList.enumerated()), id: \.offset) { it in
|
||||
Text(it.element)
|
||||
.font(logFont)
|
||||
#if os(tvOS)
|
||||
.focusable()
|
||||
#endif
|
||||
Spacer(minLength: 5)
|
||||
}
|
||||
|
||||
@@ -52,6 +55,10 @@ public struct LogView: View {
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
||||
.padding()
|
||||
}
|
||||
#if os(tvOS)
|
||||
.focusEffectDisabled()
|
||||
.focusSection()
|
||||
#endif
|
||||
.onAppear {
|
||||
reader.scrollTo(logClient.logList.count - 1)
|
||||
}
|
||||
|
||||
@@ -75,6 +75,10 @@ public extension NavigationPage {
|
||||
}
|
||||
switch self {
|
||||
case .groups:
|
||||
#if os(tvOS)
|
||||
// TODO: fix groups ui
|
||||
return false
|
||||
#endif
|
||||
return profile?.status.isConnectedStrict == true
|
||||
default:
|
||||
return true
|
||||
|
||||
@@ -1,133 +1,136 @@
|
||||
import Foundation
|
||||
import Library
|
||||
import SwiftUI
|
||||
#if os(iOS) || os(macOS)
|
||||
import Foundation
|
||||
import Library
|
||||
import SwiftUI
|
||||
|
||||
public struct EditProfileContentView: View {
|
||||
#if os(macOS)
|
||||
public static let windowID = "edit-profile-content"
|
||||
#endif
|
||||
|
||||
public struct Context: Codable, Hashable {
|
||||
public let profileID: Int64
|
||||
public let readOnly: Bool
|
||||
}
|
||||
|
||||
private let profileID: Int64?
|
||||
private let readOnly: Bool
|
||||
|
||||
public init(_ context: Context?) {
|
||||
profileID = context?.profileID
|
||||
readOnly = context?.readOnly == true
|
||||
}
|
||||
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
@State private var isLoading = true
|
||||
@State private var profile: Profile!
|
||||
@State private var profileContent: String = ""
|
||||
@State private var isChanged = false
|
||||
@State private var alert: Alert?
|
||||
|
||||
public var body: some View {
|
||||
viewBuilder {
|
||||
if isLoading {
|
||||
ProgressView().onAppear {
|
||||
Task.detached {
|
||||
loadContent()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
viewBuilder {
|
||||
if readOnly {
|
||||
TextEditor(text: .constant(profileContent))
|
||||
} else {
|
||||
TextEditor(text: $profileContent)
|
||||
}
|
||||
}
|
||||
.font(Font.system(.caption2, design: .monospaced))
|
||||
.autocorrectionDisabled()
|
||||
#if os(iOS)
|
||||
.textInputAutocapitalization(.none)
|
||||
.background(Color(UIColor.secondarySystemGroupedBackground))
|
||||
#elseif os(macOS)
|
||||
.padding()
|
||||
#endif
|
||||
.onChange(of: profileContent) { _ in
|
||||
isChanged = true
|
||||
}
|
||||
}
|
||||
}
|
||||
.alertBinding($alert)
|
||||
.navigationTitle(navigationTitle)
|
||||
public struct EditProfileContentView: View {
|
||||
#if os(macOS)
|
||||
.toolbar {
|
||||
ToolbarItemGroup(placement: .navigation) {
|
||||
if !readOnly {
|
||||
Button(action: {
|
||||
Task.detached {
|
||||
saveContent()
|
||||
}
|
||||
}, label: {
|
||||
Image("save", label: Text("Save"))
|
||||
})
|
||||
.disabled(!isChanged)
|
||||
}
|
||||
}
|
||||
}
|
||||
#elseif os(iOS)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .navigationBarTrailing) {
|
||||
if !readOnly {
|
||||
Button("Save") {
|
||||
Task.detached {
|
||||
saveContent()
|
||||
}
|
||||
}.disabled(!isChanged)
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
public static let windowID = "edit-profile-content"
|
||||
#endif
|
||||
}
|
||||
|
||||
private var navigationTitle: String {
|
||||
if readOnly {
|
||||
return "View Content"
|
||||
} else {
|
||||
return "Edit Content"
|
||||
public struct Context: Codable, Hashable {
|
||||
public let profileID: Int64
|
||||
public let readOnly: Bool
|
||||
}
|
||||
|
||||
private let profileID: Int64?
|
||||
private let readOnly: Bool
|
||||
|
||||
public init(_ context: Context?) {
|
||||
profileID = context?.profileID
|
||||
readOnly = context?.readOnly == true
|
||||
}
|
||||
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
@State private var isLoading = true
|
||||
@State private var profile: Profile!
|
||||
@State private var profileContent: String = ""
|
||||
@State private var isChanged = false
|
||||
@State private var alert: Alert?
|
||||
|
||||
public var body: some View {
|
||||
viewBuilder {
|
||||
if isLoading {
|
||||
ProgressView().onAppear {
|
||||
Task.detached {
|
||||
loadContent()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
viewBuilder {
|
||||
if readOnly {
|
||||
TextEditor(text: .constant(profileContent))
|
||||
} else {
|
||||
TextEditor(text: $profileContent)
|
||||
}
|
||||
}
|
||||
.font(Font.system(.caption2, design: .monospaced))
|
||||
.autocorrectionDisabled()
|
||||
#if os(iOS)
|
||||
.textInputAutocapitalization(.none)
|
||||
.background(Color(UIColor.secondarySystemGroupedBackground))
|
||||
#elseif os(macOS)
|
||||
.padding()
|
||||
#endif
|
||||
.onChange(of: profileContent) { _ in
|
||||
isChanged = true
|
||||
}
|
||||
}
|
||||
}
|
||||
.alertBinding($alert)
|
||||
.navigationTitle(navigationTitle)
|
||||
#if os(macOS)
|
||||
.toolbar {
|
||||
ToolbarItemGroup(placement: .navigation) {
|
||||
if !readOnly {
|
||||
Button(action: {
|
||||
Task.detached {
|
||||
saveContent()
|
||||
}
|
||||
}, label: {
|
||||
Image("save", label: Text("Save"))
|
||||
})
|
||||
.disabled(!isChanged)
|
||||
}
|
||||
}
|
||||
}
|
||||
#elseif os(iOS)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .navigationBarTrailing) {
|
||||
if !readOnly {
|
||||
Button("Save") {
|
||||
Task.detached {
|
||||
saveContent()
|
||||
}
|
||||
}.disabled(!isChanged)
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
#endif
|
||||
}
|
||||
|
||||
private var navigationTitle: String {
|
||||
if readOnly {
|
||||
return "View Content"
|
||||
} else {
|
||||
return "Edit Content"
|
||||
}
|
||||
}
|
||||
|
||||
private func loadContent() {
|
||||
do {
|
||||
try loadContent0()
|
||||
} catch {
|
||||
alert = Alert(error, dismiss.callAsFunction)
|
||||
}
|
||||
}
|
||||
|
||||
private func loadContent0() throws {
|
||||
guard let profileID else {
|
||||
throw NSError(domain: "Context destroyed", code: 0)
|
||||
}
|
||||
guard let profile = try ProfileManager.get(profileID) else {
|
||||
throw NSError(domain: "Profile missing", code: 0)
|
||||
}
|
||||
profileContent = try profile.read()
|
||||
self.profile = profile
|
||||
isLoading = false
|
||||
}
|
||||
|
||||
private func saveContent() {
|
||||
guard let profile else {
|
||||
return
|
||||
}
|
||||
do {
|
||||
try profile.write(profileContent)
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
}
|
||||
isChanged = false
|
||||
}
|
||||
}
|
||||
|
||||
private func loadContent() {
|
||||
do {
|
||||
try loadContent0()
|
||||
} catch {
|
||||
alert = Alert(error, dismiss.callAsFunction)
|
||||
}
|
||||
}
|
||||
|
||||
private func loadContent0() throws {
|
||||
guard let profileID else {
|
||||
throw NSError(domain: "Context destroyed", code: 0)
|
||||
}
|
||||
guard let profile = try ProfileManager.get(profileID) else {
|
||||
throw NSError(domain: "Profile missing", code: 0)
|
||||
}
|
||||
profileContent = try profile.read()
|
||||
self.profile = profile
|
||||
isLoading = false
|
||||
}
|
||||
|
||||
private func saveContent() {
|
||||
guard let profile else {
|
||||
return
|
||||
}
|
||||
do {
|
||||
try profile.write(profileContent)
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
}
|
||||
isChanged = false
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -46,20 +46,24 @@ public struct EditProfileView: View {
|
||||
FormTextItem("Last Updated", profile.lastUpdatedString)
|
||||
}
|
||||
}
|
||||
#if os(iOS)
|
||||
#if os(iOS) || os(tvOS)
|
||||
Section("Action") {
|
||||
if profile.type != .remote {
|
||||
NavigationLink {
|
||||
EditProfileContentView(EditProfileContentView.Context(profileID: profile.id!, readOnly: false))
|
||||
} label: {
|
||||
Text("Edit Content").foregroundColor(.accentColor)
|
||||
}
|
||||
#if os(iOS)
|
||||
NavigationLink {
|
||||
EditProfileContentView(EditProfileContentView.Context(profileID: profile.id!, readOnly: false))
|
||||
} label: {
|
||||
Text("Edit Content").foregroundColor(.accentColor)
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
NavigationLink {
|
||||
EditProfileContentView(EditProfileContentView.Context(profileID: profile.id!, readOnly: true))
|
||||
} label: {
|
||||
Text("View Content").foregroundColor(.accentColor)
|
||||
}
|
||||
#if os(iOS)
|
||||
NavigationLink {
|
||||
EditProfileContentView(EditProfileContentView.Context(profileID: profile.id!, readOnly: true))
|
||||
} label: {
|
||||
Text("View Content").foregroundColor(.accentColor)
|
||||
}
|
||||
#endif
|
||||
Button("Update") {
|
||||
isLoading = true
|
||||
Task.detached {
|
||||
@@ -67,17 +71,19 @@ public struct EditProfileView: View {
|
||||
}
|
||||
}
|
||||
.disabled(isLoading)
|
||||
}
|
||||
if #available(iOS 16.0, *) {
|
||||
ShareLink(item: profile.shareLink) {
|
||||
Text("Share")
|
||||
}
|
||||
} else {
|
||||
Button("Share") {
|
||||
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
|
||||
windowScene.keyWindow?.rootViewController?.present(UIActivityViewController(activityItems: [profile.shareLink], applicationActivities: nil), animated: true, completion: nil)
|
||||
#if os(iOS)
|
||||
if #available(iOS 16.0, *) {
|
||||
ShareLink(item: profile.shareLink) {
|
||||
Text("Share")
|
||||
}
|
||||
} else {
|
||||
Button("Share") {
|
||||
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
|
||||
windowScene.keyWindow?.rootViewController?.present(UIActivityViewController(activityItems: [profile.shareLink], applicationActivities: nil), animated: true, completion: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
#if os(tvOS)
|
||||
|
||||
import DeviceDiscoveryUI
|
||||
import Libbox
|
||||
import Library
|
||||
import SwiftUI
|
||||
|
||||
public struct ImportProfileView: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
@State private var isLoading = false
|
||||
@State private var selected = false
|
||||
@State private var alert: Alert?
|
||||
@State private var connection: NWSocket?
|
||||
@State private var profiles: [LibboxProfilePreview]?
|
||||
private let callback: () -> Void
|
||||
|
||||
public init(callback: @escaping () -> Void) {
|
||||
self.callback = callback
|
||||
}
|
||||
|
||||
public var body: some View {
|
||||
VStack {
|
||||
if !selected {
|
||||
DevicePicker(
|
||||
.applicationService(name: "sing-box:profile"))
|
||||
{ endpoint in
|
||||
selected = true
|
||||
Task.detached {
|
||||
await handleEndpoint(endpoint)
|
||||
}
|
||||
} label: {
|
||||
Text("Select Device")
|
||||
} fallback: {
|
||||
EmptyView()
|
||||
} parameters: {
|
||||
.applicationService
|
||||
}
|
||||
} else if let profiles {
|
||||
Form {
|
||||
Text("\(profiles.count) Profiles")
|
||||
ForEach(profiles, id: \.profileID) { profile in
|
||||
Button(profile.name) {
|
||||
isLoading = true
|
||||
Task.detached {
|
||||
selectProfile(profileID: profile.profileID)
|
||||
isLoading = false
|
||||
}
|
||||
}.disabled(isLoading)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Text("Connecting...")
|
||||
}
|
||||
}
|
||||
.focusSection()
|
||||
.alertBinding($alert)
|
||||
.navigationTitle("Import Profile")
|
||||
}
|
||||
|
||||
private func reset() {
|
||||
selected = false
|
||||
profiles = nil
|
||||
}
|
||||
|
||||
private func handleEndpoint(_ endpoint: NWEndpoint) async {
|
||||
let connection = NWConnection(to: endpoint, using: NWParameters.applicationService)
|
||||
self.connection = NWSocket(connection)
|
||||
connection.start(queue: .global())
|
||||
do {
|
||||
try loopMessages()
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
reset()
|
||||
}
|
||||
}
|
||||
|
||||
private func loopMessages() throws {
|
||||
guard let connection else {
|
||||
return
|
||||
}
|
||||
while true {
|
||||
let message = try connection.read()
|
||||
var error: NSError?
|
||||
switch Int64(message[0]) {
|
||||
case LibboxMessageTypeError:
|
||||
let message = LibboxDecodeErrorMessage(message, &error)
|
||||
if let error {
|
||||
throw error
|
||||
}
|
||||
if let message {
|
||||
throw NSError(domain: "remote error: \(message.message)", code: 0)
|
||||
}
|
||||
case LibboxMessageTypeProfileList:
|
||||
let decoder = LibboxProfileDecoder()
|
||||
try decoder.decode(message)
|
||||
let iterator = decoder.iterator()!
|
||||
var profiles = [LibboxProfilePreview]()
|
||||
while iterator.hasNext() {
|
||||
let profile = iterator.next()!
|
||||
if profile.type == LibboxProfileTypeiCloud {
|
||||
// not supported on tvOS
|
||||
continue
|
||||
}
|
||||
profiles.append(profile)
|
||||
}
|
||||
self.profiles = profiles
|
||||
case LibboxMessageTypeProfileContent:
|
||||
let content = LibboxDecodeProfileContent(message, &error)
|
||||
if let error {
|
||||
throw error
|
||||
}
|
||||
try importProfile(content!)
|
||||
default:
|
||||
throw NSError(domain: "unknown message type \(message[0])", code: 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func selectProfile(profileID: Int64) {
|
||||
guard let connection else {
|
||||
return
|
||||
}
|
||||
let request = LibboxProfileContentRequest()
|
||||
request.profileID = profileID
|
||||
do {
|
||||
try connection.write(request.encode())
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
reset()
|
||||
}
|
||||
}
|
||||
|
||||
private func importProfile(_ content: LibboxProfileContent) throws {
|
||||
var type: ProfileType = .local
|
||||
switch content.type {
|
||||
case LibboxProfileTypeLocal:
|
||||
type = .local
|
||||
case LibboxProfileTypeiCloud:
|
||||
type = .icloud
|
||||
case LibboxProfileTypeRemote:
|
||||
type = .remote
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
let nextProfileID = try ProfileManager.nextID()
|
||||
let profileConfigDirectory = FilePath.sharedDirectory.appendingPathComponent("configs", isDirectory: true)
|
||||
try FileManager.default.createDirectory(at: profileConfigDirectory, withIntermediateDirectories: true)
|
||||
let profileConfig = profileConfigDirectory.appendingPathComponent("config_\(nextProfileID).json")
|
||||
try content.config.write(to: profileConfig, atomically: true, encoding: .utf8)
|
||||
var lastUpdated: Date?
|
||||
if content.lastUpdated > 0 {
|
||||
lastUpdated = Date(timeIntervalSince1970: Double(content.lastUpdated))
|
||||
}
|
||||
try ProfileManager.create(Profile(name: content.name, type: type, path: profileConfig.relativePath, remoteURL: content.remotePath, autoUpdate: content.autoUpdate, lastUpdated: lastUpdated))
|
||||
DispatchQueue.main.async {
|
||||
dismiss()
|
||||
callback()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -41,8 +41,10 @@ public struct NewProfileView: View {
|
||||
.multilineTextAlignment(.trailing)
|
||||
}
|
||||
Picker(selection: $profileType) {
|
||||
Text("Local").tag(ProfileType.local)
|
||||
Text("iCloud").tag(ProfileType.icloud)
|
||||
#if !os(tvOS)
|
||||
Text("Local").tag(ProfileType.local)
|
||||
Text("iCloud").tag(ProfileType.icloud)
|
||||
#endif
|
||||
Text("Remote").tag(ProfileType.remote)
|
||||
} label: {
|
||||
Text("Type")
|
||||
@@ -54,6 +56,9 @@ public struct NewProfileView: View {
|
||||
} label: {
|
||||
Text("File")
|
||||
}
|
||||
#if os(tvOS)
|
||||
.disabled(true)
|
||||
#endif
|
||||
viewBuilder {
|
||||
if fileImport {
|
||||
HStack {
|
||||
@@ -98,21 +103,23 @@ public struct NewProfileView: View {
|
||||
}
|
||||
.navigationTitle("New Profile")
|
||||
.alertBinding($alert)
|
||||
.fileImporter(
|
||||
isPresented: $pickerPresented,
|
||||
allowedContentTypes: [.json],
|
||||
allowsMultipleSelection: false
|
||||
) { result in
|
||||
do {
|
||||
let urls = try result.get()
|
||||
if !urls.isEmpty {
|
||||
fileURL = urls[0]
|
||||
#if os(iOS) || os(macOS)
|
||||
.fileImporter(
|
||||
isPresented: $pickerPresented,
|
||||
allowedContentTypes: [.json],
|
||||
allowsMultipleSelection: false
|
||||
) { result in
|
||||
do {
|
||||
let urls = try result.get()
|
||||
if !urls.isEmpty {
|
||||
fileURL = urls[0]
|
||||
}
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
}
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
return
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
private func createProfile() async {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import Foundation
|
||||
import Libbox
|
||||
import Library
|
||||
import Network
|
||||
import SwiftUI
|
||||
|
||||
public struct ProfileView: View {
|
||||
@@ -16,12 +17,16 @@ public struct ProfileView: View {
|
||||
@State private var alert: Alert?
|
||||
@State private var profileList: [Profile] = []
|
||||
|
||||
#if os(iOS)
|
||||
#if os(iOS) || os(tvOS)
|
||||
@State private var editMode = EditMode.inactive
|
||||
#elseif os(macOS)
|
||||
@Environment(\.openWindow) private var openWindow
|
||||
#endif
|
||||
|
||||
#if os(tvOS)
|
||||
@Environment(\.devicePickerSupports) private var devicePickerSupports
|
||||
#endif
|
||||
|
||||
@State private var observer: Any?
|
||||
|
||||
public init() {}
|
||||
@@ -35,7 +40,7 @@ public struct ProfileView: View {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
#if os(iOS)
|
||||
#if os(iOS) || os(tvOS)
|
||||
ZStack {
|
||||
if let importRemoteProfileRequest {
|
||||
NavigationLink(
|
||||
@@ -51,16 +56,31 @@ public struct ProfileView: View {
|
||||
)
|
||||
}
|
||||
FormView {
|
||||
NavigationLink {
|
||||
NewProfileView {
|
||||
Task.detached {
|
||||
doReload()
|
||||
#if os(iOS) || os(tvOS)
|
||||
NavigationLink {
|
||||
NewProfileView {
|
||||
Task.detached {
|
||||
doReload()
|
||||
}
|
||||
}
|
||||
} label: {
|
||||
Text("New Profile").foregroundColor(.accentColor)
|
||||
}
|
||||
.disabled(editMode.isEditing)
|
||||
#endif
|
||||
#if os(tvOS)
|
||||
if devicePickerSupports(.applicationService(name: "sing-box"), parameters: { .applicationService }) {
|
||||
NavigationLink {
|
||||
ImportProfileView {
|
||||
Task.detached {
|
||||
doReload()
|
||||
}
|
||||
}
|
||||
} label: {
|
||||
Text("Import Profile").foregroundColor(.accentColor)
|
||||
}
|
||||
}
|
||||
} label: {
|
||||
Text("New Profile").foregroundColor(.accentColor)
|
||||
}
|
||||
.disabled(editMode.isEditing)
|
||||
#endif
|
||||
if profileList.isEmpty {
|
||||
Text("Empty Profiles")
|
||||
} else {
|
||||
@@ -77,6 +97,23 @@ public struct ProfileView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
.contextMenu {
|
||||
if profile.type == .remote {
|
||||
Button {
|
||||
isUpdating = true
|
||||
Task.detached {
|
||||
updateProfile(profile)
|
||||
}
|
||||
} label: {
|
||||
Label("Update", systemImage: "arrow.clockwise")
|
||||
}
|
||||
}
|
||||
Button(role: .destructive) {
|
||||
deleteProfile(profile)
|
||||
} label: {
|
||||
Label("Delete", systemImage: "trash.fill")
|
||||
}
|
||||
}
|
||||
}
|
||||
.onMove(perform: moveProfile)
|
||||
.onDelete(perform: deleteProfile)
|
||||
@@ -194,7 +231,7 @@ public struct ProfileView: View {
|
||||
title: Text("Import Remote Profile"),
|
||||
message: Text("Are you sure to import remote configuration \(newValue.name)? You will connect to \(newValue.host) to download the configuration."),
|
||||
primaryButton: .default(Text("Import")) {
|
||||
#if os(iOS)
|
||||
#if os(iOS) || os(tvOS)
|
||||
importRemoteProfilePresented = true
|
||||
#elseif os(macOS)
|
||||
openWindow(id: NewProfileView.windowID, value: importRemoteProfileRequest!)
|
||||
|
||||
@@ -34,12 +34,15 @@ public struct ServiceLogView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
#if !os(tvOS)
|
||||
.toolbar {
|
||||
Button("Export") {
|
||||
fileExporterPresented = true
|
||||
}
|
||||
.disabled(content.isEmpty)
|
||||
}
|
||||
#endif
|
||||
#if !os(tvOS)
|
||||
.fileExporter(
|
||||
isPresented: $fileExporterPresented,
|
||||
document: LogDocument(content),
|
||||
@@ -47,10 +50,14 @@ public struct ServiceLogView: View {
|
||||
defaultFilename: "service-log.txt",
|
||||
onCompletion: { _ in }
|
||||
)
|
||||
#endif
|
||||
.navigationTitle("Service Log")
|
||||
#if os(iOS)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
#endif
|
||||
#if os(tvOS)
|
||||
.focusable()
|
||||
#endif
|
||||
}
|
||||
|
||||
private func loadContent() {
|
||||
@@ -65,25 +72,27 @@ public struct ServiceLogView: View {
|
||||
isLoading = false
|
||||
}
|
||||
|
||||
private struct LogDocument: FileDocument {
|
||||
static var readableContentTypes = [UTType.text]
|
||||
#if !os(tvOS)
|
||||
private struct LogDocument: FileDocument {
|
||||
static var readableContentTypes = [UTType.text]
|
||||
|
||||
let content: String
|
||||
let content: String
|
||||
|
||||
init(_ content: String) {
|
||||
self.content = content
|
||||
}
|
||||
init(_ content: String) {
|
||||
self.content = content
|
||||
}
|
||||
|
||||
init(configuration: ReadConfiguration) throws {
|
||||
if let data = configuration.file.regularFileContents {
|
||||
content = String(decoding: data, as: UTF8.self)
|
||||
} else {
|
||||
content = ""
|
||||
init(configuration: ReadConfiguration) throws {
|
||||
if let data = configuration.file.regularFileContents {
|
||||
content = String(decoding: data, as: UTF8.self)
|
||||
} else {
|
||||
content = ""
|
||||
}
|
||||
}
|
||||
|
||||
func fileWrapper(configuration _: WriteConfiguration) throws -> FileWrapper {
|
||||
FileWrapper(regularFileWithContents: Data(content.utf8))
|
||||
}
|
||||
}
|
||||
|
||||
func fileWrapper(configuration _: WriteConfiguration) throws -> FileWrapper {
|
||||
FileWrapper(regularFileWithContents: Data(content.utf8))
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ public struct SettingView: View {
|
||||
Section("Core") {
|
||||
FormTextItem("Version", version)
|
||||
FormTextItem("Data Size", dataSize)
|
||||
#if os(iOS)
|
||||
#if os(iOS) || os(tvOS)
|
||||
NavigationLink(destination: ServiceLogView()) {
|
||||
Text("View Service Log")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user