Skip to content

Commit

Permalink
[refactor] 기록탭 & 회고탭 상속 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
gom1n committed May 30, 2023
1 parent 9b8a502 commit 240cc39
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 9 deletions.
8 changes: 4 additions & 4 deletions POME.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
23431C322A24293500571286 /* DeleteGoalUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 23431C312A24293500571286 /* DeleteGoalUseCase.swift */; };
23431C342A242B1D00571286 /* GetRecordsOfGoalInRecordTabUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 23431C332A242B1D00571286 /* GetRecordsOfGoalInRecordTabUseCase.swift */; };
23431C362A242C8B00571286 /* GetNoSecondEmotionRecordsUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 23431C352A242C8B00571286 /* GetNoSecondEmotionRecordsUseCase.swift */; };
2343FB032A269F7600820625 /* GoalWithRecordViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2343FB022A269F7600820625 /* GoalWithRecordViewModel.swift */; };
2343FB052A26AD7200820625 /* GoalWithRecordViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2343FB042A26AD7200820625 /* GoalWithRecordViewModel.swift */; };
2345E1C429818848003185E0 /* BaseResponseModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2345E1C329818848003185E0 /* BaseResponseModel.swift */; };
2345E1C829819471003185E0 /* UserRequestModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2345E1C729819471003185E0 /* UserRequestModel.swift */; };
2345E1CA2981947A003185E0 /* UserResponseModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2345E1C92981947A003185E0 /* UserResponseModel.swift */; };
Expand Down Expand Up @@ -328,7 +328,7 @@
23431C312A24293500571286 /* DeleteGoalUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeleteGoalUseCase.swift; sourceTree = "<group>"; };
23431C332A242B1D00571286 /* GetRecordsOfGoalInRecordTabUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetRecordsOfGoalInRecordTabUseCase.swift; sourceTree = "<group>"; };
23431C352A242C8B00571286 /* GetNoSecondEmotionRecordsUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetNoSecondEmotionRecordsUseCase.swift; sourceTree = "<group>"; };
2343FB022A269F7600820625 /* GoalWithRecordViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoalWithRecordViewModel.swift; sourceTree = "<group>"; };
2343FB042A26AD7200820625 /* GoalWithRecordViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoalWithRecordViewModel.swift; sourceTree = "<group>"; };
2345E1C329818848003185E0 /* BaseResponseModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseResponseModel.swift; sourceTree = "<group>"; };
2345E1C729819471003185E0 /* UserRequestModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserRequestModel.swift; sourceTree = "<group>"; };
2345E1C92981947A003185E0 /* UserResponseModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserResponseModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1588,7 +1588,7 @@
EE7D35DC29D5A16300D2AD60 /* Authentication */,
EE7D35DE29D5A16300D2AD60 /* Review */,
EE7D35E029D5A16300D2AD60 /* Goal */,
2343FB022A269F7600820625 /* GoalWithRecordViewModel.swift */,
2343FB042A26AD7200820625 /* GoalWithRecordViewModel.swift */,
);
path = ViewModel;
sourceTree = "<group>";
Expand Down Expand Up @@ -2281,7 +2281,7 @@
2375E4892A20397400843056 /* MyPageViewModel.swift in Sources */,
5724EC84291B29A700DC529B /* EmojiFloatingCollectionViewCell.swift in Sources */,
EE38758029D917B900A26AD5 /* TextConverter.swift in Sources */,
2343FB032A269F7600820625 /* GoalWithRecordViewModel.swift in Sources */,
2343FB052A26AD7200820625 /* GoalWithRecordViewModel.swift in Sources */,
EE7D35EC29D5A16300D2AD60 /* GenerateGoalDateViewModel.swift in Sources */,
57543B7C2951BF5300A409E3 /* CalendarSheetCollectionViewCell.swift in Sources */,
23F57834292D5460002DD28E /* SettingViewController.swift in Sources */,
Expand Down
123 changes: 123 additions & 0 deletions POME/Presentation/ViewModel/GoalWithRecordViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//
// GoalWithRecordViewModel.swift
// POME
//
// Created by gomin on 2023/05/31.
//

