| 1 |
efrain |
1 |
//
|
|
|
2 |
// NotificationListViewModel.swift
|
|
|
3 |
// twogetskills
|
|
|
4 |
//
|
|
|
5 |
// Created by Efrain Yanez Recanatini on 7/31/22.
|
|
|
6 |
//
|
|
|
7 |
|
|
|
8 |
import Foundation
|
|
|
9 |
import SwiftUI
|
|
|
10 |
|
|
|
11 |
|
| 23 |
efrain |
12 |
struct UserNotificationGroupModel :Identifiable {
|
|
|
13 |
var id : String = UUID().uuidString
|
|
|
14 |
var label : String = ""
|
|
|
15 |
var notifications = [UserNotificationModel]()
|
|
|
16 |
}
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
| 1 |
efrain |
20 |
class NotificationListViewModel: ObservableObject
|
|
|
21 |
{
|
| 23 |
efrain |
22 |
@Published public var groups = [UserNotificationGroupModel]()
|
|
|
23 |
private let appData = AppData.sharedInstance
|
| 1 |
efrain |
24 |
|
| 23 |
efrain |
25 |
|
| 1 |
efrain |
26 |
init() {
|
|
|
27 |
|
|
|
28 |
fetchAll()
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
func fetchAll()
|
|
|
32 |
{
|
| 23 |
efrain |
33 |
let now = Date()
|
|
|
34 |
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: now)!
|
|
|
35 |
|
|
|
36 |
|
|
|
37 |
let dateFormatter = DateFormatter()
|
|
|
38 |
dateFormatter.dateFormat = Constants.FORMAT_DATE_YMD
|
|
|
39 |
|
|
|
40 |
let sNow = dateFormatter.string(from: now)
|
|
|
41 |
let sYesterday = dateFormatter.string(from: yesterday)
|
|
|
42 |
|
|
|
43 |
let showFormatter = DateFormatter()
|
|
|
44 |
showFormatter.dateFormat = Constants.FORMAT_DATE_DMY
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
|
49 |
groups.removeAll()
|
|
|
50 |
|
|
|
51 |
var userNotificationGroupModel : UserNotificationGroupModel
|
| 1 |
efrain |
52 |
let userNotificationDao = UserNotificationDao.sharedInstance
|
| 23 |
efrain |
53 |
let dates = userNotificationDao.selectAllDistinctDateByUserUuid(userUuid: appData.userUuid)
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
var i : Int = 0
|
|
|
58 |
var d : Date
|
|
|
59 |
var sDate : String
|
|
|
60 |
while i < dates.count {
|
|
|
61 |
sDate = dates[i]
|
|
|
62 |
d = dateFormatter.date(from: sDate) ?? Date()
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
userNotificationGroupModel = UserNotificationGroupModel()
|
|
|
67 |
userNotificationGroupModel.label = showFormatter.string(from: d)
|
|
|
68 |
userNotificationGroupModel.notifications = userNotificationDao.selectAllByUserUuidAndDate(userUuid: appData.userUuid, date: sDate)
|
|
|
69 |
|
|
|
70 |
groups.append(userNotificationGroupModel)
|
|
|
71 |
|
|
|
72 |
i += 1
|
|
|
73 |
|
|
|
74 |
}
|
| 1 |
efrain |
75 |
}
|
|
|
76 |
|
| 22 |
efrain |
77 |
public func removeItem(id: Int) {
|
|
|
78 |
let userNotificationDao = UserNotificationDao.sharedInstance
|
|
|
79 |
userNotificationDao.remove(id: id)
|
| 23 |
efrain |
80 |
|
|
|
81 |
var i : Int = 0
|
|
|
82 |
while i < groups.count {
|
|
|
83 |
groups[i].notifications.remove(at: groups[i].notifications.firstIndex(where: { $0.id == id })!)
|
|
|
84 |
|
|
|
85 |
i += 1
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
|
| 22 |
efrain |
89 |
}
|
|
|
90 |
|
| 1 |
efrain |
91 |
|
|
|
92 |
}
|