Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

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

//
//  CardGalleryView.swift
//  twogetskills
//
//  Created by Efrain Yanez Recanatini on 3/6/22.
//

import Foundation
import SwiftUI
import AVKit
import NavigationStack

struct CardGalleryView: View {
    
    @EnvironmentObject private var networkMonitor : NetworkMonitor
    @EnvironmentObject private var appNavigation : AppNavigation
    @ObservedObject private var viewModel = GalleryCardViewModel()
    
    @State private var goToVideoPlayer : Bool = false
    @State private var goToPdfViewer : Bool = false
    @State private var goToWebViewer : Bool = false
    
    @State private var showGlobalAlert : Bool = false
    @State private var titleGlobalAlert : String = ""
    @State private var messageGlobalAlert : String = ""
    
    
    private let appDao = AppDao.sharedInstance
    
    private var buttonShow : Bool = false
    private var buttonTitle : String = ""

    
   
   
   
    

    
    init (slideUuid : String) {
        let appData = appDao.selectOne()
        self.viewModel.fetch(slideUuid: slideUuid, userUuid: appData.userUuid)



      
            
        self.buttonShow = false
        if self.viewModel.slide.type == Constants.SLIDE_TYPE_QUIZ  {
            self.buttonTitle = Config.LANG_BUTTON_LAUNCH_QUIZ
            self.buttonShow = true
        } else if self.viewModel.slide.type == Constants.SLIDE_TYPE_VIDEO {
            self.buttonTitle =  Config.LANG_BUTTON_LAUNCH_VIDEO_PLAYER
            self.buttonShow = true
        } else if self.viewModel.slide.type == Constants.SLIDE_TYPE_AUDIO   {
            self.buttonTitle = Config.LANG_BUTTON_LAUNCH_AUDIO_PLAYER
            self.buttonShow = true
        } else if self.viewModel.slide.type == Constants.SLIDE_TYPE_DOCUMENT {
            self.buttonTitle = Config.LANG_BUTTON_LAUNCH_VIEW_PDF
            self.buttonShow = true
            
        } else if self.viewModel.slide.type == Constants.SLIDE_TYPE_TEXT {
            self.buttonTitle = Config.LANG_BUTTON_LAUNCH_VIEW_PDF
            self.buttonShow = true
        } else {
            self.buttonShow = false
        }
        
    }

    
    
    
    var body: some View {
        GeometryReader { geometry in
            let checkMarkX = (geometry.size.width - 50) / 2
            let checkMarkY = -1 * ((geometry.size.height - 50) / 2)
            
            let buttonX = (geometry.size.width - 180) / 2
            let buttonY = (geometry.size.height - 80) / 2
          
            ZStack {
                if self.viewModel.slide.completed == 1  {
                    Image(uiImage: UIImage(named: "ic_slide_completado_checkmark") ?? UIImage()).offset(x: checkMarkX, y: checkMarkY).zIndex(10000)
                }
                
                if self.buttonShow {
                
                    Button(action: {
                        btnPlay()
                    }, label: {
                        Text(self.buttonTitle)
                            .font(Font.custom(Config.FONT_NAME_BOLD, size: Config.FONT_SIZE_BUTTONS))
                        .foregroundColor(Color("color_button_gallery_foreground"))
                       
                    })
              
                    .padding(.vertical, 15)
                    .padding(.horizontal, 10)
                    .background(Color("color_button_gallery_background"))
                    .cornerRadius(/*@START_MENU_TOKEN@*/3.0/*@END_MENU_TOKEN@*/)
                    .offset(x: buttonX, y: buttonY)
                    .zIndex(10000)
                }
                
                if (self.viewModel.progressCapsule >= 100 && self.viewModel.completedCapsule == 0)
                    || (self.viewModel.progressTopic >= 100 && self.viewModel.completedTopic == 0) {
                    Button(action: {
                        if self.viewModel.progressTopic >= 100 && self.viewModel.completedTopic == 0 {
                        
                            appNavigation.subpageActive = .finishtopic
                            
                        } else {
                            if self.viewModel.progressCapsule >= 100 && self.viewModel.completedCapsule == 0 {
                                appNavigation.subpageActive = .finishcapsule
                            }
                        }
                    }, label: {
                        Text(Config.LANG_BUTTON_FINISH_CAPSULE_OR_TOPIC)
                        .font(.callout)
                        .foregroundColor(.black)
                    })
                    .padding()
                    .background(Color("color_button_gallery_background"))
                    .cornerRadius(/*@START_MENU_TOKEN@*/3.0/*@END_MENU_TOKEN@*/)
                    .zIndex(10000)
                }
               
                VStack {
                
                    if self.viewModel.slide.type == Constants.SLIDE_TYPE_VIDEO || self.viewModel.slide.type == Constants.SLIDE_TYPE_AUDIO {
                    PushView(
                        destination: VideoPlayerView(slideUuid: viewModel.slide.uuid),
                        isActive: self.$goToVideoPlayer,
                        label: {
                            Text("")
                        }).frame(height: 0)
                    }
                    
                    if self.viewModel.slide.type == Constants.SLIDE_TYPE_DOCUMENT {
              
                        PushView(
                            destination: PdfViewerView(slideUuid: viewModel.slide.uuid),
                            isActive: self.$goToPdfViewer,
                            label: {
                                Text("")
                            }).frame(height: 0)
                    }
                    
                    
                    if self.viewModel.slide.type == Constants.SLIDE_TYPE_TEXT {
                    
                        PushView(
                            destination: WebViewerView(slideUuid: viewModel.slide.uuid),
                            isActive: self.$goToWebViewer,
                            label: {
                                Text("")
                        }).frame(height: 0)
                    }
                    
                    Spacer()
                    HStack {
                        if viewModel.slide.background.isEmpty && viewModel.slide.file.isEmpty {
                            Image(uiImage: UIImage(named: "logo") ?? UIImage())
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                        } else {
                            CustomAsyncImage(
                                url: URL(string: self.viewModel.slide.type == Constants.SLIDE_TYPE_IMAGE ? self.viewModel.slide.file : self.viewModel.slide.background)!,
                                placeholder: {
                                    Spacer()
                                    Text(Config.LANG_COMMON_LOADING).font(.footnote).foregroundColor(.black)
                                    Spacer()
                                },
                                image: {
                                    Image(uiImage: $0).resizable()
                                }
                            )
                        }
                    }.padding(.horizontal, 5)

                    Spacer()
                }
               // .zIndex(500)
              
            } .onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_COMPLETED_SLIDE))
            { data in
               // print("CardGalleryView  Receive " )
                let appData = appDao.selectOne()
                if data.userInfo != nil {
                    if let slideUuid = data.userInfo?["slideUuid"]! as? String {
                        if !slideUuid.isEmpty && slideUuid == self.viewModel.slide.uuid {
                            self.viewModel.fetchProgress(slideUuid: slideUuid, userUuid: appData.userUuid)
                        }
                    }
                }
          
            } .alert(isPresented: $showGlobalAlert) {
                Alert(
                    title: Text(self.titleGlobalAlert),
                    message: Text(self.messageGlobalAlert),
                    dismissButton: .default(Text(Config.LANG_COMMON_OK))
                )
            }
        }
    }
    
    private func btnPlay()
    {
        
        if networkMonitor.status == .disconnected {
        
            self.titleGlobalAlert = Config.LANG_ERROR_NETWORK_TITLE
            self.messageGlobalAlert = Config.LANG_ERROR_NETWORK_MESSAGE_LONG
            self.showGlobalAlert  = true
            return
        }
        
        
        if self.viewModel.slide.type == Constants.SLIDE_TYPE_VIDEO {
            AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeft
            UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
            UIViewController.attemptRotationToDeviceOrientation()
            
            
            self.goToVideoPlayer = true
            
            
            
        }  else if self.viewModel.slide.type == Constants.SLIDE_TYPE_AUDIO {
            
            self.goToVideoPlayer = true
            
        }  else if self.viewModel.slide.type == Constants.SLIDE_TYPE_DOCUMENT {
            
            self.goToPdfViewer = true
            
        } else if self.viewModel.slide.type == Constants.SLIDE_TYPE_TEXT {
            
            self.goToWebViewer = true
            
        } else if self.viewModel.slide.type == Constants.SLIDE_TYPE_QUIZ {
        
            
        }
    }

    
    
}

struct CardGalleryView_Previews: PreviewProvider {
    static var previews: some View {
        CardGalleryView(slideUuid: "S123")
    }
    
}