-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
프로젝트 매니저 [STEP 2] Hemg #313
base: ic_9_hemg22
Are you sure you want to change the base?
Changes from all commits
7de4e2b
af73086
68c6381
647d89e
9acef1b
3fb04ce
4c34572
5cc83b0
0283d80
0d02fd0
adeb261
9795be3
6891729
3e630d2
68d0c7b
9f2006c
b8462e4
640478a
a7c1ce4
985dd48
77580b8
0e2e827
d6cf6fa
8b65688
6d7bd96
81240ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
// | ||
// AddTodoViewController.swift | ||
// ProjectManager | ||
// | ||
// Created by Hemg on 2023/09/26. | ||
// | ||
|
||
import UIKit | ||
|
||
protocol AddTodoDelegate: AnyObject { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AnyObject 는 무엇일까요 ? |
||
func didAddTodoItem(title: String, body: String, date: Date) | ||
func didEditTodoItem(title: String, body: String, date: Date, index: Int) | ||
} | ||
|
||
final class AddTodoViewController: UIViewController { | ||
private let titleTextField: UITextField = { | ||
let textField = UITextField() | ||
textField.translatesAutoresizingMaskIntoConstraints = false | ||
textField.font = .preferredFont(forTextStyle: .title1) | ||
textField.placeholder = "Title" | ||
textField.layer.borderColor = UIColor.lightGray.cgColor | ||
textField.layer.borderWidth = 1.0 | ||
|
||
return textField | ||
}() | ||
|
||
private let bodyTextView: UITextView = { | ||
let textView = UITextView() | ||
textView.translatesAutoresizingMaskIntoConstraints = false | ||
textView.font = .preferredFont(forTextStyle: .body) | ||
textView.text = "여기는 할일 내용 입력하는 곳입니다." | ||
textView.textColor = .placeholderText | ||
textView.layer.borderColor = UIColor.lightGray.cgColor | ||
textView.layer.borderWidth = 1.0 | ||
|
||
return textView | ||
}() | ||
|
||
private let datePicker: UIDatePicker = { | ||
let datePicker = UIDatePicker() | ||
datePicker.translatesAutoresizingMaskIntoConstraints = false | ||
datePicker.preferredDatePickerStyle = .wheels | ||
datePicker.datePickerMode = .date | ||
|
||
return datePicker | ||
}() | ||
|
||
weak var delegate: AddTodoDelegate? | ||
private var todoItems: ProjectManager? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todoItems: ProjectManager? |
||
private var isNew: Bool | ||
|
||
init() { | ||
self.isNew = true | ||
super.init(nibName: nil, bundle: nil) | ||
} | ||
|
||
init(todoItems: ProjectManager?) { | ||
self.isNew = false | ||
self.todoItems = todoItems | ||
super.init(nibName: nil, bundle: nil) | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
setUpViewController() | ||
setUpBarButtonItem() | ||
configureUI() | ||
setUpViewLayout() | ||
setUpItemValues() | ||
} | ||
|
||
private func setUpViewController() { | ||
view.backgroundColor = .systemBackground | ||
title = "TODO" | ||
bodyTextView.delegate = self | ||
} | ||
|
||
private func setUpBarButtonItem() { | ||
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(doneButton)) | ||
navigationItem.rightBarButtonItem = doneButton | ||
|
||
if isNew == true { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. optional도 아닌데 == 이 필요할까요 ? |
||
let cancelButton = UIBarButtonItem(title: "Cancel", style: .done, target: self, action: #selector(cancelButton)) | ||
navigationItem.leftBarButtonItem = cancelButton | ||
} else { | ||
let editButton = UIBarButtonItem(title: "Edit", style: .done, target: self, action: #selector(editButton)) | ||
navigationItem.leftBarButtonItem = editButton | ||
} | ||
} | ||
|
||
private func setUpItemValues() { | ||
if let todoItems = todoItems { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 맨 앞줄이라면 guard문도 좋은 선택지가 될 것 같습니다. |
||
titleTextField.text = todoItems.title | ||
bodyTextView.text = todoItems.body | ||
datePicker.date = todoItems.date | ||
} | ||
} | ||
|
||
@objc private func doneButton() { | ||
setUpItemText() | ||
dismiss(animated: true) | ||
} | ||
|
||
@objc private func cancelButton() { | ||
dismiss(animated: true) | ||
} | ||
|
||
@objc private func editButton() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 눌렸을때 메서드명이 조금 이상하네요. |
||
setUpItemText() | ||
dismiss(animated: true) | ||
} | ||
|
||
private func configureUI() { | ||
view.addSubview(titleTextField) | ||
view.addSubview(bodyTextView) | ||
view.addSubview(datePicker) | ||
} | ||
|
||
private func setUpViewLayout() { | ||
NSLayoutConstraint.activate([ | ||
titleTextField.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor), | ||
titleTextField.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 4), | ||
titleTextField.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -4), | ||
|
||
datePicker.topAnchor.constraint(equalTo: titleTextField.bottomAnchor, constant: 4), | ||
datePicker.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 4), | ||
datePicker.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -4), | ||
|
||
bodyTextView.topAnchor.constraint(equalTo: datePicker.bottomAnchor, constant: 4), | ||
bodyTextView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 4), | ||
bodyTextView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -4), | ||
bodyTextView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -4) | ||
]) | ||
} | ||
|
||
private func setUpItemText() { | ||
if isNew == false { | ||
let date = datePicker.date | ||
guard let titleText = titleTextField.text, | ||
let bodyText = bodyTextView.text else { return } | ||
todoItems?.title = titleText | ||
todoItems?.body = bodyText | ||
todoItems?.date = date | ||
|
||
delegate?.didEditTodoItem(title: titleText, body: bodyText, date: date, index: 0) | ||
} else { | ||
let date = datePicker.date | ||
guard let titleText = titleTextField.text, | ||
let bodyText = bodyTextView.text else { return } | ||
|
||
delegate?.didAddTodoItem(title: titleText, body: bodyText, date: date) | ||
} | ||
} | ||
} | ||
|
||
extension AddTodoViewController: UITextViewDelegate { | ||
func textViewDidBeginEditing(_ textView: UITextView) { | ||
if textView.text == "여기는 할일 내용 입력하는 곳입니다." { | ||
textView.text = "" | ||
textView.textColor = .label | ||
} | ||
} | ||
|
||
func textViewDidEndEditing(_ textView: UITextView) { | ||
if textView.text.isEmpty { | ||
textView.text = "여기는 할일 내용 입력하는 곳입니다." | ||
textView.textColor = .placeholderText | ||
} | ||
} | ||
|
||
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { | ||
let currentText = textView.text ?? "" | ||
guard let stringRange = Range(range, in: currentText) else { return false } | ||
let changedText = currentText.replacingCharacters(in: stringRange, with: text) | ||
|
||
return changedText.count <= 999 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ReuseIdentifier 도 Namespace 인가요 ?