-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReportRegistrationViewModel.swift
More file actions
206 lines (182 loc) · 7.08 KB
/
ReportRegistrationViewModel.swift
File metadata and controls
206 lines (182 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// ReportViewModel.swift
// Presentation
//
// Created by 이동현 on 11/9/25.
//
import Combine
import Domain
import Foundation
final class ReportRegistrationViewModel: ViewModel {
enum Input {
case selectCategory(type: ReportType)
case inputTitle(title: String?)
case inputContent(content: String?)
case configureLocation
case selectPhoto(photoData: Data)
case removePhoto(id: UUID)
case checkSelectedPhotoCount
case register
}
struct Output {
let categoryPublisher: AnyPublisher<String?, Never>
let titlePublisher: AnyPublisher<String?, Never>
let contentPublisher: AnyPublisher<String?, Never>
let locationPublisher: AnyPublisher<String?, Never>
let selectedPhotoPublisher: AnyPublisher<[PhotoItem], Never>
let selectedPhotoCountPublisher: AnyPublisher<Int, Never>
let isReportValid: AnyPublisher<Bool, Never>
let exceptionPublisher: AnyPublisher<String, Never>
let reportRegistrationCompletePublisher: AnyPublisher<Int?, Never>
let maxPhotoCount: Int
let maxContentLength: Int
let maxTitleLength: Int
}
private(set) var output: Output
private let reportUseCase: ReportUseCaseProtocol
private let categorySubject = CurrentValueSubject<ReportType?, Never>(nil)
private let titleSubject = CurrentValueSubject<String?, Never>(nil)
private let contentSubject = CurrentValueSubject<String?, Never>(nil)
private let locationSubject = CurrentValueSubject<String?, Never>(nil)
private let selectedPhotoSubject = CurrentValueSubject<[PhotoItem], Never>([])
private let selectedPhotoCountSubject = PassthroughSubject<Int, Never>()
private let reportVerificationSubject = PassthroughSubject<Bool, Never>()
private let reportRegistrationCompleteSubject = PassthroughSubject<Int?, Never>()
private let exceptionSubject = PassthroughSubject<String, Never>()
private let maxPhotoCount = 3
private let maxContentLength = 150
private let maxTitleLength = 50
private var location: LocationEntity? = nil
private(set) var selectedReportType: ReportType?
var selectedPhotoCount: Int {
return selectedPhotoSubject.value.count
}
init(reportUseCase: ReportUseCaseProtocol) {
self.reportUseCase = reportUseCase
self.output = Output(
categoryPublisher: categorySubject.map { $0?.name }.eraseToAnyPublisher(),
titlePublisher: titleSubject.eraseToAnyPublisher(),
contentPublisher: contentSubject.eraseToAnyPublisher(),
locationPublisher: locationSubject.eraseToAnyPublisher(),
selectedPhotoPublisher: selectedPhotoSubject.eraseToAnyPublisher(),
selectedPhotoCountPublisher: selectedPhotoCountSubject.eraseToAnyPublisher(),
isReportValid: reportVerificationSubject.eraseToAnyPublisher(),
exceptionPublisher: exceptionSubject.eraseToAnyPublisher(),
reportRegistrationCompletePublisher: reportRegistrationCompleteSubject.eraseToAnyPublisher(),
maxPhotoCount: maxPhotoCount,
maxContentLength: maxContentLength,
maxTitleLength: maxTitleLength)
}
func action(input: Input) {
switch input {
case .selectCategory(let type):
configureCategory(type: type)
case .inputTitle(let title):
configureTitle(title: title)
case .inputContent(let content):
configureContent(content: content)
case .configureLocation:
configureLocation()
case .selectPhoto(let photoData):
selectPhoto(photoData: photoData)
case .removePhoto(let id):
removePhoto(id: id)
case .checkSelectedPhotoCount:
selectedPhotoCountSubject.send(selectedPhotoSubject.value.count)
case .register:
register()
}
}
private func configureCategory(type: ReportType?) {
categorySubject.send(type)
selectedReportType = type
verifyIsReportValid()
}
private func configureTitle(title: String?) {
titleSubject.send(title)
verifyIsReportValid()
}
private func configureContent(content: String?) {
contentSubject.send(content)
verifyIsReportValid()
}
private func configureLocation() {
Task {
do {
self.location = try await reportUseCase.fetchCurrentLocation()
locationSubject.send(location?.address)
verifyIsReportValid()
} catch {
locationSubject.send(nil)
verifyIsReportValid()
}
}
}
private func selectPhoto(photoData: Data?) {
guard let photoData else { return }
var currentSelectedPhoto = selectedPhotoSubject.value
guard currentSelectedPhoto.count < maxPhotoCount else {
exceptionSubject.send("사진은 최대 \(maxPhotoCount)장까지 선택할 수 있습니다.")
return
}
let item = PhotoItem(data: photoData)
currentSelectedPhoto.append(item)
selectedPhotoSubject.send(currentSelectedPhoto)
verifyIsReportValid()
}
private func removePhoto(id: UUID) {
let currentSelectedPhoto = selectedPhotoSubject.value.filter { $0.id != id }
selectedPhotoSubject.send(currentSelectedPhoto)
verifyIsReportValid()
}
private func register() {
guard
let name = titleSubject.value,
!name.isEmpty,
let category = categorySubject.value,
let content = contentSubject.value,
let location,
selectedPhotoSubject.value.count > 0
else { return }
let selectedPhotos = selectedPhotoSubject.value.map({ $0.data })
Task {
let minimumDuration: TimeInterval = 0.7
let startTime = Date()
let reportId: Int?
do {
reportId = try await reportUseCase.report(
title: name,
content: content,
category: category,
location: location,
photos: selectedPhotos)
} catch {
reportId = nil
}
let elapsed = Date().timeIntervalSince(startTime)
if elapsed < minimumDuration {
let remaining = minimumDuration - elapsed
try? await Task.sleep(
nanoseconds: UInt64(remaining * 1_000_000_000)
)
}
reportRegistrationCompleteSubject.send(reportId)
}
}
private func verifyIsReportValid() {
guard
let name = titleSubject.value,
!name.isEmpty,
categorySubject.value != nil,
contentSubject.value != nil,
let location,
location.latitude != nil,
location.longitude != nil,
selectedPhotoSubject.value.count > 0
else {
reportVerificationSubject.send(false)
return
}
reportVerificationSubject.send(true)
}
}