Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions BeforeGoing/Presentation/Common/Toast/ToastMessageType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enum ToastMessageType {
case todayMissionLimit
case missionLimit
case duplicateMission
case duplicateScenario

var message: String {
switch self {
Expand All @@ -21,6 +22,8 @@ enum ToastMessageType {
return "* 미션은 20개까지만 설정할 수 있어요"
case .duplicateMission:
return "* 중복된 이름의 미션은 설정할 수 없어요"
case .duplicateScenario:
return "* 중복된 이름의 시나리오는 설정할 수 없어요"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class ManageScenarioViewController: BaseViewController {

private var didCellTap: Bool = false
private var selectedIndex: Int?
private var scenarioNames: [String]?

init(viewModel: ManageScenarioViewModel) {
self.viewModel = viewModel
Expand Down Expand Up @@ -68,12 +69,23 @@ extension ManageScenarioViewController {
let viewController = ViewControllerFactory.shared.makeSettingScenarioViewController()

viewController.navigationItem.hidesBackButton = true
viewController.configure(scenarioType: scenarioType, enterType: .addScenario)
viewController.configure(
scenarioType: scenarioType,
enterType: .addScenario,
scenarioNames: scenarioNames
)

self.navigationController?.pushViewController(viewController, animated: false)
}
}

extension ManageScenarioViewController {

func configure(scenarioNames: [String]) {
self.scenarioNames = scenarioNames
}
}

extension ManageScenarioViewController: Backable {

func back() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ extension MyScenarioViewController: NetworkRequestable, NetworkRequestErrorHandl
viewController.do {
$0.navigationItem.hidesBackButton = true
$0.hidesBottomBarWhenPushed = true
$0.configure(scenarioNames: getScenariosViewModel.getScenarioNames())
}

self.navigationController?.pushViewController(viewController, animated: false)
}

Expand Down Expand Up @@ -149,6 +151,7 @@ extension MyScenarioViewController: NetworkRequestable, NetworkRequestErrorHandl
$0.hidesBottomBarWhenPushed = true
$0.configure(
scenarioID: scenario.scenarioID,
scenarioNames: getScenariosViewModel.getScenarioNames(),
scenarioName: scenario.scenarioName,
memo: scenario.memo,
missions: scenario.basicMissions.map { (missionID: $0.missionId, content: $0.content) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ final class SettingScenarioViewController: BaseViewController {

private let missionLimit = 20
private var scenarioID: Int?
private var originalScenarioName: String?
private var enterType: SettingScenarioEnterType?
private var scenarioNames: [String]?
private var isNotificationActive: Bool?
private var daysOfWeek: [Int]?
private var startHour: Int?
Expand All @@ -31,7 +33,7 @@ final class SettingScenarioViewController: BaseViewController {
self.updateScenarioViewModel = updateScnearioViewModel
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Expand Down Expand Up @@ -119,9 +121,12 @@ extension SettingScenarioViewController {

func configure(
scenarioType: ScenarioType,
enterType: SettingScenarioEnterType
enterType: SettingScenarioEnterType,
scenarioNames: [String]?
) {
self.enterType = enterType
self.scenarioNames = scenarioNames

addScenarioViewModel.setMissions(missions: scenarioType.basicMissions)
rootView.settingMissionView.updateMissionCount(addScenarioViewModel.missionsCount)

Expand All @@ -145,6 +150,7 @@ extension SettingScenarioViewController {

func configure(
scenarioID: Int,
scenarioNames: [String]?,
scenarioName: String,
memo: String,
missions: [(missionID: Int, content: String)],
Expand All @@ -156,13 +162,15 @@ extension SettingScenarioViewController {
enterType: SettingScenarioEnterType
) {
self.scenarioID = scenarioID
self.scenarioNames = scenarioNames
self.originalScenarioName = scenarioName
self.isNotificationActive = isNotificationActive
self.daysOfWeek = daysOfWeek
self.startHour = startHour
self.startMinute = startMinute
self.notificationMethod = notificationMethod
self.enterType = enterType

addScenarioViewModel.setMissions(missions: missions)

rootView.inputScenarioView.do {
Expand Down Expand Up @@ -191,19 +199,6 @@ extension SettingScenarioViewController: ToastPresentable {
bindTextField(view: rootView.inputMemoView)
}

private func bindTextField(view: InputInformationView) {
guard let text = view.textField.text else { return }

DispatchQueue.main.async { [weak self] in
guard self != nil else { return }

text.isBlank ? view.hideDeleteButton() : view.revealDeleteButton()
let trimmedText = view.trimText(text)
view.updateTextCount(trimmedText.count)
}
checkNextButtonState()
}

@objc
private func scenarioDeleteButtonDidTap() {
rootView.inputScenarioView.deleteAllText()
Expand Down Expand Up @@ -271,7 +266,24 @@ extension SettingScenarioViewController: ToastPresentable {
addScenario(scenarioName: scenarioName, memo: memo)
return
}
updateScenario(scenarioName: scenarioName, memo: memo)
updateScenario(
origianlScenarioName: originalScenarioName,
scenarioName: scenarioName,
memo: memo
)
}

private func bindTextField(view: InputInformationView) {
guard let text = view.textField.text else { return }

DispatchQueue.main.async { [weak self] in
guard self != nil else { return }

text.isBlank ? view.hideDeleteButton() : view.revealDeleteButton()
let trimmedText = view.trimText(text)
view.updateTextCount(trimmedText.count)
}
checkNextButtonState()
}

private func checkNextButtonState() {
Expand All @@ -285,9 +297,15 @@ extension SettingScenarioViewController: ToastPresentable {
private func addScenario(scenarioName: String, memo: String) {
let scenarioName = scenarioName.removeTrailingSpaces()
let memo = memo.removeTrailingSpaces()

Task {
do {
guard let scenarioNames,
!scenarioNames.contains(scenarioName) else {
self.presentToastMessage(type: .duplicateScenario)
return
}

let _ = try await addScenarioViewModel.action(
input: .nextButtonInSetScenarioDidTap(
scenarioName: scenarioName,
Expand All @@ -302,14 +320,26 @@ extension SettingScenarioViewController: ToastPresentable {
}
}

private func updateScenario(scenarioName: String, memo: String) {
private func updateScenario(
origianlScenarioName: String?,
scenarioName: String,
memo: String
) {
guard let scenarioID = scenarioID else { return }

let scenarioName = scenarioName.removeTrailingSpaces()
let memo = memo.removeTrailingSpaces()

Task {
do {
if canUpdateName(
origianlScenarioName: originalScenarioName,
scenarioName: scenarioName
) {
self.presentToastMessage(type: .duplicateScenario)
return
}

let _ = try await updateScenarioViewModel.action(
input: .nextButtonInSetScenarioDidTap(
scenarioID: scenarioID,
Expand Down Expand Up @@ -338,6 +368,19 @@ extension SettingScenarioViewController: ToastPresentable {
)
self.navigationController?.pushViewController(viewController, animated: false)
}

private func canUpdateName(
origianlScenarioName: String?,
scenarioName: String
) -> Bool {
guard let originalScenarioName,
let scenarioNames else {
return false
}

let canUpdate = origianlScenarioName != scenarioName && scenarioNames.contains(scenarioName)
return canUpdate
}
}

extension SettingScenarioViewController: UITableViewDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ extension GetScenariosViewModel {
scenariosModel.scenarios[section].scenarioName
}

func getScenarioNames() -> [String] {
scenariosModel.scenarios.map { $0.scenarioName }
}

func getNotificationInformation(section: Int) -> String? {
scenariosModel.getNotificationInformation(section: section)
}
Expand Down
Loading