Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 8 | Ir a la última revisión | | Ultima modificación | Ver Log |

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