ChatTabBarItemViewModel.swift 2.26 KB
Newer Older
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 *  Copyright (C) 2017 Savoir-faire Linux Inc.
 *
 *  Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

import Foundation
import RxSwift

class ChatTabBarItemViewModel: ViewModel, TabBarItemViewModel {

    var itemBadgeValue: Observable<String?>

    required init(with injectionBag: InjectionBag) {
        let accountService = injectionBag.accountService
        let conversationService = injectionBag.conversationsService
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
31
        let contactsService = injectionBag.contactsService
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
32
33
34
35
        self.itemBadgeValue = {
            return conversationService.conversations.map({ conversations in
                return conversations.map({ conversation in
                    return conversation.messages.filter({ message in
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
36
37
38
39
40
41
42
                        if let account = accountService.currentAccount {
                            let accountHelper = AccountModelHelper(withAccount: account)
                            //filtre out read messages, outgoing messages and messages that are displayed in contactrequest conversation
                            return message.status != .read && message.author != accountHelper.ringId
                            && (contactsService.contactRequest(withRingId: message.author) == nil)
                        }
                        return false
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
43
44
45
46
47
48
49
50
51
52
53
54
                    }).count
                }).reduce(0, +)
            })
            }()
            .map { number in
                     if number == 0 {
                         return nil
                     }
                return "\(number)"
            }
    }
}