Proyectos de Subversion Iphone Microlearning

Rev

Autoría | Ultima modificación | Ver Log |

//
//  GridCapsuleView.swift
//  twogetskills
//
//  Created by Efrain Yanez Recanatini on 2/17/22.
//

import SwiftUI

struct GridCapsuleView: View
{
    @Environment(\.presentationMode)
       var presentationMode: Binding

    private var viewModel : CapsuleGridViewModel = CapsuleGridViewModel()

     
    let topicDao : TopicDao = TopicDao()
    let topicModel : TopicModel
    
    var topicTitle : String = ""
    
    
    init(topicUuid : String)
    {
        let preference = Preference.sharedInstance
        self.topicModel = topicDao.selectByUuid(uuid: topicUuid)
        viewModel.fetch(topicUuid: self.topicModel.uuid, userUuid: preference.userUuid)
       
        if self.topicModel.name.count > Constants.APP_BAR_TITLE_MAX_LENGTH {
            topicTitle = String(Array(self.topicModel.name)[0...Constants.APP_BAR_TITLE_MAX_LENGTH]) + "..."
        } else {
            topicTitle = self.topicModel.name
        }
    }

    
    private let colorBackgroundTopic = UIColor(hex: Config.COLOR_BACKGROUND_TOPIC)
    
    private let colorAppTextView = UIColor(hex: Config.COLOR_APP_TEXT_VIEW_TITLE)

    private let colorAppBackground = UIColor(hex: Config.COLOR_APP_BAR)
    
    
    @State private var backToMain : Bool = false
    
    let itemPerRow: CGFloat = 2


    let config = [
        GridItem(.flexible()),
        GridItem(.flexible())
    ]
    
    

    
    var body: some View {
        NavigationLink("", destination: MainView(), isActive: self.$backToMain).frame(height: 0)
 
            
        VStack(spacing: 0.0) {
     
            
            ScrollView() {
                LazyVGrid(columns: config, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/, spacing: /*@START_MENU_TOKEN@*/nil/*@END_MENU_TOKEN@*/, pinnedViews: /*@START_MENU_TOKEN@*/[]/*@END_MENU_TOKEN@*/, content: {
                        
                    ForEach(0..<self.viewModel.capsules.count) { i in
                        CardCapsuleView(capsuleModel: self.viewModel.capsules[i]
                        ).frame(
                            width: Constants.CARD_WIDTH,
                            height: Constants.CARD_HEIGHT,
                            alignment: .center
                        )
                    }
                }).padding(.top, 10)
            }
            Spacer()
        }
     
        .background(Color(colorBackgroundTopic ?? .gray))
        .navigationBarBackButtonHidden(true)
        .navigationBarTitleDisplayMode(.inline)
        .navigationTitle(topicTitle)
        .toolbar {
            ToolbarItem(placement: .navigationBarLeading) {
                Button(action: {
                    //self.presentationMode.wrappedValue.dismiss()
                    self.backToMain = true
                }) {
                    HStack {
                        Image(systemName: "chevron.backward")
                            .aspectRatio(contentMode: .fit)
                            .foregroundColor(Color( colorAppTextView ?? .systemBlue))
                            .background(Color(colorAppBackground ?? .systemBlue))
                    }
                }
            
            }
        } .onAppear {
            print("GridCapsuleView")
        }
    }
}


struct GridCapsuleView_Previews: PreviewProvider {
    static var previews: some View {
       GridCapsuleView(topicUuid: "T123")
    }
}