import Foundation
import RxSwift
import RxCocoa

protocol ModifyRecordInterface {
var modifyRecordSubject: PublishSubject<Int> { get }
func modifyRecord(_ record: RecordResponseModel, index: Int)
}

protocol DeleteRecord{
var deleteRecordSubject: PublishSubject<Int> { get }
func deleteRecord(index: Int)
}

protocol PageableInterface{
var hasNextPage: Bool { get }
}

protocol GoalWithRecordViewModelInterface: BaseViewModel, ModifyRecordInterface {
var goals: [GoalResponseModel] { get }
var records: [RecordResponseModel] { get }
}

class GoalWithRecordViewModel: GoalWithRecordViewModelInterface, DeleteRecord{

private let getGoalsUseCase: GetGoalUseCaseInterface
private let deleteRecordUseCase: DeleteRecordUseCaseInterface

init(getGoalsUseCase: GetGoalUseCaseInterface = GetGoalUseCase(),
deleteRecordUseCase: DeleteRecordUseCaseInterface = DeleteRecordUseCase()){
self.getGoalsUseCase = getGoalsUseCase
self.deleteRecordUseCase = deleteRecordUseCase
}

var hasNextPage: Bool = false
var goals = [GoalResponseModel]()
var records = [RecordResponseModel]()

let changeGoalSelect = PublishSubject<Void>()
let reloadTableView = PublishRelay<Void>()
let deleteRecordSubject = PublishSubject<Int>()
let modifyRecordSubject = PublishSubject<Int>()

var page: Int = 0
var selectedGoalIndex: Int = 0

let disposeBag = DisposeBag()

struct Input{
let selectedGoalIndex: Observable<Int>
}

struct Output{}

@discardableResult
func transform(_ input: Input) -> Output{

input.selectedGoalIndex
.subscribe{ [weak self] in
self?.selectedGoalIndex = $0
self?.initializeStateAndRequestRecord()
}.disposed(by: disposeBag)

return Output()
}

func refreshData(){
getGoalsUseCase.execute()
.subscribe(onNext: { [weak self] goals in
self?.goals = goals.filter{ !$0.isEnd }
self?.initializeStateAndRequestRecord()
}).disposed(by: disposeBag)
}

func initializeStateAndRequestRecord(){
hasNextPage = false
page = 0
canRequestRecord()
}

private func canRequestRecord(){
//goal이 존재할 때만 기록 조회 요청
if goals.isEmpty {
records = []
reloadTableView.accept(Void())
} else if selectedGoalIndex >= goals.count { //현재 선택 중인 목표가 삭제되었을 경우, 목표 변경 VC으로 전달
changeGoalSelect.onNext(Void())
} else {
requestRecords()
}
}

func requestRecords(){

}

func requestNextPage(){
page += 1
requestRecords()
}

func deleteRecord(index: Int) {
deleteRecordUseCase.execute(requestValue: DeleteRecordRequestModel(recordId: records[index].id))
.subscribe{ [weak self] in
if $0 == .success {
self?.records.remove(at: index)
self?.deleteRecordSubject.onNext(index)
}
}.disposed(by: disposeBag)
}

func modifyRecord(_ record: RecordResponseModel, index: Int){
records[index] = record
modifyRecordSubject.onNext(index)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protocol NoSecondEmotionRecordViewModelInterface {
// var records: [RecordResponseModel] { get }
//}

final class RecordTabViewModel: GoalWithRecordViewModel, NoSecondEmotionViewModelInterface {
final class RecordTabViewModel: GoalWithRecordViewModel, NoSecondEmotionRecordViewModelInterface {

internal var noSecondEmotionRecords: Int = 0

Expand Down
4 changes: 0 additions & 4 deletions POME/Presentation/ViewModel/Review/ReviewViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,4 @@ final class ReviewViewModel: GoalWithRecordViewModel {
}).disposed(by: disposeBag)
}

func requestNextPage(){
page += 1
requestRecords()
}
}

0 comments on commit 240cc39

Please sign in to comment.