1 |
efrain |
1 |
//
|
|
|
2 |
// GridTopicView.swift
|
|
|
3 |
// twogetskills
|
|
|
4 |
//
|
|
|
5 |
// Created by Efrain Yanez Recanatini on 1/26/22.
|
|
|
6 |
//
|
|
|
7 |
|
|
|
8 |
import SwiftUI
|
|
|
9 |
import Network
|
|
|
10 |
import Alamofire
|
|
|
11 |
import SwiftyJSON
|
|
|
12 |
import TTGSnackbar
|
|
|
13 |
|
|
|
14 |
struct GridTopicView: View {
|
|
|
15 |
@EnvironmentObject private var networkMonitor : NetworkMonitor
|
|
|
16 |
@EnvironmentObject private var appNavigation : AppNavigation
|
|
|
17 |
|
|
|
18 |
@ObservedObject private var viewModel : TopicGridViewModel = TopicGridViewModel()
|
|
|
19 |
@State private var scrollToIndex : Int = 0
|
|
|
20 |
@State private var showProgressView : Bool = false
|
|
|
21 |
|
|
|
22 |
@State private var presentAlert : Bool = false
|
|
|
23 |
@State private var titleAlert : String = ""
|
|
|
24 |
@State private var messageAlert : String = ""
|
|
|
25 |
|
21 |
efrain |
26 |
private var appData = Environment(\.appData).wrappedValue
|
1 |
efrain |
27 |
private let config = [
|
|
|
28 |
GridItem(.flexible()),
|
|
|
29 |
GridItem(.flexible())
|
|
|
30 |
]
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
var body: some View {
|
|
|
35 |
ZStack {
|
|
|
36 |
|
|
|
37 |
|
|
|
38 |
if self.showProgressView {
|
|
|
39 |
ProgressView()
|
|
|
40 |
.progressViewStyle(CircularProgressViewStyle(tint: Color("color_progress_view_foreground")))
|
|
|
41 |
.scaleEffect(3, anchor: .center).zIndex(100000)
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
VStack(spacing: 0) {
|
|
|
45 |
HStack {
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
Image("logo")
|
|
|
49 |
.resizable()
|
|
|
50 |
.frame(width: 32, height: 32, alignment: .center)
|
|
|
51 |
.aspectRatio(contentMode: .fit)
|
|
|
52 |
.foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
|
|
|
53 |
.padding(.leading, 16)
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
Text(networkMonitor.status == .disconnected ? Config.LANG_ERROR_NETWORK_MESSAGE_SHORT : Config.LANG_TAB_BAR_BUTTON_TOPICS)
|
|
|
57 |
.font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
|
|
|
58 |
.foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
|
|
|
59 |
.padding(.leading, 4)
|
|
|
60 |
|
|
|
61 |
Spacer()
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
Button(action: {
|
|
|
65 |
if networkMonitor.status == .connected {
|
|
|
66 |
refresh()
|
|
|
67 |
} else {
|
|
|
68 |
self.titleAlert = Config.LANG_ERROR_NETWORK_TITLE
|
|
|
69 |
self.messageAlert = Config.LANG_ERROR_NETWORK_MESSAGE_LONG
|
|
|
70 |
self.presentAlert = true
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
}, label: {
|
|
|
75 |
Image(uiImage: UIImage(named: "ui_refresh") ?? UIImage())
|
|
|
76 |
.resizable()
|
|
|
77 |
.frame(width: 24, height:24, alignment: .center)
|
|
|
78 |
.aspectRatio(contentMode: .fit)
|
|
|
79 |
.foregroundColor(Color("color_app_bar_foreground"))
|
|
|
80 |
.padding(.trailing, 16)
|
|
|
81 |
})
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
}
|
|
|
85 |
.edgesIgnoringSafeArea(.top)
|
|
|
86 |
.frame(height: 50)
|
|
|
87 |
.background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
|
|
|
88 |
|
|
|
89 |
|
|
|
90 |
Divider().background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
|
|
|
95 |
ScrollView() {
|
|
|
96 |
ScrollViewReader { proxy in
|
|
|
97 |
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: {
|
|
|
98 |
ForEach(0..<self.viewModel.topics.count) { index in
|
|
|
99 |
CardTopicView(
|
|
|
100 |
topicUuid: self.viewModel.topics[index].uuid
|
|
|
101 |
)
|
|
|
102 |
.environmentObject(appNavigation)
|
|
|
103 |
.frame(
|
|
|
104 |
width: Constants.CARD_WIDTH,
|
|
|
105 |
height: Constants.CARD_HEIGHT,
|
|
|
106 |
alignment: .center
|
|
|
107 |
).id(index)
|
|
|
108 |
|
|
|
109 |
}.onChange(of: scrollToIndex, perform: { value in
|
|
|
110 |
proxy.scrollTo(value, anchor: nil)
|
|
|
111 |
})
|
|
|
112 |
})
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
.padding(.top, 5)
|
|
|
116 |
}
|
|
|
117 |
.onAppear {
|
|
|
118 |
|
|
|
119 |
|
|
|
120 |
|
|
|
121 |
if appData.refreshContentActionRequired {
|
|
|
122 |
appData.refreshContentActionRequired = false
|
|
|
123 |
appData.save()
|
|
|
124 |
|
|
|
125 |
refresh()
|
|
|
126 |
|
|
|
127 |
} else {
|
|
|
128 |
var i : Int = 0
|
|
|
129 |
while i < self.viewModel.topics.count {
|
|
|
130 |
if appData.topicUuidActive == self.viewModel.topics[i].uuid {
|
|
|
131 |
self.scrollToIndex = i
|
|
|
132 |
}
|
|
|
133 |
i += 1
|
|
|
134 |
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
|
|
|
139 |
} .alert(isPresented: $presentAlert) {
|
|
|
140 |
Alert(
|
|
|
141 |
title: Text(self.titleAlert),
|
|
|
142 |
message: Text(self.messageAlert),
|
|
|
143 |
dismissButton: .default(Text(Config.LANG_COMMON_OK))
|
|
|
144 |
)
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
private func refresh() {
|
|
|
150 |
self.showProgressView = true
|
|
|
151 |
|
|
|
152 |
|
|
|
153 |
let headerSecurity : HeaderSecurity = HeaderSecurity()
|
|
|
154 |
|
|
|
155 |
let headers: HTTPHeaders = [
|
|
|
156 |
.init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
|
|
|
157 |
.init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
|
|
|
158 |
.init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
|
|
|
159 |
.init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
|
|
|
160 |
.accept(Constants.HTTP_HEADER_ACCEPT)
|
|
|
161 |
]
|
|
|
162 |
|
|
|
163 |
print(Config.URL_REFRESH)
|
|
|
164 |
|
|
|
165 |
AF.request(Config.URL_REFRESH, method: .get, headers: headers).responseJSON{(response) in
|
|
|
166 |
self.showProgressView = false
|
|
|
167 |
switch response.result {
|
|
|
168 |
case .success:
|
|
|
169 |
|
|
|
170 |
|
|
|
171 |
let json = try? JSON(data: response.data!)
|
|
|
172 |
|
|
|
173 |
//print("json : \(json)")
|
|
|
174 |
|
|
|
175 |
|
|
|
176 |
if json?["success"] ?? "" != false {
|
|
|
177 |
let dataService = DataService()
|
|
|
178 |
|
|
|
179 |
dataService.syncFromServer(json : json, refresh: true)
|
|
|
180 |
|
|
|
181 |
self.viewModel.fetch(userUuid: appData.userUuid)
|
|
|
182 |
var i : Int = 0
|
|
|
183 |
while i < self.viewModel.topics.count {
|
|
|
184 |
if appData.topicUuidActive == self.viewModel.topics[i].uuid {
|
|
|
185 |
self.scrollToIndex = i
|
|
|
186 |
}
|
|
|
187 |
i += 1
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
let snackbar = TTGSnackbar(message: Config.LANG_TOPICS_REFRESH_CONTENT_DONE, duration: .long)
|
|
|
191 |
snackbar.show()
|
|
|
192 |
|
17 |
efrain |
193 |
let notificationDao = UserNotificationDao()
|
1 |
efrain |
194 |
notificationDao.markViewedAllPendingByUserUuidAndCommand(userUuid:appData.userUuid, command: Constants.NOTIFICATION_COMMAND_REFRESH_CONTENT)
|
|
|
195 |
|
|
|
196 |
|
|
|
197 |
} else {
|
|
|
198 |
let message = json?["data"].string ?? ""
|
|
|
199 |
if !message.isEmpty {
|
|
|
200 |
self.titleAlert = Config.LANG_ERROR_GENERIC_TITLE
|
|
|
201 |
self.messageAlert = message
|
|
|
202 |
self.presentAlert = true
|
|
|
203 |
}
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
return
|
|
|
207 |
|
|
|
208 |
case .failure :
|
|
|
209 |
self.titleAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
|
|
|
210 |
self.messageAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
|
|
|
211 |
self.presentAlert = true
|
|
|
212 |
|
|
|
213 |
return
|
|
|
214 |
}
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
struct GridTopicView_Previews: PreviewProvider {
|
|
|
220 |
static var previews: some View {
|
|
|
221 |
GridTopicView()
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
|