| 1 |
efrain |
1 |
//
|
|
|
2 |
// NotificationListView.swift
|
|
|
3 |
// twogetskills
|
|
|
4 |
//
|
|
|
5 |
// Created by Efrain Yanez Recanatini on 7/31/22.
|
|
|
6 |
//
|
|
|
7 |
|
|
|
8 |
import SwiftUI
|
|
|
9 |
|
|
|
10 |
struct NotificationListView: View {
|
| 25 |
efrain |
11 |
@Environment(\.openURL) var openURL
|
| 8 |
efrain |
12 |
@EnvironmentObject private var networkMonitor : NetworkMonitor
|
|
|
13 |
@EnvironmentObject private var appNavigation : AppNavigation
|
| 1 |
efrain |
14 |
|
|
|
15 |
@ObservedObject var viewModel :NotificationListViewModel = NotificationListViewModel()
|
| 25 |
efrain |
16 |
private let appData = AppData.sharedInstance
|
| 1 |
efrain |
17 |
|
| 25 |
efrain |
18 |
|
| 1 |
efrain |
19 |
var body: some View {
|
|
|
20 |
VStack(spacing: 0) {
|
|
|
21 |
HStack {
|
|
|
22 |
Image("logo")
|
|
|
23 |
.resizable()
|
|
|
24 |
.frame(width: 32, height: 32, alignment: .center)
|
|
|
25 |
.aspectRatio(contentMode: .fit)
|
| 8 |
efrain |
26 |
.foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
|
| 1 |
efrain |
27 |
.padding(.leading, 16)
|
|
|
28 |
|
| 8 |
efrain |
29 |
Text(networkMonitor.status == .disconnected ? Config.LANG_ERROR_NETWORK_MESSAGE_SHORT : Config.LANG_TAB_BAR_BUTTON_NOTIFICATIONS)
|
| 1 |
efrain |
30 |
.font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
|
| 8 |
efrain |
31 |
.foregroundColor(networkMonitor.status == .disconnected ? Color("color_network_disconnected_foreground") : Color("color_app_bar_foreground"))
|
| 1 |
efrain |
32 |
.padding(.leading, 4)
|
|
|
33 |
|
|
|
34 |
Spacer()
|
|
|
35 |
|
|
|
36 |
|
|
|
37 |
}
|
|
|
38 |
.edgesIgnoringSafeArea(.top)
|
|
|
39 |
.frame(height: 50)
|
| 8 |
efrain |
40 |
.background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
|
|
|
41 |
|
|
|
42 |
Divider().background(networkMonitor.status == .disconnected ? Color("color_network_disconnected_background") : Color("color_app_bar_background"))
|
| 1 |
efrain |
43 |
|
|
|
44 |
ScrollView {
|
| 22 |
efrain |
45 |
|
| 31 |
efrain |
46 |
if self.viewModel.notifications.count == 0 {
|
| 22 |
efrain |
47 |
NotificationListItemEmptyView()
|
| 1 |
efrain |
48 |
|
| 22 |
efrain |
49 |
} else {
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
LazyVStack {
|
| 1 |
efrain |
54 |
|
| 23 |
efrain |
55 |
|
| 31 |
efrain |
56 |
|
| 24 |
efrain |
57 |
|
| 31 |
efrain |
58 |
ForEach(0..<self.viewModel.notifications.count) { index in
|
| 23 |
efrain |
59 |
|
| 31 |
efrain |
60 |
let notificationItem = self.viewModel.notifications[index]
|
| 24 |
efrain |
61 |
|
| 25 |
efrain |
62 |
NotificationListItemView(notification: notificationItem,onExecute: {
|
|
|
63 |
|
|
|
64 |
if notificationItem.command == Constants.NOTIFICATION_COMMAND_REFRESH_CONTENT && notificationItem.viewed == 0 {
|
|
|
65 |
|
|
|
66 |
self.viewModel.markAllForViewedByType(command: Constants.NOTIFICATION_COMMAND_REFRESH_CONTENT)
|
|
|
67 |
|
|
|
68 |
appData.refreshContentActionRequired = true
|
|
|
69 |
appData.save()
|
|
|
70 |
|
|
|
71 |
withAnimation {
|
|
|
72 |
appNavigation.subpageActive = .topics
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
|
|
|
76 |
} else if notificationItem.command == Constants.NOTIFICATION_COMMAND_OPEN_URL &&
|
|
|
77 |
!notificationItem.url.isEmpty &&
|
|
|
78 |
notificationItem.viewed == 0 {
|
|
|
79 |
|
|
|
80 |
openURL(URL(string: notificationItem.url)!)
|
|
|
81 |
self.viewModel.markForViewedById(id: notificationItem.id)
|
| 31 |
efrain |
82 |
//self.viewModel.fetchAll()
|
| 25 |
efrain |
83 |
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
}) {
|
| 22 |
efrain |
87 |
self.viewModel.removeItem(id: notificationItem.id)
|
| 31 |
efrain |
88 |
//self.viewModel.fetchAll()
|
| 23 |
efrain |
89 |
}
|
| 25 |
efrain |
90 |
|
| 22 |
efrain |
91 |
}
|
| 31 |
efrain |
92 |
|
| 1 |
efrain |
93 |
}
|
|
|
94 |
}
|
| 8 |
efrain |
95 |
}.padding(.top, 5)
|
| 26 |
efrain |
96 |
}.onAppear {
|
|
|
97 |
viewModel.fetchAll()
|
| 1 |
efrain |
98 |
}
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
struct NotificationListView_Previews: PreviewProvider {
|
|
|
103 |
static var previews: some View {
|
|
|
104 |
NotificationListView()
|
|
|
105 |
}
|
|
|
106 |
}
|