Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

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

Rev Autor Línea Nro. Línea
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
class NotificationListViewModel: ObservableObject
12
{
31 efrain 13
    @Published public var notifications = [UserNotificationModel]()
23 efrain 14
    private let appData = AppData.sharedInstance
1 efrain 15
 
23 efrain 16
 
1 efrain 17
    init() {
18
 
19
        fetchAll()
20
    }
21
 
22
    func fetchAll()
23
    {
23 efrain 24
        let now = Date()
25
        let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: now)!
26
 
27
 
28
        let dateFormatter = DateFormatter()
29
        dateFormatter.dateFormat = Constants.FORMAT_DATE_YMD
30
 
24 efrain 31
        let sToday = dateFormatter.string(from: now)
23 efrain 32
        let sYesterday = dateFormatter.string(from: yesterday)
33
 
34
        let showFormatter = DateFormatter()
35
        showFormatter.dateFormat = Constants.FORMAT_DATE_DMY
36
 
37
 
31 efrain 38
        let userNotificationDao = UserNotificationDao.sharedInstance
23 efrain 39
 
31 efrain 40
        notifications.removeAll()
41
        notifications = userNotificationDao.selectAllByUserUuid(userUuid: appData.userUuid)
42
 
43
        var i : Int = 0
44
        while i  < notifications.count {
45
 
46
 
47
            switch notifications[i].dateOn
48
            {
49
                case sToday :
50
                    notifications[i].dateOn = Config.LANG_COMMON_TODAY
51
                    break
52
 
53
                case sYesterday :
54
                    notifications[i].dateOn = Config.LANG_COMMON_YESTERDAY
55
                    break
56
 
57
                default :
58
                    let d = dateFormatter.date(from: notifications[i].dateOn) ?? Date()
59
                    notifications[i].dateOn = showFormatter.string(from: d)
60
                    break
61
 
62
            }
63
 
64
            i += 1
65
        }
23 efrain 66
 
31 efrain 67
        /*
23 efrain 68
        let dates = userNotificationDao.selectAllDistinctDateByUserUuid(userUuid: appData.userUuid)
69
 
70
 
71
 
72
        var i : Int = 0
73
        var d : Date
74
        var sDate : String
75
        while i < dates.count {
76
            sDate = dates[i]
24 efrain 77
            userNotificationGroupModel = UserNotificationGroupModel()
23 efrain 78
 
24 efrain 79
            switch sDate
80
            {
81
                case sToday :
82
                    userNotificationGroupModel.label = Config.LANG_COMMON_TODAY
83
                    break
84
 
85
                case sYesterday :
86
                    userNotificationGroupModel.label = Config.LANG_COMMON_YESTERDAY
87
                    break
88
 
89
                default :
90
                    d = dateFormatter.date(from: sDate) ?? Date()
91
                    userNotificationGroupModel.label = showFormatter.string(from: d)
92
                    break
93
 
94
            }
95
 
23 efrain 96
 
24 efrain 97
 
23 efrain 98
            userNotificationGroupModel.notifications = userNotificationDao.selectAllByUserUuidAndDate(userUuid: appData.userUuid, date: sDate)
99
 
100
            groups.append(userNotificationGroupModel)
101
 
102
            i += 1
103
 
104
        }
31 efrain 105
         */
1 efrain 106
    }
107
 
26 efrain 108
 
109
 
22 efrain 110
    public func removeItem(id: Int) {
111
        let userNotificationDao = UserNotificationDao.sharedInstance
112
        userNotificationDao.remove(id: id)
23 efrain 113
 
31 efrain 114
        /*
23 efrain 115
        var i : Int = 0
116
        while i < groups.count {
117
            groups[i].notifications.remove(at:  groups[i].notifications.firstIndex(where: {  $0.id == id })!)
118
 
119
            i += 1
120
        }
121
 
31 efrain 122
        groups.remove(at:  groups.firstIndex(where: {  $0.notifications.count == 0 })!)
123
         */
24 efrain 124
 
31 efrain 125
        notifications.remove(at: notifications.firstIndex(where: {  $0.id == id })!)
126
 
23 efrain 127
 
22 efrain 128
    }
129
 
25 efrain 130
    public func markForViewedById(id: Int) {
131
        let userNotificationDao = UserNotificationDao.sharedInstance
132
        userNotificationDao.markViewed(id: id)
133
 
31 efrain 134
 
25 efrain 135
        var i : Int = 0
31 efrain 136
        while i < notifications.count  {
137
 
138
            if notifications[i].id == id {
139
                notifications[i].viewed = 1
140
            }
141
            i += 1
142
        }
143
        /*
144
        var i : Int = 0
25 efrain 145
        var j : Int = 0
146
        while i < groups.count {
147
            while j < groups[i].notifications.count {
148
 
149
                if groups[i].notifications[j].id == id {
150
                    groups[i].notifications[j].viewed = 1
151
                }
152
 
153
                j += 1
154
            }
155
 
156
            i += 1
31 efrain 157
        }*/
25 efrain 158
    }
159
 
160
    public func markAllForViewedByType(command : String) {
161
        let notificationDao = UserNotificationDao()
162
        notificationDao.markViewedAllPendingByUserUuidAndCommand(userUuid: appData.userUuid, command: command)
163
 
164
        var i : Int = 0
31 efrain 165
        while i < notifications.count  {
166
 
167
            if notifications[i].command == command  {
168
                notifications[i].viewed = 1
169
            }
170
            i += 1
171
        }
172
 
173
        /*
174
        var i : Int = 0
25 efrain 175
        var j : Int = 0
176
        while i < groups.count {
177
            while j < groups[i].notifications.count {
178
 
179
                if groups[i].notifications[j].command == command && groups[i].notifications[j].viewed == 0 {
180
                    groups[i].notifications[j].viewed = 1
181
                }
182
 
183
                j += 1
184
            }
185
 
186
            i += 1
31 efrain 187
        }*/
25 efrain 188
    }
189
 
1 efrain 190
 
191
}