Rev 17 | AutorÃa | Comparar con el anterior | Ultima modificación | Ver Log |
//
// GridTopicView.swift
// twogetskills
//
// Created by Efrain Yanez Recanatini on 1/26/22.
//
import SwiftUI
import Network
import Alamofire
import SwiftyJSON
import TTGSnackbar
struct 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 = Environment(\.appData).wrappedValue
private 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_TITLE
self.messageAlert = Config.LANG_ERROR_NETWORK_MESSAGE_LONG
self.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 in
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.topics.count) { index in
CardTopicView(
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 in
proxy.scrollTo(value, anchor: nil)
})
})
}
}
.padding(.top, 5)
}
.onAppear {
if appData.refreshContentActionRequired {
appData.refreshContentActionRequired = false
appData.save()
refresh()
} else {
var i : Int = 0
while 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 = true
let 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) in
self.showProgressView = false
switch 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 = 0
while 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()
notificationDao.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_TITLE
self.messageAlert = message
self.presentAlert = true
}
}
return
case .failure :
self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
self.presentAlert = true
return
}
}
}
}
struct GridTopicView_Previews: PreviewProvider {
static var previews: some View {
GridTopicView()
}
}