Skip to content

Commit de8a12d

Browse files
authored
fix: 제보 등록 화면 QA 대응 (#81)
1 parent 74bfa16 commit de8a12d

3 files changed

Lines changed: 58 additions & 3 deletions

File tree

Projects/Presentation/Sources/Report/View/Component/ReportRegistration/ReportTextView.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ final class ReportTextView: UIView {
3434
private let textView = UITextView()
3535
private let chevronImage = UIImageView()
3636
private let button = UIButton()
37+
private var maxLength: Int?
3738
weak var delegate: ReportTextViewDelegate?
39+
override var isFirstResponder: Bool {
40+
return textView.isFirstResponder
41+
}
3842

39-
init(type: ReportTextViewType, placeholder: String?) {
43+
init(type: ReportTextViewType, placeholder: String?, maxLength: Int? = nil) {
44+
self.maxLength = maxLength
4045
super.init(frame: .zero)
4146
configureAttribute(type: type, placeholder: placeholder)
4247
configureLayout(type: type)
@@ -158,11 +163,25 @@ extension ReportTextView: UITextViewDelegate {
158163

159164
func textViewDidChange(_ textView: UITextView) {
160165
updatePlaceholderVisibility()
166+
167+
if let maxLength = self.maxLength, (textView.text?.count ?? 0) > maxLength {
168+
textView.text = String(textView.text?.prefix(maxLength) ?? "")
169+
}
170+
161171
delegate?.reportTextViewDidChanged(self, text: textView.text)
162172
}
163173

164174
func textViewDidEndEditing(_ textView: UITextView) {
165175
updatePlaceholderVisibility()
166176
delegate?.reportTextViewDidChanged(self, text: textView.text)
167177
}
178+
179+
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
180+
guard let maxLength = self.maxLength else { return true }
181+
182+
let currentString = (textView.text ?? "") as NSString
183+
let newString = currentString.replacingCharacters(in: range, with: text)
184+
185+
return newString.count <= maxLength
186+
}
168187
}

Projects/Presentation/Sources/Report/View/ReportRegistrationViewController.swift

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ final class ReportRegistrationViewController: BaseViewController<ReportRegistrat
6363
private let photoCollectionView = UICollectionView(frame: .zero, collectionViewLayout: .init())
6464
private let categoryTextView = ReportTextView(type: .combo, placeholder: "카테고리 선택")
6565
private let reportTitleTextView = ReportTextView(type: .editable, placeholder: "제보 제목을 작성해주세요.")
66-
private let reportContentTextView = ReportTextView(type: .editable, placeholder: "어떤 위험인지 간단히 설명해주세요.")
66+
private let reportContentTextView = ReportTextView(
67+
type: .editable,
68+
placeholder: "어떤 위험인지 간단히 설명해주세요.",
69+
maxLength: 150)
6770
private let locationTextView = ReportTextView(type: .nonEditable, placeholder: "현재 위치 검색")
6871
private let contentTextCountLabel = UILabel()
6972
private let locationButton = LocationButton()
@@ -133,6 +136,9 @@ final class ReportRegistrationViewController: BaseViewController<ReportRegistrat
133136
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
134137
tapGesture.cancelsTouchesInView = false
135138
view.addGestureRecognizer(tapGesture)
139+
140+
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
141+
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
136142
}
137143

138144
override func configureLayout() {
@@ -514,7 +520,16 @@ final class ReportRegistrationViewController: BaseViewController<ReportRegistrat
514520
private func presentPhotoPicker() {
515521
var config = PHPickerConfiguration(photoLibrary: .shared())
516522
config.filter = .images
517-
config.selectionLimit = 3
523+
config.selectionLimit = viewModel.output.maxPhotoCount - viewModel.selectedPhotoCount
524+
525+
guard config.selectionLimit > 0 else {
526+
let message = "더 이상 사진을 선택할 수 없습니다."
527+
let alertController = UIAlertController(title: "알림", message: message, preferredStyle: .alert)
528+
alertController.addAction(UIAlertAction(title: "확인", style: .default))
529+
self.present(alertController, animated: true)
530+
return
531+
}
532+
518533
let picker = PHPickerViewController(configuration: config)
519534
picker.delegate = self
520535
present(picker, animated: true)
@@ -537,6 +552,24 @@ final class ReportRegistrationViewController: BaseViewController<ReportRegistrat
537552
@objc private func dismissKeyboard() {
538553
view.endEditing(true)
539554
}
555+
556+
@objc private func keyboardWillShow(notification: NSNotification) {
557+
guard let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
558+
let keyboardHeight = keyboardFrame.cgRectValue.height
559+
560+
scrollView.contentInset.bottom = keyboardHeight
561+
scrollView.verticalScrollIndicatorInsets.bottom = keyboardHeight
562+
563+
if reportContentTextView.isFirstResponder {
564+
let targetRect = reportContentTextView.convert(reportContentTextView.bounds, to: scrollView)
565+
scrollView.scrollRectToVisible(targetRect, animated: true)
566+
}
567+
}
568+
569+
@objc private func keyboardWillHide() {
570+
scrollView.contentInset = .zero
571+
scrollView.verticalScrollIndicatorInsets = .zero
572+
}
540573
}
541574

542575
extension ReportRegistrationViewController: ReportTextViewDelegate {

Projects/Presentation/Sources/Report/ViewModel/ReportRegistrationViewModel.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ final class ReportRegistrationViewModel: ViewModel {
4848
private let maxPhotoCount = 3
4949
private var location: LocationEntity? = nil
5050
private(set) var selectedReportType: ReportType?
51+
var selectedPhotoCount: Int {
52+
return selectedPhotoSubject.value.count
53+
}
5154

5255
init(reportUseCase: ReportUseCaseProtocol) {
5356
self.reportUseCase = reportUseCase

0 commit comments

Comments
 (0)