fix(main): create loading item for activeSection on start

Fixes #9508

The issue was caused by some calls that try to update the activeSection's item, but during chat loading, the activeSection had the possibility of being empty, thus causing a crash.
The fix here is to create a LoadingItem and set it as active during the time the chats load (if the last active section was a community or chat). Then, the updates go to that Item and do nothing, but that's normal.
This commit is contained in:
Jonathan Rainville 2023-02-09 11:27:11 -05:00 committed by osmaczko
parent 8cc80694e2
commit 3b9b514191
5 changed files with 110 additions and 71 deletions

View File

@ -1,6 +1,8 @@
const CHAT_SECTION_NAME* = "Messages" const CHAT_SECTION_NAME* = "Messages"
const CHAT_SECTION_ICON* = "chat" const CHAT_SECTION_ICON* = "chat"
const LOADING_SECTION_ID* = "loadingSection"
const COMMUNITIESPORTAL_SECTION_ID* = "communitiesPortal" const COMMUNITIESPORTAL_SECTION_ID* = "communitiesPortal"
const COMMUNITIESPORTAL_SECTION_NAME* = "Communities Portal" const COMMUNITIESPORTAL_SECTION_NAME* = "Communities Portal"
const COMMUNITIESPORTAL_SECTION_ICON* = "communities" const COMMUNITIESPORTAL_SECTION_ICON* = "communities"

View File

