From bea2f85186d1833adbf406dbda4bd803874e5ec8 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Date: Wed, 26 Jul 2023 13:58:23 +0500 Subject: [PATCH] refactor: refactor child block completion --- Source/CourseOutlineTableSource.swift | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Source/CourseOutlineTableSource.swift b/Source/CourseOutlineTableSource.swift index 5c4fe4eb0..c960264e8 100644 --- a/Source/CourseOutlineTableSource.swift +++ b/Source/CourseOutlineTableSource.swift @@ -769,19 +769,22 @@ extension CourseOutlineTableController: BlockCompletionDelegate { if let index = groups.firstIndex(where: { $0.block.blockID == blockGroup.block.blockID }) { groups[index] = blockGroup + collapsedSections.removeAll() + hasAddedToCollapsedSections = false + setGroups(groups) } else { - if let index = groups.firstIndex(where: { $0.block.blockID == blockGroup.block.blockID }), - let child = courseQuerier.childrenOfBlockWithID(blockID: groups[index].block.blockID, forMode: .full).value, - let indexOfBlock = child.children.firstIndex(where: { $0.blockID == blockGroup.block.blockID }) { - var updatedChild = child - updatedChild.children[indexOfBlock] = blockGroup.block - groups[index] = updatedChild + for (index, group) in groups.enumerated() { + guard var child = courseQuerier.childrenOfBlockWithID(blockID: group.block.blockID, forMode: .full).value else { continue } + if let indexOfBlock = child.children.firstIndex(where: { $0.blockID == blockGroup.block.blockID }) { + child.children[indexOfBlock] = blockGroup.block + groups[index] = child + collapsedSections.removeAll() + hasAddedToCollapsedSections = false + setGroups(groups) + break + } } } - - collapsedSections.removeAll() - hasAddedToCollapsedSections = false - setGroups(groups) } }