Proyectos de Subversion Iphone Microlearning - Inconcert

Rev

Rev 2 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
//
2
//  ProfileView.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 5/4/22.
6
//
7
 
8
import Foundation
9
import SwiftUI
10
import Alamofire
11
import TTGSnackbar
12
 
13
struct ProfileView: View {
14
    @EnvironmentObject private var appNavigation : AppNavigation
15
    @EnvironmentObject private var networkMonitor : NetworkMonitor
16
 
17
    @State private var showGlobalAlert : Bool = false
18
    @State private var titleGlobalAlert : String = ""
19
    @State private var messageGlobalAlert : String = ""
20
 
21
 
22
    private var userExtendedPointViewModel : UserExtendedPointViewModel = UserExtendedPointViewModel()
23
 
24
    private var appData = AppData.sharedInstance
25
 
26
 
27
    var body: some View {
28
        GeometryReader { geometry in
29
            VStack(spacing: 0) {
30
                HStack {
31
                    Image("logo")
32
                    .resizable()
33
                    .frame(width: 32, height: 32, alignment: .center)
34
                    .aspectRatio(contentMode: .fit)
35
                        .foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
36
                    .padding(.leading, 16)
37
 
38
 
39
                    Text(networkMonitor.status == .disconnected ? Config.LANG_ERROR_NETWORK_MESSAGE_SHORT :   Config.LANG_TAB_BAR_BUTTON_PROFILE)
40
                    .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
41
                        .foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
42
                    .padding(.leading, 4)
43
 
44
                    Spacer()
45
 
46
 
47
                    Button(action: {
48
                        let snackbar = TTGSnackbar(message: Config.LANG_SIGNOUT_MESSAGE, duration: .long)
49
 
50
                        // Action 1
51
                        snackbar.actionText = Config.LANG_COMMON_YES
52
                        snackbar.actionTextColor = UIColor( Color("color_snackbar_button_yes"))
53
                        snackbar.actionBlock = { (snackbar) in signout() }
54
 
55
 
56
                        snackbar.secondActionText = Config.LANG_COMMON_NO
57
                        snackbar.secondActionTextColor = UIColor(Color("color_snackbar_button_no"))
58
                        snackbar.secondActionBlock = { (snackbar) in }
59
 
60
                        snackbar.show()
61
 
62
                    }, label: {
63
                        Image(uiImage: UIImage(named: "ui_exit_app") ?? UIImage())
64
                        .resizable()
65
                        .frame(width: 24, height: 24, alignment: .center)
66
                        .aspectRatio(contentMode: .fit)
67
                        .foregroundColor(Color("color_network_disconnected_foreground"))
68
                        .padding(.trailing, 16)
69
                    })
70
                }
71
                .edgesIgnoringSafeArea(.top)
72
                .frame(height: 50)
73
                .background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
74
 
75
 
76
                Divider().background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
77
 
78
            ZStack(alignment: .top) {
79
 
80
 
81
                Rectangle()
82
                    .foregroundColor(Color("color_app_bar_background"))
83
                    .edgesIgnoringSafeArea(.top)
84
                    .frame(height: 70)
85
 
86
 
87
                HStack {
88
 
89
                    if appData.userImage.isEmpty {
90
                         Image("logo")
91
                            .resizable()
92
                            .aspectRatio(contentMode: .fit)
93
                            .frame(height: Config.PROFILE_IMAGE_SIZE)
94
                            .clipShape(Circle())
95
                            .overlay(Circle().stroke(Color.white, lineWidth: 4))
96
                            .shadow(radius: 10)
97
 
98
 
99
 
100
                    } else {
101
                        CustomAsyncImageProfile(
102
                            url: URL(string: appData.userImage)!,
103
                            placeholder: { Text(Config.LANG_COMMON_LOADING).font(.footnote).foregroundColor(.black)},
104
                            image: {
105
                                Image(uiImage: $0)
106
                                    .resizable()
107
 
108
                            }
109
                        )
110
                    }
111
                }.padding(10)
112
            }
113
            VStack(spacing: 5) {
114
                VStack(spacing: 5) {
115
                    Text("\(appData.userFirstname) \(appData.userLastname)" )
116
                        .bold()
117
                        .font(.title)
118
                    Text("\(appData.userEmail)")
119
                        .font(.body)
120
                        .foregroundColor(Color("color_textview_foreground"))
121
                }.padding()
122
 
123
 
124
            }.background(Color("color_card_view_background"))
125
 
126
            ScrollView {
127
                ForEach(0..<self.userExtendedPointViewModel.points.count) { i in
128
                    UserExtendedPointView(userExtendedPoint: self.userExtendedPointViewModel.points[i])
129
                        .listRowInsets(.init()).frame(width: geometry.size.width, height: 70, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
130
                }
131
            }
132
            .background(Color("color_card_view_background"))
133
        }.onAppear {
134
            userExtendedPointViewModel.loadAll()
135
        }.alert(isPresented: $showGlobalAlert) {
136
            Alert(
137
                title: Text(self.titleGlobalAlert),
138
                message: Text(self.messageGlobalAlert),
139
                dismissButton: .default(Text(Config.LANG_COMMON_OK))
140
            )
141
        }
142
        }
143
    }
144
 
145
    private func signout() -> Void {
146
 
147
        if networkMonitor.status == .disconnected {
148
 
149
            self.titleGlobalAlert = Config.LANG_ERROR_NETWORK_TITLE
150
            self.messageGlobalAlert = Config.LANG_ERROR_NETWORK_MESSAGE_LONG
151
            self.showGlobalAlert  = true
152
        } else {
153
            let headerSecurity = HeaderSecurity()
154
            let headers: HTTPHeaders = [
155
                .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
156
                .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
157
                .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
158
                .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
159
                .accept(Constants.HTTP_HEADER_ACCEPT)
160
            ]
161
 
162
            AF.request(Config.URL_SIGNOUT, method: .post, headers: headers).responseJSON{(response) in
163
                switch response.result {
164
                    case .success:
165
                        let now = Date()
166
                        let dateFormatter = DateFormatter()
167
                        dateFormatter.dateFormat = Constants.FORMAT_DATETIME_SERVICE
168
                        let dateOn = dateFormatter.string(from: now)
169
 
170
 
171
                        var userLog = UserLogModel()
172
                        userLog.activity = Constants.USER_LOG_ACTIVITY_SIGNOUT
173
                        userLog.userUuid = appData.userUuid
174
                        userLog.addedOn = dateOn
175
 
176
                        let userLogDao = UserLogDao.sharedInstance
177
                        userLogDao.insert(record: userLog)
178
 
179
                        var json : [String: Any] = userLog.toJson()
180
                        json[Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME] = Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_USER_LOG
181
 
182
                        var sync = SyncModel();
183
                        sync.type = Constants.SYNC_ADAPTER_TYPE_SYNC
184
                        if let theJSONData = try?  JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),
185
                           let data = String(data: theJSONData, encoding: String.Encoding.ascii) {
186
                                sync.data = data
187
                            }
188
 
189
                        let syncDao = SyncDao.sharedInstance
190
                        syncDao.insert(record : sync)
191
 
192
                        appData.userEmail = ""
193
                        appData.userFirstname = ""
194
                        appData.userLastname = ""
195
                        appData.userUuid = ""
196
                        appData.userImage = ""
197
                        appData.save()
198
 
199
 
200
                        appNavigation.pageActive = .goodbye
201
                        break;
202
 
203
                    case .failure:
204
                        self.titleGlobalAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
205
                        self.messageGlobalAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
206
                        self.showGlobalAlert = true
207
 
208
                        break;
209
                }
210
            }
211
        }
212
    }
213
}
214
 
215
 
216
struct ProfileView_Previews: PreviewProvider {
217
    static var previews: some View {
218
        ProfileView()
219
    }
220
}