Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

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

Rev 1 Rev 8
Línea 5... Línea 5...
5
//  Created by Efrain Yanez Recanatini on 7/28/22.
5
//  Created by Efrain Yanez Recanatini on 7/28/22.
6
//
6
//
Línea 7... Línea 7...
7
 
7
 
8
import SwiftUI
8
import SwiftUI
-
 
9
import AudioToolbox
-
 
10
import Network
-
 
11
import Alamofire
-
 
12
import SwiftyJSON
-
 
13
import TTGSnackbar
Línea 9... Línea 14...
9
import AudioToolbox
14
 
-
 
15
 
10
 
16
struct CommentAndRatingView: View {
-
 
17
    @EnvironmentObject private var networkMonitor : NetworkMonitor
-
 
18
    @EnvironmentObject private var appNavigation : AppNavigation
-
 
19
    
Línea 11... Línea 20...
11
struct CommentAndRatingView: View {
20
    @State private var capsuleTitle : String = ""
Línea 12... Línea 21...
12
    var introduction : String =  "<h1>Welcome to SwiftlyRush's bank</h1>, you will be able to access your <b>accounts</b> in this application"
21
   
Línea -... Línea 22...
-
 
22
    
Línea -... Línea 23...
-
 
23
    @ObservedObject var commentsViewModel  : CommentAndRatingCommentsViewModel = CommentAndRatingCommentsViewModel()
-
 
24
    
13
    
25
    @ObservedObject var  capsuleViewModel : CommentAndRatingCapsuleViewModel = CommentAndRatingCapsuleViewModel()
14
    @State var comments : [CommentAndRatingComment] = [CommentAndRatingComment]()
26
    
15
    
27
    @State var refreshListComment : Bool = true
16
    @State var isValidComment : Bool = true
28
    
17
    
29
    
18
    
30
    @State var rating : Double = 5.0
19
    @State var comment : String = "Welcome to SwiftlyRush's bank, you will be able to access your accounts in this application" {
31
    @State var comment : String = "" {
20
        didSet {
32
        didSet {
Línea -... Línea 33...
-
 
33
            if comment.count > 128 {
-
 
34
                comment =  String(comment.prefix(128))
Línea 21... Línea 35...
21
            if comment.count > 128 {
35
                AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) { return }
Línea -... Línea 36...
-
 
36
            }
-
 
37
        }
-
 
38
    }
-
 
39
    
-
 
40
 
-
 
41
    
-
 
42
    
-
 
43
    @State private var selectedType: CommentAndRatingPickerType = .introduction
-
 
44
    
-
 
45
    @State private var presentAlert : Bool = false
22
                comment =  String(comment.prefix(128))
46
    @State private var titleAlert : String = ""
Línea 23... Línea 47...
23
                AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) { return }
47
    @State private var messageAlert : String = ""
24
            }
48
    
25
        }
49
    @State private var showProgressView : Bool = false
26
    }
50
    
-
 
51
    private let appData : AppData = AppData.sharedInstance
-
 
52
    
-
 
53
 
27
    
54
    
-
 
