Skip to content

Commit

Permalink
[Feat] sopt-makers#199 - 출석하기 Feature 구현 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
0inn committed Apr 22, 2023
1 parent 77c6c6c commit 51d7a85
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public final class AttendanceVC: UIViewController, AttendanceViewControllable {
private var cancelBag = CancelBag()
public var factory: AttendanceFeatureViewBuildable

private var viewWillAppear = PassthroughSubject<Void, Never>()

// MARK: - UI Components

/// 출석하기 모달 뷰
Expand Down Expand Up @@ -74,8 +76,6 @@ public final class AttendanceVC: UIViewController, AttendanceViewControllable {
/// 출석 제목
private let titleLabel: UILabel = {
let label = UILabel()
#warning("서버 붙인 후 text 변경")
label.text = "1차 출석하기"
label.textColor = DSKitAsset.Colors.white100.color
label.setTypoStyle(.Attendance.h1)
return label
Expand Down Expand Up @@ -154,6 +154,12 @@ public final class AttendanceVC: UIViewController, AttendanceViewControllable {
self.setObserver()
}

public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

self.viewWillAppear.send(())
}

public override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

Expand Down Expand Up @@ -237,15 +243,25 @@ extension AttendanceVC {
.asDriver()

let input = AttendanceViewModel.Input(
viewWillAppear: viewWillAppear.asDriver(),
codeTextChanged: codeTextChanged,
attendanceButtonDidTap: attendanceButtonDidTap
)

let output = viewModel.transform(from: input, cancelBag: cancelBag)

output.attendanceTitle
.withUnretained(self)
.sink { owner, title in
owner.titleLabel.text = title
}
.store(in: self.cancelBag)

output.codeTextFieldInfo
.withUnretained(self)
.sink { owner, code in
owner.alertLabel.isHidden = true

let (cur, nxt) = code

[cur, nxt].forEach {
Expand All @@ -264,6 +280,23 @@ extension AttendanceVC {
owner.attendanceButton.isEnabled = isEnabled
}
.store(in: self.cancelBag)

output.attendSuccess
.filter { $0 }
.withUnretained(self)
.sink { owner, _ in
owner.dismiss(animated: true)
}
.store(in: self.cancelBag)

output.attendErrorMsg
.withUnretained(self)
.sink { owner, errorMsg in
owner.alertLabel.text = errorMsg
owner.alertLabel.isHidden = false
owner.attendanceCodeView.setCodeTextFieldEmpty()
}
.store(in: self.cancelBag)
}

private func setObserver() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,30 @@ public final class AttendanceViewModel: ViewModelType {
private let useCase: AttendanceUseCase
private var cancelBag = CancelBag()

private var lectureRound: AttendanceRoundModel = .EMPTY
private var codeText: String = ""

// MARK: - Input

public struct Input {
let viewWillAppear: Driver<Void>
let codeTextChanged: Driver<AttendanceCodeInfo>
let attendanceButtonDidTap: Driver<Void>
}

// MARK: - Output

public struct Output {
let attendanceTitle = PassthroughSubject<String, Never>()
let codeTextFieldInfo = PassthroughSubject<(AttendanceCodeInfo, AttendanceCodeInfo), Never>()
let isAttendanceButtonEnabled = CurrentValueSubject<Bool, Never>(false)
let attendSuccess = PassthroughSubject<Bool, Never>()
let attendErrorMsg = PassthroughSubject<String, Never>()
}

public init(useCase: AttendanceUseCase) {
public init(useCase: AttendanceUseCase, lectureRound: AttendanceRoundModel) {
self.useCase = useCase
self.lectureRound = lectureRound
}
}

Expand All @@ -48,6 +54,13 @@ extension AttendanceViewModel {
let output = Output()

self.bindOutput(output: output, cancelBag: cancelBag)

input.viewWillAppear
.withUnretained(self)
.sink { owner, _ in
output.attendanceTitle.send(I18N.Attendance.nthAttendance(owner.lectureRound.round))
}
.store(in: self.cancelBag)

input.codeTextChanged
.withUnretained(self)
Expand All @@ -61,17 +74,33 @@ extension AttendanceViewModel {
input.attendanceButtonDidTap
.withUnretained(self)
.sink { owner, _ in
#warning("차수 서버값 넘겨주기")
let code = Int(owner.codeText) ?? 0
owner.useCase.postAttendance(lectureRoundId: 0, code: code)
let lectureRoundId = owner.lectureRound.subLectureId
let code = owner.codeText
owner.useCase.postAttendance(lectureRoundId: lectureRoundId, code: code)
}
.store(in: self.cancelBag)

return output
}

private func bindOutput(output: Output, cancelBag: CancelBag) {
let attendSuccess = self.useCase.attendSuccess
let attendErrorMsg = self.useCase.attendErrorMsg

attendSuccess.asDriver()
.sink { isSuccess in
output.attendSuccess.send(isSuccess)
}
.store(in: self.cancelBag)

attendErrorMsg.asDriver()
.withUnretained(self)
.sink { owner, errorMsg in
output.attendErrorMsg.send(errorMsg)
output.isAttendanceButtonEnabled.send(false)
owner.codeText = ""
}
.store(in: self.cancelBag)
}

private func updateCodeText(_ code: AttendanceCodeInfo) -> (AttendanceCodeInfo, AttendanceCodeInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ extension DIContainer: Features {
return showAttendanceVC
}

func makeAttendanceVC() -> AttendanceViewControllable {
func makeAttendanceVC(lectureRound: AttendanceRoundModel) -> AttendanceViewControllable {
let repository = AttendanceRepository(service: attendanceService)
let useCase = DefaultAttendanceUseCase(repository: repository)
let viewModel = AttendanceViewModel(useCase: useCase)
let viewModel = AttendanceViewModel(useCase: useCase, lectureRound: lectureRound)
let attendanceVC = AttendanceVC(viewModel: viewModel, factory: self)
return attendanceVC
}
Expand Down

0 comments on commit 51d7a85

Please sign in to comment.