-
Notifications
You must be signed in to change notification settings - Fork 0
/
Drawing.swift
59 lines (48 loc) · 1.61 KB
/
Drawing.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// Drawing.swift
// APIExample_RTM2x
//
// Created by BBC on 2024/4/24.
//
import Foundation
import SwiftUI
// For real-time update
struct DrawingPoint: Codable {
var id: UUID
var point: CGPoint
}
struct Drawing: Identifiable, Codable {
var id: UUID = UUID()
var points: [CGPoint] = []
var color: Color = .black
var lineWidth: CGFloat = 5
init() {
}
private enum CodingKeys: String, CodingKey {
case id, points, color, lineWidth
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(UUID.self, forKey: .id)
points = try container.decode([CGPoint].self, forKey: .points)
let colorString = try container.decode(String.self, forKey: .color)
color = Color(hex: colorString) ?? .black
lineWidth = try container.decode(CGFloat.self, forKey: .lineWidth)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(points, forKey: .points)
let colorData = color.toHex()
try container.encode(colorData, forKey: .color)
try container.encode(lineWidth, forKey: .lineWidth)
}
}
// This will limit the points/location to 2 decimals and save over 70% of data
extension CGPoint {
func roundTo2Decimals() -> CGPoint {
let newX = (self.x * 100).rounded() / 100
let newY = (self.y * 100).rounded() / 100
return CGPoint(x: newX, y: newY)
}
}