wip
This commit is contained in:
parent
bec095e8de
commit
c153985bf0
|
@ -2,6 +2,12 @@ import Combine
|
||||||
import Foundation
|
import Foundation
|
||||||
import GRDB
|
import GRDB
|
||||||
|
|
||||||
|
enum ClientsListState {
|
||||||
|
case empty
|
||||||
|
case allDisabled
|
||||||
|
case haveSomeEnabled
|
||||||
|
}
|
||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
final class ClientsStore: ObservableObject {
|
final class ClientsStore: ObservableObject {
|
||||||
static let shared = ClientsStore()
|
static let shared = ClientsStore()
|
||||||
|
@ -10,6 +16,7 @@ final class ClientsStore: ObservableObject {
|
||||||
@Published private(set) var clients: [Client] = []
|
@Published private(set) var clients: [Client] = []
|
||||||
@Published private(set) var actualRosters: [Roster] = []
|
@Published private(set) var actualRosters: [Roster] = []
|
||||||
@Published private(set) var actualChats: [Chat] = []
|
@Published private(set) var actualChats: [Chat] = []
|
||||||
|
@Published private(set) var listState: ClientsListState = .empty
|
||||||
|
|
||||||
private var credentialsCancellable: AnyCancellable?
|
private var credentialsCancellable: AnyCancellable?
|
||||||
private var rostersCancellable: AnyCancellable?
|
private var rostersCancellable: AnyCancellable?
|
||||||
|
@ -41,13 +48,29 @@ final class ClientsStore: ObservableObject {
|
||||||
updatedClients.append(contentsOf: newClients)
|
updatedClients.append(contentsOf: newClients)
|
||||||
clients = updatedClients
|
clients = updatedClients
|
||||||
|
|
||||||
|
// for creds in credentials {
|
||||||
|
// if let client = client(for: creds) {
|
||||||
|
// client.credentials = creds
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
if !ready {
|
if !ready {
|
||||||
ready = true
|
ready = true
|
||||||
}
|
}
|
||||||
|
|
||||||
resubscribeRosters()
|
resubscribeRosters()
|
||||||
resubscribeChats()
|
resubscribeChats()
|
||||||
reconnectAll()
|
reconnectNeeded()
|
||||||
|
|
||||||
|
if credentials.isEmpty {
|
||||||
|
listState = .empty
|
||||||
|
} else if credentials.allSatisfy({ !$0.isActive }) {
|
||||||
|
listState = .allDisabled
|
||||||
|
} else {
|
||||||
|
listState = .haveSomeEnabled
|
||||||
|
}
|
||||||
|
|
||||||
|
objectWillChange.send()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func client(for credentials: Credentials) -> Client? {
|
private func client(for credentials: Credentials) -> Client? {
|
||||||
|
@ -67,21 +90,20 @@ extension ClientsStore {
|
||||||
try? await client.credentials.save()
|
try? await client.credentials.save()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func reconnectAll() {
|
private func reconnectNeeded() {
|
||||||
Task {
|
Task {
|
||||||
await withTaskGroup(of: Void.self) { taskGroup in
|
await withTaskGroup(of: Void.self) { taskGroup in
|
||||||
for client in clients {
|
for client in clients {
|
||||||
if client.credentials.isActive && client.state != .enabled(.connected) {
|
|
||||||
taskGroup.addTask {
|
|
||||||
await client.connect()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !client.credentials.isActive && client.state == .enabled(.connected) {
|
if !client.credentials.isActive && client.state == .enabled(.connected) {
|
||||||
taskGroup.addTask {
|
taskGroup.addTask {
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if client.credentials.isActive && client.state != .enabled(.connected) {
|
||||||
|
taskGroup.addTask {
|
||||||
|
await client.connect()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,22 +10,22 @@ struct WelcomeScreen: View {
|
||||||
Color.Material.Background.light
|
Color.Material.Background.light
|
||||||
.ignoresSafeArea()
|
.ignoresSafeArea()
|
||||||
|
|
||||||
// if !clientsStore.clients.isEmpty && clientsStore.clients.allSatisfy({ $0.state != .enabled(.connected) }) {
|
if clientsStore.listState == .allDisabled {
|
||||||
// VStack {
|
VStack {
|
||||||
// HStack {
|
HStack {
|
||||||
// Spacer()
|
Spacer()
|
||||||
// Image(systemName: "gear")
|
Image(systemName: "gear")
|
||||||
// .foregroundColor(.Material.Elements.active)
|
.foregroundColor(.Material.Elements.active)
|
||||||
// .tappablePadding(.symmetric(10)) {
|
.tappablePadding(.symmetric(10)) {
|
||||||
// router.showScreen(.push) { _ in
|
router.showScreen(.push) { _ in
|
||||||
// SettingsScreen()
|
SettingsScreen()
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// .padding()
|
.padding()
|
||||||
// Spacer()
|
Spacer()
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// content
|
// content
|
||||||
VStack(spacing: 32) {
|
VStack(spacing: 32) {
|
||||||
|
|
|
@ -22,23 +22,23 @@ struct SettingsScreen: View {
|
||||||
// Accounts section
|
// Accounts section
|
||||||
SharedSectionTitle(text: L10n.Settings.Section.Accounts.title)
|
SharedSectionTitle(text: L10n.Settings.Section.Accounts.title)
|
||||||
|
|
||||||
// ForEach(settingsStore.credentials) { creds in
|
ForEach(clientsStore.clients) { client in
|
||||||
// SharedListRow(
|
SharedListRow(
|
||||||
// iconType: .charCircle(creds.bareJid),
|
iconType: .charCircle(client.credentials.bareJid),
|
||||||
// text: creds.bareJid,
|
text: client.credentials.bareJid,
|
||||||
// controlType: .switcher(isOn: Binding(
|
controlType: .switcher(isOn: Binding(
|
||||||
// get: { creds.isActive },
|
get: { client.credentials.isActive },
|
||||||
// set: { new in
|
set: { new in
|
||||||
// Task {
|
Task {
|
||||||
// try? await creds.setActive(flag: new)
|
try? await client.credentials.setActive(flag: new)
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// ))
|
))
|
||||||
// )
|
)
|
||||||
// .onTapGesture {
|
.onTapGesture {
|
||||||
// print("Tapped account \(creds.bareJid)")
|
print("Tapped account \(client.credentials.bareJid)")
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
SharedListRow(
|
SharedListRow(
|
||||||
iconType: .image(Image(systemName: "plus"), .Material.Elements.active),
|
iconType: .image(Image(systemName: "plus"), .Material.Elements.active),
|
||||||
|
|
|
@ -7,7 +7,7 @@ struct RootView: View {
|
||||||
var body: some View {
|
var body: some View {
|
||||||
Group {
|
Group {
|
||||||
if clientsStore.ready {
|
if clientsStore.ready {
|
||||||
if clientsStore.clients.isEmpty {
|
if clientsStore.listState != .haveSomeEnabled {
|
||||||
RouterView { _ in
|
RouterView { _ in
|
||||||
WelcomeScreen()
|
WelcomeScreen()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue