Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

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

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

import Foundation
import SwiftUI


struct UserNotificationGroupModel :Identifiable {
    var id : String = UUID().uuidString
    var label : String =  ""
    var notifications = [UserNotificationModel]()
}



class NotificationListViewModel: ObservableObject
{
    @Published public var groups = [UserNotificationGroupModel]()
    private let appData = AppData.sharedInstance
    
    
    init() {

        fetchAll()
    }

    func fetchAll()
    {
        let now = Date()
        let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: now)!
        
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = Constants.FORMAT_DATE_YMD
        
        let sToday = dateFormatter.string(from: now)
        let sYesterday = dateFormatter.string(from: yesterday)
        
        let showFormatter = DateFormatter()
        showFormatter.dateFormat = Constants.FORMAT_DATE_DMY
        
        
        
        
        groups.removeAll()
        
        var userNotificationGroupModel : UserNotificationGroupModel
        let userNotificationDao = UserNotificationDao.sharedInstance
        let dates = userNotificationDao.selectAllDistinctDateByUserUuid(userUuid: appData.userUuid)
        
        

        var i : Int = 0
        var d : Date
        var sDate : String
        while i < dates.count {
            sDate = dates[i]
            userNotificationGroupModel = UserNotificationGroupModel()
            
            switch sDate
            {
                case sToday :
                    userNotificationGroupModel.label = Config.LANG_COMMON_TODAY
                    break
                
                case sYesterday :
                    userNotificationGroupModel.label = Config.LANG_COMMON_YESTERDAY
                    break
                
                default :
                    d = dateFormatter.date(from: sDate) ?? Date()
                    userNotificationGroupModel.label = showFormatter.string(from: d)
                    break
                
            }
           
            
           
            userNotificationGroupModel.notifications = userNotificationDao.selectAllByUserUuidAndDate(userUuid: appData.userUuid, date: sDate)
            
            groups.append(userNotificationGroupModel)
            
            i += 1
            
        }
    }
    
    public func removeItem(id: Int) {
        let userNotificationDao = UserNotificationDao.sharedInstance
        userNotificationDao.remove(id: id)
        
        var i : Int = 0
        while i < groups.count {
            groups[i].notifications.remove(at:  groups[i].notifications.firstIndex(where: {  $0.id == id })!)
        
            i += 1
        }
        
        i = groups.count - 1
        while i > 0 {
            if groups[i].notifications.count == 0 {
                groups.remove(at: i)
            }
        
            i -= 1
        }
        
       
    }
    
    public func markForViewedById(id: Int) {
        let userNotificationDao = UserNotificationDao.sharedInstance
        userNotificationDao.markViewed(id: id)
        
        var i : Int = 0
        var j : Int = 0
        while i < groups.count {
            while j < groups[i].notifications.count {
                
                if groups[i].notifications[j].id == id {
                    groups[i].notifications[j].viewed = 1
                }
                
                j += 1
            }
        
            i += 1
        }
    }
    
    public func markAllForViewedByType(command : String) {
        let notificationDao = UserNotificationDao()
        notificationDao.markViewedAllPendingByUserUuidAndCommand(userUuid: appData.userUuid, command: command)
        
        var i : Int = 0
        var j : Int = 0
        while i < groups.count {
            while j < groups[i].notifications.count {
                
                if groups[i].notifications[j].command == command && groups[i].notifications[j].viewed == 0 {
                    groups[i].notifications[j].viewed = 1
                }
                
                j += 1
            }
        
            i += 1
        }
    }
    
   
}