-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoreDataManager.swift
225 lines (167 loc) · 6.53 KB
/
CoreDataManager.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//
// CoreDataManager.swift
//
// Created by keisyrzk on 23.12.2016.
// Copyright © 2016 keisyrzk. All rights reserved.
//
import UIKit
import CoreData
class CoreDataManager
{
////////////////////////
// CREATE A SINGLETON //
class var sharedInstance: CoreDataManager
{
struct Singleton
{
static let instance = CoreDataManager()
}
return Singleton.instance
}
// CREATE A SINGLETON //
////////////////////////
func delete(object: NSManagedObject, completion: (Result<EmptyResponse>) -> Void)
{
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
managedContext.delete(object)
do {
try managedContext.save()
completion(Result.success(EmptyResponse()))
} catch let error as NSError
{
completion(Result<EmptyResponse>.failure(error))
}
}//end of deleteColor
/////////////
/// COLOR ///
func saveColor(name: String, red: CGFloat, green: CGFloat, blue: CGFloat, completion: (Result<NSManagedObject>) -> Void)
{
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
//add color data
let colorsEntity = NSEntityDescription.entity(forEntityName: "UserColor", in: managedContext)!
let color = NSManagedObject(entity: colorsEntity, insertInto: managedContext)
color.setValue(name, forKey: "name")
color.setValue(red, forKey: "red")
color.setValue(green, forKey: "green")
color.setValue(blue, forKey: "blue")
//save the context
do {
try managedContext.save()
completion(Result<NSManagedObject>.success(color))
} catch let error as NSError
{
completion(Result<NSManagedObject>.failure(error))
}
}//end of saveColor
func fetchColors(completion: (Result<[Color]>) -> Void)
{
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "UserColor")
do {
let colors = try managedContext.fetch(fetchRequest)
let colorsObjects = createColorObjects(colorsData: colors)
completion(Result<[Color]>.success(colorsObjects))
} catch let error as NSError {
completion(Result<[Color]>.failure(error))
}
}//end of fetchColors
private func createColorObjects(colorsData: [NSManagedObject]) -> [Color]
{
var colorsObjects = [Color]()
for colorData in colorsData
{
var nextColor = Color.init(name: colorData.value(forKeyPath: "name") as! String,
color: UIColor(red: colorData.value(forKeyPath: "red") as! CGFloat,
green: colorData.value(forKeyPath: "green") as! CGFloat,
blue: colorData.value(forKeyPath: "blue") as! CGFloat,
alpha: 1))
nextColor.object = colorData
colorsObjects.append(nextColor)
}
return colorsObjects
}
/// COLOR ///
/////////////
///////////////
/// GALLERY ///
func saveImage(newImage: UIImage, completion: (Result<NSManagedObject>) -> Void)
{
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
guard let imageData = reduceImageSize(image: newImage) else {
return
}
//add gallery
let imageEntity = NSEntityDescription.entity(forEntityName: "UserImage", in: managedContext)!
let image = NSManagedObject(entity: imageEntity, insertInto: managedContext)
image.setValue(imageData, forKey: "imageData")
//save the context
do {
try managedContext.save()
UserDefaultsManager().cacheCurrentImage()
completion(Result<NSManagedObject>.success(image))
} catch let error as NSError
{
completion(Result<NSManagedObject>.failure(error))
}
}//end of saveImage
func fetchImages(completion: (Result<[NSManagedObject]>) -> Void)
{
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "UserImage")
do {
let images = try managedContext.fetch(fetchRequest)
completion(Result<[NSManagedObject]>.success(images))
} catch let error as NSError {
completion(Result<[NSManagedObject]>.failure(error))
}
}//end of fetchImages
func objectToUIImage(object: NSManagedObject) -> UIImage?
{
let data = object.value(forKeyPath: "imageData") as! Data
if let img = UIImage(data: data)
{
return img
}
return nil
}
/// GALLERY ///
///////////////
private func reduceImageSize(image: UIImage) -> Data?
{
var compressedImage = UIImageJPEGRepresentation(image, 1) //represent image as a JPEG
var factor: CGFloat = 1.0 //percentage of the original image (compression factor)
repeat
{
factor = factor - 0.1
compressedImage = (UIImageJPEGRepresentation(image, factor)! as Data) //compress the image to 'factor' percent of the original image
} while (compressedImage?.count)! > 500000 && factor > 0.1 //reduce the image byte weight under 500kB
if let compressed = compressedImage
{
return compressed
}
else
{
return nil
}
}
}//end of class