Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 17 | 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
//  GridSlideView.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 2/17/22.
6
//
7
 
8
import SwiftUI
9
 
10
 
11
struct GridSlideView: View {
12
 
8 efrain 13
    @EnvironmentObject private var networkMonitor : NetworkMonitor
1 efrain 14
    @EnvironmentObject private var appNavigation : AppNavigation
15
    @State private var scrollToIndex : Int = 0
16
 
17 efrain 17
    private var appData = AppData.sharedInstance
1 efrain 18
    private let capsuleDao : CapsuleDao = CapsuleDao.sharedInstance
19
 
20
    private var capsuleModel : CapsuleModel = CapsuleModel()
21
    private var viewModel : SlideGridViewModel = SlideGridViewModel()
22
    private var capsuleTitle : String = ""
23
 
24
    private let config = [
25
        GridItem(.fixed(Constants.CARD_WIDTH)),
26
        GridItem(.fixed(Constants.CARD_WIDTH))
27
    ]
28
 
29
 
30
    init()
31
    {
32
        self.capsuleModel = capsuleDao.selectByUuid(uuid: appData.capsuleUuidActive)
33
        viewModel.fetch(capsuleUuid: self.capsuleModel.uuid, userUuid: appData.userUuid)
34
 
35
        if self.capsuleModel.name.count > Constants.APP_BAR_TITLE_MAX_LENGTH {
36
            capsuleTitle = String(Array(self.capsuleModel.name)[0...Constants.APP_BAR_TITLE_MAX_LENGTH]) + "..."
37
        } else {
38
            capsuleTitle = self.capsuleModel.name
39
        }
40
    }
41
 
42
    var body: some View {
43
 
44
            VStack(spacing: 0) {
45
                HStack {
46
                    Button(action: {
8 efrain 47
 
17 efrain 48
 
11 efrain 49
                        if appNavigation.subPageSource == .mycapsules {
22 efrain 50
                            //appData.topicUuidActive = ""
51
                            //appData.capsuleUuidActive = ""
52
                            appData.slideUuidActive = ""
17 efrain 53
                            appData.save()
8 efrain 54
 
55
                            withAnimation {
56
                                appNavigation.subpageActive = .mycapsules
57
                            }
58
                        } else {
59
                            appData.slideUuidActive = ""
17 efrain 60
                            appData.save()
8 efrain 61
 
62
                            withAnimation {
63
                                appNavigation.subpageActive = .capsules
64
                            }
65
                        }
1 efrain 66
 
8 efrain 67
 
1 efrain 68
                    }, label: {
69
 
70
 
71
                        Image(systemName: "chevron.backward")
72
                        .frame(width: 32, height: 32, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
73
                        .aspectRatio(contentMode: .fit)
8 efrain 74
                            .foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
1 efrain 75
                    })
76
                    .padding(.leading, 16)
77
 
8 efrain 78
                    Text(networkMonitor.status == .disconnected ? Config.LANG_ERROR_NETWORK_MESSAGE_SHORT : capsuleTitle)
1 efrain 79
                    .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
8 efrain 80
                     .foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
1 efrain 81
                        .padding(.leading, 4)
82
 
83
                    Spacer()
84
                }
85
                .edgesIgnoringSafeArea(.top)
86
                .frame(height: 50)
8 efrain 87
                .background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
88
 
89
 
90
                Divider().background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
91
 
1 efrain 92
 
93
                Divider()
94
 
95
                ScrollView() {
96
                    ScrollViewReader { proxy in
97
                        LazyVGrid(columns: config, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/, spacing: /*@START_MENU_TOKEN@*/nil/*@END_MENU_TOKEN@*/, pinnedViews: /*@START_MENU_TOKEN@*/[]/*@END_MENU_TOKEN@*/, content: {
98
 
99
                            ForEach(0..<self.viewModel.slides.count) { index in
100
                                CardSlideView(
101
                                    slideUuid: self.viewModel.slides[index].uuid,
102
                                    position: index
103
                                )
104
                                .environmentObject(appNavigation)
105
                                .frame(
106
                                    width: Constants.CARD_WIDTH,
107
                                    height: Constants.CARD_HEIGHT,
108
                                    alignment: .center
109
                                )
110
                                .id(index)
111
 
112
                            }
113
                            /*}.onChange(of:  scrollToIndex, perform: { value in
11 efrain 114
                                //print("onChange")
1 efrain 115
                                proxy.scrollTo(value, anchor: nil)
116
                            })*/
117
                        })
118
                    }
119
 
120
                }
17 efrain 121
                .onAppear {
1 efrain 122
                    var i : Int = 0
123
                    while i < self.viewModel.slides.count {
124
                        if appData.slideUuidActive == self.viewModel.slides[i].uuid {
125
                            self.scrollToIndex = i
126
                        }
127
                        i += 1
128
                    }
17 efrain 129
                }
1 efrain 130
                .padding(.top, 5)
131
 
132
 
133
 
134
 
135
        }
136
    }
137
}
138
 
139
struct GridSlideView_Previews: PreviewProvider {
140
    static var previews: some View {
141
        //GridSlideView(capsuleUuid: "C123")
142
        GridSlideView()
143
    }
144
}
145
 
146