From dfbd9218d9fd18bbcc90c53de678f66ef57c78e6 Mon Sep 17 00:00:00 2001 From: Min Hyun Date: Tue, 5 Sep 2023 10:48:55 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Swipe=20share,=20delete=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controller/DiaryListViewController.swift | 41 +++++++++++++++++++ Diary/DataManager/CoreDataManager.swift | 1 - 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Diary/Controller/DiaryListViewController.swift b/Diary/Controller/DiaryListViewController.swift index a154221d6..3265f0f42 100644 --- a/Diary/Controller/DiaryListViewController.swift +++ b/Diary/Controller/DiaryListViewController.swift @@ -90,6 +90,31 @@ extension DiaryListViewController: UITableViewDataSource, UITableViewDelegate { createVC.diary = diaryToEdit navigationController?.pushViewController(createVC, animated: true) } + + func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> + UISwipeActionsConfiguration? { + let delete = UIContextualAction(style: .normal, title: "") { (_, _, success: @escaping (Bool) -> Void) in + let selectedDiary = self.diaryList[indexPath.row] + + CoreDataManager.shared.deleteDiary(selectedDiary) + self.readCoreData() + success(true) + } + + let share = UIContextualAction(style: .normal, title: "") { (_, _, success: @escaping (Bool) -> Void) in + let selectedDiary = self.diaryList[indexPath.row] + + self.shareDiary(selectedDiary) + success(true) + } + + delete.backgroundColor = .systemRed + delete.image = UIImage(systemName: "trash.fill") + share.backgroundColor = .systemBlue + share.image = UIImage(systemName: "square.and.arrow.up") + + return UISwipeActionsConfiguration(actions: [delete, share]) + } } extension DiaryListViewController: DiaryListDelegate { @@ -101,4 +126,20 @@ extension DiaryListViewController: DiaryListDelegate { // TODO : AlertController Error } } + + 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) + } } diff --git a/Diary/DataManager/CoreDataManager.swift b/Diary/DataManager/CoreDataManager.swift index c6cda32b7..737eb6e7d 100644 --- a/Diary/DataManager/CoreDataManager.swift +++ b/Diary/DataManager/CoreDataManager.swift @@ -5,7 +5,6 @@ // Created by Max, Hemg on 2023/09/01. // -import Foundation import CoreData class CoreDataManager {