Highlihght side menu default account

This commit is contained in:
Christophe Deschamps 2024-06-17 13:14:47 +02:00
parent dfcd501dc3
commit 02e6baf9ba
4 changed files with 57 additions and 47 deletions

View file

@ -29,7 +29,6 @@ struct SideMenu: View {
let safeAreaInsets: EdgeInsets
@Binding var isShowLoginFragment: Bool
@State private var showHelp = false
@State private var selectedAccountIndex: Int = 0
var body: some View {
ZStack {
@ -69,13 +68,9 @@ struct SideMenu: View {
ForEach(0..<CoreContext.shared.accounts.count, id: \.self) { index in
SideMenuAccountRow(
model: AccountModel(account: CoreContext.shared.accounts[0],
corePublisher: CoreContext.shared.getCorePublisher()),
backgroundColor: self.selectedAccountIndex == index ? Color.grayMain2c100 : .clear
corePublisher: CoreContext.shared.getCorePublisher())
)
.background()
.onTapGesture {
self.selectedAccountIndex = index
}
.listRowInsets(EdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16))
.listRowSeparator(.hidden)
}

View file

@ -23,7 +23,6 @@ import UniformTypeIdentifiers
struct SideMenuAccountRow: View {
@ObservedObject var model: AccountModel
var backgroundColor: Color
var body: some View {
HStack {
@ -82,6 +81,6 @@ struct SideMenuAccountRow: View {
.padding(.bottom, 12)
}
.frame(height: 61)
.background(backgroundColor)
.background(model.isDefaultAccount ? Color.grayMain2c100 : .clear)
}
}

View file

@ -26,51 +26,63 @@ class AccountModel: ObservableObject {
@Published var humanReadableRegistrationState: String = ""
@Published var registrationStateAssociatedUIColor: Color = .clear
@Published var notificationsCount: Int = 0
@Published var isDefaultAccount: Bool = false
init(account: Account, corePublisher: CoreDelegatePublisher?) {
self.account = account
update()
account.publisher?.onRegistrationStateChanged?
.postOnMainQueue { _ in
self.update()
}
corePublisher?.onChatRoomRead?.postOnMainQueue(
receiveValue: { _ in
self.computeNotificationsCount()
})
corePublisher?.onMessagesReceived?.postOnMainQueue(
receiveValue: { _ in
self.computeNotificationsCount()
})
corePublisher?.onCallStateChanged?.postOnMainQueue(
receiveValue: { _ in
self.computeNotificationsCount()
})
}
func update() {
switch account.state {
case .Cleared, .None:
humanReadableRegistrationState = "drawer_menu_account_connection_status_cleared".localized()
registrationStateAssociatedUIColor = .orangeWarning600
case .Progress:
humanReadableRegistrationState = "drawer_menu_account_connection_status_progress".localized()
registrationStateAssociatedUIColor = .greenSuccess500
case .Failed:
humanReadableRegistrationState = "drawer_menu_account_connection_status_failed".localized()
registrationStateAssociatedUIColor = .redDanger500
case .Ok:
humanReadableRegistrationState = "drawer_menu_account_connection_status_connected".localized()
registrationStateAssociatedUIColor = .greenSuccess500
case .Refreshing:
humanReadableRegistrationState = "drawer_menu_account_connection_status_refreshing".localized()
registrationStateAssociatedUIColor = .grayMain2c500
CoreContext.shared.doOnCoreQueue { _ in
self.update()
}
computeNotificationsCount()
account.publisher?.onRegistrationStateChanged?.postOnCoreQueue { _ in
self.update()
}
corePublisher?.onChatRoomRead?.postOnCoreQueue(
receiveValue: { _ in
self.computeNotificationsCount()
})
corePublisher?.onMessagesReceived?.postOnCoreQueue(
receiveValue: { _ in
self.computeNotificationsCount()
})
corePublisher?.onCallStateChanged?.postOnCoreQueue(
receiveValue: { _ in
self.computeNotificationsCount()
})
}
func computeNotificationsCount() {
notificationsCount = account.unreadChatMessageCount + account.missedCallsCount
private func update() {
let state = account.state
var isDefault: Bool = false
if let defaultAccount = account.core?.defaultAccount {
isDefault = (defaultAccount == account)
}
DispatchQueue.main.async { [self] in
switch state {
case .Cleared, .None:
humanReadableRegistrationState = "drawer_menu_account_connection_status_cleared".localized()
registrationStateAssociatedUIColor = .orangeWarning600
case .Progress:
humanReadableRegistrationState = "drawer_menu_account_connection_status_progress".localized()
registrationStateAssociatedUIColor = .greenSuccess500
case .Failed:
humanReadableRegistrationState = "drawer_menu_account_connection_status_failed".localized()
registrationStateAssociatedUIColor = .redDanger500
case .Ok:
humanReadableRegistrationState = "drawer_menu_account_connection_status_connected".localized()
registrationStateAssociatedUIColor = .greenSuccess500
case .Refreshing:
humanReadableRegistrationState = "drawer_menu_account_connection_status_refreshing".localized()
registrationStateAssociatedUIColor = .grayMain2c500
}
isDefaultAccount = isDefault
}
}
private func computeNotificationsCount() {
let count = account.unreadChatMessageCount + account.missedCallsCount
DispatchQueue.main.async { [self] in
notificationsCount = count
}
}
func refreshRegiter() {

View file

@ -33,4 +33,8 @@ extension Account {
}
return address.asStringUriOnly()
}
static func == (lhs: Account, rhs: Account) -> Bool {
return lhs.params?.identityAddress?.asString() == rhs.params?.identityAddress?.asString()
}
}