Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 8 | Rev 17 | 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
//  CommentAndRatingView.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 7/28/22.
6
//
7
 
8
import SwiftUI
9
import AudioToolbox
8 efrain 10
import Network
11
import Alamofire
12
import SwiftyJSON
13
import TTGSnackbar
1 efrain 14
 
8 efrain 15
 
1 efrain 16
struct CommentAndRatingView: View {
8 efrain 17
    @EnvironmentObject private var networkMonitor : NetworkMonitor
18
    @EnvironmentObject private var appNavigation : AppNavigation
1 efrain 19
 
8 efrain 20
    @State private var capsuleTitle : String = ""
21
 
1 efrain 22
 
8 efrain 23
    @ObservedObject var commentsViewModel  : CommentAndRatingCommentsViewModel = CommentAndRatingCommentsViewModel()
1 efrain 24
 
8 efrain 25
    @ObservedObject var  capsuleViewModel : CommentAndRatingCapsuleViewModel = CommentAndRatingCapsuleViewModel()
1 efrain 26
 
8 efrain 27
    @State var refreshListComment : Bool = true
28
 
29
 
30
    @State var rating : Double = 5.0
31
    @State var comment : String = "" {
1 efrain 32
        didSet {
33
            if comment.count > 128 {
34
                comment =  String(comment.prefix(128))
35
                AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) { return }
36
            }
37
        }
38
    }
39
 
8 efrain 40
 
1 efrain 41
 
8 efrain 42
 
1 efrain 43
    @State private var selectedType: CommentAndRatingPickerType = .introduction
44
 
8 efrain 45
    @State private var presentAlert : Bool = false
46
    @State private var titleAlert : String = ""
47
    @State private var messageAlert : String = ""
48
 
49
    @State private var showProgressView : Bool = false
50
 
11 efrain 51
    private let appDao = AppDao.sharedInstance
8 efrain 52
 
53
 
54
 
1 efrain 55
    var body: some View {
56
 
57
 
58
 
59
        VStack(spacing: 0)
60
        {
61
            HStack {
8 efrain 62
                Button(action: {
63
                    withAnimation {
64
                        appNavigation.subpageActive = .mycapsules
65
                    }
66
                }, label: {
67
 
68
 
69
                    Image(systemName: "chevron.backward")
70
                    .frame(width: 32, height: 32, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
71
                    .aspectRatio(contentMode: .fit)
72
                    .foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
73
                })
1 efrain 74
                .padding(.leading, 16)
75
 
8 efrain 76
                Text(networkMonitor.status == .disconnected ? Config.LANG_ERROR_NETWORK_MESSAGE_SHORT :  capsuleTitle)
1 efrain 77
                .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
8 efrain 78
                    .foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
79
                    .padding(.leading, 4)
1 efrain 80
 
81
                Spacer()
82
            }
83
            .edgesIgnoringSafeArea(.top)
84
            .frame(height: 50)
8 efrain 85
            .background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
86
 
87
 
88
            Divider().background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
89
 
1 efrain 90
 
91
 
8 efrain 92
            CommentAndRatingImageView(capsuleModel: self.capsuleViewModel.capsule)
1 efrain 93
 
8 efrain 94
            CommentAndRatingPickerView(capsuleModel: self.capsuleViewModel.capsule, selectedType: self.$selectedType).frame(height: 48)
1 efrain 95
 
96
             switch self.selectedType
97
             {
98
                case .introduction :
8 efrain 99
                    CommentAndRatingIntroductionView(capsuleModel:  self.capsuleViewModel.capsule).padding(5)
1 efrain 100
 
101
                case .comments :
11 efrain 102
                    if self.commentsViewModel.comments.count == 0 {
103
                        CommentAndRatingCommentListEmptyView()
104
                    } else {
105
 
106
                        ScrollView {
107
                            LazyVStack  {
108
                                //ForEach(0..<self.comments.count) { index in
109
 
110
                                ForEach(self.commentsViewModel.comments) { commentItem in
111
                                    CommentAndRatingCommentListItem(comment: commentItem)
112
                                }
113
 
114
                            }
115
                        }
116
                    }
117
 
118
 
119
                   // CommentAndRatingCommentListView(comments: self.commentsViewModel.comments, showProgressView: self.$showProgressView).border(Color.blue, width: 1)
1 efrain 120
 
121
                default :
8 efrain 122
                    CommentAndRatingPostCommentView(
123
                        capsuleModel: self.capsuleViewModel.capsule, comment: self.$comment, rating: self.$rating)
124
 
1 efrain 125
            }
126
 
127
            Spacer()
8 efrain 128
        }
129
        .background(Color("color_picker_background"))
130
        .onAppear {
11 efrain 131
            let appData = appDao.selectOne()
8 efrain 132
            self.capsuleViewModel.fetch(capsuleUuid: appData.capsuleUuidActive, userUuid: appData.userUuid)
133
 
134
            if self.capsuleViewModel.capsule.name.count > Constants.APP_BAR_TITLE_MAX_LENGTH {
135
                self.capsuleTitle = String(Array(self.capsuleViewModel.capsule.name)[0...Constants.APP_BAR_TITLE_MAX_LENGTH]) + "..."
136
            } else {
137
                self.capsuleTitle = self.capsuleViewModel.capsule.name
138
            }
139
 
140
            self.reloadComments()
141
 
142
 
143
        } .alert(isPresented: $presentAlert) {
144
            Alert(
145
                title: Text(self.titleAlert),
146
                message: Text(self.messageAlert),
147
                dismissButton: .default(Text(Config.LANG_COMMON_OK))
148
            )
149
        }
150
      .onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_COMMAND_POST_COMMENT))
