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