Skip to content

Commit

Permalink
feat: Alert protocol생성
Browse files Browse the repository at this point in the history
  • Loading branch information
hemg2 committed Sep 5, 2023
1 parent e89ff57 commit a9388f4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
12 changes: 12 additions & 0 deletions Diary.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
459D493A878338290A3E0AEF /* Pods_Diary.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EDF8EB908CAD9D062C72898B /* Pods_Diary.framework */; };
636B19AC2AA6C5E900B5242D /* AlertDisplayble.swift in Sources */ = {isa = PBXBuildFile; fileRef = 636B19AB2AA6C5E900B5242D /* AlertDisplayble.swift */; };
63BB62822A9F109400524DCB /* DecodingManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63BB62812A9F109400524DCB /* DecodingManager.swift */; };
63BB62B22AA181BE00524DCB /* Diary+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63BB62B02AA181BE00524DCB /* Diary+CoreDataClass.swift */; };
63BB62B32AA181BE00524DCB /* Diary+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63BB62B12AA181BE00524DCB /* Diary+CoreDataProperties.swift */; };
Expand All @@ -27,6 +28,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
636B19AB2AA6C5E900B5242D /* AlertDisplayble.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertDisplayble.swift; sourceTree = "<group>"; };
63BB62812A9F109400524DCB /* DecodingManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecodingManager.swift; sourceTree = "<group>"; };
63BB62B02AA181BE00524DCB /* Diary+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Diary+CoreDataClass.swift"; sourceTree = SOURCE_ROOT; };
63BB62B12AA181BE00524DCB /* Diary+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Diary+CoreDataProperties.swift"; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -79,6 +81,14 @@
name = Frameworks;
sourceTree = "<group>";
};
636B19AA2AA6C5C200B5242D /* Protocol */ = {
isa = PBXGroup;
children = (
636B19AB2AA6C5E900B5242D /* AlertDisplayble.swift */,
);
path = Protocol;
sourceTree = "<group>";
};
63BB62B62AA185E700524DCB /* DataManager */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -163,6 +173,7 @@
C739AE23284DF28600741E8F /* Diary */ = {
isa = PBXGroup;
children = (
636B19AA2AA6C5C200B5242D /* Protocol */,
63BB62B62AA185E700524DCB /* DataManager */,
63E5273E2A9ECD800000FBA6 /* Extension */,
63E5273D2A9ECD660000FBA6 /* Model */,
Expand Down Expand Up @@ -296,6 +307,7 @@
C739AE29284DF28600741E8F /* DiaryListViewController.swift in Sources */,
C739AE25284DF28600741E8F /* AppDelegate.swift in Sources */,
C739AE27284DF28600741E8F /* SceneDelegate.swift in Sources */,
636B19AC2AA6C5E900B5242D /* AlertDisplayble.swift in Sources */,
63BB62822A9F109400524DCB /* DecodingManager.swift in Sources */,
BA1A55ED2A9D90810012C89D /* DateFormatter+.swift in Sources */,
63E527372A9D87660000FBA6 /* DiaryListTableViewCell.swift in Sources */,
Expand Down
9 changes: 2 additions & 7 deletions Diary/Controller/CreateDiaryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import UIKit

final class CreateDiaryViewController: UIViewController {
final class CreateDiaryViewController: UIViewController, AlertDisplayable {
private let textView: UITextView = {
let textView = UITextView()
textView.translatesAutoresizingMaskIntoConstraints = false
Expand Down Expand Up @@ -76,8 +76,6 @@ final class CreateDiaryViewController: UIViewController {
}

private func showDeleteAlert() {
let alertController = UIAlertController(title: "진짜요?", message: "정말로 삭제하시겠어요?", preferredStyle: .alert)

let cancelAction = UIAlertAction(title: "취소", style: .cancel)
let deleteAction = UIAlertAction(title: "삭제", style: .destructive) { [weak self] _ in
guard let self else { return }
Expand All @@ -86,10 +84,7 @@ final class CreateDiaryViewController: UIViewController {
self.navigationController?.popViewController(animated: true)
}

alertController.addAction(cancelAction)
alertController.addAction(deleteAction)

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

private func saveDiary() {
Expand Down
5 changes: 3 additions & 2 deletions Diary/Controller/DiaryListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,14 @@ extension DiaryListViewController: UITableViewDataSource, UITableViewDelegate {
}
}

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

Expand Down
15 changes: 14 additions & 1 deletion Diary/Protocol/AlertDisplayble.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,17 @@
// Created by 1 on 2023/09/05.
//

import Foundation
import UIKit

protocol AlertDisplayable {
func showAlert(title: String?, message: String?, actions: [UIAlertAction])
}

extension AlertDisplayable where Self: UIViewController {
func showAlert(title: String?, message: String?, actions: [UIAlertAction]) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
actions.forEach { alertController.addAction($0) }

present(alertController, animated: true)
}
}

0 comments on commit a9388f4

Please sign in to comment.