151
        { data in
152
 
153
 
154
            sendPostComment()
155
        }
11 efrain 156
        .onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_COMMAND_DELETE_COMMENT))
157
          { data in
158
            if data.userInfo != nil {
159
 
160
                //print(data.userInfo)
161
 
162
                let id = data.userInfo?["id"]! as? String ?? ""
163
                let link_delete = data.userInfo?["link_delete"]! as? String ?? ""
164
 
165
                print("id = \(id)  link_delete = \(link_delete)")
166
 
167
                if !id.isEmpty || !link_delete.isEmpty {
168
 
169
                    deleteComment(id: id, linkDelete : link_delete)
170
                }
171
 
172
            }
173
 
174
 
175
          }
1 efrain 176
    }
8 efrain 177
 
178
    func reloadComments()
179
    {
180
        self.commentsViewModel.comments.removeAll()
181
 
182
        if !self.capsuleViewModel.capsule.linkComments.isEmpty && networkMonitor.status == .connected {
183
 
11 efrain 184
            let appData = appDao.selectOne()
8 efrain 185
            let headerSecurity : HeaderSecurity = HeaderSecurity()
186
 
187
            let headers: HTTPHeaders = [
188
                .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
189
                .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
190
                .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
191
                .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
192
                .accept(Constants.HTTP_HEADER_ACCEPT)
193
            ]
194
 
11 efrain 195
            //print("URL Comments: \(self.capsuleViewModel.capsule.linkComments)")
196
 
197
            commentsViewModel.comments.removeAll()
8 efrain 198
 
199
            AF.request(self.capsuleViewModel.capsule.linkComments, method: .get, headers: headers).responseJSON{(response) in
200
                    self.showProgressView = false
201
                    switch response.result {
202
                        case .success:
203
                            let json = try? JSON(data: response.data!)
204
 
11 efrain 205
                           // print("json : \(json)")
8 efrain 206
 
207
                            if json?["success"] ?? "" != false {
208
 
11 efrain 209
                                if json?["data"]["capsule"] != nil  {
210
                                    let sTotalComments = json?["data"]["capsule"]["total_comments"].string ?? ""
211
 
212
                                    let sTotalRating = json?["data"]["capsule"]["total_rating"].string ?? ""
213
 
214
                                    capsuleViewModel.capsule.totalComments = Int(sTotalComments) ?? 0
215
                                    capsuleViewModel.capsule.totalRating = Decimal(Double(sTotalRating) ?? 0)
216
 
217
                                    let capsuleDao = CapsuleDao.sharedInstance
218
                                    capsuleDao.update(capsule: capsuleViewModel.capsule)
219
                                }
220
 
221
                                if json?["data"]["comments"] != nil  {
222
                                    var newComment : CommentAndRatingComment
223
                                    for jcomment in json!["data"]["comments"]
224
                                    {
225
                                        newComment = CommentAndRatingComment()
226
                                        newComment.date = jcomment.1["date"].string ?? ""
227
                                        newComment.rating = Decimal(Double(jcomment.1["rating"].string ??  "") ?? 0)
228
                                        newComment.fullname = jcomment.1["fullname"].string ?? ""
229
                                        newComment.comment = jcomment.1["comment"].string ?? ""
230
                                        newComment.image = jcomment.1["image"].string ?? ""
231
                                        newComment.link_delete = jcomment.1["link_delete"].string ?? ""
232
                                        newComment.date = jcomment.1["date"].string ?? ""
233
 
234
                                        commentsViewModel.comments.append(newComment)
235
                                    }
236
                                }
237
 
238
 
239
 
8 efrain 240
                            } else {
241
                                let message = json?["data"].string ?? ""
242
                                if !message.isEmpty {
243
                                    self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE
244
                                    self.messageAlert = message
245
                                    self.presentAlert = true
246
                                }
247
                            }
248
 
249
                           return
250
 
251
                        case .failure :
252
                            self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
253
                            self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
254
                            self.presentAlert = true
255
 
256
                            return
257
                    }
258
                }
259
 
260
        }
261
 
262
 
263
    }
