Skip to content

Commit 403c767

Browse files
committed
add store images service
1 parent 5d6b15e commit 403c767

File tree

8 files changed

+176
-12
lines changed

8 files changed

+176
-12
lines changed

Diff for: ImagesDefault.swift

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// ImagesDefault.swift
3+
// PicturesViewer
4+
//
5+
// Created by Bryan Zec on 30/03/2022.
6+
//
7+
8+
import Foundation
9+
import SwiftUI
10+
11+
class ImagesDefault {
12+
private static var defaults = UserDefaults.standard
13+
14+
static func load() -> Array<UIImage> {
15+
var list : Array<UIImage> = []
16+
if let savedImg = defaults.object(forKey: "SavedImage") as? Data {
17+
let decoder = JSONDecoder()
18+
if let loadedImgs = try? decoder.decode([Data].self, from: savedImg) {
19+
var listImages : Array<UIImage> = []
20+
loadedImgs.forEach { im in
21+
listImages.append(UIImage(data: im)!)
22+
}
23+
list = listImages
24+
}
25+
}
26+
return list
27+
28+
}
29+
30+
static func save(images: Array<UIImage>) {
31+
32+
var imagesEncode: [Data] = []
33+
images.forEach { image in
34+
let imageData: Data = image.pngData()!
35+
// let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
36+
imagesEncode.append(imageData)
37+
}
38+
let encoder = JSONEncoder()
39+
if let encoded = try? encoder.encode(imagesEncode) {
40+
defaults.set(encoded, forKey: "SavedImage")
41+
}
42+
}
43+
}

Diff for: ImagesStore.swift

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// ImagesStore.swift
3+
// PicturesViewer
4+
//
5+
// Created by Bryan Zec on 30/03/2022.
6+
//
7+
8+
import Foundation
9+
import SwiftUI
10+
11+
class ImageStore: ObservableObject {
12+
@Published var images: Array<UIImage> = []
13+
14+
private static func fileURL() throws -> URL {
15+
try FileManager.default.url(for: .documentDirectory,
16+
in: .userDomainMask,
17+
appropriateFor: nil,
18+
create: false)
19+
.appendingPathComponent("images.data")
20+
}
21+
22+
static func load(completion: @escaping (Result<Array<UIImage>, Error>)->Void) {
23+
DispatchQueue.global(qos: .background).async {
24+
do {
25+
let fileURL = try fileURL()
26+
guard let file = try? FileHandle(forReadingFrom: fileURL) else {
27+
DispatchQueue.main.async {
28+
completion(.success([]))
29+
}
30+
return
31+
}
32+
33+
let listImagesData = try JSONDecoder().decode(Array<Data>.self, from: file.availableData)
34+
35+
DispatchQueue.main.async {
36+
37+
var listImages : Array<UIImage> = []
38+
listImagesData.forEach { im in
39+
listImages.append(UIImage(data: im)!)
40+
}
41+
completion(.success(listImages))
42+
}
43+
} catch {
44+
DispatchQueue.main.async {
45+
completion(.failure(error))
46+
}
47+
}
48+
}
49+
}
50+
51+
static func save(images: Array<UIImage>, completion: @escaping (Result<Int, Error>)->Void) {
52+
DispatchQueue.global(qos: .background).async {
53+
do {
54+
print("on encode les images")
55+
56+
var imagesEncode: [Data] = []
57+
images.forEach { image in
58+
let imageData: Data = image.pngData()!
59+
imagesEncode.append(imageData)
60+
}
61+
let data = try JSONEncoder().encode(imagesEncode)
62+
print("on get le outfile")
63+
let outfile = try fileURL()
64+
try data.write(to: outfile)
65+
print("write")
66+
DispatchQueue.main.async {
67+
print("finish")
68+
69+
completion(.success(imagesEncode.count))
70+
}
71+
} catch {
72+
print("ERROR")
73+
74+
DispatchQueue.main.async {
75+
completion(.failure(error))
76+
}
77+
}
78+
}
79+
}
80+
}

