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 |

//
//  CardTopicView.swift
//  twogetskills
//
//  Created by Efrain Yanez Recanatini on 1/26/22.
//

import SwiftUI

struct CardTopicView: View
{
    @EnvironmentObject private var appNavigation : AppNavigation
    @ObservedObject private var viewModel = TopicCardViewModel()
    
    
    private let appDao = AppDao.sharedInstance
    private var topicName : String = ""
    private var preview : Bool = false
    

    
    init(topicUuid : String, preview : Bool = false)
    {
        
        let appData = appDao.selectOne()
        self.preview = preview
        if(preview) {
            viewModel.topic.companyUuid = "C123"
            viewModel.topic.uuid = "T123"
            viewModel.topic.name = "Topic 123"
            viewModel.topic.description = "Description Topic 123"
            viewModel.topic.position = 0
            viewModel.topic.progress = 50
            viewModel.topic.totalSlides = 10
            viewModel.topic.viewSlides = 5
            viewModel.topic.image = Config.URL_IMAGE_TESTING
            viewModel.topic.completed = 0
        } else {
            viewModel.fetch(topicUuid:topicUuid, userUuid: appData.userUuid)
        }
        
        
       
  
        if viewModel.topic.name.count > Constants.CAPSULE_TITLE_MAX_LENGTH {
            topicName = String(Array(viewModel.topic.name)[0...Constants.CAPSULE_TITLE_MAX_LENGTH]) + "..."
        } else {
            topicName = viewModel.topic.name
        }
        
        
    }
    

    
    var body: some View {

        Button(action: {
            var appData = appDao.selectOne()
            appData.topicUuidActive = self.viewModel.topic.uuid
            appData.capsuleUuidActive = ""
            appData.slideUuidActive = ""
            
            print("update query : 4")
            appDao.update(model: appData)
            
     
            appNavigation.subPageSource = .topics
            appNavigation.subpageActive = .capsules
     
            
        }) {

            VStack(spacing: 0) {
 
                Text(self.topicName)
                .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_CARD_VIEW_TITLE))
                .foregroundColor(Color("color_textview_foreground"))
                .padding(.horizontal, 5)
                .padding(.top, 5)
                .multilineTextAlignment(/*@START_MENU_TOKEN@*/.leading/*@END_MENU_TOKEN@*/)
                            
                Spacer()
                    
                
                CardTopicImageGroup(requestImage: preview ? "": self.viewModel.topic.image )
                .padding(.horizontal, 5)

                Spacer()

                Group() {
                    HStack {
                        ProgressBar(
                            value: self.viewModel.topic.progress,
                            maxValue: 100,
                            backgroundColor: Color("color_card_view_progress_background"),
                            foregroundColor: Color("color_card_view_progress_foreground")
                            
                        )
                        .frame(height: 6)
                        .padding(10)
                                
                        Text( String(format: "%.1f", self.viewModel.topic.progress) + "%")
                            .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_CARD_VIEW_FOOTER ))
                        .foregroundColor(Color("color_textview_foreground"))
                        .padding(5)
                    }
                }
                .background(Color("color_card_view_background_footer"))
                        
            }
            .background(Color("color_card_view_background"))
            .cornerRadius(16)
            .overlay(
                RoundedRectangle(cornerRadius: 16)
                    .stroke(Color("color_card_view_border"), lineWidth:1)
            )
            .padding(.horizontal, 5)
        } .onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_CHANGE_PERCENTAJE_COMPLETED_TOPIC))
        { data in
            let appData =  appDao.selectOne()
            if data.userInfo != nil {
                if let topicUuid = data.userInfo?["topicUuid"]! as? String {
                    if !topicUuid.isEmpty && topicUuid == self.viewModel.topic.uuid {
                        self.viewModel.fetchProgress(topicUuid: topicUuid, userUuid: appData.userUuid)
                        
                    }
                }
            }
        } .padding(.top, 5)
    }
}



struct CardTopicView_Previews: PreviewProvider {
    static var previews: some View {

        CardTopicView(topicUuid: "T123", preview: true)
    }
}

struct CardTopicImageGroup : View {
    
    var requestImage : String

    var body: some View {
        HStack {
            if requestImage.isEmpty {
                
                
                Image(uiImage: UIImage(named: "logo") ?? UIImage())
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                   
                
            } else {
                CustomAsyncImage(
                    url: URL(string: requestImage)!,
                    placeholder: { Text(Config.LANG_COMMON_LOADING).font(.footnote).foregroundColor(.black)},
                    image: {
                        Image(uiImage: $0).resizable()
                    }
                )
            }
        }
    }
}