-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release] release <- main
- Loading branch information
Showing
87 changed files
with
2,714 additions
and
976 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,3 +70,4 @@ fastlane/test_output | |
timeline.xctimeline | ||
playground.xcworkspace | ||
*.xcconfig | ||
GoogleService-Info.plist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// CollectionData+CoreDataClass.swift | ||
// Reazy | ||
// | ||
// Created by 유지수 on 11/27/24. | ||
// | ||
|
||
import Foundation | ||
import CoreData | ||
|
||
@objc(CollectionData) | ||
public class CollectionData: NSManagedObject { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// CollectionData+CoreDataProperties.swift | ||
// Reazy | ||
// | ||
// Created by 유지수 on 11/27/24. | ||
// | ||
|
||
import Foundation | ||
import CoreData | ||
|
||
extension CollectionData { | ||
|
||
@nonobjc public class func fetchRequest() -> NSFetchRequest<CollectionData> { | ||
return NSFetchRequest<CollectionData>(entityName: "CollectionData") | ||
} | ||
|
||
@NSManaged public var id: String | ||
@NSManaged public var head: String? | ||
@NSManaged public var coords: [String] | ||
|
||
@NSManaged public var paperData: PaperData? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
Data/CoreData/Repositories/CollectionDataRepositoryImpl.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// | ||
// CollectionDataRepositoryImpl.swift | ||
// Reazy | ||
// | ||
// Created by 유지수 on 11/27/24. | ||
// | ||
|
||
import Foundation | ||
import CoreData | ||
|
||
class CollectionDataRepositoryImpl: CollectionDataRepository { | ||
private let container: NSPersistentContainer = PersistantContainer.shared.container | ||
|
||
func loadCollectionData(for pdfID: UUID) -> Result<[Figure], any Error> { | ||
let dataContext = container.viewContext | ||
let fetchRequest: NSFetchRequest<CollectionData> = CollectionData.fetchRequest() | ||
fetchRequest.predicate = NSPredicate(format: "paperData.id == %@", pdfID as CVarArg) | ||
|
||
do { | ||
let fetchedCollections = try dataContext.fetch(fetchRequest) | ||
let collections = fetchedCollections.map { collectionData -> Figure in | ||
|
||
return Figure( | ||
id: collectionData.id, | ||
head: collectionData.head, | ||
coords: collectionData.coords | ||
) | ||
} | ||
return .success(collections) | ||
} catch { | ||
return .failure(error) | ||
} | ||
} | ||
|
||
func saveCollectionData(for pdfID: UUID, with collection: Figure) -> Result<VoidResponse, any Error> { | ||
var result: Result<VoidResponse, any Error>? | ||
|
||
/// NSManagedObject는 Thread-safe 하지 못해 하나의 쓰레드에서만 사용해야 함 | ||
/// 해결 방법으로 performBackgroundTask 사용 | ||
container.performBackgroundTask { context in | ||
// 저장되어 있는 것을 우선으로 하는 merge policy | ||
context.mergePolicy = NSMergePolicy(merge: .mergeByPropertyStoreTrumpMergePolicyType) | ||
|
||
let fetchRequest: NSFetchRequest<PaperData> = PaperData.fetchRequest() | ||
fetchRequest.predicate = NSPredicate(format: "id == %@", pdfID as CVarArg) | ||
|
||
do { | ||
if let paperData = try context.fetch(fetchRequest).first { | ||
|
||
let newCollectionData = CollectionData(context: context) | ||
|
||
newCollectionData.id = collection.id | ||
newCollectionData.head = collection.head | ||
newCollectionData.coords = collection.coords | ||
|
||
newCollectionData.paperData = paperData | ||
|
||
do { | ||
try context.save() | ||
result = .success(.init()) | ||
} catch { | ||
print(String(describing: error)) | ||
result = .failure(error) | ||
} | ||
} else { | ||
result = .failure(NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "CollectionData not found"])) | ||
} | ||
} catch { | ||
result = .failure(error) | ||
} | ||
} | ||
|
||
if result == nil { | ||
return .failure(NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "CollectionData not found"])) | ||
} | ||
|
||
return result! | ||
} | ||
|
||
func editFigureData(for pdfID: UUID, with collection: Figure) -> Result<VoidResponse, any Error> { | ||
let dataContext = container.viewContext | ||
let fetchRequest: NSFetchRequest<CollectionData> = CollectionData.fetchRequest() | ||
fetchRequest.predicate = NSPredicate(format: "paperData.id == %@ AND id == %@", pdfID as CVarArg, collection.id as CVarArg) | ||
|
||
do { | ||
let result = try dataContext.fetch(fetchRequest) | ||
if let collectionToEdit = result.first { | ||
|
||
collectionToEdit.head = collection.head | ||
collectionToEdit.coords = collection.coords | ||
|
||
try dataContext.save() | ||
return .success(VoidResponse()) | ||
} else { | ||
return .failure(NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Collection not found"])) | ||
} | ||
} catch { | ||
return .failure(error) | ||
} | ||
} | ||
|
||
func deleteCollectionData(for pdfID: UUID, id: String) -> Result<VoidResponse, any Error> { | ||
let dataContext = container.viewContext | ||
let fetchRequest: NSFetchRequest<CollectionData> = CollectionData.fetchRequest() | ||
fetchRequest.predicate = NSPredicate(format: "paperData.id == %@ AND id == %@", pdfID as CVarArg, id as CVarArg) | ||
|
||
do { | ||
let result = try dataContext.fetch(fetchRequest) | ||
if let collectionToDelete = result.first { | ||
dataContext.delete(collectionToDelete) | ||
try dataContext.save() | ||
return .success(VoidResponse()) | ||
} else { | ||
return .failure(NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Collection not found"])) | ||
} | ||
} catch { | ||
return .failure(error) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.