mirror of
https://github.com/dscyrescotti/Memola.git
synced 2026-05-10 09:50:04 +02:00
feat: save photo in persistence
This commit is contained in:
@@ -139,8 +139,9 @@ extension GraphicContext {
|
||||
}
|
||||
}
|
||||
case 1:
|
||||
#warning("TODO: implement photo")
|
||||
break
|
||||
guard let photo = element.photo, photo.imageURL != nil else { return }
|
||||
let _photo = Photo(object: photo)
|
||||
tree.insert(_photo.element, in: _photo.photoBox)
|
||||
default:
|
||||
break
|
||||
}
|
||||
@@ -150,9 +151,8 @@ extension GraphicContext {
|
||||
}
|
||||
|
||||
func loadQuads(_ bounds: CGRect, on context: NSManagedObjectContext) {
|
||||
#warning("TODO: implement photo")
|
||||
for _stroke in self.tree.search(box: bounds.box) {
|
||||
guard let stroke = _stroke.stroke(as: PenStroke.self), stroke.isEmpty else { continue }
|
||||
for element in self.tree.search(box: bounds.box) {
|
||||
guard let stroke = element.stroke(as: PenStroke.self), stroke.isEmpty else { continue }
|
||||
stroke.loadQuads(with: self)
|
||||
}
|
||||
}
|
||||
@@ -309,7 +309,26 @@ extension GraphicContext {
|
||||
let origin = point
|
||||
let bounds = [origin.x - size.width / 2, origin.y - size.height / 2, origin.x + size.width / 2, origin.y + size.height / 2]
|
||||
let photo = Photo(url: url, size: size, origin: origin, bounds: bounds, createdAt: .now)
|
||||
tree.insert(.photo(photo), in: photo.photoBox)
|
||||
tree.insert(photo.element, in: photo.photoBox)
|
||||
withPersistence(\.backgroundContext) { [_photo = photo, graphicContext = object] context in
|
||||
let photo = PhotoObject(\.backgroundContext)
|
||||
photo.imageURL = _photo.url
|
||||
photo.bounds = _photo.bounds
|
||||
photo.width = _photo.size.width
|
||||
photo.originY = _photo.origin.y
|
||||
photo.originX = _photo.origin.x
|
||||
photo.height = _photo.size.height
|
||||
photo.createdAt = _photo.createdAt
|
||||
let element = ElementObject(\.backgroundContext)
|
||||
element.createdAt = _photo.createdAt
|
||||
element.type = 1
|
||||
element.graphicContext = graphicContext
|
||||
photo.element = element
|
||||
element.photo = photo
|
||||
graphicContext?.elements.add(element)
|
||||
_photo.object = photo
|
||||
try context.saveIfNeeded()
|
||||
}
|
||||
self.previousElement = .photo(photo)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,15 +24,6 @@ final class Photo: @unchecked Sendable, Equatable, Comparable {
|
||||
var vertexCount: Int = 0
|
||||
var vertexBuffer: MTLBuffer?
|
||||
|
||||
init(image: UIImage?, size: CGSize, origin: CGPoint, bounds: [CGFloat], createdAt: Date) {
|
||||
self.size = size
|
||||
self.origin = origin
|
||||
self.image = image
|
||||
self.bounds = bounds
|
||||
self.createdAt = createdAt
|
||||
generateVertices()
|
||||
}
|
||||
|
||||
init(url: URL?, size: CGSize, origin: CGPoint, bounds: [CGFloat], createdAt: Date) {
|
||||
self.size = size
|
||||
self.origin = origin
|
||||
@@ -44,7 +35,7 @@ final class Photo: @unchecked Sendable, Equatable, Comparable {
|
||||
|
||||
convenience init(object: PhotoObject) {
|
||||
self.init(
|
||||
image: UIImage(data: object.image ?? .init()),
|
||||
url: object.imageURL,
|
||||
size: .init(width: object.width, height: object.height),
|
||||
origin: .init(x: object.originX, y: object.originY),
|
||||
bounds: object.bounds,
|
||||
@@ -117,4 +108,8 @@ extension Photo {
|
||||
func isVisible(in bounds: CGRect) -> Bool {
|
||||
bounds.contains(photoBounds) || bounds.intersects(photoBounds)
|
||||
}
|
||||
|
||||
var element: Element {
|
||||
.photo(self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,6 @@ class PhotoObject: NSManagedObject {
|
||||
@NSManaged var height: CGFloat
|
||||
@NSManaged var bounds: [CGFloat]
|
||||
@NSManaged var createdAt: Date?
|
||||
@NSManaged var image: Data?
|
||||
@NSManaged var imageURL: URL?
|
||||
@NSManaged var element: ElementObject?
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@
|
||||
<attribute name="bounds" attributeType="Transformable" valueTransformerName="NSSecureUnarchiveFromDataTransformer" customClassName="[CGFloat]"/>
|
||||
<attribute name="createdAt" attributeType="Date" usesScalarValueType="NO"/>
|
||||
<attribute name="height" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES"/>
|
||||
<attribute name="image" optional="YES" attributeType="Binary" allowsExternalBinaryDataStorage="YES"/>
|
||||
<attribute name="imageURL" optional="YES" attributeType="URI"/>
|
||||
<attribute name="originX" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES"/>
|
||||
<attribute name="originY" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES"/>
|
||||
<attribute name="width" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES"/>
|
||||
|
||||
Reference in New Issue
Block a user