Skip to content

Commit

Permalink
[release] release <- main
Browse files Browse the repository at this point in the history
[release] release <- main
  • Loading branch information
wltnryu authored Dec 3, 2024
2 parents 2a8e0e9 + 604b135 commit fa17f22
Show file tree
Hide file tree
Showing 87 changed files with 2,714 additions and 976 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ fastlane/test_output
timeline.xctimeline
playground.xcworkspace
*.xcconfig
GoogleService-Info.plist
14 changes: 14 additions & 0 deletions Data/CoreData/DTO/CollectionData+CoreDataClass.swift
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 {

}
22 changes: 22 additions & 0 deletions Data/CoreData/DTO/CollectionData+CoreDataProperties.swift
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?
}
3 changes: 0 additions & 3 deletions Data/CoreData/DTO/FigureData+CoreDataProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ extension FigureData {

@NSManaged public var id: String
@NSManaged public var head: String?
@NSManaged public var label: String?
@NSManaged public var figDesc: String?
@NSManaged public var coords: [String]
@NSManaged public var graphicCoord: [String]?

@NSManaged public var paperData: PaperData?
}
1 change: 1 addition & 0 deletions Data/CoreData/DTO/PaperData+CoreDataProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ extension PaperData {
@NSManaged public var folderID: UUID?

@NSManaged public var figureData: Set<FigureData>?
@NSManaged public var collectionData: Set<CollectionData>?
@NSManaged public var commentData: Set<CommentData>?
}
10 changes: 7 additions & 3 deletions Data/CoreData/Reazy.xcdatamodeld/Reazy.xcdatamodel/contents
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<attribute name="selectedLine" optional="YES" attributeType="Binary" valueTransformerName="CGRectValueTransformer"/>
<relationship name="paperData" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="PaperData" inverseName="buttonGroup" inverseEntity="PaperData"/>
</entity>
<entity name="CollectionData" representedClassName="CollectionData" syncable="YES">
<attribute name="coords" optional="YES" attributeType="Transformable" valueTransformerName="NSSecureUnarchiveFromData"/>
<attribute name="head" optional="YES" attributeType="String"/>
<attribute name="id" optional="YES" attributeType="String"/>
<relationship name="paperData" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="PaperData" inverseName="collectionData" inverseEntity="PaperData"/>
</entity>
<entity name="CommentData" representedClassName="CommentData" syncable="YES">
<attribute name="bounds" optional="YES" attributeType="Binary"/>
<attribute name="buttonID" optional="YES" attributeType="UUID" usesScalarValueType="NO"/>
Expand All @@ -19,11 +25,8 @@
</entity>
<entity name="FigureData" representedClassName="FigureData" syncable="YES">
<attribute name="coords" optional="YES" attributeType="Transformable" valueTransformerName="NSSecureUnarchiveFromData"/>
<attribute name="figDesc" optional="YES" attributeType="String"/>
<attribute name="graphicCoord" optional="YES" attributeType="Transformable" valueTransformerName="NSSecureUnarchiveFromData"/>
<attribute name="head" optional="YES" attributeType="String"/>
<attribute name="id" optional="YES" attributeType="String"/>
<attribute name="label" optional="YES" attributeType="String"/>
<relationship name="paperData" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="PaperData" inverseName="figureData" inverseEntity="PaperData"/>
</entity>
<entity name="FolderData" representedClassName="FolderData" syncable="YES">
Expand All @@ -47,6 +50,7 @@
<attribute name="title" optional="YES" attributeType="String"/>
<attribute name="url" optional="YES" attributeType="Binary"/>
<relationship name="buttonGroup" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="ButtonGroupData" inverseName="paperData" inverseEntity="ButtonGroupData"/>
<relationship name="collectionData" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="CollectionData" inverseName="paperData" inverseEntity="CollectionData"/>
<relationship name="commentData" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="CommentData" inverseName="paperData" inverseEntity="CommentData"/>
<relationship name="figureData" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="FigureData" inverseName="paperData" inverseEntity="FigureData"/>
</entity>
Expand Down
120 changes: 120 additions & 0 deletions Data/CoreData/Repositories/CollectionDataRepositoryImpl.swift
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)
}
}
}
12 changes: 2 additions & 10 deletions Data/CoreData/Repositories/FigureDataRepositoryImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ class FigureDataRepositoryImpl: FigureDataRepository {
return Figure(
id: figureData.id,
head: figureData.head,
label: figureData.label,
figDesc: figureData.figDesc,
coords: figureData.coords,
graphicCoord: figureData.graphicCoord
coords: figureData.coords
)
}
return .success(figures)
Expand Down Expand Up @@ -55,10 +52,7 @@ class FigureDataRepositoryImpl: FigureDataRepository {

newFigureData.id = figure.id
newFigureData.head = figure.head
newFigureData.label = figure.label
newFigureData.figDesc = figure.figDesc
newFigureData.coords = figure.coords
newFigureData.graphicCoord = figure.graphicCoord

newFigureData.paperData = paperData

Expand Down Expand Up @@ -94,10 +88,7 @@ class FigureDataRepositoryImpl: FigureDataRepository {
if let figureToEdit = result.first {

figureToEdit.head = figure.head
figureToEdit.label = figure.label
figureToEdit.figDesc = figure.figDesc
figureToEdit.coords = figure.coords
figureToEdit.graphicCoord = figure.graphicCoord

try dataContext.save()
return .success(VoidResponse())
Expand Down Expand Up @@ -143,6 +134,7 @@ class FigureDataRepositoryImpl: FigureDataRepository {
dataToEdit.isFavorite = info.isFavorite
dataToEdit.memo = info.memo
dataToEdit.isFigureSaved = info.isFigureSaved
dataToEdit.focusURL = info.focusURL

try dataContext.save()
return .success(VoidResponse())
Expand Down
33 changes: 20 additions & 13 deletions Data/CoreData/Repositories/PaperDataRepositoryImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@ class PaperDataRepositoryImpl: PaperDataRepository {
let results = try dataContext.fetch(fetchRequest)
if let dataToEdit = results.first {
if info.title != dataToEdit.title {
let url = try URL(resolvingBookmarkData: dataToEdit.url, bookmarkDataIsStale: &isStale)
let newURL = url.deletingLastPathComponent().appending(path: "\(info.title).pdf")

try FileManager.default.moveItem(at: url, to: newURL)

dataToEdit.url = try newURL.bookmarkData(options: .minimalBookmark)
if let url = try? URL.init(resolvingBookmarkData: info.url, bookmarkDataIsStale: &isStale) {
// 실제 파일 이름 변경
let newUrl = url.deletingLastPathComponent().appending(path: info.title + ".pdf")

if let _ = try? FileManager.default.moveItem(at: url, to: newUrl) {
dataToEdit.url = try! newUrl.bookmarkData(options: .minimalBookmark)
} else {
return .failure(PDFUploadError.fileNameDuplication)
}
}
}

// 기존 데이터 수정
Expand All @@ -100,7 +104,6 @@ class PaperDataRepositoryImpl: PaperDataRepository {
return .failure(NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Data not found"]))
}
} catch {
print(error)
return .failure(error)
}
}
Expand All @@ -111,15 +114,22 @@ class PaperDataRepositoryImpl: PaperDataRepository {
let fetchRequest: NSFetchRequest<PaperData> = PaperData.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "id == %@", id as CVarArg)

var isStale: Bool = false
var isStaleOriginal = false
var isStaleConcentrate = false

do {
let results = try dataContext.fetch(fetchRequest)

if let dataToDelete = results.first {
// 실제 파일 삭제
if let url = try? URL.init(resolvingBookmarkData: dataToDelete.url, bookmarkDataIsStale: &isStale),
FileManager.default.fileExists(atPath: url.path()) {
if let url = try? URL.init(resolvingBookmarkData: dataToDelete.url, bookmarkDataIsStale: &isStaleOriginal),
let _ = try? Data(contentsOf: url) {
try FileManager.default.removeItem(at: url)
}

if let focusURL = dataToDelete.focusURL,
let url = try? URL.init(resolvingBookmarkData: focusURL, bookmarkDataIsStale: &isStaleConcentrate),
let _ = try? Data(contentsOf: url) {
try FileManager.default.removeItem(at: url)
}

Expand Down Expand Up @@ -168,12 +178,9 @@ class PaperDataRepositoryImpl: PaperDataRepository {
for figure in figures {
let temp = FigureData(context: dataContext)
temp.coords = figure.coords
temp.figDesc = figure.figDesc
temp.graphicCoord = figure.graphicCoord
temp.head = figure.head
temp.id = figure.id
temp.paperData = newPaperData
temp.label = figure.label

figureData.insert(temp)
}
Expand Down
3 changes: 1 addition & 2 deletions Data/Extension/FileManager + Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ extension FileManager {
guard !fileExists(atPath: newUrl.path()) else { return false }

do {
try copyItem(at: fileURL, to: newUrl)
try removeItem(at: fileURL)
try moveItem(at: fileURL, to: newUrl)
} catch {
print(error)
return false
Expand Down
Loading

0 comments on commit fa17f22

Please sign in to comment.