From f7d55bdc6001415d0450d40d057bf780fbdaa166 Mon Sep 17 00:00:00 2001 From: heopaka <94223526+dev-domo@users.noreply.github.com> Date: Fri, 11 Sep 2026 16:59:52 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20#519=20=EB=94=95=EC=85=94=EB=84=88?= =?UTF-8?q?=EB=A6=AC=EB=A5=BC=20=EC=A0=81=EC=9A=A9=ED=95=98=EC=97=AC=20?= =?UTF-8?q?=ED=80=98=EC=8A=A4=ED=8A=B8=20=EA=B2=80=EC=83=89=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../QuestCheckViewController.swift | 13 ++------ .../ProgressingQuestsViewModel.swift | 32 ++++++++++--------- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/ByeBoo-iOS/ByeBoo-iOS/Presentation/Feature/Quest/ViewController/QuestCheckViewController.swift b/ByeBoo-iOS/ByeBoo-iOS/Presentation/Feature/Quest/ViewController/QuestCheckViewController.swift index 872f7916..0b577004 100644 --- a/ByeBoo-iOS/ByeBoo-iOS/Presentation/Feature/Quest/ViewController/QuestCheckViewController.swift +++ b/ByeBoo-iOS/ByeBoo-iOS/Presentation/Feature/Quest/ViewController/QuestCheckViewController.swift @@ -185,8 +185,8 @@ extension QuestCheckViewController { } private func scrollToStep() { - guard let sectionIndex = findCurrentStepSectionIndex(), - let step = viewModel.getStep(section: sectionIndex) else { return } + let sectionIndex = viewModel.currentQuestIndexPath.section + guard let step = viewModel.getStep(section: sectionIndex) else { return } if isLastStep(stepNumber: step.stepNumber) { scrollToBottom() @@ -195,15 +195,6 @@ extension QuestCheckViewController { scrollToHeader(at: sectionIndex) } - private func findCurrentStepSectionIndex() -> Int? { - for (sectionIndex, step) in viewModel.steps.enumerated() { - if step.quests.contains(where: { $0.questNumber == viewModel.currentStep }) { - return sectionIndex - } - } - return nil - } - private func isLastStep(stepNumber: Int) -> Bool { stepNumber == QuestCheckViewController.lastStep } diff --git a/ByeBoo-iOS/ByeBoo-iOS/Presentation/Feature/Quest/ViewModel/ProgressingQuestsViewModel.swift b/ByeBoo-iOS/ByeBoo-iOS/Presentation/Feature/Quest/ViewModel/ProgressingQuestsViewModel.swift index eefb2b9a..52d5a33e 100644 --- a/ByeBoo-iOS/ByeBoo-iOS/Presentation/Feature/Quest/ViewModel/ProgressingQuestsViewModel.swift +++ b/ByeBoo-iOS/ByeBoo-iOS/Presentation/Feature/Quest/ViewModel/ProgressingQuestsViewModel.swift @@ -26,6 +26,7 @@ final class ProgressingQuestsViewModel { private(set) var questsEntity: ProgressingQuestsEntity? private var timeCancellabels: AnyCancellable? + private var questIndexMap: [Int: IndexPath] = [:] init( progressingQuestsUseCase: GetProgressingQuestsUseCase, @@ -70,7 +71,8 @@ final class ProgressingQuestsViewModel { do { let questsEntity = try await progressingQuestsUseCase.execute() self.questsEntity = questsEntity - self.setQuestTimer() + setQuestTimer() + buildQuestIndexMap(from: questsEntity) questsSubject.send(.success(questsEntity)) loadingSubject.send(false) } catch(let error as ByeBooError) { @@ -125,6 +127,16 @@ final class ProgressingQuestsViewModel { private func formatTime(_ hours: Int, _ minutes: Int) -> String { String(format: "%02d:%02d", hours, minutes) } + + private func buildQuestIndexMap(from quests: ProgressingQuestsEntity) { + questIndexMap = [:] + + for (sectionIndex, step) in quests.steps.enumerated() { + for (itemIndex, quest) in step.quests.enumerated() { + questIndexMap[quest.questNumber] = IndexPath(item: itemIndex, section: sectionIndex) + } + } + } } extension ProgressingQuestsViewModel { @@ -138,15 +150,7 @@ extension ProgressingQuestsViewModel { ) } var currentQuestIndexPath: IndexPath { - var indexPath = IndexPath() - - for (sectionIndex, step) in steps.enumerated() { - if let itemIndex = step.quests.firstIndex(where: { $0.questNumber == currentStep }) { - indexPath = IndexPath(item: itemIndex, section: sectionIndex) - break - } - } - return indexPath + questIndexMap[currentStep] ?? IndexPath() } func getStep(section: Int) -> StepEntity? { @@ -180,13 +184,11 @@ extension ProgressingQuestsViewModel { } func findQuest(questNumber: Int) -> QuestEntity? { - for (sectionIndex, step) in steps.enumerated() { - if let itemIndex = step.quests.firstIndex(where: { $0.questNumber == questNumber }) { - return getQuest(section: sectionIndex, item: itemIndex) - } + guard let indexPath = questIndexMap[questNumber] else { + return nil } - return nil + return getQuest(section: indexPath.section, item: indexPath.item) } }