Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 1 | Rev 9 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 8
Línea 4... Línea 4...
4
//
4
//
5
//  Created by Efrain Yanez Recanatini on 1/26/22.
5
//  Created by Efrain Yanez Recanatini on 1/26/22.
6
//
6
//
Línea 7... Línea 7...
7
 
7
 
-
 
8
import SwiftUI
-
 
9
import Network
-
 
10
import Alamofire
-
 
11
import SwiftyJSON
Línea 8... Línea 12...
8
import SwiftUI
12
import TTGSnackbar
-
 
13
 
9
 
14
struct GridTopicView: View {
-
 
15
    @EnvironmentObject private var networkMonitor : NetworkMonitor
10
struct GridTopicView: View {
16
    @EnvironmentObject private var appNavigation : AppNavigation
11
    @EnvironmentObject private var appNavigation : AppNavigation
17
    
-
 
18
    @ObservedObject private var viewModel : TopicGridViewModel = TopicGridViewModel()
Línea -... Línea 19...
-
 
19
    @State private var scrollToIndex : Int = 0
-
 
20
    @State private var showProgressView : Bool = false
-
 
21
    
Línea 12... Línea 22...
12
    @ObservedObject private var viewModel : TopicGridViewModel = TopicGridViewModel()
22
    @State private var presentAlert : Bool = false
13
    @State private var scrollToIndex : Int = 0
23
    @State private var titleAlert : String = ""
14
    
24
    @State private var messageAlert : String = ""
15
    
25
    
16
    private let appData = AppData.sharedInstance
26
    private let appData = AppData.sharedInstance
Línea 17... Línea 27...
17
    private let config = [
27
    private let config = [
18
        GridItem(.flexible()),
-
 
19
        GridItem(.flexible())
28
        GridItem(.flexible()),
-
 
29
        GridItem(.flexible())
-
 
30
    ]
20
    ]
31
 
21
 
32
 
22
 
33
 
23
 
34
    var body: some View {
24
    var body: some View {
-
 
25
        VStack(spacing: 0) {
35
        ZStack {
26
            HStack {
-
 
27
                Image("logo")
36
            
28
                .resizable()
37
            
29
                .frame(width: 32, height: 32, alignment: .center)
-
 
30
                .aspectRatio(contentMode: .fit)
-
 
31
                .foregroundColor(Color("color_app_bar_foreground"))
38
            if self.showProgressView {
32
                .padding(.leading, 16)
39
                ProgressView()
33
             
-
 
34
                
40
                    .progressViewStyle(CircularProgressViewStyle(tint: Color("color_progress_view_foreground")))
35
                Text(Config.LANG_TAB_BAR_BUTTON_TOPICS)
41
                    .scaleEffect(3, anchor: .center).zIndex(100000)
36
                .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
-
 
37
                .foregroundColor(Color("color_app_bar_foreground"))
42
            }
38
                .padding(.leading, 4)
43
            
39
                
44
            VStack(spacing: 0) {
-
 
45
                HStack {
-
 
46
                    
-
 
47
                    
-
 
48
                    Image("logo")
-
 
49
                    .resizable()
-
 
50
                    .frame(width: 32, height: 32, alignment: .center)
-
 
51
                    .aspectRatio(contentMode: .fit)
-
 
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
                        }
40
                Spacer()
72
                        
41
                
73
                        
-
 
74
                    }, label: {
-
 
75
                        Image(uiImage: UIImage(named: "ui_refresh") ?? UIImage())
-
 
76
                        .resizable()
42
                Button(action: {}, label: {
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)
Línea -... Línea 87...
-
 
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
        
-
 
92
                
-
 
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)
43
                    Image(uiImage: UIImage(named: "ui_refresh") ?? UIImage())
108
                     
44
                    .resizable()
-
 
45
                    .frame(width: 32, height: 32, alignment: .center)
-
 
46
                    .aspectRatio(contentMode: .fit)
-
 
47
                    .foregroundColor(Color("color_app_bar_foreground"))
109
                            }.onChange(of:  scrollToIndex, perform: { value in
48
                    .padding(.trailing, 16)
-
 
49
                })
-
 
50
                
110
                                proxy.scrollTo(value, anchor: nil)
51
            }
-
 
52
            .background(Color("color_app_bar_background"))
-
 
53
            .edgesIgnoringSafeArea(.top)
111
                            })
54
            .frame(height: 50)
-
 
55
            Divider()
112
                       })
56
            
113
                    }
57
            
-
 
58
            ScrollView() {
114
                }
59
                ScrollViewReader { proxy in
-
 
60
                    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: {
-
 
61
                        ForEach(0..<self.viewModel.topics.count) { index in
-
 
62
                            CardTopicView(
115
                .padding(.top, 5)
63
                                topicUuid: self.viewModel.topics[index].uuid
116
            }
64
                            )
-
 
65
                            .environmentObject(appNavigation)
-
 
66
                            .frame(
-
 
67
                                width: Constants.CARD_WIDTH,
-
 
68
                                height: Constants.CARD_HEIGHT,
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
69
                                alignment: .center
124
                        
70
                            ).id(index)
-
 
71
                 
125
                }
-
 
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
                )
-
 
132
            }
-
 
133
        }
-
 
134
    }
-
 
135
    
-
 
136
    private func refresh() {
-
 
137
        self.showProgressView = true
-
 
138
        
72
                        }.onChange(of:  scrollToIndex, perform: { value in
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)")
73
                            proxy.scrollTo(value, anchor: nil)
160
 
74
                        })
161
                        
75
                   })
162
                        if json?["success"] ?? "" != false {
76
                }
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
                        
77
            }
188
                       return
78
            .padding(.top, 5)
-
 
79
        }
-
 
80
        .onAppear {
189
                                
81
            var i : Int = 0
190
                    case .failure :
82
            while i < self.viewModel.topics.count {
-
 
83
                if appData.topicUuidActive == self.viewModel.topics[i].uuid {
191
                        self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
Línea 84... Línea 192...
84
                    self.scrollToIndex = i
192
                        self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
85
                }
193
                        self.presentAlert = true
86
                i += 1
194