Skip to content

Commit

Permalink
refactor: preferredStyle 추가 , Namespace추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hemg2 committed Sep 10, 2023
1 parent f864693 commit bab2de1
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 36 deletions.
16 changes: 12 additions & 4 deletions Diary.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
/* 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 */; };
63B12BAE2AAD9C9000D614A6 /* AlertNamespace.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63B12BAD2AAD9C9000D614A6 /* AlertNamespace.swift */; };
63B12BB02AAD9D3400D614A6 /* ButtonNamespace.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63B12BAF2AAD9D3400D614A6 /* ButtonNamespace.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 */; };
63BB62B52AA182AA00524DCB /* CoreDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63BB62B42AA182AA00524DCB /* CoreDataManager.swift */; };
63E527352A9D7EBF0000FBA6 /* .swiftlint.yml in Resources */ = {isa = PBXBuildFile; fileRef = 63E527342A9D7EBF0000FBA6 /* .swiftlint.yml */; };
63E527372A9D87660000FBA6 /* DiaryListTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63E527362A9D87660000FBA6 /* DiaryListTableViewCell.swift */; };
63E527392A9D97160000FBA6 /* CreateDiaryViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63E527382A9D97160000FBA6 /* CreateDiaryViewController.swift */; };
63E527392A9D97160000FBA6 /* DiaryDetailViewContoller.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63E527382A9D97160000FBA6 /* DiaryDetailViewContoller.swift */; };
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 */; };
Expand All @@ -31,13 +33,15 @@