Diff for: PicturesViewer.xcodeproj/project.pbxproj

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
/* Begin PBXBuildFile section */
1010
3306F89927ED1A5000EF602D /* ModalImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3306F89827ED1A5000EF602D /* ModalImageView.swift */; };
11+
33B29C8127F4F25200910A69 /* ImagesDefault.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33B29C8027F4F25200910A69 /* ImagesDefault.swift */; };
12+
33B29C8327F4F64E00910A69 /* ImagesStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33B29C8227F4F64E00910A69 /* ImagesStore.swift */; };
1113
33B2B20227E9112E00F8D6BB /* PicturesViewerApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33B2B20127E9112E00F8D6BB /* PicturesViewerApp.swift */; };
1214
33B2B20427E9112E00F8D6BB /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33B2B20327E9112E00F8D6BB /* ContentView.swift */; };
1315
33B2B20627E9113200F8D6BB /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 33B2B20527E9113200F8D6BB /* Assets.xcassets */; };
@@ -20,6 +22,8 @@
2022

2123
/* Begin PBXFileReference section */
2224
3306F89827ED1A5000EF602D /* ModalImageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModalImageView.swift; sourceTree = "<group>"; };
25+
33B29C8027F4F25200910A69 /* ImagesDefault.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImagesDefault.swift; sourceTree = "<group>"; };
26+
33B29C8227F4F64E00910A69 /* ImagesStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImagesStore.swift; sourceTree = "<group>"; };
2327
33B2B1FE27E9112E00F8D6BB /* PicturesViewer.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PicturesViewer.app; sourceTree = BUILT_PRODUCTS_DIR; };
2428
33B2B20127E9112E00F8D6BB /* PicturesViewerApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PicturesViewerApp.swift; sourceTree = "<group>"; };
2529
33B2B20327E9112E00F8D6BB /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
@@ -45,6 +49,8 @@
4549
33B2B1F527E9112E00F8D6BB = {
4650
isa = PBXGroup;
4751
children = (
52+
33B29C8227F4F64E00910A69 /* ImagesStore.swift */,
53+
33B29C8027F4F25200910A69 /* ImagesDefault.swift */,
4854
3306F89827ED1A5000EF602D /* ModalImageView.swift */,
4955
33D0C90827EA5ADB00A45147 /* ItemPictureView.swift */,
5056
33B2B20027E9112E00F8D6BB /* PicturesViewer */,
@@ -153,9 +159,11 @@
153159
buildActionMask = 2147483647;
154160
files = (
155161
3306F89927ED1A5000EF602D /* ModalImageView.swift in Sources */,
162+
33B29C8127F4F25200910A69 /* ImagesDefault.swift in Sources */,
156163
33B2B21027E9120000F8D6BB /* ListPicturesView.swift in Sources */,
157164
33B2B21827E91E3F00F8D6BB /* PhotoPicker.swift in Sources */,
158165
33B2B21427E914F800F8D6BB /* ButtonView.swift in Sources */,
166+
33B29C8327F4F64E00910A69 /* ImagesStore.swift in Sources */,
159167
33B2B20427E9112E00F8D6BB /* ContentView.swift in Sources */,
160168
33D0C90927EA5ADB00A45147 /* ItemPictureView.swift in Sources */,
161169
33B2B20227E9112E00F8D6BB /* PicturesViewerApp.swift in Sources */,

Diff for: PicturesViewer.xcodeproj/xcuserdata/bzec.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
endingColumnNumber = "9223372036854775807"
1717
startingLineNumber = "14"
1818
endingLineNumber = "14"
19-
landmarkName = "lastScale"
20-
landmarkType = "24">
19+
landmarkName = "ModalImageView"
20+
landmarkType = "14">
2121
</BreakpointContent>
2222
</BreakpointProxy>
2323
</Breakpoints>

Diff for: PicturesViewer/ButtonView.swift

+7-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import SwiftUI
99

1010
struct ButtonView: View {
1111
@State private var showPhotoSheet = false
12-
@Binding var images: Array<UIImage>?
12+
@Binding var images: Array<UIImage>
1313
@Binding var imageIndex : Int
1414

15+
let saveAction: ()->Void
16+
1517
var body: some View {
1618
VStack {
1719
Button(action: { showPhotoSheet = true }) {
@@ -24,8 +26,9 @@ struct ButtonView: View {
2426
print(error)
2527
}
2628
if imagesOrNil != nil {
27-
images = imagesOrNil
29+
images = imagesOrNil!
2830
imageIndex = 0
31+
saveAction()
2932
}
3033
}
3134
}
@@ -36,11 +39,11 @@ struct ButtonView: View {
3639
}
3740

3841
struct ButtonView_Previews: PreviewProvider {
39-
@State static var prev: Array<UIImage>? = []
42+
@State static var prev: Array<UIImage> = []
4043
@State static var index = 0
4144

4245
static var previews: some View {
43-
ButtonView(images : $prev, imageIndex: $index)
46+
ButtonView(images : $prev, imageIndex: $index, saveAction: {})
4447
}
4548
}
4649

Diff for: PicturesViewer/ContentView.swift

+14-3
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,32 @@
88
import SwiftUI
99

1010
struct ContentView: View {
11-
@State private var images: Array<UIImage>? = []
11+
@Binding var images: Array<UIImage>
1212
@State var imageIndex : Int = 0
13+
@Environment(\.scenePhase) private var scenePhase
14+
let saveAction: ()->Void
1315

1416
var body: some View {
1517
HStack{
1618
VStack{
1719
ListPicturesView(images: $images, imageIndex: $imageIndex)
18-
ButtonView(images: $images, imageIndex: $imageIndex)
20+
ButtonView(images: $images, imageIndex: $imageIndex) {
21+
saveAction()
22+
}
1923
}
2024
}
25+
/*
26+
.onChange(of: scenePhase) { phase in
27+
if phase == .inactive { saveAction() }
28+
}
29+
*/
2130
}
2231
}
2332

2433
struct ContentView_Previews: PreviewProvider {
34+
@State static var prev: Array<UIImage> = []
35+
2536
static var previews: some View {
26-
ContentView()
37+
ContentView(images: $prev, saveAction: {})
2738
}
2839
}

Diff for: PicturesViewer/ListPicturesView.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import SwiftUI
99

1010
struct ListPicturesView: View {
11-
@Binding var images : Array<UIImage>!
11+
@Binding var images : Array<UIImage>
1212
@Binding var imageIndex : Int
1313

1414
@State var isFullScreen : Bool = false;
@@ -58,7 +58,7 @@ struct ListPicturesView: View {
5858
}
5959

6060
struct ListPicturesView_Previews: PreviewProvider {
61-
@State static var prev: Array<UIImage>? = []
61+
@State static var prev: Array<UIImage> = []
6262
@State static var index = 0
6363

6464
static var previews: some View {

Diff for: PicturesViewer/PicturesViewerApp.swift

+20-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,28 @@ import SwiftUI
99

1010
@main
1111
struct PicturesViewerApp: App {
12+
@StateObject private var store = ImageStore()
13+
1214
var body: some Scene {
1315
WindowGroup {
14-
ContentView()
16+
ContentView(images: $store.images){
17+
ImageStore.save(images: store.images) { result in
18+
if case .failure(let error) = result {
19+
fatalError(error.localizedDescription)
20+
}
21+
}
22+
23+
}
24+
.onAppear {
25+
ImageStore.load { result in
26+
switch result {
27+
case .failure(let error):
28+
fatalError(error.localizedDescription)
29+
case .success(let imgs):
30+
store.images = imgs
31+
}
32+
}
33+
}
1534
}
1635
}
1736
}

0 commit comments

Comments
 (0)