Proyectos de Subversion Iphone Microlearning - Inconcert

Rev

Rev 11 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

//
//  NotificationListView.swift
//  twogetskills
//
//  Created by Efrain Yanez Recanatini on 7/31/22.
//

import SwiftUI
import SafariServices

struct NotificationListView: View {
    @EnvironmentObject private var networkMonitor : NetworkMonitor
    @EnvironmentObject private var appNavigation : AppNavigation
    
    @ObservedObject var viewModel :NotificationListViewModel
    
    @State private var sheetURL: String = ""
    @State private var sheetShow = false
    
    private let appData = AppData.sharedInstance
    
    init() {

        let userNotificationDao = UserNotificationDao()
        
        userNotificationDao.removeExpired(userUuid:appData.userUuid)
        
        viewModel = NotificationListViewModel()
    }
    
    var body: some View {
        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_NOTIFICATIONS)
                .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()

                
            }
            .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 {
                
                if self.viewModel.notifications.count == 0  {
                    NotificationListItemEmptyView()
                    
                } else {
                
                
                
                    LazyVStack  {
                        

                        
                            
                        ForEach(self.viewModel.notifications) { notificationItem  in
                            
                            //let notificationItem = self.viewModel.notifications[index]
                            
                            NotificationListItemView(notification: notificationItem).onTapGesture {
                                if !notificationItem.command.isEmpty && notificationItem.viewed == 0 {
                                    if notificationItem.command == Constants.NOTIFICATION_COMMAND_REFRESH_CONTENT && notificationItem.viewed == 0 {
                                        
                                        let notificationDao = UserNotificationDao()
                                        notificationDao.markViewedAllPendingByUserUuidAndCommand(userUuid: appData.userUuid, command: Constants.NOTIFICATION_COMMAND_REFRESH_CONTENT)
                                        
                                        appData.refreshContentActionRequired = true
                                        appData.save()
                                        
                                        withAnimation {
                                            appNavigation.subpageActive = .topics
                                        }
                                        
                                        
                                    } else if notificationItem.command == Constants.NOTIFICATION_COMMAND_OPEN_URL &&
                                                !notificationItem.url.isEmpty  &&
                                                notificationItem.viewed == 0  {
                                        
                                        
                                        let notificationDao = UserNotificationDao()
                                        notificationDao.markViewed(id: notificationItem.id)
                                        
                                        self.viewModel.fetchAll()
                                        
                                        self.sheetURL =  notificationItem.url
                                        self.sheetShow = true
                                        
                                    }
                                } else {
                                    DispatchQueue.main.async {
                                        let notificationDao = UserNotificationDao()
                                        notificationDao.remove(id: notificationItem.id)
                                        
                                        self.viewModel.fetchAll()
                                    }
                                }
                                   
                            }
                        }
 
                        
                    }
                }
            }.padding(.top, 5)
        }.onAppear {
            viewModel.fetchAll()
        }.popover(isPresented: self.$sheetShow, content: {
            
           
            
            SafariView(sURL: self.sheetURL)

            
        })    }
}

struct NotificationListView_Previews: PreviewProvider {
    static var previews: some View {
        NotificationListView()
    }
}