Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
//
2
//  GridGalleryView.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 3/7/22.
6
//
7
 
8
import Foundation
9
 
10
import SwiftUI
11
 
12
class GridGalleryTabController: ObservableObject {
13
    @Published var activeTab : Int = 0
14
 
15
    func open( tab: Int) {
16
        activeTab = tab
17
    }
18
}
19
 
20
struct GridGalleryView: View {
21
    private let capsuleDao : CapsuleDao = CapsuleDao()
22
 
23
    private var capsuleModel : CapsuleModel = CapsuleModel()
24
    private var viewModel : SlideGridViewModel = SlideGridViewModel()
25
    private var capsuleTitle : String = ""
26
 
27
    private let colorBackgroundTopic = UIColor(hex: Config.COLOR_BACKGROUND_TOPIC)
28
 
29
    private let colorAppTextView = UIColor(hex: Config.COLOR_APP_TEXT_VIEW_TITLE)
30
 
31
    private let colorAppBackground = UIColor(hex: Config.COLOR_APP_BAR)
32
 
33
    private var position : Int
34
    @State private var selectedTab : Int = 0
35
 
36
    //@StateObject private var tabController = GridGalleryTabController()
37
 
38
 
39
    @State private var backToSlides : Bool = false
40
 
41
    private let config = [GridItem(.flexible())]
42
 
43
    init(capsuleUuid : String, position : Int)
44
    {
45
        let preference = Preference.sharedInstance
46
 
47
        self.position = position
48
        self.capsuleModel = capsuleDao.selectByUuid(uuid: capsuleUuid)
49
        viewModel.fetch(capsuleUuid: self.capsuleModel.uuid, userUuid: preference.userUuid)
50
 
51
        if self.capsuleModel.name.count > Constants.APP_BAR_TITLE_MAX_LENGTH {
52
            capsuleTitle = String(Array(self.capsuleModel.name)[0...Constants.APP_BAR_TITLE_MAX_LENGTH]) + "..."
53
        } else {
54
            capsuleTitle = self.capsuleModel.name
55
        }
56
 
57
 
58
 
59
 
60
 
61
 
62
    }
63
 
64
 
65
 
66
    var body: some View {
67
        //let screenSize = UIScreen.main.bounds.size
68
 
69
        GeometryReader { geometry in
70
            NavigationLink("", destination: GridSlideView(capsuleUuid: self.capsuleModel.uuid), isActive: self.$backToSlides).frame(height: 0)
71
            TabView(selection: self.$selectedTab) {
72
                ForEach(0..<self.viewModel.slides.count) { i in
73
                    CardGalleryView(slideUuid:  self.viewModel.slides[i].uuid, position : i
74
                    )
75
                    .frame(
76
                        width: geometry.size.width,
77
                        height: geometry.size.height - 4,
78
                        alignment: .center
79
                    ).tag(i)
80
 
81
                }
82
            }.tabViewStyle(PageTabViewStyle())
83
            .onChange(of: self.selectedTab) { selectedTab in
84
                     print("selectedTab : \(selectedTab)")
85
 
86
 
87
                completeSlide(position: self.selectedTab)
88
            }.onAppear {
89
 
90
 
91
                self.selectedTab = self.position
92
                completeSlide(position: self.selectedTab)
93
            }
94
            .background(Color(colorBackgroundTopic ?? .gray))
95
                       .navigationBarTitleDisplayMode(.inline)
96
                       .navigationTitle(capsuleTitle)
97
                       .navigationBarBackButtonHidden(true)
98
                       .toolbar {
99
                           ToolbarItem(placement: .navigationBarLeading) {
100
                               Button(action: {
101
                                   self.backToSlides = true
102
                               }) {
103
                                   HStack {
104
                                       Image(systemName: "chevron.backward")
105
                                           .aspectRatio(contentMode: .fit)
106
                                           .foregroundColor(Color( colorAppTextView ?? .white))
107
                                           .background(Color(colorAppBackground ?? .systemBlue))
108
                                   }
109
                               }
110
 
111
                           }
112
                       }
113
 
114
        }
115
    }
116
 
117
    private func completeSlide(position: Int)
118
    {
119
        if self.viewModel.slides[position].type == Constants.SLIDE_TYPE_IMAGE {
120
 
121
            let dataService = DataService()
122
            dataService.completeSlide(slide: self.viewModel.slides[position])
123
        }
124
    }
125
 
126
 
127
 
128
 
129
 
130
}
131
 
132
 
133
 
134
 
135
 
136
struct GridGalleryView_Previews: PreviewProvider {
137
    static var previews: some View {
138
        GridGalleryView(capsuleUuid: "C123", position: 2)
139
    }
140
}
141
 
142
 
143