From cb7865bd9e0575eef2a52bb840f21ceac41fbef6 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Mon, 15 Nov 2021 10:15:21 -0500 Subject: [PATCH] refactor: move contact requests to new architecture Fixes #4061 Most of the contact request code was already moved, but it wasn't hooked to the QML yet and also there were missing events and some code to improve. --- .../main/profile_section/contacts/controller.nim | 10 +++++++--- .../profile_section/contacts/controller_interface.nim | 2 +- .../profile_section/contacts/models/contact_list.nim | 2 +- .../modules/main/profile_section/contacts/module.nim | 3 +++ src/app/modules/main/profile_section/contacts/view.nim | 3 +++ src/app_service/service/contacts/service.nim | 9 ++++++++- ui/app/AppLayouts/Chat/ChatLayout.qml | 9 ++++----- ui/app/AppLayouts/Chat/views/ChatColumnView.qml | 10 ++++------ 8 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/app/modules/main/profile_section/contacts/controller.nim b/src/app/modules/main/profile_section/contacts/controller.nim index d5b0f7247c..c7b727dfde 100644 --- a/src/app/modules/main/profile_section/contacts/controller.nim +++ b/src/app/modules/main/profile_section/contacts/controller.nim @@ -1,6 +1,7 @@ import ./controller_interface import io_interface -#import ../../../../core/signals/types + +import ../../../../core/signals/types import ../../../../../app_service/service/contacts/service as contacts_service import ../../../../../app_service/service/contacts/dto/contacts import ../../../../../app_service/service/accounts/service as accounts_service @@ -19,6 +20,9 @@ type contactsService: contacts_service.Service accountsService: accounts_service.ServiceInterface +# forward declaration: +method getContacts*[T](self: Controller[T], useCache: bool = true): seq[ContactsDto] + proc newController*[T](delegate: io_interface.AccessInterface, events: EventEmitter, contactsService: contacts_service.Service, @@ -91,8 +95,8 @@ method init*[T](self: Controller[T]) = # let contactDto = self.contactsService.getContactById(args.id) # self.delegate.onContactUpdated(contactDto) -method getContacts*[T](self: Controller[T]): seq[ContactsDto] = - return self.contactsService.getContacts() +method getContacts*[T](self: Controller[T], useCache: bool = true): seq[ContactsDto] = + return self.contactsService.getContacts(useCache) method getContact*[T](self: Controller[T], id: string): ContactsDto = return self.contactsService.getContactById(id) diff --git a/src/app/modules/main/profile_section/contacts/controller_interface.nim b/src/app/modules/main/profile_section/contacts/controller_interface.nim index 93c833d45a..ed321b70ab 100644 --- a/src/app/modules/main/profile_section/contacts/controller_interface.nim +++ b/src/app/modules/main/profile_section/contacts/controller_interface.nim @@ -12,7 +12,7 @@ method delete*(self: AccessInterface) {.base.} = method init*(self: AccessInterface) {.base.} = raise newException(ValueError, "No implementation available") -method getContacts*(self: AccessInterface): seq[ContactDto.ContactsDto] {.base.} = +method getContacts*(self: AccessInterface, useCache: bool = true): seq[ContactDto.ContactsDto] {.base.} = raise newException(ValueError, "No implementation available") method getContact*(self: AccessInterface, id: string): ContactDto.ContactsDto {.base.} = diff --git a/src/app/modules/main/profile_section/contacts/models/contact_list.nim b/src/app/modules/main/profile_section/contacts/models/contact_list.nim index 8519dc6250..bdc1f7730a 100644 --- a/src/app/modules/main/profile_section/contacts/models/contact_list.nim +++ b/src/app/modules/main/profile_section/contacts/models/contact_list.nim @@ -132,7 +132,7 @@ QtObject: self.endRemoveRows() self.countChanged() - proc hasAddedContacts(self: ContactList): bool {.slot.} = + proc hasAddedContacts(self: ContactList): bool {.slot.} = for c in self.contacts: if(c.isContact()): return true return false diff --git a/src/app/modules/main/profile_section/contacts/module.nim b/src/app/modules/main/profile_section/contacts/module.nim index 49df89cd48..5e27329812 100644 --- a/src/app/modules/main/profile_section/contacts/module.nim +++ b/src/app/modules/main/profile_section/contacts/module.nim @@ -42,6 +42,9 @@ method setContactList*[T](self: Module[T], contacts: seq[ContactsDto]) = method updateContactList*[T](self: Module[T], contacts: seq[ContactsDto]) = self.view.model().updateContactList(contacts) +method updateContactList*[T](self: Module[T], contacts: seq[ContactsDto]) = + self.view.updateContactList(contacts) + method load*[T](self: Module[T]) = self.controller.init() let contacts = self.controller.getContacts() diff --git a/src/app/modules/main/profile_section/contacts/view.nim b/src/app/modules/main/profile_section/contacts/view.nim index 58ba64168d..244f1a8e84 100644 --- a/src/app/modules/main/profile_section/contacts/view.nim +++ b/src/app/modules/main/profile_section/contacts/view.nim @@ -36,6 +36,9 @@ QtObject: proc model*(self: View): Model = return self.model + proc updateContactList*(self: View, contacts: seq[ContactsDto]) = + self.model.updateContactList(contacts) + proc modelChanged*(self: View) {.signal.} proc getModel*(self: View): QVariant {.slot.} = diff --git a/src/app_service/service/contacts/service.nim b/src/app_service/service/contacts/service.nim index 2a1fd89ea5..8a55bbcb5d 100644 --- a/src/app_service/service/contacts/service.nim +++ b/src/app_service/service/contacts/service.nim @@ -26,6 +26,10 @@ type ContactAddedArgs* = ref object of Args contact*: ContactsDto +type + ContactArgs* = ref object of Args + contact*: ContactsDto + ContactUpdatedArgs* = ref object of Args id*: string @@ -38,6 +42,7 @@ const SIGNAL_CONTACT_UNBLOCKED* = "new-contactUnblocked" const SIGNAL_CONTACT_REMOVED* = "new-contactRemoved" const SIGNAL_CONTACT_NICKNAME_CHANGED* = "new-contactNicknameChanged" + QtObject: type Service* = ref object of QObject threadpool: ThreadPool @@ -71,7 +76,9 @@ QtObject: proc init*(self: Service) = self.fetchContacts() - proc getContacts*(self: Service): seq[ContactsDto] = + proc getContacts*(self: Service, useCache: bool = true): seq[ContactsDto] = + if (not useCache): + self.fetchContacts() return toSeq(self.contacts.values) proc fetchContact(self: Service, id: string): ContactsDto = diff --git a/ui/app/AppLayouts/Chat/ChatLayout.qml b/ui/app/AppLayouts/Chat/ChatLayout.qml index 60336f9a51..4d943162ef 100644 --- a/ui/app/AppLayouts/Chat/ChatLayout.qml +++ b/ui/app/AppLayouts/Chat/ChatLayout.qml @@ -91,7 +91,7 @@ StatusAppThreePanelLayout { userList: chatColumn.userList messageContextMenu: quickActionMessageOptionsMenu profilePubKey: userProfile.pubKey - contactsList: root.rootStore.profileModelInst.contacts.list + contactsList: root.rootStore.allContacts isOnline: root.rootStore.chatsModelInst.isOnline } } @@ -144,10 +144,9 @@ StatusAppThreePanelLayout { //% "Are you sure you want to remove this contact?" confirmationText: qsTrId("are-you-sure-you-want-to-remove-this-contact-") onConfirmButtonClicked: { - // Not Refactored -// if (root.rootStore.profileModelInst.contacts.isAdded(chatColumn.contactToRemove)) { -// root.rootStore.profileModelInst.contacts.removeContact(chatColumn.contactToRemove) -// } + if (root.rootStore.contactsModuleInst.model.isAdded(chatColumn.contactToRemove)) { + root.rootStore.contactsModuleInst.model.removeContact(chatColumn.contactToRemove) + } removeContactConfirmationDialog.parentPopup.close(); removeContactConfirmationDialog.close(); } diff --git a/ui/app/AppLayouts/Chat/views/ChatColumnView.qml b/ui/app/AppLayouts/Chat/views/ChatColumnView.qml index 876a55789d..c08347ea2e 100644 --- a/ui/app/AppLayouts/Chat/views/ChatColumnView.qml +++ b/ui/app/AppLayouts/Chat/views/ChatColumnView.qml @@ -34,7 +34,7 @@ Item { property string contactToRemove: "" property string activeChatId: root.rootStore.chatsModelInst.channelView.activeChannel.id property bool isBlocked: root.rootStore.contactsModuleInst.model.isContactBlocked(activeChatId) - property bool isContact: root.rootStore.isContactAdded(activeChatId) + property bool isContact: root.rootStore.contactsModuleInst.model.isAdded(activeChatId) // property bool contactRequestReceived: root.rootStore.contactsModuleInst.model.contactRequestReceived(activeChatId) property string currentNotificationChatId property string currentNotificationCommunityId @@ -152,7 +152,7 @@ Item { chatInfoButton.subTitle: { switch (root.rootStore.chatsModelInst.channelView.activeChannel.chatType) { case Constants.chatTypeOneToOne: - return (root.isContact ? + return (root.rootStore.contactsModuleInst.model.isAdded(topBar.chatId) ? //% "Contact" qsTrId("chat-is-a-contact") : //% "Not a contact" @@ -402,10 +402,8 @@ Item { Layout.bottomMargin: Style.current.bigPadding isContact: root.isContact visible: root.rootStore.chatsModelInst.channelView.activeChannel.chatType === Constants.chatTypeOneToOne - && (!root.isContact /*|| !contactRequestReceived*/) - onAddContactClicked: { - root.rootStore.addContact(activeChatId); - } + && (!isContact /*|| !contactRequestReceived*/) + onAddContactClicked: root.rootStore.contactsModuleInst.addContact(activeChatId) } }