Proyectos de Subversion Iphone Microlearning

Rev

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

//
//  GridGalleryView.swift
//  twogetskills
//
//  Created by Efrain Yanez Recanatini on 3/7/22.
//

import Foundation

import SwiftUI

class GridGalleryTabController: ObservableObject {
    @Published var activeTab : Int = 0

    func open( tab: Int) {
        activeTab = tab
    }
}

struct GridGalleryView: View {
    private let capsuleDao : CapsuleDao = CapsuleDao()
    
    private var capsuleModel : CapsuleModel = CapsuleModel()
    private var viewModel : SlideGridViewModel = SlideGridViewModel()
    private var capsuleTitle : String = ""
    
    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)
    
    private var position : Int
    @State private var selectedTab : Int = 0

    //@StateObject private var tabController = GridGalleryTabController()
    

    @State private var backToSlides : Bool = false
    
    private let config = [GridItem(.flexible())]
    
    init(capsuleUuid : String, position : Int)
    {
        let preference = Preference.sharedInstance
        
        self.position = position
        self.capsuleModel = capsuleDao.selectByUuid(uuid: capsuleUuid)
        viewModel.fetch(capsuleUuid: self.capsuleModel.uuid, userUuid: preference.userUuid)
       
        if self.capsuleModel.name.count > Constants.APP_BAR_TITLE_MAX_LENGTH {
            capsuleTitle = String(Array(self.capsuleModel.name)[0...Constants.APP_BAR_TITLE_MAX_LENGTH]) + "..."
        } else {
            capsuleTitle = self.capsuleModel.name
        }
        




       
    }

    

    var body: some View {
        //let screenSize = UIScreen.main.bounds.size
        
        GeometryReader { geometry in
            NavigationLink("", destination: GridSlideView(capsuleUuid: self.capsuleModel.uuid), isActive: self.$backToSlides).frame(height: 0)
            TabView(selection: self.$selectedTab) {
                ForEach(0..<self.viewModel.slides.count) { i in
                    CardGalleryView(slideUuid:  self.viewModel.slides[i].uuid, position : i
                    )
                    .frame(
                        width: geometry.size.width,
                        height: geometry.size.height - 4,
                        alignment: .center
                    ).tag(i)
                    
                }
            }.tabViewStyle(PageTabViewStyle())
            .onChange(of: self.selectedTab) { selectedTab in
                     print("selectedTab : \(selectedTab)")
                
                
                completeSlide(position: self.selectedTab)
            }.onAppear {
                
                
                self.selectedTab = self.position
                completeSlide(position: self.selectedTab)
            }
            .background(Color(colorBackgroundTopic ?? .gray))
                       .navigationBarTitleDisplayMode(.inline)
                       .navigationTitle(capsuleTitle)
                       .navigationBarBackButtonHidden(true)
                       .toolbar {
                           ToolbarItem(placement: .navigationBarLeading) {
                               Button(action: {
                                   self.backToSlides = true
                               }) {
                                   HStack {
                                       Image(systemName: "chevron.backward")
                                           .aspectRatio(contentMode: .fit)
                                           .foregroundColor(Color( colorAppTextView ?? .white))
                                           .background(Color(colorAppBackground ?? .systemBlue))
                                   }
                               }
                           
                           }
                       }

        }
    }
    
    private func completeSlide(position: Int)
    {
        if self.viewModel.slides[position].type == Constants.SLIDE_TYPE_IMAGE {
            
            let dataService = DataService()
            dataService.completeSlide(slide: self.viewModel.slides[position])
        }
    }
    
    

 

}





struct GridGalleryView_Previews: PreviewProvider {
    static var previews: some View {
        GridGalleryView(capsuleUuid: "C123", position: 2)
    }
}