Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 32 | Rev 34 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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