Refactor to use consolidated CommandClient
This commit is contained in:
@@ -2,7 +2,7 @@ import Library
|
||||
import SwiftUI
|
||||
|
||||
public struct GroupListView: View {
|
||||
@Environment(\.scenePhase) private var scenePhase
|
||||
@EnvironmentObject private var commandClient: CommandClient
|
||||
@StateObject private var viewModel = GroupListViewModel()
|
||||
|
||||
public init() {}
|
||||
@@ -23,17 +23,8 @@ public struct GroupListView: View {
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
viewModel.setCommandClient(commandClient)
|
||||
viewModel.connect()
|
||||
}
|
||||
.onDisappear {
|
||||
viewModel.disconnect()
|
||||
}
|
||||
.onChangeCompat(of: scenePhase) { newValue in
|
||||
if newValue == .active {
|
||||
viewModel.connect()
|
||||
} else {
|
||||
viewModel.disconnect()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,11 +8,14 @@ public class GroupListViewModel: ObservableObject {
|
||||
@Published public var isLoading = true
|
||||
@Published public var groups: [OutboundGroup] = []
|
||||
|
||||
private let commandClient = CommandClient(.groups)
|
||||
private var commandClient: CommandClient?
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
public init() {
|
||||
commandClient.$groups
|
||||
public init() {}
|
||||
|
||||
public func setCommandClient(_ client: CommandClient) {
|
||||
commandClient = client
|
||||
client.$groups
|
||||
.compactMap { $0 }
|
||||
.sink { [weak self] goGroups in
|
||||
self?.setGroups(goGroups)
|
||||
@@ -34,15 +37,9 @@ public class GroupListViewModel: ObservableObject {
|
||||
}),
|
||||
]
|
||||
isLoading = false
|
||||
} else {
|
||||
commandClient.connect()
|
||||
}
|
||||
}
|
||||
|
||||
public func disconnect() {
|
||||
commandClient.disconnect()
|
||||
}
|
||||
|
||||
private func setGroups(_ goGroups: [LibboxOutboundGroup]) {
|
||||
var groups = [OutboundGroup]()
|
||||
for goGroup in goGroups {
|
||||
|
||||
Reference in New Issue
Block a user