264
 
265
    func sendPostComment() {
266
 
267
 
11 efrain 268
 
269
        let appData = appDao.selectOne()
8 efrain 270
        showProgressView = true
271
        let parameters = [
272
            Constants.POST_COMMENT_FIELD_COMMENT: "\(comment)",
11 efrain 273
            Constants.POST_COMMENT_FIELD_RATING: "\(Int(rating))",
8 efrain 274
        ]
275
 
276
        let headerSecurity : HeaderSecurity = HeaderSecurity()
277
 
278
        let headers: HTTPHeaders = [
279
            .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
280
            .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
281
            .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
282
            .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
283
            .accept(Constants.HTTP_HEADER_ACCEPT)
284
        ]
285
 
11 efrain 286
       // print("URL POST COMMENT : \(capsuleViewModel.capsule.linkComments)")
8 efrain 287
 
288
        self.showProgressView = true
11 efrain 289
        AF.request(capsuleViewModel.capsule.linkCommentAdd, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON{(response) in
8 efrain 290
            self.showProgressView = false
291
 
292
            switch response.result {
293
                case .success:
11 efrain 294
 
8 efrain 295
                    let json = try? JSON(data: response.data!)
296
 
297
                    print("json : \(json)")
298
 
299
                    if json?["success"] ?? "" != false {
300
 
301
 
302
                        if json?["data"]["comment"] != nil  {
303
 
304
                            let now = Date()
305
                            let dateFormatter = DateFormatter()
306
                            dateFormatter.dateFormat = Constants.FORMAT_DATETIME_SERVICE
307
                            let dateOn = dateFormatter.string(from: now)
308
 
11 efrain 309
                            let link_delete =  json?["data"]["comment"]["link_delete"].string ?? ""
310
 
8 efrain 311
 
11 efrain 312
                            var newComment = CommentAndRatingComment()
313
                            newComment.date = dateOn
314
                            newComment.image = appData.userImage
315
                            newComment.fullname = "\(appData.userFirstname) \(appData.userLastname)"
316
                            newComment.rating = Decimal(rating)
317
                            newComment.comment = comment
318
                            newComment.link_delete = link_delete
8 efrain 319
 
11 efrain 320
                            commentsViewModel.prepend(newComment: newComment)
8 efrain 321
 
11 efrain 322
                            self.comment = ""
323
                            self.rating = 5
324
                            self.selectedType = .comments
8 efrain 325
                         }
326
 
327
 
328
                        if json?["data"]["capsule"] != nil  {
329
                            let sTotalComments = json?["data"]["capsule"]["total_comments"].string ?? ""
330
 
331
                            let sTotalRating = json?["data"]["capsule"]["total_rating"].string ?? ""
332
 
333
                            capsuleViewModel.capsule.totalComments = Int(sTotalComments) ?? 0
334
                            capsuleViewModel.capsule.totalRating = Decimal(Double(sTotalRating) ?? 0)
335
 
336
                            let capsuleDao = CapsuleDao.sharedInstance
337
                            capsuleDao.update(capsule: capsuleViewModel.capsule)
338
                        }
339
 
340
 
341
 
342
                        if json?["data"]["message"] != nil  {
343
                            let snackbar = TTGSnackbar(message: json?["data"]["message"].string ?? "", duration: .short)
344
                            snackbar.show()
345
 
346
                        }
347
 
348
 
349
 
350
                    } else {
351
                        let message = json?["data"].string ?? ""
352
                        if !message.isEmpty {
353
                            self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE
354
                            self.messageAlert = message
355
                            self.presentAlert = true
356
                        }
357
                    }
358
 
359
                   return
360
 
361
                case .failure :
362
                    self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
363
                    self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
364
                    self.presentAlert = true
365
 
366
                    return
367
            }
368
        }
369
 
370
 
371
    }
11 efrain 372
 
373
    func deleteComment(id : String, linkDelete : String) {
374
 
375
 
376
 
377
        let appData = appDao.selectOne()
378
        showProgressView = true
379
 
380
 
381
        let headerSecurity : HeaderSecurity = HeaderSecurity()
382
 
383
        let headers: HTTPHeaders = [
384
            .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
385
            .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
386
            .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
387
            .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
388
            .accept(Constants.HTTP_HEADER_ACCEPT)
389
        ]
390
 
391
        self.showProgressView = true
392
        AF.request(linkDelete, method: .post, encoding: URLEncoding.default, headers: headers).responseJSON{(response) in
393
            self.showProgressView = false
394
 
395
            switch response.result {
396
                case .success:
397
 
398
                    let json = try? JSON(data: response.data!)
399
 
400
                   // print("json : \(json)")
401
 
402
                    if json?["success"] ?? "" != false {
403
 
404
 
405
 
406
 
407
 
408
                        if json?["data"]["capsule"] != nil  {
409
                            let sTotalComments = json?["data"]["capsule"]["total_comments"].string ?? ""
410
 
411
                            let sTotalRating = json?["data"]["capsule"]["total_rating"].string ?? ""
412
 
413
                            capsuleViewModel.capsule.totalComments = Int(sTotalComments) ?? 0
414
                            capsuleViewModel.capsule.totalRating = Decimal(Double(sTotalRating) ?? 0)
415
 
416
                            let capsuleDao = CapsuleDao.sharedInstance
417
                            capsuleDao.update(capsule: capsuleViewModel.capsule)
418
                        }
419
 
420
 
421
 
422
                        if json?["data"]["message"] != nil  {
423
                            let snackbar = TTGSnackbar(message: json?["data"]["message"].string ?? "", duration: .short)
424
                            snackbar.show()
425
 
426
                        }
427
 
428
                        self.commentsViewModel.removeItem(id: id)
429
 
430
 
431
 
432
                    } else {
433
                        let message = json?["data"].string ?? ""
434
                        if !message.isEmpty {
435
                            self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE
436
                            self.messageAlert = message
437
                            self.presentAlert = true
438
                        }
439
                    }
440
 
441
                   return
442
 
443
                case .failure :
444
                    self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
445
                    self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
446
                    self.presentAlert = true
447
 
448
                    return
449
            }
450
        }
451
 
452
 
453
    }
1 efrain 454
}
455
 
456
struct CommentAndRatingView_Previews: PreviewProvider {
457
    static var previews: some View {
458
        CommentAndRatingView()
459
    }
460
}
461
 
462