Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 8 | Rev 11 | 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
//  GridTopicView.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 1/26/22.
6
//
7
 
8
import SwiftUI
8 efrain 9
import Network
10
import Alamofire
11
import SwiftyJSON
12
import TTGSnackbar
1 efrain 13
 
14
struct GridTopicView: View {
8 efrain 15
    @EnvironmentObject private var networkMonitor : NetworkMonitor
1 efrain 16
    @EnvironmentObject private var appNavigation : AppNavigation
8 efrain 17
 
1 efrain 18
    @ObservedObject private var viewModel : TopicGridViewModel = TopicGridViewModel()
19
    @State private var scrollToIndex : Int = 0
8 efrain 20
    @State private var showProgressView : Bool = false
1 efrain 21
 
8 efrain 22
    @State private var presentAlert : Bool = false
23
    @State private var titleAlert : String = ""
24
    @State private var messageAlert : String = ""
1 efrain 25
 
9 efrain 26
    private let appDao = AppDao.sharedInstance
1 efrain 27
    private let config = [
28
        GridItem(.flexible()),
29
        GridItem(.flexible())
30
    ]
31
 
32
 
33
 
34
    var body: some View {
8 efrain 35
        ZStack {
36
 
37
 
38
            if self.showProgressView {
39
                ProgressView()
40
                    .progressViewStyle(CircularProgressViewStyle(tint: Color("color_progress_view_foreground")))
41
                    .scaleEffect(3, anchor: .center).zIndex(100000)
42
            }
43
 
44
            VStack(spacing: 0) {
45
                HStack {
46
 
47
 
48
                    Image("logo")
1 efrain 49
                    .resizable()
50
                    .frame(width: 32, height: 32, alignment: .center)
51
                    .aspectRatio(contentMode: .fit)
8 efrain 52
                        .foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
53
                    .padding(.leading, 16)
54
 
55
 
56
                    Text(networkMonitor.status == .disconnected ? Config.LANG_ERROR_NETWORK_MESSAGE_SHORT : Config.LANG_TAB_BAR_BUTTON_TOPICS)
57
                    .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
58
                        .foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
59
                    .padding(.leading, 4)
60
 
61
                    Spacer()
62
 
63
 
64
                    Button(action: {
65
                        if networkMonitor.status == .connected {
66
                            refresh()
67
                        } else {
68
                            self.titleAlert = Config.LANG_ERROR_NETWORK_TITLE
69
                            self.messageAlert = Config.LANG_ERROR_NETWORK_MESSAGE_LONG
70
                            self.presentAlert  = true
71
                        }
72
 
73
 
74
                    }, label: {
75
                        Image(uiImage: UIImage(named: "ui_refresh") ?? UIImage())
76
                        .resizable()
77
                        .frame(width: 24, height:24, alignment: .center)
78
                        .aspectRatio(contentMode: .fit)
79
                        .foregroundColor(Color("color_app_bar_foreground"))
80
                        .padding(.trailing, 16)
81
                    })
82
 
83
 
84
                }
85
                .edgesIgnoringSafeArea(.top)
86
                .frame(height: 50)
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
 
8 efrain 93
 
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
                            ForEach(0..<self.viewModel.topics.count) { index in
99
                                CardTopicView(
100
                                    topicUuid: self.viewModel.topics[index].uuid
101
                                )
102
                                .environmentObject(appNavigation)
103
                                .frame(
104
                                    width: Constants.CARD_WIDTH,
105
                                    height: Constants.CARD_HEIGHT,
106
                                    alignment: .center
107
                                ).id(index)
108
 
109
                            }.onChange(of:  scrollToIndex, perform: { value in
110
                                proxy.scrollTo(value, anchor: nil)
111
                            })
112
                       })
113
                    }
114
                }
115
                .padding(.top, 5)
1 efrain 116
            }
8 efrain 117
            .onAppear {
118
                var i : Int = 0
119
                while i < self.viewModel.topics.count {
120
                    if appData.topicUuidActive == self.viewModel.topics[i].uuid {
121
                        self.scrollToIndex = i
122
                    }
123
                    i += 1
124
 
1 efrain 125
                }
8 efrain 126
            } .alert(isPresented: $presentAlert) {
127
                Alert(
128
                    title: Text(self.titleAlert),
129
                    message: Text(self.messageAlert),
130
                    dismissButton: .default(Text(Config.LANG_COMMON_OK))
131
                )
1 efrain 132
            }
133
        }
8 efrain 134
    }
135
 
136
    private func refresh() {
137
        self.showProgressView = true
138
 
139
        let headerSecurity : HeaderSecurity = HeaderSecurity()
140
 
141
        let headers: HTTPHeaders = [
142
            .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
143
            .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
144
            .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
145
            .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
146
            .accept(Constants.HTTP_HEADER_ACCEPT)
147
        ]
148
 
149
        print(Config.URL_REFRESH)
150
 
151
        AF.request(Config.URL_REFRESH, method: .get, headers: headers).responseJSON{(response) in
152
                self.showProgressView = false
153
                switch response.result {
154
                    case .success:
155
 
156
 
157
                        let json = try? JSON(data: response.data!)
158
 
159
                        print("json : \(json)")
160
 
161
 
162
                        if json?["success"] ?? "" != false {
163
                            let dataService = DataService()
164
 
165
                            dataService.syncFromServer(json : json, refresh: true)
166
 
167
                            self.viewModel.fetch(userUuid: appData.userUuid)
168
                            var i : Int = 0
169
                            while i < self.viewModel.topics.count {
170
                                if appData.topicUuidActive == self.viewModel.topics[i].uuid {
171
                                    self.scrollToIndex = i
172
                                }
173
                                i += 1
174
                            }
175
 
176
                            let snackbar = TTGSnackbar(message: Config.LANG_TOPICS_REFRESH_CONTENT_DONE, duration: .short)
177
                            snackbar.show()
178
 
179
                        } else {
180
                            let message = json?["data"].string ?? ""
181
                            if !message.isEmpty {
182
                                self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE
183
                                self.messageAlert = message
184
                                self.presentAlert = true
185
                            }
186
                        }
187
 
188
                       return
189
 
190
                    case .failure :
191
                        self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
192
                        self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
193
                        self.presentAlert = true
194
 
195
                        return
1 efrain 196
                }
197
            }
198
        }
199
}
200
 
201
struct GridTopicView_Previews: PreviewProvider {
202
    static var previews: some View {
203
        GridTopicView()
204
    }
205
}
206
 
207