Skip to content

Commit

Permalink
[#4] input, output 구분
Browse files Browse the repository at this point in the history
  • Loading branch information
ezidayzi committed Sep 24, 2021
1 parent b702f6e commit bf74d1b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
31 changes: 15 additions & 16 deletions CheoMooRac/CheoMooRac/Sources/ViewModels/MainViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@ protocol MainViewModelInput {
func refreshTableView()
func searchResults(text: String)
func cancelSearch()
func itemDidSelect(at: Int)
}

protocol MainViewModelOutput {
var list: Dynamic<[Person]> {get}
var items: Dynamic<[Person]> {get}
var nowRefreshing: Dynamic<Bool> {get}
var isFiltering: Dynamic<Bool> {get}

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

protocol MainViewModelProtocol : MainViewModelInput, MainViewModelOutput {}
protocol MainViewModelProtocol : MainViewModelInput, MainViewModelOutput {
var input : MainViewModelInput { get }
var output : MainViewModelOutput { get }
}

class MainViewModel: MainViewModelProtocol {
var input: MainViewModelInput { return self }
var output: MainViewModelOutput { return self }

// MARK: - INPUT
func refreshTableView() {
if !nowRefreshing.value {
Expand All @@ -41,7 +46,7 @@ class MainViewModel: MainViewModelProtocol {
setTableView()
} else {
self.sectionHeaderList.removeAll()
self.list.value.removeAll()
self.items.value.removeAll()

isFiltering.value = true

Expand All @@ -54,7 +59,7 @@ class MainViewModel: MainViewModelProtocol {

filterdHeaderList$ = Array(Set(filterdHeaderList$)).sorted()
filteredData = filteredArr
list.value = filteredData
items.value = filteredData
sectionHeaderList = filterdHeaderList$
}
}
Expand All @@ -63,23 +68,17 @@ class MainViewModel: MainViewModelProtocol {
setTableView()
isFiltering.value = false
}

func itemDidSelect(at: Int) {

}


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


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

func getSectionArrayPerson(at section: Int) -> [Person] {
func getSectionPersonArray(at section: Int) -> [Person] {
return sectionArrayPerson(at: section, data: isFiltering.value ? self.filteredData : self.data)
}

Expand Down Expand Up @@ -116,9 +115,9 @@ class MainViewModel: MainViewModelProtocol {
]

private func setTableView() {
self.list.value = data
self.items.value = data
var sectionHeaderList$: [String] = []
self.list.value.forEach { person in
self.items.value.forEach { person in
sectionHeaderList$.append(StringManager.shared.chosungCheck(word: person.familyName + person.firstName))
}
self.sectionHeaderList = Array(Set(sectionHeaderList$)).sorted()
Expand Down
24 changes: 12 additions & 12 deletions CheoMooRac/CheoMooRac/Sources/Views/MainTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ class MainTableViewController: UITableViewController {
}

private func bindViewModel() {
viewModel.list.bind { [weak self] _ in
viewModel.output.items.bind { [weak self] _ in
guard let self = self else {return}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}

viewModel.isFiltering.bind { [weak self] _ in
viewModel.output.isFiltering.bind { [weak self] _ in
guard let self = self else {return}
self.isFiltering = self.viewModel.isFiltering.value
}


viewModel.nowRefreshing.bind { [weak self] _ in
viewModel.output.nowRefreshing.bind { [weak self] _ in
guard let self = self else {return}
self.refreshControl?.endRefreshing()
}
Expand All @@ -63,7 +63,7 @@ class MainTableViewController: UITableViewController {
extension MainTableViewController {
@objc
private func tableViewDidPulled(refresh: UIRefreshControl) {
viewModel.refreshTableView()
viewModel.input.refreshTableView()
}
}

Expand All @@ -72,7 +72,7 @@ extension MainTableViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)

let person = viewModel.getSectionArrayPerson(at: indexPath.section)[indexPath.row]
let person = viewModel.output.getSectionPersonArray(at: indexPath.section)[indexPath.row]

let contactDetailViewModel = ContactDetailViewModel(person: person)
navigationController?.pushViewController(ContactDetailViewController(with: contactDetailViewModel), animated: true)
Expand All @@ -91,7 +91,7 @@ extension MainTableViewController {
// MARK: - Data Source
extension MainTableViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return viewModel.sectionHeaderList.count + 1
return viewModel.output.sectionHeaderList.count + 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
Expand All @@ -100,7 +100,7 @@ extension MainTableViewController {
guard let isFiltering = isFiltering else {return 1}
return isFiltering ? 0 : 1
default:
return viewModel.getSectionArrayPerson(at: section).count
return viewModel.output.getSectionPersonArray(at: section).count
}
}

Expand All @@ -112,7 +112,7 @@ extension MainTableViewController {

case 1...viewModel.sectionHeaderList.count:
let cell = UITableViewCell()
let person = viewModel.getSectionArrayPerson(at: indexPath.section)[indexPath.row]
let person = viewModel.output.getSectionPersonArray(at: indexPath.section)[indexPath.row]
cell.textLabel?.text = person.familyName + person.firstName
return cell

Expand All @@ -123,26 +123,26 @@ extension MainTableViewController {

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

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

extension MainTableViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
guard let text = searchController.searchBar.text else { return }
viewModel.searchResults(text: text)
viewModel.input.searchResults(text: text)
}
}

extension MainTableViewController: UISearchBarDelegate {
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
viewModel.cancelSearch()
viewModel.input.cancelSearch()
}
}

0 comments on commit bf74d1b

Please sign in to comment.