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