Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 22 | Rev 32 | 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
class GridGalleryTabController: ObservableObject {
14
    @Published var activeTab : Int = 0
15
 
16
    func open( tab: Int) {
17
        activeTab = tab
18
    }
19
}
20
 
21
struct GridGalleryView: View {
22
 
8 efrain 23
    @EnvironmentObject private var networkMonitor : NetworkMonitor
1 efrain 24
    @EnvironmentObject private var appNavigation : AppNavigation
8 efrain 25
 
1 efrain 26
    @State private var selectedTab : Int = 0
27
 
28
 
17 efrain 29
    private var appData = AppData.sharedInstance
1 efrain 30
    private var capsuleTitle : String = ""
31
    private var viewModel : GalleryGridViewModel = GalleryGridViewModel()
32
 
33
 
17 efrain 34
 
1 efrain 35
 
36
    init()
37
    {
38
        self.viewModel.fetch(capsuleUuid: appData.capsuleUuidActive, userUuid: appData.userUuid)
39
 
40
        let capsuleDao = CapsuleDao.sharedInstance
41
        let capsule = capsuleDao.selectByUuid(uuid: appData.capsuleUuidActive)
42
 
43
        if capsule.name.count > Constants.APP_BAR_TITLE_MAX_LENGTH {
44
            capsuleTitle = String(Array(capsule.name)[0...Constants.APP_BAR_TITLE_MAX_LENGTH]) + "..."
45
        } else {
46
            capsuleTitle = capsule.name
47
        }
48
    }
49
 
50
 
51
 
52
    var body: some View {
53
        GeometryReader { geometry in
54
        ZStack {
20 efrain 55
 
1 efrain 56
            if self.selectedTab > 0 {
57
                Button(action: {
17 efrain 58
                    appData.slideUuidActive = self.viewModel.slides[self.selectedTab - 1].uuid
59
                    appData.save()
60
 
61
                    self.selectedTab = self.selectedTab - 1
62
                }, label: {
63
                    Image(systemName: "arrow.left")
64
                })
1 efrain 65
 
66
                .frame(width: 40.0, height: 40.0, alignment: .center)
67
                     .cornerRadius(/*@START_MENU_TOKEN@*/3.0/*@END_MENU_TOKEN@*/)
68
                .background(Color("color_button_gallery_background"))
69
                .foregroundColor(Color("color_button_gallery_foreground"))
70
                    .offset(x: -1 * ((geometry.size.width / 2) - 30), y :  25).zIndex(10000)
71
            }
72
 
73
            if self.selectedTab < (self.viewModel.slides.count - 1) {
74
                Button(action: {
17 efrain 75
                    appData.slideUuidActive = self.viewModel.slides[self.selectedTab + 1].uuid
76
                    appData.save()
77
 
78
                    self.selectedTab = self.selectedTab + 1
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
 
120
 
121
                TabView(selection: self.$selectedTab) {
122
                    ForEach(0..<self.viewModel.slides.count) { index in
123
                        CardGalleryView(
124
                            slideUuid:  self.viewModel.slides[index].uuid
125
                        )
126
                        .environmentObject(appNavigation)
127
                        .tag(index)
128
                        .transition(.slide)
129
 
130
                    }
131
                }
26 efrain 132
                //.tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))
133
                .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
1 efrain 134
 
135
                .onChange(of: self.selectedTab) { selectedTab in
17 efrain 136
                    appData.slideUuidActive = self.viewModel.slides[selectedTab].uuid
137
                    appData.save()
138
 
1 efrain 139
                    completeSlide(position: self.selectedTab)
140
                }.onAppear {
17 efrain 141
 
142
                    var position : Int = 0
143
                    var i : Int = 0
144
                    while i < self.viewModel.slides.count {
145
                        if appData.slideUuidActive == self.viewModel.slides[i].uuid {
146
                            position = i
147
                        }
148
                        i += 1
149
                    }
150
 
151
                    self.selectedTab =  position
1 efrain 152
                    completeSlide(position: self.selectedTab)
153
                }
154
 
155
            }
156
 
157
            }
158
        }
159
    }
160
 
161
    private func completeSlide(position: Int)
162
    {
163
 
164
        if self.viewModel.slides[position].type == Constants.SLIDE_TYPE_IMAGE {
165
 
166
            let dataService = DataService()
167
            dataService.completeSlide(slide: self.viewModel.slides[position])
168
        }
169
    }
170
 
171
 
172
 
173
 
174
 
175
}
176
 
177
 
178
 
179
 
180
 
181
struct GridGalleryView_Previews: PreviewProvider {
182
    static var previews: some View {
183
        //GridGalleryView(capsuleUuid: "C123", position: 2)
184
 
185
        GridGalleryView()
186
    }
187
}
188
 
189
 
190