| 1 |
efrain |
1 |
//
|
|
|
2 |
// GridGalleryView.swift
|
|
|
3 |
// twogetskills
|
|
|
4 |
//
|
|
|
5 |
// Created by Efrain Yanez Recanatini on 3/7/22.
|
|
|
6 |
//
|
|
|
7 |
|
|
|
8 |
import Foundation
|
|
|
9 |
|
|
|
10 |
import SwiftUI
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
struct GridGalleryView: View {
|
|
|
14 |
|
| 8 |
efrain |
15 |
@EnvironmentObject private var networkMonitor : NetworkMonitor
|
| 1 |
efrain |
16 |
@EnvironmentObject private var appNavigation : AppNavigation
|
| 32 |
efrain |
17 |
@ObservedObject private var viewModel : GalleryGridViewModel = GalleryGridViewModel()
|
| 35 |
efrain |
18 |
@State private var positionVisible: Int = 0
|
| 1 |
efrain |
19 |
|
|
|
20 |
|
| 17 |
efrain |
21 |
private var appData = AppData.sharedInstance
|
| 1 |
efrain |
22 |
private var capsuleTitle : String = ""
|
| 35 |
efrain |
23 |
private let rows = [
|
|
|
24 |
GridItem(.flexible())
|
|
|
25 |
]
|
| 1 |
efrain |
26 |
|
|
|
27 |
|
| 17 |
efrain |
28 |
|
| 1 |
efrain |
29 |
|
|
|
30 |
init()
|
|
|
31 |
{
|
|
|
32 |
self.viewModel.fetch(capsuleUuid: appData.capsuleUuidActive, userUuid: appData.userUuid)
|
|
|
33 |
|
|
|
34 |
let capsuleDao = CapsuleDao.sharedInstance
|
|
|
35 |
let capsule = capsuleDao.selectByUuid(uuid: appData.capsuleUuidActive)
|
|
|
36 |
|
|
|
37 |
if capsule.name.count > Constants.APP_BAR_TITLE_MAX_LENGTH {
|
|
|
38 |
capsuleTitle = String(Array(capsule.name)[0...Constants.APP_BAR_TITLE_MAX_LENGTH]) + "..."
|
|
|
39 |
} else {
|
|
|
40 |
capsuleTitle = capsule.name
|
|
|
41 |
}
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
|
| 33 |
efrain |
45 |
@ViewBuilder
|
| 1 |
efrain |
46 |
var body: some View {
|
|
|
47 |
GeometryReader { geometry in
|
| 35 |
efrain |
48 |
ZStack {
|
|
|
49 |
|
|
|
50 |
if self.viewModel.hasPrevious() {
|
|
|
51 |
Button(action: {
|
|
|
52 |
print("Button previouse")
|
|
|
53 |
self.viewModel.previous()
|
|
|
54 |
print("slideActiveIndex: \(self.viewModel.slideActiveIndex)")
|
|
|
55 |
self.completeSlide(position: self.viewModel.slideActiveIndex)
|
|
|
56 |
|
|
|
57 |
appData.slideUuidActive = self.viewModel.slides[self.viewModel.slideActiveIndex].uuid
|
|
|
58 |
appData.save()
|
|
|
59 |
}, label: {
|
|
|
60 |
Image(systemName: "arrow.left")
|
|
|
61 |
})
|
| 32 |
efrain |
62 |
|
| 35 |
efrain |
63 |
.frame(width: 40.0, height: 40.0, alignment: .center)
|
|
|
64 |
.cornerRadius(/*@START_MENU_TOKEN@*/3.0/*@END_MENU_TOKEN@*/)
|
|
|
65 |
.background(Color("color_button_gallery_background"))
|
|
|
66 |
.foregroundColor(Color("color_button_gallery_foreground"))
|
|
|
67 |
.offset(x: -1 * ((geometry.size.width / 2) - 30), y : 25).zIndex(10000)
|
|
|
68 |
}
|
| 1 |
efrain |
69 |
|
| 35 |
efrain |
70 |
if self.viewModel.hasNext() {
|
| 1 |
efrain |
71 |
Button(action: {
|
| 35 |
efrain |
72 |
print("Button next")
|
|
|
73 |
self.viewModel.next()
|
|
|
74 |
print("slideActiveIndex: \(self.viewModel.slideActiveIndex)")
|
|
|
75 |
self.completeSlide(position: self.viewModel.slideActiveIndex)
|
| 22 |
efrain |
76 |
|
| 35 |
efrain |
77 |
appData.slideUuidActive = self.viewModel.slides[self.viewModel.slideActiveIndex].uuid
|
|
|
78 |
appData.save()
|
| 1 |
efrain |
79 |
|
|
|
80 |
|
|
|
81 |
}, label: {
|
| 35 |
efrain |
82 |
Image(systemName: "arrow.right")
|
| 1 |
efrain |
83 |
})
|
| 35 |
efrain |
84 |
.frame(width: 40.0, height: 40.0, alignment: .center)
|
|
|
85 |
.background(Color("color_button_gallery_background"))
|
|
|
86 |
.foregroundColor(Color("color_button_gallery_foreground"))
|
|
|
87 |
.offset(x: ((geometry.size.width / 2) - 30), y : 25).zIndex(10000)
|
| 1 |
efrain |
88 |
}
|
|
|
89 |
|
| 35 |
efrain |
90 |
VStack(spacing: 0) {
|
|
|
91 |
HStack {
|
|
|
92 |
Button(action: {
|
|
|
93 |
|
|
|
94 |
|
|
|
95 |
appNavigation.subpageActive = .slides
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
}, label: {
|
| 34 |
efrain |
99 |
|
| 35 |
efrain |
100 |
|
|
|
101 |
Image(systemName: "chevron.backward")
|
|
|
102 |
.frame(width: 32, height: 32, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
|
|
|
103 |
.aspectRatio(contentMode: .fit)
|
|
|
104 |
.foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
|
|
|
105 |
})
|
|
|
106 |
.padding(.leading, 16)
|
| 34 |
efrain |
107 |
|
| 35 |
efrain |
108 |
Text(networkMonitor.status == .disconnected ? Config.LANG_ERROR_NETWORK_MESSAGE_SHORT : capsuleTitle)
|
|
|
109 |
.font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
|
|
|
110 |
.foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
|
|
|
111 |
.padding(.leading, 4)
|
| 34 |
efrain |
112 |
|
| 35 |
efrain |
113 |
Spacer()
|
|
|
114 |
}
|
|
|
115 |
.edgesIgnoringSafeArea(.top)
|
|
|
116 |
.frame(height: 50)
|
|
|
117 |
.background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
|
|
|
118 |
|
|
|
119 |
|
|
|
120 |
Divider().background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
|
|
|
121 |
|
| 34 |
efrain |
122 |
|
| 35 |
efrain |
123 |
ScrollViewReader { scrollProxy in
|
|
|
124 |
ScrollView(.horizontal) {
|
|
|
125 |
LazyHGrid(rows: rows, alignment: .center) {
|
|
|
126 |
ForEach(0..<self.viewModel.slides.count) { index in
|
|
|
127 |
CardGalleryView(
|
|
|
128 |
slideUuid: self.viewModel.slides[self.viewModel.slideActiveIndex].uuid
|
|
|
129 |
)
|
|
|
130 |
.environmentObject(appNavigation)
|
|
|
131 |
.frame(width: geometry.size.width, height: geometry.size.height)
|
|
|
132 |
.id(index)
|
| 34 |
efrain |
133 |
}
|
| 35 |
efrain |
134 |
}
|
|
|
135 |
|
| 34 |
efrain |
136 |
}
|
| 35 |
efrain |
137 |
|
|
|
138 |
.onChange(of: self.positionVisible) { id in
|
|
|
139 |
guard id != nil else { return }
|
| 34 |
efrain |
140 |
|
| 35 |
efrain |
141 |
print("positionVisible : \(positionVisible)")
|
|
|
142 |
scrollProxy.scrollTo(id)
|
| 34 |
efrain |
143 |
|
|
|
144 |
}
|
| 35 |
efrain |
145 |
.onAppear {
|
|
|
146 |
var position : Int = 0
|
|
|
147 |
var i : Int = 0
|
|
|
148 |
while i < self.viewModel.slides.count {
|
|
|
149 |
if appData.slideUuidActive == self.viewModel.slides[i].uuid {
|
|
|
150 |
position = i
|
|
|
151 |
}
|
|
|
152 |
i += 1
|
| 32 |
efrain |
153 |
}
|
| 1 |
efrain |
154 |
|
| 35 |
efrain |
155 |
self.viewModel.slideActiveIndex = position
|
|
|
156 |
scrollProxy.scrollTo(position)
|
| 17 |
efrain |
157 |
}
|
|
|
158 |
}
|
| 32 |
efrain |
159 |
}
|
| 1 |
efrain |
160 |
}
|
|
|
161 |
}
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
private func completeSlide(position: Int)
|
|
|
165 |
{
|
|
|
166 |
|
|
|
167 |
if self.viewModel.slides[position].type == Constants.SLIDE_TYPE_IMAGE {
|
|
|
168 |
|
|
|
169 |
let dataService = DataService()
|
|
|
170 |
dataService.completeSlide(slide: self.viewModel.slides[position])
|
|
|
171 |
}
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
|
177 |
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
|
|
|
181 |
|
|
|
182 |
|
|
|
183 |
|
|
|
184 |
struct GridGalleryView_Previews: PreviewProvider {
|
|
|
185 |
static var previews: some View {
|
|
|
186 |
//GridGalleryView(capsuleUuid: "C123", position: 2)
|
|
|
187 |
|
|
|
188 |
GridGalleryView()
|
|
|
189 |
}
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
|