Skip to content

Commit

Permalink
[#4] ContactDetailViewController MVVM 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
ezidayzi committed Sep 24, 2021
1 parent c83e9b7 commit b702f6e
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 119 deletions.
16 changes: 12 additions & 4 deletions CheoMooRac/CheoMooRac.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
DD8A91E226FD7AFB004DAF3B /* Dynamic.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD8A91E126FD7AFB004DAF3B /* Dynamic.swift */; };
DD8A91E526FD7B6C004DAF3B /* MainTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD8A91E426FD7B6C004DAF3B /* MainTableViewController.swift */; };
DD8A91E726FD94FA004DAF3B /* UIViewController+embed.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD8A91E626FD94FA004DAF3B /* UIViewController+embed.swift */; };
DD8A91EA26FDC6AB004DAF3B /* ContactDetailTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD8A91E926FDC6AB004DAF3B /* ContactDetailTableViewController.swift */; };
DD8A91ED26FDCA4E004DAF3B /* ContactDetailViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD8A91EC26FDCA4E004DAF3B /* ContactDetailViewModel.swift */; };
DD96AA7126FCC44A00C9AA43 /* Person.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD96AA7026FCC44A00C9AA43 /* Person.swift */; };
DD96AA7426FCC61200C9AA43 /* MainViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD96AA7326FCC61200C9AA43 /* MainViewModel.swift */; };
DDC49F1C26F8A829001921C1 /* ReadViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC49F1B26F8A829001921C1 /* ReadViewController.swift */; };
DDC49F1C26F8A829001921C1 /* ContactDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC49F1B26F8A829001921C1 /* ContactDetailViewController.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -58,9 +60,11 @@
DD8A91E126FD7AFB004DAF3B /* Dynamic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Dynamic.swift; sourceTree = "<group>"; };
DD8A91E426FD7B6C004DAF3B /* MainTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainTableViewController.swift; sourceTree = "<group>"; };
DD8A91E626FD94FA004DAF3B /* UIViewController+embed.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+embed.swift"; sourceTree = "<group>"; };
DD8A91E926FDC6AB004DAF3B /* ContactDetailTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactDetailTableViewController.swift; sourceTree = "<group>"; };
DD8A91EC26FDCA4E004DAF3B /* ContactDetailViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactDetailViewModel.swift; sourceTree = "<group>"; };
DD96AA7026FCC44A00C9AA43 /* Person.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Person.swift; sourceTree = "<group>"; };
DD96AA7326FCC61200C9AA43 /* MainViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainViewModel.swift; sourceTree = "<group>"; };
DDC49F1B26F8A829001921C1 /* ReadViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReadViewController.swift; sourceTree = "<group>"; };
DDC49F1B26F8A829001921C1 /* ContactDetailViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContactDetailViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -161,7 +165,7 @@
DD2844C126EF3684004FC840 /* ViewControllers */ = {
isa = PBXGroup;
children = (
DDC49F1B26F8A829001921C1 /* ReadViewController.swift */,
DDC49F1B26F8A829001921C1 /* ContactDetailViewController.swift */,
DD2844A926EF35C4004FC840 /* MainViewController.swift */,
DD6CCCEE26F08EB200C7BA2C /* CreateViewController.swift */,
);
Expand Down Expand Up @@ -209,6 +213,7 @@
isa = PBXGroup;
children = (
DD8A91E426FD7B6C004DAF3B /* MainTableViewController.swift */,
DD8A91E926FDC6AB004DAF3B /* ContactDetailTableViewController.swift */,
);
path = Views;
sourceTree = "<group>";
Expand All @@ -233,6 +238,7 @@
isa = PBXGroup;
children = (
DD96AA7326FCC61200C9AA43 /* MainViewModel.swift */,
DD8A91EC26FDCA4E004DAF3B /* ContactDetailViewModel.swift */,
);
path = ViewModels;
sourceTree = "<group>";
Expand Down Expand Up @@ -330,14 +336,16 @@
DD372A5F26F0FD760033D40A /* UIButton+setBackgroundColor.swift in Sources */,
DD2844A626EF35C4004FC840 /* AppDelegate.swift in Sources */,
DD6CCCEA26F0814300C7BA2C /* UIStackView+addArrangedSubviews.swift in Sources */,
DDC49F1C26F8A829001921C1 /* ReadViewController.swift in Sources */,
DDC49F1C26F8A829001921C1 /* ContactDetailViewController.swift in Sources */,
DD8A91E226FD7AFB004DAF3B /* Dynamic.swift in Sources */,
DD2844A826EF35C4004FC840 /* SceneDelegate.swift in Sources */,
DD2844B026EF35C5004FC840 /* CheoMooRac.xcdatamodeld in Sources */,
DD6CCCE626F080F300C7BA2C /* UITableView+Generic.swift in Sources */,
DD8A91ED26FDCA4E004DAF3B /* ContactDetailViewModel.swift in Sources */,
DD6CCCED26F081C600C7BA2C /* MyCardTableViewCell.swift in Sources */,
DD372A5A26F0EBC30033D40A /* TextFieldTableViewCell.swift in Sources */,
DD96AA7426FCC61200C9AA43 /* MainViewModel.swift in Sources */,
DD8A91EA26FDC6AB004DAF3B /* ContactDetailTableViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// ReadViewController.swift
// CheoMooRac
//
// Created by 김윤서 on 2021/09/15.
//

import UIKit

import Then
import SnapKit

class ContactDetailViewController: UIViewController {

private var tableViewController: ContactDetailTableViewController!

init(with viewModel: ContactDetailViewModel) {
tableViewController = ContactDetailTableViewController(with: viewModel)
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()
initViewController()
setLayout()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationItem.largeTitleDisplayMode = .never
}

private func initViewController() {
view.backgroundColor = .white
}

private func setLayout() {
setViewHierarchy()
setConstraints()
}

private func setViewHierarchy() {
embed(tableViewController)
view.addSubview(tableViewController.view)
}

private func setConstraints() {
tableViewController.view.snp.makeConstraints {
$0.edges.equalToSuperview()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Then

class MainViewController: UIViewController {

var tableViewController: MainTableViewController!
private var tableViewController: MainTableViewController!

override func viewDidLoad() {
super.viewDidLoad()
Expand Down
105 changes: 0 additions & 105 deletions CheoMooRac/CheoMooRac/Sources/ViewControllers/ReadViewController.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// ContactDetailViewModel.swift
// CheoMooRac
//
// Created by 김윤서 on 2021/09/24.
//

import Foundation

class ContactDetailViewModel {
var items: [String]

init(person: Person){
self.items = [person.familyName+person.firstName, person.phoneNumber]
}
}
20 changes: 18 additions & 2 deletions CheoMooRac/CheoMooRac/Sources/ViewModels/MainViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ protocol MainViewModelInput {
func refreshTableView()
func searchResults(text: String)
func cancelSearch()
func itemDidSelect(at: Int)
}

protocol MainViewModelOutput {
Expand All @@ -19,7 +20,7 @@ protocol MainViewModelOutput {
var isFiltering: Dynamic<Bool> {get}

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

protocol MainViewModelProtocol : MainViewModelInput, MainViewModelOutput {}
Expand Down Expand Up @@ -63,6 +64,10 @@ class MainViewModel: MainViewModelProtocol {
isFiltering.value = false
}

func itemDidSelect(at: Int) {

}


// MARK: - OUTPUT
let list: Dynamic<[Person]> = Dynamic([])
Expand All @@ -74,6 +79,10 @@ class MainViewModel: MainViewModelProtocol {
return sectionArray(at: section, data: isFiltering.value ? self.filteredData : self.data)
}

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

var sectionHeaderList: [String] = []


Expand All @@ -85,7 +94,7 @@ class MainViewModel: MainViewModelProtocol {

private var filteredData: [Person] = []

private let data = [Person(firstName: "윤서", familyName: "", phoneNumber: "010-6515-6030"),
private var data = [Person(firstName: "윤서", familyName: "", phoneNumber: "010-6515-6030"),
Person(firstName: "루희", familyName: "", phoneNumber: "010-6515-6030"),
Person(firstName: "예지", familyName: "", phoneNumber: "010-6515-6030"),
Person(firstName: "혜수", familyName: "", phoneNumber: "010-6515-6030"),
Expand Down Expand Up @@ -123,5 +132,12 @@ class MainViewModel: MainViewModelProtocol {
}
return list
}

private func sectionArrayPerson(at section: Int, data: [Person]) -> [Person] {
let list = data.filter {
return StringManager.shared.chosungCheck(word: $0.familyName + $0.firstName) == sectionHeaderList[section-1]
}
return list
}

}
Loading

0 comments on commit b702f6e

Please sign in to comment.