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