feat(ControlNode): Show toast notifications when the control node state changes

1. Fix an issue where importing a community using private key triggers the import finished event without updating the community data if the community is already imported with public key
2. Show toast messages on importCommunity and privateKeyRemoved events
3. Group community import toast messages handlers and move them from ContactsColumnView to AppMain. IMO these toast messages handlers should not be dependent on ContactsColumnView.
This commit is contained in:
Alex Jbanca 2023-07-27 15:33:33 +03:00 committed by Alex Jbanca
parent c7aa1cf9e8
commit 90f4d60059
6 changed files with 101 additions and 49 deletions

View File

@ -330,7 +330,7 @@ method isCommunityRequestPending*(self: Module, communityId: string): bool =
self.controller.isCommunityRequestPending(communityId)
method communityImported*(self: Module, community: CommunityDto) =
self.view.addItem(self.getCommunityItem(community))
self.view.addOrUpdateItem(self.getCommunityItem(community))
self.view.emitImportingCommunityStateChangedSignal(community.id, ImportCommunityState.Imported.int, errorMsg = "")
method communityDataImported*(self: Module, community: CommunityDto) =

View File

@ -274,7 +274,17 @@ QtObject:
proc addItem*(self: View, item: SectionItem) =
self.model.addItem(item)
self.communityAdded(item.id)
proc updateItem(self: View, item: SectionItem) =
self.model.editItem(item)
self.communityChanged(item.id)
proc addOrUpdateItem*(self: View, item: SectionItem) =
if self.model.itemExists(item.id):
self.updateItem(item)
else:
self.addItem(item)
proc model*(self: View): SectionModel =
result = self.model
@ -484,6 +494,7 @@ QtObject:
"name": communityItem.name,
"image": communityItem.image,
"color": communityItem.color,
"isControlNode": communityItem.isControlNode,
}
return $jsonObj

View File

@ -204,7 +204,7 @@ QtObject:
of ModelRole.AmIBanned:
result = newQVariant(item.amIBanned)
proc isItemExist(self: SectionModel, id: string): bool =
proc itemExists*(self: SectionModel, id: string): bool =
for it in self.items:
if(it.id == id):
return true
@ -215,7 +215,7 @@ QtObject:
let parentModelIndex = newQModelIndex()
defer: parentModelIndex.delete
if not self.isItemExist(item.id):
if not self.itemExists(item.id):
self.beginInsertRows(parentModelIndex, self.items.len, self.items.len)
self.items.add(item)
self.endInsertRows()
@ -226,7 +226,7 @@ QtObject:
let parentModelIndex = newQModelIndex()
defer: parentModelIndex.delete
if not self.isItemExist(item.id):
if not self.itemExists(item.id):
self.beginInsertRows(parentModelIndex, index, index)
self.items.insert(item, index)
self.endInsertRows()

View File

@ -217,49 +217,4 @@ Item {
}
}
}
Connections {
target: root.store
function onImportingCommunityStateChanged(communityId, state, errorMsg) {
const community = root.store.getCommunityDetailsAsJson(communityId)
let title = ""
let subTitle = ""
let loading = false
switch (state)
{
case Constants.communityImported:
title = qsTr("'%1' community imported").arg(community.name);
break
case Constants.communityImportingInProgress:
title = qsTr("Importing community is in progress")
loading = true
break
case Constants.communityImportingError:
title = qsTr("Failed to import community '%1'").arg(community.name)
subTitle = errorMsg
break
default:
console.error("unknown state while importing community: %1").arg(state)
return
}
Global.displayToastMessage(title,
subTitle,
"",
loading,
Constants.ephemeralNotificationType.normal,
"")
}
function onCommunityInfoAlreadyRequested() {
Global.displayToastMessage(qsTr("Community data not loaded yet."),
qsTr("Please wait for the unfurl to show"),
"",
true,
Constants.ephemeralNotificationType.normal,
"")
}
}
}

View File

@ -57,6 +57,10 @@ QtObject {
property string communityTags: communitiesModuleInst.tags
signal importingCommunityStateChanged(string communityId, int state, string errorMsg)
signal communityPrivateKeyRemoved(string communityId)
signal communityInfoAlreadyRequested()
function createCommunity(args = {
name: "",
@ -143,6 +147,17 @@ QtObject {
root.communitiesModuleInst.resetDiscordImport(false)
}
function getCommunityDetailsAsJson(id) {
const jsonObj = communitiesModuleInst.getCommunityDetails(id)
try {
return JSON.parse(jsonObj)
}
catch (e) {
console.warn("error parsing community by id: ", id, " error: ", e.message)
return {}
}
}
function requestImportDiscordCommunity(args = {
name: "",
description: "",
@ -176,5 +191,13 @@ QtObject {
function onImportingCommunityStateChanged(communityId, state, errorMsg) {
root.importingCommunityStateChanged(communityId, state, errorMsg)
}
function onCommunityInfoAlreadyRequested() {
root.communityInfoAlreadyRequested()
}
function onCommunityPrivateKeyRemoved(communityId) {
root.communityPrivateKeyRemoved(communityId)
}
}
}

View File

@ -227,6 +227,69 @@ Item {
}
}
Connections {
target: appMain.communitiesStore
function onImportingCommunityStateChanged(communityId, state, errorMsg) {
const community = appMain.communitiesStore.getCommunityDetailsAsJson(communityId)
let title = ""
let subTitle = ""
let loading = false
let notificationType = Constants.ephemeralNotificationType.normal
let icon = ""
switch (state)
{
case Constants.communityImported:
if(community.isControlNode) {
title = qsTr("This device is now the control node for the %1 Community").arg(community.name)
notificationType = Constants.ephemeralNotificationType.success
icon = "checkmark-circle"
} else {
title = qsTr("'%1' community imported").arg(community.name)
}
break
case Constants.communityImportingInProgress:
title = qsTr("Importing community is in progress")
loading = true
break
case Constants.communityImportingError:
title = qsTr("Failed to import community '%1'").arg(community.name)
subTitle = errorMsg
break
default:
console.error("unknown state while importing community: %1").arg(state)
return
}
Global.displayToastMessage(title,
subTitle,
icon,
loading,
notificationType,
"")
}
function onCommunityInfoAlreadyRequested() {
Global.displayToastMessage(qsTr("Community data not loaded yet."),
qsTr("Please wait for the unfurl to show"),
"",
true,
Constants.ephemeralNotificationType.normal,
"")
}
function onCommunityPrivateKeyRemoved(communityId) {
const community = appMain.communitiesStore.getCommunityDetailsAsJson(communityId)
Global.displayToastMessage(qsTr("This device is no longer the control node for the %1 Community").arg(community.name),
"",
"info",
false,
Constants.ephemeralNotificationType.normal,
"")
}
}
Connections {
target: Global.applicationWindow