Persist log and connection search state across tabs

This commit is contained in:
世界
2026-03-06 10:09:56 +08:00
parent 0636b47d36
commit c7e1f667c3
4 changed files with 18 additions and 4 deletions
@@ -60,10 +60,15 @@ public struct ConnectionListView: View {
#endif
.alert($viewModel.alert)
.onAppear {
if !environments.connectionSearchText.isEmpty {
viewModel.searchText = environments.connectionSearchText
viewModel.isSearching = true
}
viewModel.connect()
commandClient.connect()
}
.onDisappear {
environments.connectionSearchText = viewModel.searchText
viewModel.disconnect()
commandClient.disconnect()
}
+7 -3
View File
@@ -15,15 +15,16 @@ public struct LogView: View {
public init() {}
public var body: some View {
LogViewContent(commandClient: environments.commandClient)
LogViewContent(commandClient: environments.commandClient, initialSearchText: environments.logSearchText)
}
}
private struct LogViewContent: View {
@EnvironmentObject private var environments: ExtensionEnvironments
@StateObject private var viewModel: LogViewModel
init(commandClient: CommandClient) {
_viewModel = StateObject(wrappedValue: LogViewModel(commandClient: commandClient))
init(commandClient: CommandClient, initialSearchText: String = "") {
_viewModel = StateObject(wrappedValue: LogViewModel(commandClient: commandClient, searchText: initialSearchText))
}
var body: some View {
@@ -35,6 +36,9 @@ private struct LogViewContent: View {
toolbarButtons
}
}
.onDisappear {
environments.logSearchText = viewModel.searchText
}
.alert($viewModel.alert)
.background(
LogExportView(
@@ -176,8 +176,10 @@ public class LogViewModel: BaseViewModel {
public let commandClient: CommandClient
public private(set) var dataModel: LogDataModel!
public init(commandClient: CommandClient) {
public init(commandClient: CommandClient, searchText: String = "") {
self.commandClient = commandClient
self.searchText = searchText
self.isSearching = !searchText.isEmpty
super.init()
dataModel = LogDataModel(commandClient: commandClient, viewModel: self)
}
@@ -180,6 +180,9 @@ public class ExtensionEnvironments: ObservableObject {
@Published public var emptyProfiles = false
@Published public var pendingImportRemoteProfile: ImportRemoteProfileRequest?
public var logSearchText = ""
public var connectionSearchText = ""
public let profileUpdate = ObjectWillChangePublisher()
public let selectedProfileUpdate = ObjectWillChangePublisher()
public let openSettings = ObjectWillChangePublisher()