Skip to content

Commit

Permalink
feat: ShareDiary 프로토콜 추가 및 delegate 삭제, 오류수정
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhyunm committed Sep 5, 2023
1 parent 5855f85 commit 5beba54
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 55 deletions.
4 changes: 4 additions & 0 deletions Diary.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
BA1A55EB2A9D84AF0012C89D /* DiaryEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA1A55EA2A9D84AF0012C89D /* DiaryEntity.swift */; };
BA1A55ED2A9D90810012C89D /* DateFormatter+.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA1A55EC2A9D90810012C89D /* DateFormatter+.swift */; };
BABBDAE52A9F13A200D8D50B /* DecodingError.swift in Sources */ = {isa = PBXBuildFile; fileRef = BABBDAE42A9F13A200D8D50B /* DecodingError.swift */; };
BABBDB342AA6D05A00D8D50B /* ShareDiary.swift in Sources */ = {isa = PBXBuildFile; fileRef = BABBDB332AA6D05A00D8D50B /* ShareDiary.swift */; };
C739AE25284DF28600741E8F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C739AE24284DF28600741E8F /* AppDelegate.swift */; };
C739AE27284DF28600741E8F /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C739AE26284DF28600741E8F /* SceneDelegate.swift */; };
C739AE29284DF28600741E8F /* DiaryListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C739AE28284DF28600741E8F /* DiaryListViewController.swift */; };
Expand All @@ -40,6 +41,7 @@
BA1A55EA2A9D84AF0012C89D /* DiaryEntity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiaryEntity.swift; sourceTree = "<group>"; };
BA1A55EC2A9D90810012C89D /* DateFormatter+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DateFormatter+.swift"; sourceTree = "<group>"; };
BABBDAE42A9F13A200D8D50B /* DecodingError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecodingError.swift; sourceTree = "<group>"; };
BABBDB332AA6D05A00D8D50B /* ShareDiary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareDiary.swift; sourceTree = "<group>"; };
C739AE21284DF28600741E8F /* Diary.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Diary.app; sourceTree = BUILT_PRODUCTS_DIR; };
C739AE24284DF28600741E8F /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
C739AE26284DF28600741E8F /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -85,6 +87,7 @@
isa = PBXGroup;
children = (
636B19AB2AA6C5E900B5242D /* AlertDisplayble.swift */,
BABBDB332AA6D05A00D8D50B /* ShareDiary.swift */,
);
path = Protocol;
sourceTree = "<group>";
Expand Down Expand Up @@ -315,6 +318,7 @@
63BB62B52AA182AA00524DCB /* CoreDataManager.swift in Sources */,
63E527392A9D97160000FBA6 /* CreateDiaryViewController.swift in Sources */,
BABBDAE52A9F13A200D8D50B /* DecodingError.swift in Sources */,
BABBDB342AA6D05A00D8D50B /* ShareDiary.swift in Sources */,
63BB62B32AA181BE00524DCB /* Diary+CoreDataProperties.swift in Sources */,
BA1A55EB2A9D84AF0012C89D /* DiaryEntity.swift in Sources */,
63BB62B22AA181BE00524DCB /* Diary+CoreDataClass.swift in Sources */,
Expand Down
37 changes: 21 additions & 16 deletions Diary/Controller/CreateDiaryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@

import UIKit