55
    var body: some View {
-
 
56
        
-
 
57
 
28
    
58
        
29
    @State private var selectedType: CommentAndRatingPickerType = .introduction
59
        VStack(spacing: 0)
30
    
60
        {
-
 
61
            HStack {
31
    var body: some View {
62
                Button(action: {
32
        
-
 
Línea 33... Línea 63...
33
 
63
                    withAnimation {
34
        
64
                        appNavigation.subpageActive = .mycapsules
35
        VStack(spacing: 0)
65
                    }
36
        {
66
                }, label: {
Línea 37... Línea 67...
37
            HStack {
67
 
38
                Image("logo")
-
 
39
                .resizable()
68
                    
40
                .frame(width: 32, height: 32, alignment: .center)
-
 
41
                .aspectRatio(contentMode: .fit)
69
                    Image(systemName: "chevron.backward")
42
                .foregroundColor(Color("color_app_bar_foreground"))
70
                    .frame(width: 32, height: 32, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
-
 
71
                    .aspectRatio(contentMode: .fit)
43
                .padding(.leading, 16)
72
                    .foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
-
 
73
                })
44
             
74
                .padding(.leading, 16)
-
 
75
                
Línea 45... Línea 76...
45
                
76
                Text(networkMonitor.status == .disconnected ? Config.LANG_ERROR_NETWORK_MESSAGE_SHORT :  capsuleTitle)
Línea 46... Línea 77...
46
                Text(Config.LANG_TAB_BAR_BUTTON_TOPICS)
77
                .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
Línea 47... Línea 78...
47
                .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
78
                    .foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
48
                .foregroundColor(Color("color_app_bar_foreground"))
79
                    .padding(.leading, 4)
49
                .padding(.leading, 4)
80
                
50
                
81
                Spacer()
Línea 51... Línea 82...
51
                Spacer()
82
            }
52
                
83
            .edgesIgnoringSafeArea(.top)
Línea 53... Línea 84...
53
            }
84
            .frame(height: 50)
54
            .background(Color("color_app_bar_background"))
85
            .background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
-
 
86
    
-
 
87
    
55
            .edgesIgnoringSafeArea(.top)
88
            Divider().background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
Línea 56... Línea 89...
56
            .frame(height: 50)
89
    
-
 
90
            
57
            
91
            
-
 
92
            CommentAndRatingImageView(capsuleModel: self.capsuleViewModel.capsule)
-
 
93
            
-
 
94
            CommentAndRatingPickerView(capsuleModel: self.capsuleViewModel.capsule, selectedType: self.$selectedType).frame(height: 48)
-
 
95
            
-
 
96
             switch self.selectedType
-
 
97
             {
-
 
98
                case .introduction :
-
 
99
                    CommentAndRatingIntroductionView(capsuleModel:  self.capsuleViewModel.capsule).padding(5)
-
 
100
                
-
 
101
                case .comments :
-
 
102
                    CommentAndRatingCommentListView(comments: self.commentsViewModel.comments, showProgressView: self.$showProgressView).border(Color.blue, width: 1)
-
 
103
          
-
 
104
                default :
-
 
105
                    CommentAndRatingPostCommentView(
-
 
106
                        capsuleModel: self.capsuleViewModel.capsule, comment: self.$comment, rating: self.$rating)
-
 
107
 
-
 
108
            }
-
 
109
            
-
 
110
            Spacer()
-
 
111
        }
-
 
112
        .background(Color("color_picker_background"))
-
 
113
        .onAppear {
-
 
114
 
-
 
115
            self.capsuleViewModel.fetch(capsuleUuid: appData.capsuleUuidActive, userUuid: appData.userUuid)
-
 
116
 
-
 
117
            if self.capsuleViewModel.capsule.name.count > Constants.APP_BAR_TITLE_MAX_LENGTH {
-
 
118
                self.capsuleTitle = String(Array(self.capsuleViewModel.capsule.name)[0...Constants.APP_BAR_TITLE_MAX_LENGTH]) + "..."
-
 
119
            } else {
-
 
120
                self.capsuleTitle = self.capsuleViewModel.capsule.name
-
 
121
            }
-
 
122
            
-
 
123
            self.reloadComments()
-
 
124
            
-
 
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
      .onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_COMMAND_POST_COMMENT))
-
 
134
        { data in
-
 
135
        
-
 
136
 
-
 
137
            sendPostComment()
-
 
138
        }
-
 
139
    }
-
 
140
    
-
 
141
    func reloadComments()
-
 
142
    {
-
 
143
        self.commentsViewModel.comments.removeAll()
-
 
144
        
-
 
145
        if !self.capsuleViewModel.capsule.linkComments.isEmpty && networkMonitor.status == .connected {
-
 
146
            
-
 
147
            let headerSecurity : HeaderSecurity = HeaderSecurity()
-
 
148
            
-
 
149
            let headers: HTTPHeaders = [
-
 
150
                .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
-
 
151
                .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
-
 
152
                .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
-
 
153
                .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
-
 
154
                .accept(Constants.HTTP_HEADER_ACCEPT)
-
 
155
            ]
-
 
156
                
-
 
157
            print("URL Comments: \(self.capsuleViewModel.capsule.linkComments)")
-
 
158
                
-
 
159
            AF.request(self.capsuleViewModel.capsule.linkComments, method: .get, headers: headers).responseJSON{(response) in
-
 
160
                    self.showProgressView = false
-
 
161
                    switch response.result {
-
 
162
                        case .success:
-
 
163
                            let json = try? JSON(data: response.data!)
-
 
164
                            
-
 
165
                            print("json : \(json)")
-
 
166
                            
-
 
167
                            if json?["success"] ?? "" != false {
-
 
168
                                let dataService = DataService()
-
 
169
                                
-
 
170
                                dataService.syncFromServer(json : json)
-
 
171
                            } else {
-
 
172
                                let message = json?["data"].string ?? ""
-
 
173
                                if !message.isEmpty {
-
 
174
                                    self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE
-
 
175
                                    self.messageAlert = message
-
 
176
                                    self.presentAlert = true
-
 
177
                                }
-
 
178
                            }
-
 
179
                            
-
 
180
                           return
-
 
181
                                    
-
 
182
                        case .failure :
-
 
183
                            self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
-
 
184
                            self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
-
 
185
                            self.presentAlert = true
-
 
186
                            
-
 
187
                            return
-
 
188
                    }
-
 
189
                }
-
 
190
            
-
 
191
        }
-
 
192
        
-
 
193
        
-
 
194
    }
-
 
195
    
-
 
196
    func sendPostComment() {
-
 
197
        
-
 
198
        
-
 
199
        showProgressView = true
-
 
200
        let parameters = [
-
 
201
            Constants.POST_COMMENT_FIELD_COMMENT: "\(comment)",
-
 
202
            Constants.POST_COMMENT_FIELD_RATING: "\(rating)",
-
 
203
        ]
-
 
204
        
-
 
205
        let headerSecurity : HeaderSecurity = HeaderSecurity()
-
 
206
        
-
 
207
        let headers: HTTPHeaders = [
-
 
208
            .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
-
 
209
            .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
-
 
210
            .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
-
 
211
            .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
-
 
212
            .accept(Constants.HTTP_HEADER_ACCEPT)
-
 
213
        ]
-
 
214
        
-
 
215
        print("URL POST COMMENT : \(capsuleViewModel.capsule.linkComments)")
-
 
216
        
-
 
217
        self.showProgressView = true
-
 
218
        AF.request(capsuleViewModel.capsule.linkComments, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON{(response) in
-
 
219
            self.showProgressView = false
-
 
220
            
-
 
221
            switch response.result {
-
 
222
                case .success:
-
 
223
                    let json = try? JSON(data: response.data!)
-
 
224
                    
-
 
225
                    print("json : \(json)")
-
 
226
                    
-
 
227
                    if json?["success"] ?? "" != false {
-
 
228
                        
-
 
229
                    
-
 
230
                        if json?["data"]["comment"] != nil  {
-
 
231
                            
-
 
232
                            let now = Date()
-
 
233
                            let dateFormatter = DateFormatter()
-
 
234
                            dateFormatter.dateFormat = Constants.FORMAT_DATETIME_SERVICE
-
 
235
                            let dateOn = dateFormatter.string(from: now)
-
 
236
      
-
 
237
                            let link_delete =  json?["data"]["comment"]["comment"].string ?? ""
-
 
238
                            
-
 
239
                            
-
 
240
                      
-
 
241
                            
-
 
242
                            let newComment = CommentAndRatingComment(date: dateOn,
-
 
243
                                image: appData.image,
-
 
244
                                fullname: "\(appData.firstName) \(appData.lastName)",
-
 
245
                                rating: Decimal(rating),
-
 
246
                                comment: comment, link_delete:
-
 
247
                                    link_delete)
-
 
248
                            
-
 
249
                            commentsViewModel.comments.insert(newComment, at: 0)
-
 
250
                         }
-
 
251
                 
-
 
252
                        
-
 
253
                        if json?["data"]["capsule"] != nil  {
-
 
254
                            let sTotalComments = json?["data"]["capsule"]["total_comments"].string ?? ""
-
 
255
                            
-
 
256
                            let sTotalRating = json?["data"]["capsule"]["total_rating"].string ?? ""
-
 
257
                          
-
 
258
                            capsuleViewModel.capsule.totalComments = Int(sTotalComments) ?? 0
-
 
259
                            capsuleViewModel.capsule.totalRating = Decimal(Double(sTotalRating) ?? 0)
-
 
260
                            
-
 
261
                            let capsuleDao = CapsuleDao.sharedInstance
-
 
262
                            capsuleDao.update(capsule: capsuleViewModel.capsule)
-
 
263
                        }
-
 
264
                        
-
 
265
                        
-
 
266
                        
-
 
267
                        if json?["data"]["message"] != nil  {
-
 
268
                            let snackbar = TTGSnackbar(message: json?["data"]["message"].string ?? "", duration: .short)
-
 
269
                            snackbar.show()
-
 
270
                            
-
 
271
                        }
-
 
272
                        
-
 
273
   
-
 
274
             
58
            Divider()
275
                    } else {
59
            
276
                        let message = json?["data"].string ?? ""
Línea 60... Línea 277...
60
            
277
                        if !message.isEmpty {
61
            CommentAndRatingImageView()
278
                            self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE