Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
//// GridTopicView.swift// twogetskills//// Created by Efrain Yanez Recanatini on 1/26/22.//import SwiftUIimport Networkimport Alamofireimport SwiftyJSONimport TTGSnackbarstruct GridTopicView: View {@EnvironmentObject private var networkMonitor : NetworkMonitor@EnvironmentObject private var appNavigation : AppNavigation@ObservedObject private var viewModel : TopicGridViewModel = TopicGridViewModel()@State private var scrollToIndex : Int = 0@State private var showProgressView : Bool = false@State private var presentAlert : Bool = false@State private var titleAlert : String = ""@State private var messageAlert : String = ""private var appData = AppData.sharedInstanceprivate let config = [GridItem(.flexible()),GridItem(.flexible())]var body: some View {ZStack {if self.showProgressView {ProgressView().progressViewStyle(CircularProgressViewStyle(tint: Color("color_progress_view_foreground"))).scaleEffect(3, anchor: .center).zIndex(100000)}VStack(spacing: 0) {HStack {Image("logo").resizable().frame(width: 32, height: 32, alignment: .center).aspectRatio(contentMode: .fit).foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground")).padding(.leading, 16)Text(networkMonitor.status == .disconnected ? Config.LANG_ERROR_NETWORK_MESSAGE_SHORT : Config.LANG_TAB_BAR_BUTTON_TOPICS).font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 )).foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground")).padding(.leading, 4)Spacer()Button(action: {if networkMonitor.status == .connected {refresh()} else {self.titleAlert = Config.LANG_ERROR_NETWORK_TITLEself.messageAlert = Config.LANG_ERROR_NETWORK_MESSAGE_LONGself.presentAlert = true}}, label: {Image(uiImage: UIImage(named: "ui_refresh") ?? UIImage()).resizable().frame(width: 24, height:24, alignment: .center).aspectRatio(contentMode: .fit).foregroundColor(Color("color_app_bar_foreground")).padding(.trailing, 16)})}.edgesIgnoringSafeArea(.top).frame(height: 50).background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))Divider().background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))ScrollView() {ScrollViewReader { proxy inLazyVGrid(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.topics.count) { index inCardTopicView(topicUuid: self.viewModel.topics[index].uuid).environmentObject(appNavigation).frame(width: Constants.CARD_WIDTH,height: Constants.CARD_HEIGHT,alignment: .center).id(index)}.onChange(of: scrollToIndex, perform: { value inproxy.scrollTo(value, anchor: nil)})})}}.padding(.top, 5)}.onAppear {if appData.refreshContentActionRequired {appData.refreshContentActionRequired = falseappData.save()refresh()} else {var i : Int = 0while i < self.viewModel.topics.count {if appData.topicUuidActive == self.viewModel.topics[i].uuid {self.scrollToIndex = i}i += 1}}} .alert(isPresented: $presentAlert) {Alert(title: Text(self.titleAlert),message: Text(self.messageAlert),dismissButton: .default(Text(Config.LANG_COMMON_OK)))}}}private func refresh() {self.showProgressView = truelet headerSecurity : HeaderSecurity = HeaderSecurity()let headers: HTTPHeaders = [.init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),.init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),.init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),.init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),.accept(Constants.HTTP_HEADER_ACCEPT)]print(Config.URL_REFRESH)AF.request(Config.URL_REFRESH, method: .get, headers: headers).responseJSON{(response) inself.showProgressView = falseswitch response.result {case .success:let json = try? JSON(data: response.data!)//print("json : \(json)")if json?["success"] ?? "" != false {let dataService = DataService()dataService.syncFromServer(json : json, refresh: true)self.viewModel.fetch(userUuid: appData.userUuid)var i : Int = 0while i < self.viewModel.topics.count {if appData.topicUuidActive == self.viewModel.topics[i].uuid {self.scrollToIndex = i}i += 1}let snackbar = TTGSnackbar(message: Config.LANG_TOPICS_REFRESH_CONTENT_DONE, duration: .long)snackbar.show()let notificationDao = UserNotificationDao.sharedInstancenotificationDao.markViewedAllPendingByUserUuidAndCommand(userUuid:appData.userUuid, command: Constants.NOTIFICATION_COMMAND_REFRESH_CONTENT)} else {let message = json?["data"].string ?? ""if !message.isEmpty {self.titleAlert = Config.LANG_ERROR_GENERIC_TITLEself.messageAlert = messageself.presentAlert = true}}returncase .failure :self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLEself.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGEself.presentAlert = truereturn}}}}struct GridTopicView_Previews: PreviewProvider {static var previews: some View {GridTopicView()}}