fix: sidebar default selection

This commit is contained in:
mrFq1
2023-06-03 23:47:06 +08:00
parent 4ecc5bb3d1
commit e53503cdbe
2 changed files with 22 additions and 17 deletions

View File

@@ -5,57 +5,65 @@
// //
import SwiftUI import SwiftUI
import Introspect
struct SidebarListView: View { struct SidebarListView: View {
@Binding var selectionName: String? @Binding var selectionName: SidebarItem?
var body: some View { var body: some View {
List { List {
NavigationLink(destination: OverviewView(), NavigationLink(destination: OverviewView(),
tag: SidebarItem.overview.rawValue, tag: SidebarItem.overview,
selection: $selectionName) { selection: $selectionName) {
Label(SidebarItem.overview.rawValue, systemImage: "chart.bar.xaxis") Label(SidebarItem.overview.rawValue, systemImage: "chart.bar.xaxis")
} }
NavigationLink(destination: ProxiesView(), NavigationLink(destination: ProxiesView(),
tag: SidebarItem.proxies.rawValue, tag: SidebarItem.proxies,
selection: $selectionName) { selection: $selectionName) {
Label(SidebarItem.proxies.rawValue, systemImage: "globe.asia.australia") Label(SidebarItem.proxies.rawValue, systemImage: "globe.asia.australia")
} }
NavigationLink(destination: ProvidersView(), NavigationLink(destination: ProvidersView(),
tag: SidebarItem.providers.rawValue, tag: SidebarItem.providers,
selection: $selectionName) { selection: $selectionName) {
Label(SidebarItem.providers.rawValue, systemImage: "link.icloud") Label(SidebarItem.providers.rawValue, systemImage: "link.icloud")
} }
NavigationLink(destination: RulesView(), NavigationLink(destination: RulesView(),
tag: SidebarItem.rules.rawValue, tag: SidebarItem.rules,
selection: $selectionName) { selection: $selectionName) {
Label(SidebarItem.rules.rawValue, systemImage: "waveform.and.magnifyingglass") Label(SidebarItem.rules.rawValue, systemImage: "waveform.and.magnifyingglass")
} }
NavigationLink(destination: ConnectionsView(), NavigationLink(destination: ConnectionsView(),
tag: SidebarItem.conns.rawValue, tag: SidebarItem.conns,
selection: $selectionName) { selection: $selectionName) {
Label(SidebarItem.conns.rawValue, systemImage: "app.connected.to.app.below.fill") Label(SidebarItem.conns.rawValue, systemImage: "app.connected.to.app.below.fill")
} }
NavigationLink(destination: ConfigView(), NavigationLink(destination: ConfigView(),
tag: SidebarItem.config.rawValue, tag: SidebarItem.config,
selection: $selectionName) { selection: $selectionName) {
Label(SidebarItem.config.rawValue, systemImage: "slider.horizontal.3") Label(SidebarItem.config.rawValue, systemImage: "slider.horizontal.3")
} }
NavigationLink(destination: LogsView(), NavigationLink(destination: LogsView(),
tag: SidebarItem.logs.rawValue, tag: SidebarItem.logs,
selection: $selectionName) { selection: $selectionName) {
Label(SidebarItem.logs.rawValue, systemImage: "wand.and.stars.inverse") Label(SidebarItem.logs.rawValue, systemImage: "wand.and.stars.inverse")
} }
}
.introspectTableView {
if selectionName == nil {
selectionName = SidebarItem.overview
$0.allowsEmptySelection = false
if $0.selectedRow == -1 {
$0.selectRowIndexes(.init(integer: 0), byExtendingSelection: false)
}
}
} }
.listStyle(.sidebar) .listStyle(.sidebar)
} }

View File

@@ -13,10 +13,10 @@ struct SidebarView: View {
private let connsQueue = DispatchQueue(label: "thread-safe-connsQueue", attributes: .concurrent) private let connsQueue = DispatchQueue(label: "thread-safe-connsQueue", attributes: .concurrent)
private let timer = Timer.publish(every: 1, on: .main, in: .default).autoconnect() private let timer = Timer.publish(every: 1, on: .main, in: .default).autoconnect()
@State private var sidebarSelectionName: String? = "Overview" @State private var sidebarSelectionName: SidebarItem?
var body: some View { var body: some View {
ScrollViewReader { scrollViewProxy in Group {
SidebarListView(selectionName: $sidebarSelectionName) SidebarListView(selectionName: $sidebarSelectionName)
} }
.environmentObject(clashApiDatasStorage.overviewData) .environmentObject(clashApiDatasStorage.overviewData)
@@ -27,8 +27,6 @@ struct SidebarView: View {
ConfigManager.selectLoggingApiLevel = .info ConfigManager.selectLoggingApiLevel = .info
} }
sidebarItemChanged(sidebarSelectionName)
clashApiDatasStorage.resetStreamApi() clashApiDatasStorage.resetStreamApi()
connsQueue.sync { connsQueue.sync {
clashApiDatasStorage.connsStorage.conns clashApiDatasStorage.connsStorage.conns
@@ -57,9 +55,8 @@ struct SidebarView: View {
} }
} }
func sidebarItemChanged(_ name: String?) { func sidebarItemChanged(_ item: SidebarItem?) {
guard let str = name, guard let item else { return }
let item = SidebarItem(rawValue: str) else { return }
NotificationCenter.default.post(name: .sidebarItemChanged, object: nil, userInfo: ["item": item]) NotificationCenter.default.post(name: .sidebarItemChanged, object: nil, userInfo: ["item": item])
} }