/* Begin PBXFileReference section */
636B19AB2AA6C5E900B5242D /* AlertDisplayble.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertDisplayble.swift; sourceTree = "<group>"; };
63B12BAD2AAD9C9000D614A6 /* AlertNamespace.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertNamespace.swift; sourceTree = "<group>"; };
63B12BAF2AAD9D3400D614A6 /* ButtonNamespace.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonNamespace.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; };
63BB62B42AA182AA00524DCB /* CoreDataManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataManager.swift; sourceTree = "<group>"; };
63E527342A9D7EBF0000FBA6 /* .swiftlint.yml */ = {isa = PBXFileReference; lastKnownFileType = text.yaml; path = .swiftlint.yml; sourceTree = "<group>"; };
63E527362A9D87660000FBA6 /* DiaryListTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiaryListTableViewCell.swift; sourceTree = "<group>"; };
63E527382A9D97160000FBA6 /* CreateDiaryViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreateDiaryViewController.swift; sourceTree = "<group>"; };
63E527382A9D97160000FBA6 /* DiaryDetailViewContoller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiaryDetailViewContoller.swift; sourceTree = "<group>"; };
7B86D20E06F2B506DECF94F6 /* Pods-Diary.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Diary.release.xcconfig"; path = "Target Support Files/Pods-Diary/Pods-Diary.release.xcconfig"; sourceTree = "<group>"; };
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>"; };
Expand Down Expand Up @@ -118,6 +122,8 @@
63BB62B12AA181BE00524DCB /* Diary+CoreDataProperties.swift */,
BA1A55EA2A9D84AF0012C89D /* DiaryEntity.swift */,
63BB62812A9F109400524DCB /* DecodingManager.swift */,
63B12BAD2AAD9C9000D614A6 /* AlertNamespace.swift */,
63B12BAF2AAD9D3400D614A6 /* ButtonNamespace.swift */,
);
path = Model;
sourceTree = "<group>";
Expand All @@ -134,7 +140,7 @@
isa = PBXGroup;
children = (
C739AE28284DF28600741E8F /* DiaryListViewController.swift */,
63E527382A9D97160000FBA6 /* CreateDiaryViewController.swift */,
63E527382A9D97160000FBA6 /* DiaryDetailViewContoller.swift */,
);
path = Controller;
sourceTree = "<group>";
Expand Down Expand Up @@ -319,12 +325,14 @@
63E527372A9D87660000FBA6 /* DiaryListTableViewCell.swift in Sources */,
C739AE2F284DF28600741E8F /* Diary.xcdatamodeld in Sources */,
63BB62B52AA182AA00524DCB /* CoreDataManager.swift in Sources */,
63E527392A9D97160000FBA6 /* CreateDiaryViewController.swift in Sources */,
63E527392A9D97160000FBA6 /* DiaryDetailViewContoller.swift in Sources */,
BABBDAE52A9F13A200D8D50B /* DecodingError.swift in Sources */,
BABBDB362AAD904100D8D50B /* CoreDataError.swift in Sources */,
BABBDB342AA6D05A00D8D50B /* ShareDisplayable.swift in Sources */,
63B12BB02AAD9D3400D614A6 /* ButtonNamespace.swift in Sources */,
63BB62B32AA181BE00524DCB /* Diary+CoreDataProperties.swift in Sources */,
BA1A55EB2A9D84AF0012C89D /* DiaryEntity.swift in Sources */,
63B12BAE2AAD9C9000D614A6 /* AlertNamespace.swift in Sources */,
63BB62B22AA181BE00524DCB /* Diary+CoreDataClass.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// CreateDiaryViewController.swift
// DiaryDetailViewContoller.swift
// Diary
//
// Created by Maxhyunm, Hamg on 2023/08/29.
//

import UIKit

final class CreateDiaryViewController: UIViewController, AlertDisplayable, ShareDisplayable {
final class DiaryDetailViewContoller: UIViewController, AlertDisplayable, ShareDisplayable {
private let textView: UITextView = {
let textView = UITextView()
textView.translatesAutoresizingMaskIntoConstraints = false
Expand Down Expand Up @@ -72,7 +72,10 @@ final class CreateDiaryViewController: UIViewController, AlertDisplayable, Share
}

private func setupNavigationBarButton() {
let moreButton = UIBarButtonItem(title: "더보기", style: .plain, target: self, action: #selector(showMoreOptions))
let moreButton = UIBarButtonItem(title: ButtonNamespace.more,
style: .plain,
target: self,
action: #selector(showMoreOptions))
navigationItem.rightBarButtonItem = moreButton
}

Expand All @@ -91,34 +94,39 @@ final class CreateDiaryViewController: UIViewController, AlertDisplayable, Share
}

private func showDeleteAlert() {
let cancelAction = UIAlertAction(title: "취소", style: .cancel)
let deleteAction = UIAlertAction(title: "삭제", style: .destructive) { [weak self] _ in
let cancelAction = UIAlertAction(title: ButtonNamespace.cancel, style: .cancel)
let deleteAction = UIAlertAction(title: ButtonNamespace.delete, style: .destructive) { [weak self] _ in
guard let self else { return }
do {
try CoreDataManager.shared.deleteDiary(self.diary)
self.navigationController?.popViewController(animated: true)
} catch CoreDataError.deleteFailure {
let cancelAction = UIAlertAction(title: "확인", style: .cancel)
let cancelAction = UIAlertAction(title: ButtonNamespace.confirm, style: .cancel)
self.showAlert(title: CoreDataError.deleteFailure.alertTitle,
message: CoreDataError.deleteFailure.message,
actions: [cancelAction])
actions: [cancelAction],
preferredStyle: .alert)
} catch {
let cancelAction = UIAlertAction(title: "확인", style: .cancel)
let cancelAction = UIAlertAction(title: ButtonNamespace.confirm, style: .cancel)
self.showAlert(title: CoreDataError.deleteFailure.alertTitle,
message: CoreDataError.unknown.message,
actions: [cancelAction])
actions: [cancelAction],
preferredStyle: .alert)
}
}

showAlert(title: "진짜요?", message: "정말로 삭제하시겠어요?", actions: [cancelAction, deleteAction])
showAlert(title: AlertNamespace.deleteTitle,
message: AlertNamespace.deleteMessage,
actions: [cancelAction, deleteAction],
preferredStyle: .alert)
}

deinit {
NotificationCenter.default.removeObserver(self)
}
}

extension CreateDiaryViewController {
extension DiaryDetailViewContoller {
@objc private func keyboardWillShow(_ notification: Notification) {
guard let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey]
as? CGRect else { return }
Expand All @@ -129,17 +137,17 @@ extension CreateDiaryViewController {
@objc private func showMoreOptions() {
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

let deleteAction = UIAlertAction(title: "Delete", style: .destructive) { [weak self] _ in
let deleteAction = UIAlertAction(title: ButtonNamespace.deleteEnglish, style: .destructive) { [weak self] _ in
guard let self else { return }
self.showDeleteAlert()
}

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

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
let cancelAction = UIAlertAction(title: ButtonNamespace.cancelEnglish, style: .cancel)

alertController.addAction(shareAction)
alertController.addAction(deleteAction)
Expand All @@ -153,23 +161,25 @@ extension CreateDiaryViewController {
}
}

extension CreateDiaryViewController: UITextViewDelegate {
extension DiaryDetailViewContoller: UITextViewDelegate {
func textViewDidEndEditing(_ textView: UITextView) {
let contents = textView.text.split(separator: "\n")
guard !contents.isEmpty else { return }

do {
try CoreDataManager.shared.saveContext()
} catch CoreDataError.saveFailure {
let cancelAction = UIAlertAction(title: "확인", style: .cancel)
let cancelAction = UIAlertAction(title: ButtonNamespace.confirm, style: .cancel)
self.showAlert(title: CoreDataError.saveFailure.alertTitle,
message: CoreDataError.saveFailure.message,
actions: [cancelAction])
actions: [cancelAction],
preferredStyle: .alert)
} catch {
let cancelAction = UIAlertAction(title: "확인", style: .cancel)
let cancelAction = UIAlertAction(title: ButtonNamespace.confirm, style: .cancel)
self.showAlert(title: CoreDataError.saveFailure.alertTitle,
message: CoreDataError.unknown.message,
actions: [cancelAction])
actions: [cancelAction],
preferredStyle: .alert)
}
}

Expand Down
24 changes: 14 additions & 10 deletions Diary/Controller/DiaryListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ final class DiaryListViewController: UIViewController {
private func setupNavigationBarButton() {
let addDiary = UIAction(image: UIImage(systemName: "plus")) { [weak self] _ in
guard let self else { return }
let createDiaryView = CreateDiaryViewController()
let createDiaryView = DiaryDetailViewContoller()
self.navigationController?.pushViewController(createDiaryView, animated: true)
}

Expand All @@ -70,15 +70,17 @@ extension DiaryListViewController: AlertDisplayable {
diaryList = fetchedDiaries.filter { $0.title != nil }
tableView.reloadData()
} catch CoreDataError.dataNotFound {
let cancelAction = UIAlertAction(title: "확인", style: .cancel)
let cancelAction = UIAlertAction(title: ButtonNamespace.confirm, style: .cancel)
showAlert(title: CoreDataError.dataNotFound.alertTitle,
message: CoreDataError.dataNotFound.message,
actions: [cancelAction])
actions: [cancelAction],
preferredStyle: .alert)
} catch {
let cancelAction = UIAlertAction(title: "확인", style: .cancel)
let cancelAction = UIAlertAction(title: ButtonNamespace.confirm, style: .cancel)
showAlert(title: CoreDataError.dataNotFound.alertTitle,
message: CoreDataError.unknown.message,
actions: [cancelAction])
actions: [cancelAction],
preferredStyle: .alert)
}
}
}
Expand Down Expand Up @@ -111,7 +113,7 @@ extension DiaryListViewController: UITableViewDelegate, ShareDisplayable {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let diaryToEdit = diaryList[indexPath.row]
let createVC = CreateDiaryViewController(diaryToEdit)
let createVC = DiaryDetailViewContoller(diaryToEdit)

navigationController?.pushViewController(createVC, animated: true)
}
Expand All @@ -125,15 +127,17 @@ extension DiaryListViewController: UITableViewDelegate, ShareDisplayable {
self.readCoreData()
success(true)
} catch CoreDataError.deleteFailure {
let cancelAction = UIAlertAction(title: "확인", style: .cancel)
let cancelAction = UIAlertAction(title: ButtonNamespace.confirm, style: .cancel)
self.showAlert(title: CoreDataError.deleteFailure.alertTitle,
message: CoreDataError.deleteFailure.message,
actions: [cancelAction])
actions: [cancelAction],
preferredStyle: .alert)
} catch {
let cancelAction = UIAlertAction(title: "확인", style: .cancel)
let cancelAction = UIAlertAction(title: ButtonNamespace.confirm, style: .cancel)
self.showAlert(title: CoreDataError.deleteFailure.alertTitle,
message: CoreDataError.unknown.message,
actions: [cancelAction])
actions: [cancelAction],
preferredStyle: .alert)
}
}

Expand Down
11 changes: 11 additions & 0 deletions Diary/Model/AlertNamespace.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// AlertNamespace.swift
// Diary
//
// Created by Maxhyunm, Hamg on 2023/09/10.
//

enum AlertNamespace {
static let deleteTitle = "진짜요?"
static let deleteMessage = "정말로 삭제하시겠어요?"
}
16 changes: 16 additions & 0 deletions Diary/Model/ButtonNamespace.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// ButtonNamespace.swift
// Diary
//
// Created by Maxhyunm, Hamg on 2023/09/10.
//

enum ButtonNamespace {
static let more = "더보기"
static let cancel = "취소"
static let delete = "삭제"
static let confirm = "확인"
static let deleteEnglish = "Delete"
static let shareEnglish = "Share..."
static let cancelEnglish = "Cancel"
}
9 changes: 6 additions & 3 deletions Diary/Protocol/AlertDisplayble.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import UIKit

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

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

present(alertController, animated: true)
Expand Down

0 comments on commit bab2de1

Please sign in to comment.