Skip to content

Commit

Permalink
feat: Swipe share, delete 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhyunm committed Sep 5, 2023
1 parent 21ccf7c commit dfbd921
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
41 changes: 41 additions & 0 deletions Diary/Controller/DiaryListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
1 change: 0 additions & 1 deletion Diary/DataManager/CoreDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Created by Max, Hemg on 2023/09/01.
//

import Foundation
import CoreData

class CoreDataManager {
Expand Down

0 comments on commit dfbd921

Please sign in to comment.