Skip to content

Commit

Permalink
refactor: UITextViewDelegate 추가, 일기 자동 저장 기능 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhyunm committed Sep 5, 2023
1 parent 5beba54 commit a7c664a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Diary/App/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
// to restore the scene back to its current state.

// Save changes in the application's managed object context when the application transitions to the background.
// (UIApplication.shared.delegate as? AppDelegate)?.saveContext()
CoreDataManager.shared.saveContext()
}


Expand Down
62 changes: 33 additions & 29 deletions Diary/Controller/CreateDiaryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ final class CreateDiaryViewController: UIViewController, AlertDisplayable, Share
}()

private let container = CoreDataManager.shared.persistentContainer
var diary: Diary?
var diary: Diary
var isNew: Bool

init() {
self.diary = CoreDataManager.shared.createDiary()
self.isNew = true
super.init(nibName: nil, bundle: nil)
}

init(_ diary: Diary) {
self.diary = diary
self.isNew = false
super.init(nibName: nil, bundle: nil)
}

Expand All @@ -40,18 +43,16 @@ final class CreateDiaryViewController: UIViewController, AlertDisplayable, Share
setupNavigationBarButton()
setupNotification()
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if diary != nil {
saveDiary()
}
}


private func configureUI() {
view.backgroundColor = .systemBackground
self.title = DateFormatter().formatToString(from: Date(), with: "YYYY년 MM월 dd일")
view.addSubview(textView)
textView.delegate = self

if isNew {
textView.becomeFirstResponder()
}

NSLayoutConstraint.activate([
textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
Expand All @@ -62,8 +63,7 @@ final class CreateDiaryViewController: UIViewController, AlertDisplayable, Share
}

private func setupBodyText() {
guard let diary,
let title = diary.title,
guard let title = diary.title,
let body = diary.body else {
return
}
Expand Down Expand Up @@ -94,28 +94,13 @@ final class CreateDiaryViewController: UIViewController, AlertDisplayable, Share
let cancelAction = UIAlertAction(title: "취소", style: .cancel)
let deleteAction = UIAlertAction(title: "삭제", style: .destructive) { [weak self] _ in
guard let self else { return }
self.deleteDiary(self.diary)
CoreDataManager.shared.deleteDiary(diary)
self.navigationController?.popViewController(animated: true)
}

showAlert(title: "진짜요?", message: "정말로 삭제하시겠어요?", actions: [cancelAction, deleteAction])
}

private func saveDiary() {
let contents = textView.text.split(separator: "\n")
guard !contents.isEmpty,
let title = contents.first else { return }

let body = contents.dropFirst().joined(separator: "\n")

CoreDataManager.shared.saveDiary(title: "\(title)", body: body, diary: diary)
}

private func deleteDiary(_ diary: Diary?) {
CoreDataManager.shared.deleteDiary(diary)
self.diary = nil
}


deinit {
NotificationCenter.default.removeObserver(self)
}
Expand Down Expand Up @@ -153,6 +138,25 @@ extension CreateDiaryViewController {

@objc private func keyboardWillHide(_ notification: Notification) {
textView.contentInset = .zero
saveDiary()
}
}

extension CreateDiaryViewController: UITextViewDelegate {
func textViewDidEndEditing(_ textView: UITextView) {
let contents = textView.text.split(separator: "\n")
guard !contents.isEmpty else { return }

CoreDataManager.shared.saveContext()
}

func textViewDidChange(_ textView: UITextView) {
let contents = textView.text.split(separator: "\n")
guard !contents.isEmpty,
let title = contents.first else { return }

let body = contents.dropFirst().joined(separator: "\n")

diary.title = "\(title)"
diary.body = body
}
}
10 changes: 1 addition & 9 deletions Diary/DataManager/CoreDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,14 @@ class CoreDataManager {
static let shared = CoreDataManager()
private init() {}

func createDiary() -> Diary? {
func createDiary() -> Diary {
let newDiary = Diary(context: persistentContainer.viewContext)
newDiary.id = UUID()
newDiary.createdAt = Date()

return newDiary
}

func saveDiary(title: String, body: String, diary: Diary?) {
guard let diary else { return }
diary.title = title
diary.body = body

saveContext()
}

func deleteDiary(_ diary: Diary?) {
if let diary = diary {
persistentContainer.viewContext.delete(diary)
Expand Down

0 comments on commit a7c664a

Please sign in to comment.