Rev 26 | Rev 61 | 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 Foundationimport SwiftUIclass NotificationListViewModel: ObservableObject{@Published public var notifications = [UserNotificationModel]()private let appData = AppData.sharedInstanceinit() {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_YMDlet sToday = dateFormatter.string(from: now)let sYesterday = dateFormatter.string(from: yesterday)let showFormatter = DateFormatter()showFormatter.dateFormat = Constants.FORMAT_DATE_DMYlet userNotificationDao = UserNotificationDao.sharedInstancenotifications.removeAll()notifications = userNotificationDao.selectAllByUserUuid(userUuid: appData.userUuid)var i : Int = 0while i < notifications.count {switch notifications[i].dateOn{case sToday :notifications[i].dateOn = Config.LANG_COMMON_TODAYbreakcase sYesterday :notifications[i].dateOn = Config.LANG_COMMON_YESTERDAYbreakdefault :let d = dateFormatter.date(from: notifications[i].dateOn) ?? Date()notifications[i].dateOn = showFormatter.string(from: d)break}i += 1}/*let dates = userNotificationDao.selectAllDistinctDateByUserUuid(userUuid: appData.userUuid)var i : Int = 0var d : Datevar sDate : Stringwhile i < dates.count {sDate = dates[i]userNotificationGroupModel = UserNotificationGroupModel()switch sDate{case sToday :userNotificationGroupModel.label = Config.LANG_COMMON_TODAYbreakcase sYesterday :userNotificationGroupModel.label = Config.LANG_COMMON_YESTERDAYbreakdefault :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.sharedInstanceuserNotificationDao.remove(id: id)/*var i : Int = 0while i < groups.count {groups[i].notifications.remove(at: groups[i].notifications.firstIndex(where: { $0.id == id })!)i += 1}groups.remove(at: groups.firstIndex(where: { $0.notifications.count == 0 })!)*/notifications.remove(at: notifications.firstIndex(where: { $0.id == id })!)}public func markForViewedById(id: Int) {let userNotificationDao = UserNotificationDao.sharedInstanceuserNotificationDao.markViewed(id: id)var i : Int = 0while i < notifications.count {if notifications[i].id == id {notifications[i].viewed = 1}i += 1}/*var i : Int = 0var j : Int = 0while 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 = 0while i < notifications.count {if notifications[i].command == command {notifications[i].viewed = 1}i += 1}/*var i : Int = 0var j : Int = 0while 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}*/}}