Skip to content

Commit

Permalink
feat: DiaryManager 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhyunm committed Sep 21, 2023
1 parent 7f22d7f commit fc50531
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 123 deletions.
12 changes: 8 additions & 4 deletions Diary.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
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 */; };
63BB62B52AA182AA00524DCB /* CoreDataManagerProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63BB62B42AA182AA00524DCB /* CoreDataManagerProtocol.swift */; };
63E527352A9D7EBF0000FBA6 /* .swiftlint.yml in Resources */ = {isa = PBXBuildFile; fileRef = 63E527342A9D7EBF0000FBA6 /* .swiftlint.yml */; };
63E527372A9D87660000FBA6 /* DiaryListTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63E527362A9D87660000FBA6 /* DiaryListTableViewCell.swift */; };
63E527392A9D97160000FBA6 /* DiaryDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63E527382A9D97160000FBA6 /* DiaryDetailViewController.swift */; };
BA6463162ABC6DDE0080E80D /* DiaryManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA6463152ABC6DDE0080E80D /* DiaryManager.swift */; };
BABBDAE52A9F13A200D8D50B /* DecodingError.swift in Sources */ = {isa = PBXBuildFile; fileRef = BABBDAE42A9F13A200D8D50B /* DecodingError.swift */; };
BABBDB342AA6D05A00D8D50B /* ShareDisplayable.swift in Sources */ = {isa = PBXBuildFile; fileRef = BABBDB332AA6D05A00D8D50B /* ShareDisplayable.swift */; };
BABBDB362AAD904100D8D50B /* CoreDataError.swift in Sources */ = {isa = PBXBuildFile; fileRef = BABBDB352AAD904100D8D50B /* CoreDataError.swift */; };
Expand All @@ -43,11 +44,12 @@
63BB62812A9F109400524DCB /* DecodingManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecodingManager.swift; sourceTree = "<group>"; };
63BB62B02AA181BE00524DCB /* Diary+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "Diary+CoreDataClass.swift"; path = "Diary/Model/CoreData/Diary+CoreDataClass.swift"; sourceTree = SOURCE_ROOT; };
63BB62B12AA181BE00524DCB /* Diary+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "Diary+CoreDataProperties.swift"; path = "Diary/Model/CoreData/Diary+CoreDataProperties.swift"; sourceTree = SOURCE_ROOT; };
63BB62B42AA182AA00524DCB /* CoreDataManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataManager.swift; sourceTree = "<group>"; };
63BB62B42AA182AA00524DCB /* CoreDataManagerProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataManagerProtocol.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 /* DiaryDetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiaryDetailViewController.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>"; };
BA6463152ABC6DDE0080E80D /* DiaryManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiaryManager.swift; sourceTree = "<group>"; };
BABBDAE42A9F13A200D8D50B /* DecodingError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecodingError.swift; sourceTree = "<group>"; };
BABBDB332AA6D05A00D8D50B /* ShareDisplayable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareDisplayable.swift; sourceTree = "<group>"; };
BABBDB352AAD904100D8D50B /* CoreDataError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataError.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -122,7 +124,8 @@
children = (
63BB62B02AA181BE00524DCB /* Diary+CoreDataClass.swift */,
63BB62B12AA181BE00524DCB /* Diary+CoreDataProperties.swift */,
63BB62B42AA182AA00524DCB /* CoreDataManager.swift */,
63BB62B42AA182AA00524DCB /* CoreDataManagerProtocol.swift */,
BA6463152ABC6DDE0080E80D /* DiaryManager.swift */,
);
path = CoreData;
sourceTree = "<group>";
Expand Down Expand Up @@ -360,6 +363,7 @@
files = (
C739AE29284DF28600741E8F /* DiaryListViewController.swift in Sources */,
C739AE25284DF28600741E8F /* AppDelegate.swift in Sources */,
BA6463162ABC6DDE0080E80D /* DiaryManager.swift in Sources */,
C739AE27284DF28600741E8F /* SceneDelegate.swift in Sources */,
BAECB2DD2AB187D6006B4A46 /* ImageCachingManager.swift in Sources */,
63BB62822A9F109400524DCB /* DecodingManager.swift in Sources */,
Expand All @@ -368,7 +372,7 @@
BAECB2E82AB72869006B4A46 /* AlertBuilder.swift in Sources */,
C739AE2F284DF28600741E8F /* Diary.xcdatamodeld in Sources */,
BAECB2D12AB157CB006B4A46 /* NetworkConfiguration.swift in Sources */,
63BB62B52AA182AA00524DCB /* CoreDataManager.swift in Sources */,
63BB62B52AA182AA00524DCB /* CoreDataManagerProtocol.swift in Sources */,
632F74F02AB14D2C003E1B97 /* NetworkManager.swift in Sources */,
BAECB2D92AB18611006B4A46 /* DiaryV2.xcmappingmodel in Sources */,
63E527392A9D97160000FBA6 /* DiaryDetailViewController.swift in Sources */,
Expand Down
6 changes: 3 additions & 3 deletions Diary/App/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?
let coreDataManager = CoreDataManager(entityName: "Diary")
let diaryManager = DiaryManager<Diary>()

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }

