Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 1 | Rev 11 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

//
//  CommentAndRatingView.swift
//  twogetskills
//
//  Created by Efrain Yanez Recanatini on 7/28/22.
//

import SwiftUI
import AudioToolbox
import Network
import Alamofire
import SwiftyJSON
import TTGSnackbar


struct CommentAndRatingView: View {
    @EnvironmentObject private var networkMonitor : NetworkMonitor
    @EnvironmentObject private var appNavigation : AppNavigation
    
    @State private var capsuleTitle : String = ""
   
    
    @ObservedObject var commentsViewModel  : CommentAndRatingCommentsViewModel = CommentAndRatingCommentsViewModel()
    
    @ObservedObject var  capsuleViewModel : CommentAndRatingCapsuleViewModel = CommentAndRatingCapsuleViewModel()
    
    @State var refreshListComment : Bool = true
    
    
    @State var rating : Double = 5.0
    @State var comment : String = "" {
        didSet {
            if comment.count > 128 {
                comment =  String(comment.prefix(128))
                AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)) { return }
            }
        }
    }
    

    
    
    @State private var selectedType: CommentAndRatingPickerType = .introduction
    
    @State private var presentAlert : Bool = false
    @State private var titleAlert : String = ""
    @State private var messageAlert : String = ""
    
    @State private var showProgressView : Bool = false
    
    private let appData : AppData = AppData.sharedInstance
    

    
    var body: some View {
        

        
        VStack(spacing: 0)
        {
            HStack {
                Button(action: {
                    withAnimation {
                        appNavigation.subpageActive = .mycapsules
                    }
                }, label: {

                    
                    Image(systemName: "chevron.backward")
                    .frame(width: 32, height: 32, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                    .aspectRatio(contentMode: .fit)
                    .foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
                })
                .padding(.leading, 16)
                
                Text(networkMonitor.status == .disconnected ? Config.LANG_ERROR_NETWORK_MESSAGE_SHORT :  capsuleTitle)
                .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
                    .foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
                    .padding(.leading, 4)
                
                Spacer()
            }
            .edgesIgnoringSafeArea(.top)
            .frame(height: 50)
            .background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
    
    
            Divider().background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
    
            
            
            CommentAndRatingImageView(capsuleModel: self.capsuleViewModel.capsule)
            
            CommentAndRatingPickerView(capsuleModel: self.capsuleViewModel.capsule, selectedType: self.$selectedType).frame(height: 48)
            
             switch self.selectedType
             {
                case .introduction :
                    CommentAndRatingIntroductionView(capsuleModel:  self.capsuleViewModel.capsule).padding(5)
                
                case .comments :
                    CommentAndRatingCommentListView(comments: self.commentsViewModel.comments, showProgressView: self.$showProgressView).border(Color.blue, width: 1)
          
                default :
                    CommentAndRatingPostCommentView(
                        capsuleModel: self.capsuleViewModel.capsule, comment: self.$comment, rating: self.$rating)

            }
            
            Spacer()
        }
        .background(Color("color_picker_background"))
        .onAppear {

            self.capsuleViewModel.fetch(capsuleUuid: appData.capsuleUuidActive, userUuid: appData.userUuid)

            if self.capsuleViewModel.capsule.name.count > Constants.APP_BAR_TITLE_MAX_LENGTH {
                self.capsuleTitle = String(Array(self.capsuleViewModel.capsule.name)[0...Constants.APP_BAR_TITLE_MAX_LENGTH]) + "..."
            } else {
                self.capsuleTitle = self.capsuleViewModel.capsule.name
            }
            
            self.reloadComments()
            
            
        } .alert(isPresented: $presentAlert) {
            Alert(
                title: Text(self.titleAlert),
                message: Text(self.messageAlert),
                dismissButton: .default(Text(Config.LANG_COMMON_OK))
            )
        }
      .onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_COMMAND_POST_COMMENT))
        { data in
        

            sendPostComment()
        }
    }
    
    func reloadComments()
    {
        self.commentsViewModel.comments.removeAll()
        
        if !self.capsuleViewModel.capsule.linkComments.isEmpty && networkMonitor.status == .connected {
            
            let headerSecurity : HeaderSecurity = HeaderSecurity()
            
            let headers: HTTPHeaders = [
                .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
                .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
                .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
                .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
                .accept(Constants.HTTP_HEADER_ACCEPT)
            ]
                
            print("URL Comments: \(self.capsuleViewModel.capsule.linkComments)")
                
            AF.request(self.capsuleViewModel.capsule.linkComments, method: .get, headers: headers).responseJSON{(response) in
                    self.showProgressView = false
                    switch response.result {
                        case .success:
                            let json = try? JSON(data: response.data!)
                            
                            print("json : \(json)")
                            
                            if json?["success"] ?? "" != false {
                                let dataService = DataService()
                                
                                dataService.syncFromServer(json : json)
                            } else {
                                let message = json?["data"].string ?? ""
                                if !message.isEmpty {
                                    self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE
                                    self.messageAlert = message
                                    self.presentAlert = true
                                }
                            }
                            
                           return
                                    
                        case .failure :
                            self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
                            self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
                            self.presentAlert = true
                            
                            return
                    }
                }
            
        }
        
        
    }
    
    func sendPostComment() {
        
        
        showProgressView = true
        let parameters = [
            Constants.POST_COMMENT_FIELD_COMMENT: "\(comment)",
            Constants.POST_COMMENT_FIELD_RATING: "\(rating)",
        ]
        
        let headerSecurity : HeaderSecurity = HeaderSecurity()
        
        let headers: HTTPHeaders = [
            .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
            .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
            .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
            .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
            .accept(Constants.HTTP_HEADER_ACCEPT)
        ]
        
        print("URL POST COMMENT : \(capsuleViewModel.capsule.linkComments)")
        
        self.showProgressView = true
        AF.request(capsuleViewModel.capsule.linkComments, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON{(response) in
            self.showProgressView = false
            
            switch response.result {
                case .success:
                    let json = try? JSON(data: response.data!)
                    
                    print("json : \(json)")
                    
                    if json?["success"] ?? "" != false {
                        
                    
                        if json?["data"]["comment"] != nil  {
                            
                            let now = Date()
                            let dateFormatter = DateFormatter()
                            dateFormatter.dateFormat = Constants.FORMAT_DATETIME_SERVICE
                            let dateOn = dateFormatter.string(from: now)
      
                            let link_delete =  json?["data"]["comment"]["comment"].string ?? ""
                            
                            
                      
                            
                            let newComment = CommentAndRatingComment(date: dateOn,
                                image: appData.image,
                                fullname: "\(appData.firstName) \(appData.lastName)",
                                rating: Decimal(rating),
                                comment: comment, link_delete:
                                    link_delete)
                            
                            commentsViewModel.comments.insert(newComment, at: 0)
                         }
                 
                        
                        if json?["data"]["capsule"] != nil  {
                            let sTotalComments = json?["data"]["capsule"]["total_comments"].string ?? ""
                            
                            let sTotalRating = json?["data"]["capsule"]["total_rating"].string ?? ""
                          
                            capsuleViewModel.capsule.totalComments = Int(sTotalComments) ?? 0
                            capsuleViewModel.capsule.totalRating = Decimal(Double(sTotalRating) ?? 0)
                            
                            let capsuleDao = CapsuleDao.sharedInstance
                            capsuleDao.update(capsule: capsuleViewModel.capsule)
                        }
                        
                        
                        
                        if json?["data"]["message"] != nil  {
                            let snackbar = TTGSnackbar(message: json?["data"]["message"].string ?? "", duration: .short)
                            snackbar.show()
                            
                        }
                        
   
             
                    } else {
                        let message = json?["data"].string ?? ""
                        if !message.isEmpty {
                            self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE
                            self.messageAlert = message
                            self.presentAlert = true
                        }
                    }
                    
                   return
                            
                case .failure :
                    self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
                    self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
                    self.presentAlert = true
                    
                    return
            }
        }
        
        
    }
}

struct CommentAndRatingView_Previews: PreviewProvider {
    static var previews: some View {
        CommentAndRatingView()
    }
}