@ -46,7 +46,7 @@ type
authenticateUserFlowRequestedBy: string authenticateUserFlowRequestedBy: string
# Forward declaration # Forward declaration
proc setActiveSection*(self: Controller, sectionId: string) proc setActiveSection*(self: Controller, sectionId: string, skipSavingInSettings: bool = false)
proc newController*(delegate: io_interface.AccessInterface, proc newController*(delegate: io_interface.AccessInterface,
events: EventEmitter, events: EventEmitter,
@ -320,10 +320,11 @@ proc getChannelGroups*(self: Controller): seq[ChannelGroupDto] =
proc getActiveSectionId*(self: Controller): string = proc getActiveSectionId*(self: Controller): string =
result = self.activeSectionId result = self.activeSectionId
proc setActiveSection*(self: Controller, sectionId: string) = proc setActiveSection*(self: Controller, sectionId: string, skipSavingInSettings: bool = false) =
self.activeSectionId = sectionId self.activeSectionId = sectionId
let sectionIdToSave = if (sectionId == conf.SETTINGS_SECTION_ID): "" else: sectionId if not skipSavingInSettings:
singletonInstance.localAccountSensitiveSettings.setActiveSection(sectionIdToSave) let sectionIdToSave = if (sectionId == conf.SETTINGS_SECTION_ID): "" else: sectionId
singletonInstance.localAccountSensitiveSettings.setActiveSection(sectionIdToSave)
self.delegate.activeSectionSet(self.activeSectionId) self.delegate.activeSectionSet(self.activeSectionId)
proc getNumOfNotificaitonsForChat*(self: Controller): tuple[unviewed:int, mentions:int] = proc getNumOfNotificaitonsForChat*(self: Controller): tuple[unviewed:int, mentions:int] =

View File

@ -177,7 +177,7 @@ method onNetworkDisconnected*(self: AccessInterface) {.base.} =
method viewDidLoad*(self: AccessInterface) {.base.} = method viewDidLoad*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method setActiveSection*(self: AccessInterface, item: SectionItem) {.base.} = method setActiveSection*(self: AccessInterface, item: SectionItem, skipSavingInSettings: bool = false) {.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method setActiveSectionById*(self: AccessInterface, id: string) {.base.} = method setActiveSectionById*(self: AccessInterface, id: string) {.base.} =

View File

@ -360,92 +360,109 @@ method load*[T](
activeSectionId = singletonInstance.userProfile.getPubKey() activeSectionId = singletonInstance.userProfile.getPubKey()
# Communities Portal Section # Communities Portal Section
let communitiesPortalSectionItem = initItem(conf.COMMUNITIESPORTAL_SECTION_ID, SectionType.CommunitiesPortal, conf.COMMUNITIESPORTAL_SECTION_NAME, let communitiesPortalSectionItem = initItem(
amISectionAdmin = false, conf.COMMUNITIESPORTAL_SECTION_ID,
description = "", SectionType.CommunitiesPortal,
image = "", conf.COMMUNITIESPORTAL_SECTION_NAME,
icon = conf.COMMUNITIESPORTAL_SECTION_ICON, amISectionAdmin = false,
color = "", description = "",
hasNotification = false, image = "",
notificationsCount = 0, icon = conf.COMMUNITIESPORTAL_SECTION_ICON,
active = false, color = "",
enabled = true) hasNotification = false,
notificationsCount = 0,
active = false,
enabled = true,
)
self.view.model().addItem(communitiesPortalSectionItem) self.view.model().addItem(communitiesPortalSectionItem)
if(activeSectionId == communitiesPortalSectionItem.id): if(activeSectionId == communitiesPortalSectionItem.id):
activeSection = communitiesPortalSectionItem activeSection = communitiesPortalSectionItem
# Wallet Section # Wallet Section
let walletSectionItem = initItem(conf.WALLET_SECTION_ID, SectionType.Wallet, conf.WALLET_SECTION_NAME, let walletSectionItem = initItem(
amISectionAdmin = false, conf.WALLET_SECTION_ID,
description = "", SectionType.Wallet,
introMessage = "", conf.WALLET_SECTION_NAME,
outroMessage = "", amISectionAdmin = false,
image = "", description = "",
icon = conf.WALLET_SECTION_ICON, introMessage = "",
color = "", outroMessage = "",
hasNotification = false, image = "",
notificationsCount = 0, icon = conf.WALLET_SECTION_ICON,
active = false, color = "",
enabled = singletonInstance.localAccountSensitiveSettings.getIsWalletEnabled()) hasNotification = false,
notificationsCount = 0,
active = false,
enabled = singletonInstance.localAccountSensitiveSettings.getIsWalletEnabled(),
)
self.view.model().addItem(walletSectionItem) self.view.model().addItem(walletSectionItem)
if(activeSectionId == walletSectionItem.id): if(activeSectionId == walletSectionItem.id):
activeSection = walletSectionItem activeSection = walletSectionItem
# Browser Section # Browser Section
let browserSectionItem = initItem(conf.BROWSER_SECTION_ID, SectionType.Browser, conf.BROWSER_SECTION_NAME, let browserSectionItem = initItem(
amISectionAdmin = false, conf.BROWSER_SECTION_ID,
description = "", SectionType.Browser,
introMessage = "", conf.BROWSER_SECTION_NAME,
outroMessage = "", amISectionAdmin = false,
image = "", description = "",
icon = conf.BROWSER_SECTION_ICON, introMessage = "",
color = "", outroMessage = "",
hasNotification = false, image = "",
notificationsCount = 0, icon = conf.BROWSER_SECTION_ICON,
active = false, color = "",
enabled = singletonInstance.localAccountSensitiveSettings.getIsBrowserEnabled()) hasNotification = false,
notificationsCount = 0,
active = false,
enabled = singletonInstance.localAccountSensitiveSettings.getIsBrowserEnabled(),
)
self.view.model().addItem(browserSectionItem) self.view.model().addItem(browserSectionItem)
if(activeSectionId == browserSectionItem.id): if(activeSectionId == browserSectionItem.id):
activeSection = browserSectionItem activeSection = browserSectionItem
# Node Management Section # Node Management Section
let nodeManagementSectionItem = initItem(conf.NODEMANAGEMENT_SECTION_ID, SectionType.NodeManagement, let nodeManagementSectionItem = initItem(
conf.NODEMANAGEMENT_SECTION_NAME, conf.NODEMANAGEMENT_SECTION_ID,
amISectionAdmin = false, SectionType.NodeManagement,
description = "", conf.NODEMANAGEMENT_SECTION_NAME,
introMessage = "", amISectionAdmin = false,
outroMessage = "", description = "",
image = "", introMessage = "",
icon = conf.NODEMANAGEMENT_SECTION_ICON, outroMessage = "",
color = "", image = "",
hasNotification = false, icon = conf.NODEMANAGEMENT_SECTION_ICON,
notificationsCount = 0, color = "",
active = false, hasNotification = false,
enabled = singletonInstance.localAccountSensitiveSettings.getNodeManagementEnabled()) notificationsCount = 0,
active = false,
enabled = singletonInstance.localAccountSensitiveSettings.getNodeManagementEnabled(),
)
self.view.model().addItem(nodeManagementSectionItem) self.view.model().addItem(nodeManagementSectionItem)
if(activeSectionId == nodeManagementSectionItem.id): if(activeSectionId == nodeManagementSectionItem.id):
activeSection = nodeManagementSectionItem activeSection = nodeManagementSectionItem
# Profile Section # Profile Section
let profileSettingsSectionItem = initItem(conf.SETTINGS_SECTION_ID, SectionType.ProfileSettings, let profileSettingsSectionItem = initItem(
conf.SETTINGS_SECTION_NAME, conf.SETTINGS_SECTION_ID,
amISectionAdmin = false, SectionType.ProfileSettings,
description = "", conf.SETTINGS_SECTION_NAME,
introMessage = "", amISectionAdmin = false,
outroMessage = "", description = "",
image = "", introMessage = "",
icon = conf.SETTINGS_SECTION_ICON, outroMessage = "",
color = "", image = "",
hasNotification = self.calculateProfileSectionHasNotification(), icon = conf.SETTINGS_SECTION_ICON,
notificationsCount = 0, color = "",
active = false, hasNotification = self.calculateProfileSectionHasNotification(),
enabled = true) notificationsCount = 0,
active = false,
enabled = true,
)
self.view.model().addItem(profileSettingsSectionItem) self.view.model().addItem(profileSettingsSectionItem)
if(activeSectionId == profileSettingsSectionItem.id): if(activeSectionId == profileSettingsSectionItem.id):
activeSection = profileSettingsSectionItem activeSection = profileSettingsSectionItem
self.browserSectionModule.load() self.browserSectionModule.load()
# self.nodeManagementSectionModule.load()
self.profileSectionModule.load() self.profileSectionModule.load()
self.stickersModule.load() self.stickersModule.load()
self.networksModule.load() self.networksModule.load()
@ -455,12 +472,27 @@ method load*[T](
self.nodeSectionModule.load() self.nodeSectionModule.load()
# Load wallet last as it triggers events that are listened by other modules # Load wallet last as it triggers events that are listened by other modules
self.walletSectionModule.load() self.walletSectionModule.load()
#self.communitiesPortalSectionModule.load()
# Set active section on app start # Set active section on app start
# If section is profile then open chat by default # If section is empty or profile then open the loading section until chats are loaded
if activeSection.isEmpty() or activeSection.sectionType == SectionType.ProfileSettings: if activeSection.isEmpty() or activeSection.sectionType == SectionType.ProfileSettings:
self.setActiveSection(self.view.model().getItemBySectionType(SectionType.Chat)) # Set bogus Item as active until the chat is loaded
let loadingItem = initItem(
LOADING_SECTION_ID,
SectionType.LoadingSection,
name = "",
amISectionAdmin = false,
description = "",
image = "",
icon = "",
color = "",
hasNotification = false,
notificationsCount = 0,
active = false,
enabled = true,
)
self.view.model().addItem(loadingItem)
self.setActiveSection(loadingItem, skipSavingInSettings = true)
else: else:
self.setActiveSection(activeSection) self.setActiveSection(activeSection)
@ -509,6 +541,9 @@ method onChatsLoaded*[T](
if not activeSection.isEmpty(): if not activeSection.isEmpty():
self.setActiveSection(activeSection) self.setActiveSection(activeSection)
# Remove old loading section
self.view.model().removeItem(LOADING_SECTION_ID)
self.view.chatsLoaded() self.view.chatsLoaded()
method onChatsLoadingFailed*[T](self: Module[T]) = method onChatsLoadingFailed*[T](self: Module[T]) =
@ -606,11 +641,11 @@ method setCommunityIdToSpectate*[T](self: Module[T], communityId: string) =
method getActiveSectionId*[T](self: Module[T]): string = method getActiveSectionId*[T](self: Module[T]): string =
return self.controller.getActiveSectionId() return self.controller.getActiveSectionId()
method setActiveSection*[T](self: Module[T], item: SectionItem) = method setActiveSection*[T](self: Module[T], item: SectionItem, skipSavingInSettings: bool = false) =
if(item.isEmpty()): if(item.isEmpty()):
echo "section is empty and cannot be made as active one" echo "section is empty and cannot be made as active one"
return return
self.controller.setActiveSection(item.id) self.controller.setActiveSection(item.id, skipSavingInSettings)
method setActiveSectionById*[T](self: Module[T], id: string) = method setActiveSectionById*[T](self: Module[T], id: string) =
let item = self.view.model().getItemById(id) let item = self.view.model().getItemById(id)

View File

@ -7,6 +7,7 @@ import ../../../app_service/common/types
type type
SectionType* {.pure.} = enum SectionType* {.pure.} = enum
LoadingSection = -1
Chat = 0 Chat = 0
Community, Community,
Wallet, Wallet,