Skip to content

Commit

Permalink
feat: 새로운 일기 생성 및 저장 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhyunm committed Sep 1, 2023
1 parent e7a1f0d commit 6b02f7a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 30 deletions.
39 changes: 36 additions & 3 deletions Diary/Controller/CreateDiaryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,23 @@ final class CreateDiaryViewController: UIViewController {
return textView
}()

weak var delegate: DiaryListDelegate?
private let container = CoreDataManager.shared.persistentContainer
var diary: Diary?

override func viewDidLoad() {
super.viewDidLoad()
createDiary()
configureUI()
setupNotification()
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
saveDiary()
delegate?.readCoreData()
}

private func configureUI() {
view.backgroundColor = .systemBackground
self.title = DateFormatter().formatToString(from: Date(), with: "YYYY년 MM월 dd일")
Expand Down Expand Up @@ -51,15 +62,37 @@ final class CreateDiaryViewController: UIViewController {

@objc private func keyboardWillShow(_ notification: Notification) {
guard let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey]
as? CGRect else {
return
}
as? CGRect else { return }

textView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardFrame.size.height, right: 0)
}

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

private func createDiary() {
let newDiary = Diary(context: container.viewContext)
newDiary.id = UUID()
newDiary.createdAt = Date()

diary = newDiary
}

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")

guard let diary else { return }

diary.title = "\(title)"
diary.body = body

CoreDataManager.shared.saveContext()
}

deinit {
Expand Down
45 changes: 19 additions & 26 deletions Diary/Controller/DiaryListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@

import UIKit

protocol DiaryListDelegate: AnyObject {
func readCoreData()
}

final class DiaryListViewController: UIViewController {
private let tableView: UITableView = {
let tableView = UITableView()
tableView.translatesAutoresizingMaskIntoConstraints = false

return tableView
}()
private var diaryEntities = [DiaryEntity]()

private let dateFormatter = DateFormatter()
private let container = CoreDataManager.shard.persistentContainer
private let container = CoreDataManager.shared.persistentContainer
private var diaryList = [Diary]()

override func viewDidLoad() {
super.viewDidLoad()

setupData()

readCoreData()
configureUI()
setupTableView()
Expand All @@ -34,6 +37,7 @@ final class DiaryListViewController: UIViewController {
let addDiary = UIAction(image: UIImage(systemName: "plus")) { [weak self] _ in
guard let self else { return }
let createDiaryView = CreateDiaryViewController()
createDiaryView.delegate = self
self.navigationController?.pushViewController(createDiaryView, animated: true)
}

Expand All @@ -54,28 +58,6 @@ final class DiaryListViewController: UIViewController {
tableView.delegate = self
tableView.register(DiaryListTableViewCell.self, forCellReuseIdentifier: DiaryListTableViewCell.identifier)
}

private func setupData() {
do {
let loadDiaryEntity: [DiaryEntity] = try DecodingManager.decodeJson(from: "sample")
diaryEntities = loadDiaryEntity
} catch DecodingError.fileNotFound {
print(DecodingError.fileNotFound.message)
} catch DecodingError.decodingFailure {
print(DecodingError.decodingFailure.message)
} catch {
print(DecodingError.unknown.message)
}
}

private func readCoreData() {
do {
let diaryList = try container.viewContext.fetch(Diary.fetchRequest())
tableView.reloadData()
} catch {
// TODO : AlertController Error
}
}
}

extension DiaryListViewController: UITableViewDataSource, UITableViewDelegate {
Expand Down Expand Up @@ -103,3 +85,14 @@ extension DiaryListViewController: UITableViewDataSource, UITableViewDelegate {
tableView.deselectRow(at: indexPath, animated: true)
}
}

extension DiaryListViewController: DiaryListDelegate {
func readCoreData() {
do {
diaryList = try container.viewContext.fetch(Diary.fetchRequest())
tableView.reloadData()
} catch {
// TODO : AlertController Error
}
}
}
2 changes: 1 addition & 1 deletion Diary/DataManager/CoreDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation
import CoreData

class CoreDataManager {
static let shard = CoreDataManager()
static let shared = CoreDataManager()
private init() {}

// MARK: - Core Data stack
Expand Down

0 comments on commit 6b02f7a

Please sign in to comment.