Skip to content

Commit

Permalink
refact: #35 현재 station 찾는 로직 분리
Browse files Browse the repository at this point in the history
station 로직 분리 및 textField에 따라서 검색 결과 화면 전환하는 combine 로직 변경
  • Loading branch information
TaekH committed Oct 8, 2023
1 parent 7b2719f commit aadd141
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
7 changes: 7 additions & 0 deletions FREEWAY/FREEWAY/Global/Extension/StringExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ extension String {
dateFormatter.locale = Locale(identifier: "ko_KR")
return dateFormatter.date(from: self)
}

func removeStationSuffix() -> String {
if self.hasSuffix("") {
return String(self.dropLast())
}
return self
}
}
8 changes: 1 addition & 7 deletions FREEWAY/FREEWAY/Presentation/BaseView/BaseViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,13 @@ class BaseViewModel {
}
}

func getStationDTO() -> StationDTO? {
do {
let stationName = try inputText.value()
func getStationDTO(_ stationName: String) -> StationDTO? {
// StationName을 사용하여 StationDTO를 검색하고 반환
if let station = stationDatas.first(where: { $0.stationName == stationName }) {
self.currentStationData = station
return station
}
return nil // 찾을 수 없는 경우
} catch {
// BehaviorSubject에서 값을 가져오지 못한 경우
return nil
}
}

func getStationDTOWithId(id: Int) -> StationDTO? {
Expand Down
30 changes: 17 additions & 13 deletions FREEWAY/FREEWAY/Presentation/SearchView/SearchViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,32 +186,36 @@ extension SearchViewController: UITextFieldDelegate {
return true
}
}
guard textField.text!.count < 10 else { return false }
viewModel.inputTextPublisher.send(textField.text ?? "")
return true
let currentText = textField.text ?? ""
guard let stringRange = Range(range, in: currentText) else { return false }
let updatedText = currentText.replacingCharacters(in: stringRange, with: string)
return updatedText.count < 10
}

private func findStationDetailDTO(_ stationName: String) -> StationDTO? {
var stationName = stationName
if stationName.hasSuffix("") {
stationName = String(stationName.dropLast())
}
return viewModel.stationDatas.first { $0.stationName == stationName }
func textFieldDidChangeSelection(_ textField: UITextField) {
if textField.text?.isEmpty == true {
viewModel.inputTextPublisher.send("")
} else {
viewModel.inputTextPublisher.send(textField.text ?? "")
}
}


//TODO: 추후 AlertManager로 들어갈 부분
private func showInvalidStationNameAlert() {
let alert = UIAlertController(title: "역 이름을 다시 한 번 확인해주세요!", message: "", preferredStyle: .alert)
let okAction = UIAlertAction(title: "닫기", style: .cancel, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}

func navigateToMapsViewControllerIfNeeded(_ searchText: String) {
if let currentStation = findStationDetailDTO(searchText) {
func navigateToMapsViewControllerIfNeeded(_ inputText: String) {
let currentStationName = inputText.removeStationSuffix()
if let currentStation = viewModel.getStationDTO(currentStationName) {
viewModel.currentStationData = currentStation
viewModel.getCurrentStationDetailData(stationData: viewModel.currentStationData)
self.navigationController?.pushViewController(MapsViewController(viewModel: viewModel), animated: true)
} else {
}
else {
showInvalidStationNameAlert()
}
}
Expand Down

0 comments on commit aadd141

Please sign in to comment.