-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReportRegistrationViewController.swift
More file actions
696 lines (578 loc) · 26.5 KB
/
ReportRegistrationViewController.swift
File metadata and controls
696 lines (578 loc) · 26.5 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
//
// ReportViewController.swift
// Presentation
//
// Created by 이동현 on 11/8/25.
//
import Combine
import Domain
import PhotosUI
import SnapKit
import UIKit
protocol ReportRegistrationViewControllerDelegate: AnyObject {
func reportRegistrationViewController(_ sender: ReportRegistrationViewController, completeRegistration reportId: Int?)
}
final class ReportRegistrationViewController: BaseViewController<ReportRegistrationViewModel> {
private enum CollectionViewSection { case main }
private enum Layout {
static let horizontalInset: CGFloat = 20
static let navigationBarBottomSpacing: CGFloat = 80
static let titleLabelTopSpacing: CGFloat = 28
static let titleLabelBottomSpacing: CGFloat = 8
static let titleLabelHeight: CGFloat = 24
static let cameraButtonTopSpacing: CGFloat = 8
static let cameraButtonSize: CGFloat = 64
static let cameraButtonCornerRadius: CGFloat = 8
static let reportPhotoCollectionViewTopSpacing: CGFloat = 1
static let reportCollectionLeadingSpacing: CGFloat = 14
static let reportCollectionViewCellSize: CGFloat = 72
static let reportCollectionViewCellSpacing: CGFloat = 7
static let reportCollectionViewHeight: CGFloat = 72
static let inputViewCornerRadius: CGFloat = 12
static let textViewHeight: CGFloat = 52
static let reportContentTextViewHeight: CGFloat = 88
static let locationButtonSize: CGFloat = 52
static let locationButtonLeadingSpacing: CGFloat = 12
static let locationButtonCornerRadius: CGFloat = 12
static let registerButtonCornerRadius: CGFloat = 12
static let registerButtonTopSpacing: CGFloat = 47
static let registerButtonBottomSpacing: CGFloat = 14
static let registerButtonHeight: CGFloat = 54
static let categoryBottomSheetHeight: CGFloat = 362
static let cameraBottomSheetHeight: CGFloat = 174
static let countLabelTopSpacing: CGFloat = 6
static let countLabelHeight: CGFloat = 18
}
private typealias Section = ReportRegistrationViewController.CollectionViewSection
private typealias DataSource = UICollectionViewDiffableDataSource<Section, PhotoItem>
private typealias Snapshot = NSDiffableDataSourceSnapshot<Section, PhotoItem>
private let scrollView = UIScrollView()
private let scrollContentView = UIView()
private let collectionViewTitleLabel = RequiredTitleLabel(title: "제보사진 등록")
private let categoryTitleLabel = RequiredTitleLabel(title: "제보 카테고리")
private let nameTitleLabel = RequiredTitleLabel(title: "제목")
private let contentTitleLabel = RequiredTitleLabel(title: "제보 내용")
private let locationTitleLabel = RequiredTitleLabel(title: "신고 위치")
private let cameraButton = ReportCameraButton(frame: .zero)
private let photoCollectionView = UICollectionView(frame: .zero, collectionViewLayout: .init())
private let categoryTextView = ReportTextView(type: .combo, placeholder: "카테고리 선택")
private let reportTitleTextView = ReportTextView(
type: .editable,
placeholder: "제보 제목을 작성해주세요."
,maxLength: 50)
private let reportContentTextView = ReportTextView(
type: .editable,
placeholder: "어떤 위험인지 간단히 설명해주세요.",
maxLength: 150)
private let locationTextView = ReportTextView(type: .nonEditable, placeholder: "현재 위치 검색")
private let titleCountLabel = UILabel()
private let contentTextCountLabel = UILabel()
private let locationButton = LocationButton()
private let registerButton = PrimaryButton(buttonState: .disabled, buttonTitle: "제출하기")
private let photoSelectionView = SelectableItemTableView<SelectPhotoType>(items: SelectPhotoType.allCases.sorted(by: { $0.id < $1.id }), markIsSelected: false)
private var cancellables: Set<AnyCancellable> = []
private var dataSource: DataSource?
weak var delegate: ReportRegistrationViewControllerDelegate?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
configureCustomNavigationBar(navigationBarStyle: .withBackButton(title: "제보하기"))
}
override func configureAttribute() {
configureCollectionViewDataSource()
configurePhotoCollectionLayout()
view.backgroundColor = .white
scrollView.showsVerticalScrollIndicator = false
photoCollectionView.delegate = self
[
categoryTextView,
locationTextView,
reportTitleTextView,
reportContentTextView].forEach { textView in
textView.layer.cornerRadius = Layout.inputViewCornerRadius
textView.layer.masksToBounds = true
textView.delegate = self
}
cameraButton.layer.cornerRadius = Layout.locationButtonCornerRadius
cameraButton.layer.masksToBounds = true
cameraButton.addAction(
UIAction { [weak self] _ in
self?.viewModel.action(input: .checkSelectedPhotoCount)
},
for: .touchUpInside)
locationButton.layer.cornerRadius = Layout.locationButtonCornerRadius
locationButton.layer.masksToBounds = true
locationButton.addAction(
UIAction { [weak self] _ in
self?.viewModel.action(input: .configureLocation)
},
for: .touchUpInside)
registerButton.layer.cornerRadius = Layout.registerButtonCornerRadius
registerButton.layer.masksToBounds = true
registerButton.addAction(
UIAction { [weak self] _ in
let loadingViewController = ReportLoadingViewController()
self?.delegate = loadingViewController
self?.navigationController?.pushViewController(loadingViewController, animated: true)
self?.viewModel.action(input: .register)
},
for: .touchUpInside)
titleCountLabel.font = BitnagilFont.init(style: .caption1, weight: .medium).font
titleCountLabel.textColor = BitnagilColor.gray80
contentTextCountLabel.font = BitnagilFont.init(style: .caption1, weight: .medium).font
contentTextCountLabel.textColor = BitnagilColor.gray80
photoSelectionView.delegate = self
applySnapshot(items: [], animating: false)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
tapGesture.cancelsTouchesInView = false
view.addGestureRecognizer(tapGesture)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
override func configureLayout() {
let safeArea = view.safeAreaLayoutGuide
view.addSubview(scrollView)
scrollView.addSubview(scrollContentView)
scrollContentView.addSubview(collectionViewTitleLabel)
scrollContentView.addSubview(categoryTitleLabel)
scrollContentView.addSubview(nameTitleLabel)
scrollContentView.addSubview(titleCountLabel)
scrollContentView.addSubview(contentTitleLabel)
scrollContentView.addSubview(locationTitleLabel)
scrollContentView.addSubview(cameraButton)
scrollContentView.addSubview(photoCollectionView)
scrollContentView.addSubview(categoryTextView)
scrollContentView.addSubview(reportTitleTextView)
scrollContentView.addSubview(reportContentTextView)
scrollContentView.addSubview(contentTextCountLabel)
scrollContentView.addSubview(locationTextView)
scrollContentView.addSubview(locationButton)
scrollContentView.addSubview(registerButton)
scrollView.snp.makeConstraints { make in
make.edges.equalTo(safeArea)
}
scrollContentView.snp.makeConstraints { make in
make.edges.equalTo(scrollView.contentLayoutGuide)
make.width.equalTo(scrollView.frameLayoutGuide)
make.height.equalTo(scrollView.frameLayoutGuide).priority(.low)
}
collectionViewTitleLabel.snp.makeConstraints { make in
make.top
.equalToSuperview()
.offset(Layout.navigationBarBottomSpacing)
make.leading
.equalToSuperview()
.offset(Layout.horizontalInset)
make.height
.equalTo(Layout.titleLabelHeight)
}
cameraButton.snp.makeConstraints { make in
make.top
.equalTo(collectionViewTitleLabel.snp.bottom)
.offset(Layout.cameraButtonTopSpacing)
make.leading
.equalToSuperview()
.offset(Layout.horizontalInset)
make.size.equalTo(Layout.cameraButtonSize)
}
photoCollectionView.snp.makeConstraints { make in
make.top
.equalTo(collectionViewTitleLabel.snp.bottom)
.offset(Layout.reportPhotoCollectionViewTopSpacing)
make.leading
.equalTo(cameraButton.snp.trailing)
.offset(Layout.reportCollectionLeadingSpacing)
make.trailing
.equalToSuperview()
.inset(Layout.horizontalInset)
make.height
.equalTo(Layout.reportCollectionViewHeight)
}
nameTitleLabel.snp.makeConstraints { make in
make.top
.equalTo(cameraButton.snp.bottom)
.offset(Layout.titleLabelTopSpacing)
make.leading
.equalToSuperview()
.offset(Layout.horizontalInset)
make.height
.equalTo(Layout.titleLabelHeight)
}
reportTitleTextView.snp.makeConstraints { make in
make.top
.equalTo(nameTitleLabel.snp.bottom)
.offset(Layout.titleLabelBottomSpacing)
make.horizontalEdges
.equalToSuperview()
.inset(Layout.horizontalInset)
make.height
.equalTo(Layout.textViewHeight)
}
titleCountLabel.snp.makeConstraints { make in
make.top
.equalTo(reportTitleTextView.snp.bottom)
.offset(Layout.countLabelTopSpacing)
make.height
.equalTo(Layout.countLabelHeight)
make.trailing.equalTo(reportTitleTextView)
}
categoryTitleLabel.snp.makeConstraints { make in
make.top
.equalTo(titleCountLabel.snp.bottom)
.offset(Layout.titleLabelTopSpacing)
make.leading
.equalToSuperview()
.offset(Layout.horizontalInset)
make.height
.equalTo(Layout.titleLabelHeight)
}
categoryTextView.snp.makeConstraints { make in
make.top
.equalTo(categoryTitleLabel.snp.bottom)
.offset(Layout.titleLabelBottomSpacing)
make.horizontalEdges
.equalToSuperview()
.inset(Layout.horizontalInset)
make.height
.equalTo(Layout.textViewHeight)
}
contentTitleLabel.snp.makeConstraints { make in
make.top
.equalTo(categoryTextView.snp.bottom)
.offset(Layout.titleLabelTopSpacing)
make.leading
.equalToSuperview()
.offset(Layout.horizontalInset)
make.height
.equalTo(Layout.titleLabelHeight)
}
reportContentTextView.snp.makeConstraints { make in
make.top
.equalTo(contentTitleLabel.snp.bottom)
.offset(Layout.titleLabelBottomSpacing)
make.horizontalEdges
.equalToSuperview()
.inset(Layout.horizontalInset)
make.height
.equalTo(Layout.reportContentTextViewHeight)
.priority(.medium)
}
contentTextCountLabel.snp.makeConstraints { make in
make.top
.equalTo(reportContentTextView.snp.bottom)
.offset(Layout.countLabelTopSpacing)
make.trailing
.equalToSuperview()
.offset(-Layout.horizontalInset)
make.height.equalTo(Layout.countLabelHeight)
}
locationTitleLabel.snp.makeConstraints { make in
make.top
.equalTo(reportContentTextView.snp.bottom)
.offset(Layout.titleLabelTopSpacing)
make.leading
.equalToSuperview()
.offset(Layout.horizontalInset)
make.height
.equalTo(Layout.titleLabelHeight)
}
locationButton.snp.makeConstraints { make in
make.top
.equalTo(locationTitleLabel.snp.bottom)
.offset(Layout.titleLabelBottomSpacing)
make.trailing
.equalToSuperview()
.inset(Layout.horizontalInset)
make.size
.equalTo(Layout.locationButtonSize)
}
locationTextView.snp.makeConstraints { make in
make.leading
.equalToSuperview()
.offset(Layout.horizontalInset)
make.trailing
.equalTo(locationButton.snp.leading)
.offset(-Layout.locationButtonLeadingSpacing)
make.top
.equalTo(locationButton)
make.height
.equalTo(Layout.textViewHeight)
}
registerButton.snp.makeConstraints { make in
make.horizontalEdges
.equalToSuperview()
.inset(Layout.horizontalInset)
make.top
.greaterThanOrEqualTo(locationTextView.snp.bottom)
.offset(Layout.registerButtonTopSpacing)
make.bottom
.equalToSuperview()
.inset(Layout.registerButtonBottomSpacing)
make.height
.equalTo(Layout.registerButtonHeight)
}
}
override func bind() {
viewModel.output.categoryPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] category in
self?.categoryTextView.configure(text: category ?? "")
}
.store(in: &cancellables)
viewModel.output.titlePublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] title in
guard let self else { return }
self.reportTitleTextView.configure(text: title ?? "")
let title = title ?? ""
self.titleCountLabel.text = "\(title.count) / \(viewModel.output.maxTitleLength)"
}
.store(in: &cancellables)
viewModel.output.contentPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] content in
guard let self else { return }
self.reportContentTextView.configure(text: content ?? "")
let content = content ?? ""
self.contentTextCountLabel.text = "\(content.count) / \(viewModel.output.maxContentLength)"
}
.store(in: &cancellables)
viewModel.output.locationPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] address in
self?.locationTextView.configure(text: address ?? "")
}
.store(in: &cancellables)
viewModel.output.exceptionPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] message in
let alertController = UIAlertController(title: "알림", message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "확인", style: .default))
self?.present(alertController, animated: true)
}
.store(in: &cancellables)
viewModel.output.selectedPhotoPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] items in
guard let self else { return }
self.applySnapshot(items: items, animating: true)
self.cameraButton
.configure(imageCount: items.count, maxCount: viewModel.output.maxPhotoCount)
}
.store(in: &cancellables)
viewModel.output.selectedPhotoCountPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] photoCount in
guard let self else { return }
if photoCount >= self.viewModel.output.maxPhotoCount {
let message = "사진은 최대 \(self.viewModel.output.maxPhotoCount)장까지 선택할 수 있습니다."
let alertController = UIAlertController(title: "알림", message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "확인", style: .default))
self.present(alertController, animated: true)
} else {
self.showCameraBottomSheet()
}
}
.store(in: &cancellables)
viewModel.output.isReportValid
.receive(on: DispatchQueue.main)
.sink { [weak self] isReportValid in
if isReportValid {
self?.registerButton.updateButtonState(buttonState: .default)
} else {
self?.registerButton.updateButtonState(buttonState: .disabled)
}
}
.store(in: &cancellables)
viewModel.output.reportRegistrationCompletePublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] reportId in
guard let self else { return }
delegate?.reportRegistrationViewController(self, completeRegistration: reportId)
}
.store(in: &cancellables)
}
private func showCameraBottomSheet() {
presentCustomBottomSheet(contentViewController: photoSelectionView, maxHeight: Layout.cameraBottomSheetHeight)
}
private func showCategoryBottomSheet() {
let categorySelectionView = ReportCategoryTableViewController(reportType: viewModel.selectedReportType)
categorySelectionView.delegate = self
presentCustomBottomSheet(contentViewController: categorySelectionView, maxHeight: Layout.categoryBottomSheetHeight)
}
private func configureCollectionViewDataSource() {
let registration = UICollectionView.CellRegistration<ReportPhotoCollectionViewCell, PhotoItem> { cell, _, item in
cell.configure(with: item)
cell.delegate = self
}
dataSource = DataSource(collectionView: photoCollectionView) { collectionView, indexPath, item in
collectionView.dequeueConfiguredReusableCell(using: registration, for: indexPath, item: item)
}
photoCollectionView.dataSource = dataSource
}
private func configurePhotoCollectionLayout() {
let itemSize = NSCollectionLayoutSize(
widthDimension: .absolute(Layout.reportCollectionViewCellSize),
heightDimension: .absolute(Layout.reportCollectionViewCellSize)
)
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(
widthDimension: .estimated(Layout.reportCollectionViewCellSize),
heightDimension: .estimated(Layout.reportCollectionViewCellSize)
)
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
group.interItemSpacing = .fixed(Layout.reportCollectionViewCellSpacing)
let section = NSCollectionLayoutSection(group: group)
section.contentInsets = .zero
section.interGroupSpacing = .zero
section.orthogonalScrollingBehavior = .continuous
let layout = UICollectionViewCompositionalLayout(section: section)
photoCollectionView.setCollectionViewLayout(layout, animated: false)
}
private func applySnapshot(items: [PhotoItem], animating: Bool) {
var snapshot = Snapshot()
snapshot.appendSections([.main])
snapshot.appendItems(items, toSection: .main)
dataSource?.apply(snapshot, animatingDifferences: true)
}
private func presentCamera() {
guard UIImagePickerController.isSourceTypeAvailable(.camera) else {
let alertController = UIAlertController(title: "카메라 사용 불가", message: "이 기기에서 카메라를 사용할 수 없습니다.", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "확인", style: .default))
present(alertController, animated: true)
return
}
let picker = UIImagePickerController()
picker.sourceType = .camera
picker.allowsEditing = false
picker.delegate = self
present(picker, animated: true)
}
private func presentPhotoPicker() {
var config = PHPickerConfiguration(photoLibrary: .shared())
config.filter = .images
config.selectionLimit = viewModel.output.maxPhotoCount - viewModel.selectedPhotoCount
guard config.selectionLimit > 0 else {
let message = "더 이상 사진을 선택할 수 없습니다."
let alertController = UIAlertController(title: "알림", message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "확인", style: .default))
self.present(alertController, animated: true)
return
}
let picker = PHPickerViewController(configuration: config)
picker.delegate = self
present(picker, animated: true)
}
private func dismissIfNeededThen(_ action: @escaping () -> Void) {
if Thread.isMainThread {
if let presented = presentedViewController {
presented.dismiss(animated: true, completion: action)
} else {
action()
}
} else {
DispatchQueue.main.async { [weak self] in
self?.dismissIfNeededThen(action)
}
}
}
@objc private func dismissKeyboard() {
view.endEditing(true)
}
@objc private func keyboardWillShow(notification: NSNotification) {
guard let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardHeight = keyboardFrame.cgRectValue.height
scrollView.contentInset.bottom = keyboardHeight
scrollView.verticalScrollIndicatorInsets.bottom = keyboardHeight
if reportContentTextView.isFirstResponder {
let targetRect = reportContentTextView.convert(reportContentTextView.bounds, to: scrollView)
scrollView.scrollRectToVisible(targetRect, animated: true)
}
}
@objc private func keyboardWillHide() {
scrollView.contentInset = .zero
scrollView.verticalScrollIndicatorInsets = .zero
}
}
extension ReportRegistrationViewController: ReportTextViewDelegate {
func reportTextViewDidChanged(_ reportTextView: ReportTextView, text: String?) {
switch reportTextView {
case reportTitleTextView:
viewModel.action(input: .inputTitle(title: text))
case reportContentTextView:
viewModel.action(input: .inputContent(content: text))
default: return
}
}
func reportTextViewDidTapped(_ reportTextView: ReportTextView) {
showCategoryBottomSheet()
}
}
extension ReportRegistrationViewController: ReportPhotoCollectionViewCellDelegate {
func reportPhotoCollectionViewCellWillDeleteCell(_ cell: ReportPhotoCollectionViewCell, uuid: UUID) {
viewModel.action(input: .removePhoto(id: uuid))
}
}
extension ReportRegistrationViewController: UICollectionViewDelegate {
}
extension ReportRegistrationViewController: ReportCategoryTableViewControllerDelegate {
func reportCategoryTableViewController(_ sender: ReportCategoryTableViewController, selectedCategory: ReportType?) {
guard let selectedCategory else { return }
viewModel.action(input: .selectCategory(type: selectedCategory))
}
}
extension ReportRegistrationViewController: SelectableItemTableViewDelegate {
func selectableItemTableView<T>(_ sender: SelectableItemTableView<T>, didSelectItem: T?) where T : SelectableItem, T : CaseIterable, T : Equatable {
if
T.self == ReportType.self,
let item = didSelectItem as? ReportType
{
viewModel.action(input: .selectCategory(type: item))
} else if
T.self == SelectPhotoType.self,
let item = didSelectItem as? SelectPhotoType
{
switch item {
case .camera:
dismissIfNeededThen { [weak self] in
self?.presentCamera()
}
case .library:
dismissIfNeededThen { [weak self] in
self?.presentPhotoPicker()
}
}
}
}
}
extension ReportRegistrationViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
defer { picker.dismiss(animated: true) }
guard
let image = info[.originalImage] as? UIImage,
let data = image.jpegData(compressionQuality: 0.5)
else { return }
viewModel.action(input: .selectPhoto(photoData: data))
}
}
extension ReportRegistrationViewController: PHPickerViewControllerDelegate {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true, completion: nil)
guard !results.isEmpty else { return }
for result in results {
guard result.itemProvider.canLoadObject(ofClass: UIImage.self)
else { continue }
result.itemProvider.loadObject(ofClass: UIImage.self) { image, _ in
guard
let selectedImage = image as? UIImage,
let imageData = selectedImage.jpegData(compressionQuality: 0.5)
else { return }
DispatchQueue.main.async {
self.viewModel.action(input: .selectPhoto(photoData: imageData))
}
}
}
}
}