Skip to content

Commit

Permalink
[#5] 불필요한 Observing 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
ezidayzi committed Sep 24, 2021
1 parent 2320632 commit c83e9b7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
20 changes: 11 additions & 9 deletions CheoMooRac/CheoMooRac/Sources/ViewModels/MainViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ protocol MainViewModelInput {

protocol MainViewModelOutput {
var list: Dynamic<[Person]> {get}
var sectionHeaderList: Dynamic<[String]> {get}
var nowRefreshing: Dynamic<Bool> {get}
var isFiltering: Dynamic<Bool> {get}
func getSectionArray(at section: Int) -> Dynamic<[String]>

var sectionHeaderList: [String] {get}
func getSectionArray(at section: Int) -> [String]
}

protocol MainViewModelProtocol : MainViewModelInput, MainViewModelOutput {}
Expand All @@ -38,7 +39,7 @@ class MainViewModel: MainViewModelProtocol {
isFiltering.value = false
setTableView()
} else {
self.sectionHeaderList.value.removeAll()
self.sectionHeaderList.removeAll()
self.list.value.removeAll()

isFiltering.value = true
Expand All @@ -53,7 +54,7 @@ class MainViewModel: MainViewModelProtocol {
filterdHeaderList$ = Array(Set(filterdHeaderList$)).sorted()
filteredData = filteredArr
list.value = filteredData
sectionHeaderList.value = filterdHeaderList$
sectionHeaderList = filterdHeaderList$
}
}

Expand All @@ -65,15 +66,16 @@ class MainViewModel: MainViewModelProtocol {

// MARK: - OUTPUT
let list: Dynamic<[Person]> = Dynamic([])
let sectionHeaderList: Dynamic<[String]> = Dynamic([])
let nowRefreshing: Dynamic<Bool> = Dynamic(false)
let isFiltering: Dynamic<Bool> = Dynamic(false)


func getSectionArray(at section: Int) -> Dynamic<[String]> {
return Dynamic(sectionArray(at: section, data: isFiltering.value ? self.filteredData : self.data))
func getSectionArray(at section: Int) -> [String] {
return sectionArray(at: section, data: isFiltering.value ? self.filteredData : self.data)
}

var sectionHeaderList: [String] = []


init() {
setTableView()
Expand Down Expand Up @@ -110,12 +112,12 @@ class MainViewModel: MainViewModelProtocol {
self.list.value.forEach { person in
sectionHeaderList$.append(StringManager.shared.chosungCheck(word: person.familyName + person.firstName))
}
self.sectionHeaderList.value = Array(Set(sectionHeaderList$)).sorted()
self.sectionHeaderList = Array(Set(sectionHeaderList$)).sorted()
}

private func sectionArray(at section: Int, data: [Person]) -> [String] {
let list = data.filter {
return StringManager.shared.chosungCheck(word: $0.familyName + $0.firstName) == sectionHeaderList.value[section-1]
return StringManager.shared.chosungCheck(word: $0.familyName + $0.firstName) == sectionHeaderList[section-1]
}.map { person in
return person.familyName + person.firstName
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ extension MainTableViewController {

extension MainTableViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return viewModel.sectionHeaderList.value.count + 1
return viewModel.sectionHeaderList.count + 1
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
Expand All @@ -94,7 +94,7 @@ extension MainTableViewController {
guard let isFiltering = isFiltering else {return 1}
return isFiltering ? 0 : 1
default:
return viewModel.getSectionArray(at: section).value.count
return viewModel.getSectionArray(at: section).count
}
}

Expand All @@ -104,9 +104,9 @@ extension MainTableViewController {
let cell = tableView.dequeueReusableCell(indexPath: indexPath) as MyCardTableViewCell
return cell

case 1...viewModel.sectionHeaderList.value.count:
case 1...viewModel.sectionHeaderList.count:
let cell = UITableViewCell()
cell.textLabel?.text = viewModel.getSectionArray(at: indexPath.section).value[indexPath.row]
cell.textLabel?.text = viewModel.getSectionArray(at: indexPath.section)[indexPath.row]
return cell

default:
Expand All @@ -116,14 +116,14 @@ extension MainTableViewController {

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section != 0 {
return viewModel.sectionHeaderList.value[section - 1]
return viewModel.sectionHeaderList[section - 1]
} else {
return nil
}
}

override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return viewModel.sectionHeaderList.value
return viewModel.sectionHeaderList
}
}

Expand Down

0 comments on commit c83e9b7

Please sign in to comment.