window = UIWindow(windowScene: windowScene)
let diaryListViewController = DiaryListViewController()
diaryListViewController.coreDataManager = coreDataManager
diaryListViewController.diaryManager = diaryManager
let navigationController = UINavigationController(rootViewController: diaryListViewController)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
Expand Down Expand Up @@ -51,7 +51,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {

// Save changes in the application's managed object context when the application transitions to the background.
do {
try coreDataManager.saveContext()
try diaryManager.saveContext()
} catch {
fatalError(CoreDataError.saveFailure.message)
}
Expand Down
16 changes: 8 additions & 8 deletions Diary/Controller/DiaryDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ final class DiaryDetailViewController: UIViewController, ShareDisplayable {
return textView
}()

private let coreDataManager: CoreDataManager
private let diaryManager: DiaryManager<Diary>
private var diary: Diary
private var isNew: Bool
private var latitude: Double?
private var longitude: Double?

init(latitude: Double?, longitude: Double?, coreDataManager: CoreDataManager) {
self.coreDataManager = coreDataManager
self.diary = coreDataManager.createDiary()
init(latitude: Double?, longitude: Double?, diaryManager: DiaryManager<Diary>) {
self.diaryManager = diaryManager
self.diary = diaryManager.createDiary()
self.isNew = true
self.latitude = latitude
self.longitude = longitude
Expand All @@ -33,8 +33,8 @@ final class DiaryDetailViewController: UIViewController, ShareDisplayable {
fetchWeather()
}

init(_ diary: Diary, coreDataManager: CoreDataManager) {
self.coreDataManager = coreDataManager
init(_ diary: Diary, diaryManager: DiaryManager<Diary>) {
self.diaryManager = diaryManager
self.diary = diary
self.isNew = false

Expand Down Expand Up @@ -137,7 +137,7 @@ final class DiaryDetailViewController: UIViewController, ShareDisplayable {
alertBuilder.addAction(.delete) { [weak self] _ in
guard let self else { return }
do {
try coreDataManager.deleteEntity(self.diary)
try diaryManager.deleteData(self.diary)
self.navigationController?.popViewController(animated: true)
} catch CoreDataError.deleteFailure {
let additionalAlertBuilder = AlertBuilder(viewController: self, prefferedStyle: .alert)
Expand Down Expand Up @@ -194,7 +194,7 @@ extension DiaryDetailViewController: UITextViewDelegate {
guard !contents.isEmpty else { return }

do {
try coreDataManager.saveContext()
try diaryManager.saveContext()
} catch CoreDataError.saveFailure {
let alertBulder = AlertBuilder(viewController: self, prefferedStyle: .alert)
alertBulder.setType(.coreDataError(error: .saveFailure))
Expand Down
33 changes: 11 additions & 22 deletions Diary/Controller/DiaryListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class DiaryListViewController: UIViewController {
return formatter
}()

var coreDataManager: CoreDataManager?
var diaryManager: DiaryManager<Diary>?
private var diaryList = [Diary]()

private var latitude: Double?
Expand Down Expand Up @@ -62,10 +62,10 @@ final class DiaryListViewController: UIViewController {

private func setupNavigationBarButton() {
let addDiary = UIAction(image: UIImage(systemName: "plus")) { [weak self] _ in
guard let self, let coreDataManager else { return }
guard let self, let diaryManager else { return }
let createDiaryView = DiaryDetailViewController(latitude: self.latitude,
longitude: self.longitude,
coreDataManager: coreDataManager)
diaryManager: diaryManager)
self.navigationController?.pushViewController(createDiaryView, animated: true)
}

Expand All @@ -79,14 +79,10 @@ final class DiaryListViewController: UIViewController {
}

private func readCoreData() {
guard let coreDataManager else { return }
guard let diaryManager else { return }

do {
guard let fetchedDiaries = try coreDataManager.fetchEntity(sortBy: "createdAt") as? [Diary] else {
throw CoreDataError.unknown
}

diaryList = fetchedDiaries.filter { $0.title != nil }
diaryList = try diaryManager.fetchDiary()
tableView.reloadData()
} catch CoreDataError.dataNotFound {
let alertBuilder = AlertBuilder(viewController: self, prefferedStyle: .alert)
Expand Down Expand Up @@ -137,21 +133,21 @@ extension DiaryListViewController: UITableViewDelegate, ShareDisplayable {
tableView.deselectRow(at: indexPath, animated: true)
let diaryToEdit = diaryList[indexPath.row]

guard let coreDataManager else { return }
guard let diaryManager else { return }

let createVC = DiaryDetailViewController(diaryToEdit, coreDataManager: coreDataManager)
let createVC = DiaryDetailViewController(diaryToEdit, diaryManager: diaryManager)

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

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) ->
UISwipeActionsConfiguration? {
guard let coreDataManager else { return nil }
guard let diaryManager else { return nil }

let delete = UIContextualAction(style: .normal, title: "") { (_, _, success: @escaping (Bool) -> Void) in
let selectedDiary = self.diaryList[indexPath.row]
do {
try coreDataManager.deleteEntity(selectedDiary)
try diaryManager.deleteData(selectedDiary)
self.readCoreData()
success(true)
} catch CoreDataError.deleteFailure {
Expand Down Expand Up @@ -208,18 +204,11 @@ extension DiaryListViewController: CLLocationManagerDelegate {

extension DiaryListViewController: UISearchBarDelegate {
func searchDiary(with keyword: String) {
guard let coreDataManager else { return }
guard let diaryManager else { return }

if keyword.count > 0 {
do {
let predicate = "title CONTAINS[cd] %@ OR body CONTAINS[cd] %@"
guard let fetchedDiaries = try coreDataManager.filterEntity(keyword,
predicate: predicate,
sortBy: "createdAt") as? [Diary] else {
throw CoreDataError.unknown
}

diaryList = fetchedDiaries.filter { $0.title != nil }
diaryList = try diaryManager.filterDiary(keyword)
tableView.reloadData()
} catch CoreDataError.dataNotFound {
let alertBuilder = AlertBuilder(viewController: self, prefferedStyle: .alert)
Expand Down
10 changes: 10 additions & 0 deletions Diary/Error/CoreDataError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@
import Foundation

enum CoreDataError: Error {
case createFailure
case dataNotFound
case saveFailure
case updateFailure
case deleteFailure
case unknown

var alertTitle: String {
switch self {
case .createFailure:
return NSLocalizedString("createFailureTitle", comment: "")
case .dataNotFound:
return NSLocalizedString("dataNotFoundTitle", comment: "")
case .saveFailure:
return NSLocalizedString("saveFailureTitle", comment: "")
case .updateFailure:
return NSLocalizedString("updateFailureTitle", comment: "")
case .deleteFailure:
return NSLocalizedString("deleteFailureTitle", comment: "")
case .unknown:
Expand All @@ -28,10 +34,14 @@ enum CoreDataError: Error {

var message: String {
switch self {
case .createFailure:
return NSLocalizedString("createFailure", comment: "")
case .dataNotFound:
return NSLocalizedString("dataNotFound", comment: "")
case .saveFailure:
return NSLocalizedString("saveFailure", comment: "")
case .updateFailure:
return NSLocalizedString("updateFailure", comment: "")
case .deleteFailure:
return NSLocalizedString("deleteFailure", comment: "")
case .unknown:
Expand Down
86 changes: 0 additions & 86 deletions Diary/Model/CoreData/CoreDataManager.swift

This file was deleted.

Loading

0 comments on commit fc50531

Please sign in to comment.