final class CreateDiaryViewController: UIViewController, AlertDisplayable {
final class CreateDiaryViewController: UIViewController, AlertDisplayable, ShareDiary {
private let textView: UITextView = {
let textView = UITextView()
textView.translatesAutoresizingMaskIntoConstraints = false
textView.font = .preferredFont(forTextStyle: .body)

return textView
}()

weak var delegate: DiaryListDelegate?

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

init(_ diary: Diary? = nil) {
if diary == nil {
self.diary = CoreDataManager.shared.createDiary()
} else {
self.diary = diary
}
init() {
self.diary = CoreDataManager.shared.createDiary()
super.init(nibName: nil, bundle: nil)
}

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

Expand All @@ -36,7 +36,7 @@ final class CreateDiaryViewController: UIViewController, AlertDisplayable {
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
configureTextView()
setupBodyText()
setupNavigationBarButton()
setupNotification()
}
Expand All @@ -51,11 +51,6 @@ final class CreateDiaryViewController: UIViewController, AlertDisplayable {
private func configureUI() {
view.backgroundColor = .systemBackground
self.title = DateFormatter().formatToString(from: Date(), with: "YYYY년 MM월 dd일")

}

private func configureTextView() {
textView.text = "\(diary?.title ?? "")\n\(diary?.body ?? "")"
view.addSubview(textView)

NSLayoutConstraint.activate([
Expand All @@ -66,6 +61,16 @@ final class CreateDiaryViewController: UIViewController, AlertDisplayable {
])
}

private func setupBodyText() {
guard let diary,
let title = diary.title,
let body = diary.body else {
return
}

textView.text = "\(title)\n\(body)"
}

private func setupNavigationBarButton() {
let moreButton = UIBarButtonItem(title: "더보기", style: .plain, target: self, action: #selector(showMoreOptions))
navigationItem.rightBarButtonItem = moreButton
Expand Down Expand Up @@ -134,7 +139,7 @@ extension CreateDiaryViewController {

let shareAction = UIAlertAction(title: "Share...", style: .default) { [weak self] _ in
guard let self else { return }
self.delegate?.shareDiary(self.diary)
self.shareDiary(self.diary)
}

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
Expand Down
51 changes: 13 additions & 38 deletions Diary/Controller/DiaryListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@

import UIKit

protocol DiaryListDelegate: AnyObject {
func readCoreData()
func shareDiary(_ diary: Diary?)
}

final class DiaryListViewController: UIViewController {
final class DiaryListViewController: UIViewController, AlertDisplayable, ShareDiary {
private let tableView: UITableView = {
let tableView = UITableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -26,7 +21,6 @@ final class DiaryListViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

readCoreData()
configureUI()
setupNavigationBarButton()
setupTableView()
Expand Down Expand Up @@ -56,7 +50,6 @@ 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 @@ -68,6 +61,17 @@ final class DiaryListViewController: UIViewController {
tableView.delegate = self
tableView.register(DiaryListTableViewCell.self, forCellReuseIdentifier: DiaryListTableViewCell.identifier)
}

private func readCoreData() {
do {
let fetchedDiaries = try container.viewContext.fetch(Diary.fetchRequest())
diaryList = fetchedDiaries.filter { $0.title != nil }
tableView.reloadData()
} catch {
let cancelAction = UIAlertAction(title: "확인", style: .cancel)
showAlert(title: "로드 실패", message: "데이터를 불러오지 못했습니다.", actions: [cancelAction])
}
}
}

extension DiaryListViewController: UITableViewDataSource, UITableViewDelegate {
Expand Down Expand Up @@ -95,8 +99,7 @@ extension DiaryListViewController: UITableViewDataSource, UITableViewDelegate {
tableView.deselectRow(at: indexPath, animated: true)
let diaryToEdit = diaryList[indexPath.row]
let createVC = CreateDiaryViewController(diaryToEdit)

createVC.delegate = self

navigationController?.pushViewController(createVC, animated: true)
}

Expand Down Expand Up @@ -125,31 +128,3 @@ extension DiaryListViewController: UITableViewDataSource, UITableViewDelegate {
return UISwipeActionsConfiguration(actions: [delete, share])
}
}

extension DiaryListViewController: DiaryListDelegate, AlertDisplayable {
func readCoreData() {
do {
diaryList = try container.viewContext.fetch(Diary.fetchRequest())
tableView.reloadData()
} catch {
let cancelAction = UIAlertAction(title: "확인", style: .cancel)
showAlert(title: "로드 실패", message: "데이터를 불러오지 못했습니다.", actions: [cancelAction])
}
}

func shareDiary(_ diary: Diary?) {
guard let diary,
let title = diary.title,
let createdAt = diary.createdAt,
let body = diary.body else {
return
}

let date = dateFormatter.formatToString(from: createdAt, with: "YYYY년 MM월 dd일")
let shareText = "제목: \(title)\n작성일자: \(date)\n내용: \(body)"
let activityViewController = UIActivityViewController(activityItems: [shareText], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view

self.present(activityViewController, animated: true, completion: nil)
}
}
2 changes: 1 addition & 1 deletion Diary/Protocol/AlertDisplayble.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// AlertDisplayble.swift
// Diary
//
// Created by 1 on 2023/09/05.
// Created by Max, Hemg on 2023/09/05.
//

import UIKit
Expand Down
30 changes: 30 additions & 0 deletions Diary/Protocol/ShareDiary.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// ShareDiary.swift
// Diary
//
// Created by Max, Hemg on 2023/09/05.
//

import UIKit

protocol ShareDiary {
func shareDiary(_ diary: Diary?)
}

extension ShareDiary where Self: UIViewController {
func shareDiary(_ diary: Diary?) {
guard let diary,
let title = diary.title,
let createdAt = diary.createdAt,
let body = diary.body else {
return
}

let date = DateFormatter().formatToString(from: createdAt, with: "YYYY년 MM월 dd일")
let shareText = "제목: \(title)\n작성일자: \(date)\n내용: \(body)"
let activityViewController = UIActivityViewController(activityItems: [shareText], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view

self.present(activityViewController, animated: true, completion: nil)
}
}

0 comments on commit 5beba54

Please sign in to comment.