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