Improve dashboard

This commit is contained in:
世界
2023-08-02 16:47:07 +08:00
parent b9a7ea4afa
commit 5e0573d15c
24 changed files with 619 additions and 257 deletions
@@ -1,6 +1,6 @@
import SwiftUI
public func NavigationStackCompat<T>(@ViewBuilder content: () -> T) -> some View where T: View{
public func NavigationStackCompat(@ViewBuilder content: () -> some View) -> some View {
viewBuilder {
if #available(iOS 17.0, *) {
// view not updating in iOS 16, but why?
@@ -4,82 +4,53 @@ import Library
import SwiftUI
public struct ActiveDashboardView: View {
public static let NotificationUpdateSelectedProfile = Notification.Name("update-selected-profile")
@Environment(\.scenePhase) var scenePhase
@Environment(\.selection) private var selection
@Environment(\.selection) private var parentSelection
@EnvironmentObject private var profile: ExtensionProfile
@State private var isLoading = true
@State private var profileList: [Profile] = []
@State private var selectedProfileID: Int64!
@State private var reasserting = false
@State private var observer: Any?
@State private var alert: Alert?
@State private var selection = DashboardPage.overview
public init() {}
public var body: some View {
viewBuilder {
if isLoading {
ProgressView().onAppear {
Task.detached {
await doReload()
}
}
} else {
if profileList.isEmpty {
Text("Empty profiles")
} else {
VStack {
#if os(iOS) || os(tvOS)
if ApplicationLibrary.inPreview || profile.status.isConnected {
ExtensionStatusView()
.listStyle(.automatic)
#if os(iOS)
.navigationBarTitleDisplayMode(.inline)
#endif
}
FormView {
StartStopButton()
Section("Profile") {
Picker(selection: $selectedProfileID) {
ForEach(profileList, id: \.id) { profile in
Text(profile.name).tag(profile.id)
}
} label: {}
.pickerStyle(.inline)
}
}
#elseif os(macOS)
if ApplicationLibrary.inPreview || profile.status.isConnected {
ExtensionStatusView()
}
FormView {
Section("Profile") {
ForEach(profileList, id: \.id) { profile in
Picker(profile.name, selection: $selectedProfileID) {
Text("").tag(profile.id)
}
}
.pickerStyle(.radioGroup)
}
}
#endif
}
.onChangeCompat(of: selectedProfileID) {
reasserting = true
Task.detached {
await switchProfile(selectedProfileID!)
}
}
.disabled(!ApplicationLibrary.inPreview && (!profile.status.isSwitchable || reasserting))
if isLoading {
ProgressView().onAppear {
Task.detached {
await doReload()
}
}
}
.alertBinding($alert)
#if os(iOS) || os(tvOS)
} else {
viewBuilder {
#if os(iOS) || os(tvOS)
if ApplicationLibrary.inPreview || profile.status.isConnectedStrict {
Picker("Page", selection: $selection) {
ForEach(DashboardPage.allCases) { page in
page.label
}
}
.pickerStyle(.segmented)
#if os(iOS)
.padding([.leading, .trailing])
.navigationBarTitleDisplayMode(.inline)
#endif
TabView(selection: $selection) {
ForEach(DashboardPage.allCases) { page in
page.contentView($profileList, $selectedProfileID)
.tag(page)
}
}
.tabViewStyle(.page(indexDisplayMode: .always))
} else {
OverviewView($profileList, $selectedProfileID)
}
#elseif os(macOS)
OverviewView($profileList, $selectedProfileID)
#endif
}
#if os(iOS) || os(tvOS)
.onChangeCompat(of: scenePhase) { newValue in
if newValue == .active {
Task.detached {
@@ -87,29 +58,16 @@ public struct ActiveDashboardView: View {
}
}
}
.onChangeCompat(of: selection.wrappedValue) { newValue in
.onChangeCompat(of: parentSelection.wrappedValue) { 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
#endif
.alertBinding($alert)
}
}
private func doReload() {
@@ -143,28 +101,4 @@ public struct ActiveDashboardView: View {
}
}
}
private func switchProfile(_ newProfileID: Int64) {
SharedPreferences.selectedProfileID = newProfileID
NotificationCenter.default.post(name: ActiveDashboardView.NotificationUpdateSelectedProfile, object: nil)
if profile.status.isConnected {
do {
try LibboxNewStandaloneCommandClient()!.serviceReload()
} catch {
alert = Alert(error)
}
}
reasserting = false
}
private struct ServiceErrorReporter: View {
private let parent: ActiveDashboardView
init(_ parent: ActiveDashboardView) {
self.parent = parent
}
var body: some View {
EmptyView()
}
}
}
@@ -0,0 +1,43 @@
import Foundation
import Library
import SwiftUI
public enum DashboardPage: Int, CaseIterable, Identifiable {
public var id: Self {
self
}
case overview
case groups
}
public extension DashboardPage {
var title: String {
switch self {
case .overview:
return NSLocalizedString("Overview", comment: "")
case .groups:
return NSLocalizedString("Groups", comment: "")
}
}
var label: some View {
switch self {
case .overview:
return Label("Overview", systemImage: "text.and.command.macwindow")
case .groups:
return Label("Groups", systemImage: "rectangle.3.group.fill")
}
}
func contentView(_ profileList: Binding<[Profile]>, _ selectedProfileID: Binding<Int64?>) -> some View {
viewBuilder {
switch self {
case .overview:
OverviewView(profileList, selectedProfileID)
case .groups:
GroupListView()
}
}
}
}
@@ -40,7 +40,6 @@ public struct DashboardView: View {
}
}
#endif
.navigationTitle("Dashboard")
}
#if os(macOS)
@@ -18,28 +18,50 @@ public struct ExtensionStatusView: View {
VStack {
LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: columnCount), alignment: .leading) {
if ApplicationLibrary.inPreview {
StatusItem("Memory", "6.4 MiB")
StatusItem("Goroutines", "89")
StatusItem("Inbound Connections", "34")
StatusItem("Outbound Connections", "28")
StatusItem("Uplink", "38 B/s")
StatusItem("Downlink", "249 MiB/s")
StatusItem("Uplink Total", "52 MiB")
StatusItem("Downlink Total", "5.6 GiB")
StatusItem("Status") {
StatusLine("Memory", "6.4 MiB")
StatusLine("Goroutines", "89")
}
StatusItem("Connections") {
StatusLine("Inbound", "34")
StatusLine("Outbound", "28")
}
StatusItem("Traffic") {
StatusLine("Uplink", "38 B/s")
StatusLine("Downlink", "249 MiB/s")
}
StatusItem("TrafficTotal") {
StatusLine("Uplink", "52 MiB")
StatusLine("Downlink", "5.6 GiB")
}
} else if let message {
StatusItem("Memory", LibboxFormatBytes(message.memory))
StatusItem("Goroutines", "\(message.goroutines)")
StatusItem("Status") {
StatusLine("Memory", LibboxFormatBytes(message.memory))
StatusLine("Goroutines", "\(message.goroutines)")
}
StatusItem("Connections") {
StatusLine("Inbound", "\(message.connectionsIn)")
StatusLine("Outbound", "\(message.connectionsOut)")
}
if message.trafficAvailable {
StatusItem("Inbound Connections", "\(message.connectionsIn)")
StatusItem("Outbound Connections", "\(message.connectionsOut)")
StatusItem("Uplink", LibboxFormatBytes(message.uplink) + "/s")
StatusItem("Downlink", LibboxFormatBytes(message.downlink) + "/s")
StatusItem("Uplink Total", LibboxFormatBytes(message.uplinkTotal))
StatusItem("Downlink Total", LibboxFormatBytes(message.downlinkTotal))
StatusItem("Traffic") {
StatusLine("Uplink", "\(LibboxFormatBytes(message.uplink))/s")
StatusLine("Downlink", "\(LibboxFormatBytes(message.downlink))/s")
}
StatusItem("TrafficTotal") {
StatusLine("Uplink", LibboxFormatBytes(message.uplinkTotal))
StatusLine("Downlink", LibboxFormatBytes(message.downlinkTotal))
}
}
} else {
StatusItem("Memory", "Loading...")
StatusItem("Goroutines", "Loading...")
StatusItem("Status") {
StatusLine("Memory", "...")
StatusLine("Goroutines", "...")
}
StatusItem("Connections") {
StatusLine("Inbound", "...")
StatusLine("Outbound", "...")
}
}
}.background {
GeometryReader { geometry in
@@ -142,26 +164,25 @@ public struct ExtensionStatusView: View {
func writeGroups(_: LibboxOutboundGroupIteratorProtocol?) {}
}
private struct StatusItem: View {
private let name: String
private let value: String
init(_ name: String, _ value: String) {
self.name = name
self.value = value
private struct StatusItem<T>: View where T: View {
private let title: String
@ViewBuilder private let content: () -> T
init(_ title: String, @ViewBuilder content: @escaping () -> T) {
self.title = title
self.content = content
}
var body: some View {
VStack(alignment: .leading, spacing: 8) {
VStack(alignment: .leading) {
HStack {
Text(name)
.font(.subheadline)
.foregroundColor(.secondary)
Text(title)
.font(.headline)
Spacer()
}
Text(value)
.font(.system(size: 16))
}.padding(.bottom, 8)
content()
}
.frame(minWidth: 125)
.frame(minWidth: 125, alignment: .topLeading)
#if os(tvOS)
.padding(EdgeInsets(top: 20, leading: 26, bottom: 20, trailing: 26))
#else
@@ -181,4 +202,25 @@ public struct ExtensionStatusView: View {
#endif
}
}
private struct StatusLine: View {
private let name: String
private let value: String
init(_ name: String, _ value: String) {
self.name = name
self.value = value
}
var body: some View {
HStack {
Text(name)
.font(.subheadline)
.foregroundColor(.secondary)
Spacer()
Text(value)
.font(.subheadline)
}
}
}
}
@@ -0,0 +1,87 @@
import Foundation
import Libbox
import Library
import SwiftUI
public struct OverviewView: View {
public static let NotificationUpdateSelectedProfile = Notification.Name("update-selected-profile")
@Environment(\.scenePhase) var scenePhase
@Environment(\.selection) private var selection
@EnvironmentObject private var profile: ExtensionProfile
@Binding private var profileList: [Profile]
@Binding private var selectedProfileID: Int64!
@State private var alert: Alert?
@State private var reasserting = false
@State private var observer: Any?
public init(_ profileList: Binding<[Profile]>, _ selectedProfileID: Binding<Int64?>) {
_profileList = profileList
_selectedProfileID = selectedProfileID
}
public var body: some View {
VStack {
if ApplicationLibrary.inPreview || profile.status.isConnected {
ExtensionStatusView()
}
FormView {
#if os(iOS) || os(tvOS)
StartStopButton()
Section("Profile") {
Picker(selection: $selectedProfileID) {
ForEach(profileList, id: \.id) { profile in
Text(profile.name).tag(profile.id)
}
} label: {}
.pickerStyle(.inline)
}
#elseif os(macOS)
Section("Profile") {
ForEach(profileList, id: \.id) { profile in
Picker(profile.name, selection: $selectedProfileID) {
Text("").tag(profile.id)
}
}
.pickerStyle(.radioGroup)
}
#endif
}
}
.alertBinding($alert)
.onChangeCompat(of: selectedProfileID) {
reasserting = true
Task.detached {
await switchProfile(selectedProfileID!)
}
}
.disabled(!ApplicationLibrary.inPreview && (!profile.status.isSwitchable || reasserting))
#if os(macOS)
.onAppear {
if observer == nil {
observer = NotificationCenter.default.addObserver(forName: OverviewView.NotificationUpdateSelectedProfile, object: nil, queue: nil, using: { newProfileID in
selectedProfileID = newProfileID.object as! Int64
})
}
}
.onDisappear {
if let observer {
NotificationCenter.default.removeObserver(observer)
}
}
#endif
}
private func switchProfile(_ newProfileID: Int64) {
SharedPreferences.selectedProfileID = newProfileID
NotificationCenter.default.post(name: OverviewView.NotificationUpdateSelectedProfile, object: newProfileID)
if profile.status.isConnected {
do {
try LibboxNewStandaloneCommandClient()!.serviceReload()
} catch {
alert = Alert(error)
}
}
reasserting = false
}
}
@@ -17,48 +17,54 @@ public struct GroupItemView: View {
@State private var alert: Alert?
public var body: some View {
HStack {
if group.selected == item.tag {
Rectangle()
.fill(Color.accentColor)
.frame(width: 6)
} else {
Rectangle()
.fill(.clear)
.frame(width: 6)
}
VStack {
HStack {
Text(item.tag)
.truncationMode(.tail)
.lineLimit(1)
.font(.system(size: 14))
Spacer(minLength: 6)
}
Spacer(minLength: 6)
HStack(alignment: .center) {
Text(item.type)
.foregroundColor(.secondary)
.font(.system(size: 12))
Spacer(minLength: 6)
if item.urlTestDelay > 0 {
Text(item.delayString)
.foregroundColor(item.delayColor)
.font(.system(size: 11))
}
}
}
.frame(height: 36)
.padding([.top, .bottom, .trailing], 12)
}
.background(backgroundColor)
.onTapGesture {
Button(action: {
if group.selectable, group.selected != item.tag {
Task.detached {
selectOutbound()
}
}
}
}, label: {
HStack {
VStack {
HStack {
Text(item.tag)
.font(.caption)
.foregroundStyle(.foreground)
.lineLimit(1)
.truncationMode(.tail)
Spacer(minLength: 0)
}
Spacer(minLength: 8)
HStack {
Text(item.type)
.font(.caption)
.foregroundColor(.secondary)
Spacer(minLength: 0)
}
}
Spacer(minLength: 0)
VStack {
if group.selected == item.tag {
Image(systemName: "checkmark.circle.fill")
.foregroundStyle(Color.accentColor)
}
Spacer(minLength: 0)
if item.urlTestDelay > 0 {
Text(item.delayString)
.font(.caption)
.foregroundColor(item.delayColor)
}
}
}
})
#if !os(tvOS)
.buttonStyle(.borderless)
.padding(EdgeInsets(top: 10, leading: 13, bottom: 10, trailing: 13))
.background(backgroundColor)
.cornerRadius(10)
#endif
.alertBinding($alert)
}
@@ -23,7 +23,6 @@ public struct GroupListView: View {
}, set: { newValue in
groupExpand[it.tag] = newValue
}))
Spacer()
}
}.padding()
}
@@ -40,7 +39,6 @@ public struct GroupListView: View {
}
commandClient = nil
}
.navigationTitle("Groups")
}
private func doReload() {
@@ -51,7 +49,7 @@ public struct GroupListView: View {
OutboundGroupItem(tag: "server2", type: "WireGuard", urlTestTime: .now, urlTestDelay: 34),
OutboundGroupItem(tag: "auto", type: "URLTest", urlTestTime: .now, urlTestDelay: 100),
]),
OutboundGroup(tag: "group2", type: "urltest", selected: "client", selectable: false, items:
OutboundGroup(tag: "group2", type: "urltest", selected: "client", selectable: true, items:
(0 ..< 234).map { index in
OutboundGroupItem(tag: "client\(index)", type: "Shadowsocks", urlTestTime: .now, urlTestDelay: UInt16(100 + index * 10))
}),
+23 -13
View File
@@ -15,12 +15,12 @@ public struct GroupView: View {
private var title: some View {
HStack {
Text(group.tag)
.font(.system(size: 17))
.font(.headline)
Text(group.displayType)
.font(.system(size: 13))
.font(.subheadline)
.foregroundColor(.secondary)
Text("\(group.items.count)")
.font(.system(size: 11))
.font(.subheadline)
.padding(EdgeInsets(top: 2, leading: 4, bottom: 2, trailing: 4))
.background(Color.gray.opacity(0.5))
.cornerRadius(4)
@@ -33,7 +33,7 @@ public struct GroupView: View {
Image(systemName: "arrow.up.to.line")
}
}
#if os(macOS)
#if os(macOS) || os(tvOS)
.buttonStyle(.plain)
#endif
Button {
@@ -43,11 +43,10 @@ public struct GroupView: View {
} label: {
Image(systemName: "bolt.fill")
}
#if os(macOS)
#if os(macOS) || os(tvOS)
.buttonStyle(.plain)
#endif
Spacer(minLength: 6)
}
}.padding([.top, .bottom], 8)
}
public var body: some View {
@@ -61,13 +60,17 @@ public struct GroupView: View {
}
}
} else {
VStack {
VStack(spacing: 5) {
ForEach(Array(itemGroups.enumerated()), id: \.offset) { items in
HStack {
HStack(spacing: 5) {
ForEach(items.element, id: \.tag) { it in
Rectangle()
.fill(it.delayColor)
#if !os(tvOS)
.frame(width: 10, height: 10)
#else
.frame(width: 30, height: 30)
#endif
}
}.frame(maxWidth: .infinity, alignment: .topLeading)
}
@@ -93,7 +96,12 @@ public struct GroupView: View {
}
private var itemGroups: [[OutboundGroupItem]] {
let count = Int(Int(geometryWidth) / 20)
let count: Int
#if os(tvOS)
count = Int(Int(geometryWidth) / 40)
#else
count = Int(Int(geometryWidth) / 20)
#endif
if count == 0 {
return [group.items]
} else {
@@ -104,11 +112,13 @@ public struct GroupView: View {
}
private func explandColumnCount() -> Int {
let count = Int(Int(geometryWidth) / 180)
let standardCount = Int(Int(geometryWidth) / 180)
#if os(iOS)
return count < 2 ? 2 : count
return standardCount < 2 ? 2 : standardCount
#elseif os(tvOS)
return Int(Int(geometryWidth) / 400)
#else
return count < 1 ? 1 : count
return standardCount < 1 ? 1 : standardCount
#endif
}
+1 -2
View File
@@ -13,7 +13,6 @@ public struct LogView: View {
Text("Service not started")
}
}
.navigationTitle("Logs")
}
private struct LogView0: View {
@@ -43,7 +42,7 @@ public struct LogView: View {
#if os(tvOS)
.focusable()
#endif
Spacer(minLength: 5)
Spacer(minLength: 8)
}
.onChangeCompat(of: logClient.logList.count) { newCount in
+27 -22
View File
@@ -8,13 +8,19 @@ public enum NavigationPage: Int, CaseIterable, Identifiable {
}
case dashboard
case groups
#if os(macOS)
case groups
#endif
case logs
case profiles
case settings
}
public extension NavigationPage {
static var macosDefaultPages: [NavigationPage] {
[.logs, .profiles, .settings]
}
var label: some View {
Label(title, systemImage: iconImage)
}
@@ -23,8 +29,10 @@ public extension NavigationPage {
switch self {
case .dashboard:
return NSLocalizedString("Dashboard", comment: "")
case .groups:
return NSLocalizedString("Groups", comment: "")
#if os(macOS)
case .groups:
return NSLocalizedString("Groups", comment: "")
#endif
case .logs:
return NSLocalizedString("Logs", comment: "")
case .profiles:
@@ -38,8 +46,10 @@ public extension NavigationPage {
switch self {
case .dashboard:
return "text.and.command.macwindow"
case .groups:
return "rectangle.3.group.fill"
#if os(macOS)
case .groups:
return "rectangle.3.group.fill"
#endif
case .logs:
return "doc.text.fill"
case .profiles:
@@ -54,9 +64,10 @@ public extension NavigationPage {
switch self {
case .dashboard:
DashboardView()
case .groups:
GroupListView()
#if os(macOS)
case .groups:
GroupListView()
#endif
case .logs:
LogView()
case .profiles:
@@ -70,20 +81,14 @@ public extension NavigationPage {
#endif
}
func visible(_ profile: ExtensionProfile?) -> Bool {
switch self {
case .groups:
#if os(tvOS)
// TODO: fix groups ui
return false
#else
if ApplicationLibrary.inPreview {
return true
}
#if os(macOS)
func visible(_ profile: ExtensionProfile?) -> Bool {
switch self {
case .groups:
return profile?.status.isConnectedStrict == true
#endif
default:
return true
default:
return true
}
}
}
#endif
}
@@ -115,7 +115,6 @@ public struct ProfileView: View {
}
}
.disabled(isUpdating)
.navigationTitle("Profiles")
.alertBinding($alert, $isLoading)
.onAppear {
if let profile = importProfile.wrappedValue {
@@ -129,7 +129,6 @@ public struct SettingView: View {
}
}
}
.navigationTitle("Settings")
.alertBinding($alert)
}