Skip to content

Commit

Permalink
Make CreateReaderAnnotationsDbRequest a class
Browse files Browse the repository at this point in the history
  • Loading branch information
mvasilak committed Dec 9, 2024
1 parent 9630d1e commit af36e6e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@ import Foundation

import RealmSwift

struct CreateHtmlEpubAnnotationsDbRequest: CreateReaderAnnotationsDbRequest {
let attachmentKey: String
let libraryId: LibraryIdentifier
let annotations: [HtmlEpubAnnotation]
let userId: Int
class CreateHtmlEpubAnnotationsDbRequest: CreateReaderAnnotationsDbRequest<HtmlEpubAnnotation> {
override func addFields(for annotation: HtmlEpubAnnotation, to item: RItem, database: Realm) {
super.addFields(for: annotation, to: item, database: database)

unowned let schemaController: SchemaController

func addExtraFields(for annotation: HtmlEpubAnnotation, to item: RItem, database: Realm) {
for field in FieldKeys.Item.Annotation.extraHtmlEpubFields(for: annotation.type) {
let value: String

Expand Down Expand Up @@ -59,7 +54,7 @@ struct CreateHtmlEpubAnnotationsDbRequest: CreateReaderAnnotationsDbRequest {
}
}

func addTags(for annotation: HtmlEpubAnnotation, to item: RItem, database: Realm) {
override func addTags(for annotation: HtmlEpubAnnotation, to item: RItem, database: Realm) {
let allTags = database.objects(RTag.self)

for tag in annotation.tags {
Expand All @@ -73,6 +68,4 @@ struct CreateHtmlEpubAnnotationsDbRequest: CreateReaderAnnotationsDbRequest {
rTypedTag.tag = rTag
}
}

func addAdditionalProperties(for annotation: HtmlEpubAnnotation, fromRestore: Bool, to item: RItem, changes: inout RItemChanges, database: Realm) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ import Foundation

import RealmSwift

struct CreatePDFAnnotationsDbRequest: CreateReaderAnnotationsDbRequest {
let attachmentKey: String
let libraryId: LibraryIdentifier
let annotations: [PDFDocumentAnnotation]
let userId: Int

unowned let schemaController: SchemaController
class CreatePDFAnnotationsDbRequest: CreateReaderAnnotationsDbRequest<PDFDocumentAnnotation> {
unowned let boundingBoxConverter: AnnotationBoundingBoxConverter

func addExtraFields(for annotation: PDFDocumentAnnotation, to item: RItem, database: Realm) {
init(
attachmentKey: String,
libraryId: LibraryIdentifier,
annotations: [PDFDocumentAnnotation],
userId: Int,
schemaController: SchemaController,
boundingBoxConverter: AnnotationBoundingBoxConverter
) {
self.boundingBoxConverter = boundingBoxConverter
super.init(attachmentKey: attachmentKey, libraryId: libraryId, annotations: annotations, userId: userId, schemaController: schemaController)
}

override func addFields(for annotation: PDFDocumentAnnotation, to item: RItem, database: Realm) {
super.addFields(for: annotation, to: item, database: database)

for field in FieldKeys.Item.Annotation.extraPDFFields(for: annotation.type) {
let rField = RItemField()
rField.key = field.key
Expand Down Expand Up @@ -50,9 +58,7 @@ struct CreatePDFAnnotationsDbRequest: CreateReaderAnnotationsDbRequest {
}
}

func addTags(for annotation: PDFDocumentAnnotation, to item: RItem, database: Realm) { }

func addAdditionalProperties(for annotation: PDFDocumentAnnotation, fromRestore: Bool, to item: RItem, changes: inout RItemChanges, database: Realm) {
override func addAdditionalProperties(for annotation: PDFDocumentAnnotation, fromRestore: Bool, to item: RItem, changes: inout RItemChanges, database: Realm) {
add(rects: annotation.rects, fromRestore: fromRestore, to: item, changes: &changes, database: database)
add(paths: annotation.paths, fromRestore: fromRestore, to: item, changes: &changes, database: database)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,23 @@ import Foundation
import CocoaLumberjackSwift
import RealmSwift

protocol CreateReaderAnnotationsDbRequest: DbRequest {
associatedtype Annotation: ReaderAnnotation

var attachmentKey: String { get }
var libraryId: LibraryIdentifier { get }
var annotations: [Annotation] { get }
var userId: Int { get }
var schemaController: SchemaController { get }

func create(annotation: Annotation, parent: RItem, in database: Realm)
func addFields(for annotation: Annotation, to item: RItem, database: Realm)
func addExtraFields(for annotation: Annotation, to item: RItem, database: Realm)
func addTags(for annotation: Annotation, to item: RItem, database: Realm)
func addAdditionalProperties(for annotation: Annotation, fromRestore: Bool, to item: RItem, changes: inout RItemChanges, database: Realm)
}
class CreateReaderAnnotationsDbRequest<Annotation: ReaderAnnotation>: DbRequest {
let attachmentKey: String
let libraryId: LibraryIdentifier
let annotations: [Annotation]
let userId: Int
unowned let schemaController: SchemaController

extension CreateReaderAnnotationsDbRequest {
var needsWrite: Bool { return true }

init(attachmentKey: String, libraryId: LibraryIdentifier, annotations: [Annotation], userId: Int, schemaController: SchemaController) {
self.attachmentKey = attachmentKey
self.libraryId = libraryId
self.annotations = annotations
self.userId = userId
self.schemaController = schemaController
}

func process(in database: Realm) throws {
guard let parent = database.objects(RItem.self).uniqueObject(key: attachmentKey, libraryId: libraryId) else { return }

Expand Down Expand Up @@ -75,12 +73,11 @@ extension CreateReaderAnnotationsDbRequest {
let user = database.object(ofType: RUser.self, forPrimaryKey: userId)
item.createdBy = user
if user == nil {
DDLogWarn("CreateReaderAnnotationsDbRequest: user not found for userId \(userId)")
DDLogWarn("CreateReaderAnnotationsDbRequest: user not found for userId \(userId) when creating annotation \(annotation.key) in library \(libraryId)")
}
}

addFields(for: annotation, to: item, database: database)
addExtraFields(for: annotation, to: item, database: database)
addTags(for: annotation, to: item, database: database)
// We need to submit tags on creation even if they are empty, so we need to mark them as changed
var changes: RItemChanges = [.parent, .fields, .type, .tags]
Expand Down Expand Up @@ -119,4 +116,8 @@ extension CreateReaderAnnotationsDbRequest {
item.fields.append(rField)
}
}

func addTags(for annotation: Annotation, to item: RItem, database: Realm) { }

func addAdditionalProperties(for annotation: Annotation, fromRestore: Bool, to item: RItem, changes: inout RItemChanges, database: Realm) { }
}

0 comments on commit af36e6e

Please sign in to comment.