Skip to content

Commit

Permalink
fix(categories): fixes categories being always closed on start
Browse files Browse the repository at this point in the history
Part of #14886

The problem was that we converted `collapsed` to `opened` way too early and with a logic error.

By using collapsed until the module, it's way easier to understand and also fixes the logic mistakes.

I also renamed a couple of functions and variables so that the mistake doesn't happen again.
  • Loading branch information
jrainville committed May 24, 2024
1 parent bc580a1 commit 34c3d5e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/app/modules/main/chat_section/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -695,12 +695,12 @@ proc addNewChat(
if category.id == "":
error "No category found for chat", chatName=chatDto.name, categoryId=chatDto.categoryId
else:
categoryOpened = category.categoryOpened
categoryOpened = not category.collapsed
categoryPosition = category.position

# TODO: This call will update the model for each category in channels which is not
# preferable. Please fix-me in https://github.com/status-im/status-desktop/issues/14431
self.view.chatsModel().changeCategoryOpened(category.id, category.categoryOpened)
self.view.chatsModel().changeCategoryOpened(category.id, categoryOpened)

var canPostReactions = true
var hideIfPermissionsNotMet = false
Expand Down Expand Up @@ -1394,7 +1394,7 @@ method toggleCollapsedCommunityCategory*(self: Module, categoryId: string, colla
self.controller.toggleCollapsedCommunityCategory(categoryId, collapsed)

method onToggleCollapsedCommunityCategory*(self: Module, categoryId: string, collapsed: bool) =
self.view.chatsModel().changeCategoryOpened(categoryId, collapsed)
self.view.chatsModel().changeCategoryOpened(categoryId, not collapsed)

method reorderCommunityChat*(self: Module, categoryId: string, chatId: string, toPosition: int) =
self.controller.reorderCommunityChat(categoryId, chatId, toPosition + 1)
Expand Down
4 changes: 2 additions & 2 deletions src/app_service/service/chat/dto/chat.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Category* = object
id*: string
name*: string
position*: int
categoryOpened*: bool
collapsed*: bool

type
Permission* = object
Expand Down Expand Up @@ -189,7 +189,7 @@ proc toCategory*(jsonObj: JsonNode): Category =
discard jsonObj.getProp("id", result.id)
discard jsonObj.getProp("name", result.name)
discard jsonObj.getProp("position", result.position)
discard jsonObj.getProp("categoryOpened", result.categoryOpened)
discard jsonObj.getProp("collapsed", result.collapsed)

proc toChatMember*(jsonObj: JsonNode, memberId: string): ChatMember =
# Parse status-go "Member" type
Expand Down
15 changes: 9 additions & 6 deletions src/app_service/service/community/dto/community.nim
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,13 @@ proc toCommunityMembershipRequestDto*(jsonObj: JsonNode): CommunityMembershipReq
discard jsonObj.getProp("communityId", result.communityId)
discard jsonObj.getProp("our", result.our)

proc toCategoryDto*(jsonObj: JsonNode): Category =
proc toCollapsedCategoryDto*(jsonObj: JsonNode, isCollapsed: bool = false): Category =
result = Category()
discard jsonObj.getProp("categoryId", result.id)
discard jsonObj.getProp("collapsed", result.categoryOpened)
# The CollapsedCommunityCategories API only returns **collapsed** categories.
# So if a category is **not** collapsed, it's not in the list
# The collapsed property on the json is always false
result.collapsed = true

proc toCommunityDto*(jsonObj: JsonNode): CommunityDto =
result = CommunityDto()
Expand Down Expand Up @@ -499,16 +502,16 @@ proc toCommunitySettingsDto*(jsonObj: JsonNode): CommunitySettingsDto =
discard jsonObj.getProp("historyArchiveSupportEnabled", result.historyArchiveSupportEnabled)

proc parseCommunities*(response: JsonNode, categories: seq[Category]): seq[CommunityDto] =
var categoryMap = initTable[string, bool]()
var categoryCollapsedMap = initTable[string, bool]()
for category in categories:
categoryMap[category.id] = true
categoryCollapsedMap[category.id] = true

for communityNode in response["result"].getElems():
var community = communityNode.toCommunityDto()

for category in community.categories.mitems:
if categoryMap.hasKey(category.id):
category.categoryOpened = true
if categoryCollapsedMap.hasKey(category.id):
category.collapsed = true
result.add(community)

proc parseKnownCuratedCommunities(jsonCommunities: JsonNode): seq[CommunityDto] =
Expand Down
2 changes: 1 addition & 1 deletion src/app_service/service/community/service.nim
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ QtObject:
let collapsedCommunityCategories = responseObj["collapsedCommunityCategories"]
if collapsedCommunityCategories{"result"}.kind != JNull:
for jsonCategory in collapsedCommunityCategories["result"]:
categories.add(jsonCategory.toCategoryDto())
categories.add(jsonCategory.toCollapsedCategoryDto())

# All communities
let communities = parseCommunities(responseObj["communities"], categories)
Expand Down
6 changes: 4 additions & 2 deletions ui/StatusQ/src/StatusQ/Components/StatusChatList.qml
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ Item {
highlighted = true;
categoryPopupMenuSlot.item.popup()
} else if (mouse.button === Qt.LeftButton) {
root.toggleCollapsedCommunityCategory(model.categoryId, !statusChatListCategoryItem.opened)
// We pass the value for collapsed that we want
// So if opened == true, we want opened == false -> we pass collapsed = true
root.toggleCollapsedCommunityCategory(model.categoryId, statusChatListCategoryItem.opened)
}
}
onToggleButtonClicked: {
root.toggleCollapsedCommunityCategory(model.categoryId, !statusChatListCategoryItem.opened)
root.toggleCollapsedCommunityCategory(model.categoryId, statusChatListCategoryItem.opened)
}
onMenuButtonClicked: {
statusChatListCategoryItem.setupPopup()
Expand Down

0 comments on commit 34c3d5e

Please sign in to comment.