Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 11 | Rev 22 | 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 {
8 efrain 50
                            appData.topicUuidActive = ""
51
                            appData.capsuleUuidActive = ""
17 efrain 52
                            appData.save()
8 efrain 53
 
54
                            withAnimation {
55
                                appNavigation.subpageActive = .mycapsules
56
                            }
57
                        } else {
58
                            appData.slideUuidActive = ""
17 efrain 59
                            appData.save()
8 efrain 60
 
61
                            withAnimation {
62
                                appNavigation.subpageActive = .capsules
63
                            }
64
                        }
1 efrain 65
 
8 efrain 66
 
1 efrain 67
                    }, label: {
68
 
69
 
70
                        Image(systemName: "chevron.backward")
71
                        .frame(width: 32, height: 32, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
72
                        .aspectRatio(contentMode: .fit)
8 efrain 73
                            .foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
1 efrain 74
                    })
75
                    .padding(.leading, 16)
76
 
8 efrain 77
                    Text(networkMonitor.status == .disconnected ? Config.LANG_ERROR_NETWORK_MESSAGE_SHORT : capsuleTitle)
1 efrain 78
                    .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
8 efrain 79
                     .foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
1 efrain 80
                        .padding(.leading, 4)
81
 
82
                    Spacer()
83
                }
84
                .edgesIgnoringSafeArea(.top)
85
                .frame(height: 50)
8 efrain 86
                .background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
87
 
88
 
89
                Divider().background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
90
 
1 efrain 91
 
92
                Divider()
93
 
94
                ScrollView() {
95
                    ScrollViewReader { proxy in
96
                        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: {
97
 
98
                            ForEach(0..<self.viewModel.slides.count) { index in
99
                                CardSlideView(
100
                                    slideUuid: self.viewModel.slides[index].uuid,
101
                                    position: index
102
                                )
103
                                .environmentObject(appNavigation)
104
                                .frame(
105
                                    width: Constants.CARD_WIDTH,
106
                                    height: Constants.CARD_HEIGHT,
107
                                    alignment: .center
108
                                )
109
                                .id(index)
110
 
111
                            }
112
                            /*}.onChange(of:  scrollToIndex, perform: { value in
11 efrain 113
                                //print("onChange")
1 efrain 114
                                proxy.scrollTo(value, anchor: nil)
115
                            })*/
116
                        })
117
                    }
118
 
119
                }
17 efrain 120
                .onAppear {
1 efrain 121
                    var i : Int = 0
122
                    while i < self.viewModel.slides.count {
123
                        if appData.slideUuidActive == self.viewModel.slides[i].uuid {
124
                            self.scrollToIndex = i
125
                        }
126
                        i += 1
127
                    }
17 efrain 128
                }
1 efrain 129
                .padding(.top, 5)
130
 
131
 
132
 
133
 
134
        }
135
    }
136
}
137
 
138
struct GridSlideView_Previews: PreviewProvider {
139
    static var previews: some View {
140
        //GridSlideView(capsuleUuid: "C123")
141
        GridSlideView()
142
    }
143
}
144
 
145