Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Swift] MVC -> MVVM 리팩토링 #76

Closed
seungchan2 opened this issue Jun 9, 2022 · 0 comments
Closed

[Swift] MVC -> MVVM 리팩토링 #76

seungchan2 opened this issue Jun 9, 2022 · 0 comments
Assignees
Labels

Comments

@seungchan2
Copy link
Owner

MVVM으로 구조를 짜놔서 View에서는 비지니스 로직을 빼고 View 자체만 두고 싶었음

제대로 한 건 아니지만 . . 코드의 양이 확연히 줄고 . .

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(for: indexPath, cellType: AddTimeCollectionViewCell.self)
        cell.timeLabel.text = medicineTimeData[indexPath.row]
        cell.deleteCellClosure = {
            // delete했을 때 자신의 index값을 지워야 함
            self.medicineTimeData.remove(at: indexPath.row)
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        guard let cell = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: AddMedicineTimeFooterView.reuseIdentifier, for: indexPath) as? AddMedicineTimeFooterView else { return UICollectionReusableView()}
        
        // FooterView +버튼 클릭 시 셀 추가
        cell.addCellClosure = {
            let addMedicineTimeSheet = MedicineTimeViewController.instanceFromNib()
            addMedicineTimeSheet.modalPresentationStyle = .overCurrentContext
            addMedicineTimeSheet.modalTransitionStyle = .crossDissolve
            addMedicineTimeSheet.sendTimeDelegate = self
            self.present(addMedicineTimeSheet, animated: false
            ) {
                DispatchQueue.main.async {
                    addMedicineTimeSheet.sheetWithAnimation()
                }
            }
        }
        if medicineTimeData.count == 6 {
            cell.addMedicineCellButton.isHidden = true
            cell.countCautionLabel.isHidden = false
        } else {
            cell.addMedicineCellButton.isHidden = false
            cell.countCautionLabel.isHidden = true
        }
        
        return cell
    }

이게 앱잼 때 짠 코드인데 ㅋㅋ 진짜 동작만 하게 해놓고 . .

현재의 코드 퀄리티가 좋은 건 아니지만 적용한 예시를 보면

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  
        let cell = collectionView.dequeueReusableCell(for: indexPath, cellType: PillTimeCollectionViewCell.self)
        
        // cell의 UI업데이트
        cell.updateCell(pillTimeViewModel, indexPath: indexPath)
        
        // XButton 클릭 시 지움
        cell.viewModel.deleteCellClosure = { [weak self] in
            guard let self = self else { return }
            self.pillTimeViewModel.deleteCell(index: indexPath.row)
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        guard let cell = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: AddPillFirstFooterView.reuseIdentifier, for: indexPath) as? AddPillFirstFooterView else { return UICollectionReusableView()}
       
        cell.viewModel.addCellClosure = { [weak self] in
            guard let self = self else { return }
            self.presentTimeView()
        }
        
        self.pillTimeViewModel.hideFooterView(button: &cell.addTimeButton.isHidden, stackView: &cell.horizontalStackView.isHidden)
        
        return cell
    }

ViewModel

class PillTimeViewModel {
    var timeList: Helper<[String]> = Helper(["오전 8:00", "오후 1:00", "오후 7:00"])

    var deleteCellClosure: (() -> Void)?
    
    var numberOfItemsInSection: Int {
        return timeList.value.count
    }
    
    func deleteCell(index: Int) {
        timeList.value.remove(at: index)
    }
    
    func addPillTime(pillTime: String) {
        timeList.value += [pillTime]
    }
    
    func hideFooterView(button: inout Bool, stackView: inout Bool) {
        if timeList.value.count == 5 {
            button = true
            stackView = true
        } else {
            button = false
            stackView = false
        }
    }
}
2022-06-09.11.18.36.mov

적용하면서 느낀 점 VC가 깔끔해지긴 하는데 규모가 작아서 아직까지 장점을 모르겠음

하지만 데이터 바인딩하는 과정은 편함

@seungchan2 seungchan2 added the iOS label Jun 9, 2022
@seungchan2 seungchan2 self-assigned this Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant