Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 22 | Rev 24 | 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 sNow = 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]
            d = dateFormatter.date(from: sDate) ?? Date()
            
            
            
            userNotificationGroupModel = UserNotificationGroupModel()
            userNotificationGroupModel.label = showFormatter.string(from: d)
            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
        }
        
       
    }
    
   
}