Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 24 | Rev 26 | 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
 
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
 
24 efrain 40
        let sToday = dateFormatter.string(from: now)
23 efrain 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]
24 efrain 62
            userNotificationGroupModel = UserNotificationGroupModel()
23 efrain 63
 
24 efrain 64
            switch sDate
65
            {
66
                case sToday :
67
                    userNotificationGroupModel.label = Config.LANG_COMMON_TODAY
68
                    break
69
 
70
                case sYesterday :
71
                    userNotificationGroupModel.label = Config.LANG_COMMON_YESTERDAY
72
                    break
73
 
74
                default :
75
                    d = dateFormatter.date(from: sDate) ?? Date()
76
                    userNotificationGroupModel.label = showFormatter.string(from: d)
77
                    break
78
 
79
            }
80
 
23 efrain 81
 
24 efrain 82
 
23 efrain 83
            userNotificationGroupModel.notifications = userNotificationDao.selectAllByUserUuidAndDate(userUuid: appData.userUuid, date: sDate)
84
 
85
            groups.append(userNotificationGroupModel)
86
 
87
            i += 1
88
 
89
        }
1 efrain 90
    }
91
 
22 efrain 92
    public func removeItem(id: Int) {
93
        let userNotificationDao = UserNotificationDao.sharedInstance
94
        userNotificationDao.remove(id: id)
23 efrain 95
 
96
        var i : Int = 0
97
        while i < groups.count {
98
            groups[i].notifications.remove(at:  groups[i].notifications.firstIndex(where: {  $0.id == id })!)
99
 
100
            i += 1
101
        }
102
 
24 efrain 103
        i = groups.count - 1
104
        while i > 0 {
105
            if groups[i].notifications.count == 0 {
106
                groups.remove(at: i)
107
            }
108
 
109
            i -= 1
110
        }
111
 
23 efrain 112
 
22 efrain 113
    }
114
 
25 efrain 115
    public func markForViewedById(id: Int) {
116
        let userNotificationDao = UserNotificationDao.sharedInstance
117
        userNotificationDao.markViewed(id: id)
118
 
119
        var i : Int = 0
120
        var j : Int = 0
121
        while i < groups.count {
122
            while j < groups[i].notifications.count {
123
 
124
                if groups[i].notifications[j].id == id {
125
                    groups[i].notifications[j].viewed = 1
126
                }
127
 
128
                j += 1
129
            }
130
 
131
            i += 1
132
        }
133
    }
134
 
135
    public func markAllForViewedByType(command : String) {
136
        let notificationDao = UserNotificationDao()
137
        notificationDao.markViewedAllPendingByUserUuidAndCommand(userUuid: appData.userUuid, command: command)
138
 
139
        var i : Int = 0
140
        var j : Int = 0
141
        while i < groups.count {
142
            while j < groups[i].notifications.count {
143
 
144
                if groups[i].notifications[j].command == command && groups[i].notifications[j].viewed == 0 {
145
                    groups[i].notifications[j].viewed = 1
146
                }
147
 
148
                j += 1
149
            }
150
 
151
            i += 1
152
        }
153
    }
154
 
1 efrain 155
 
156
}