ConversationCell.swift 4.32 KB
Newer Older
1
/*
Sébastien Blin's avatar
Sébastien Blin committed
2
 *  Copyright (C) 2017-2019 Savoir-faire Linux Inc.
3 4
 *
 *  Author: Silbino Gonçalves Matado <silbino.gmatado@savoirfairelinux.com>
5
 *  Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Quentin Muret's avatar
Quentin Muret committed
6
 *  Author: Quentin Muret <quentin.muret@savoirfairelinux.com>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 *  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 UIKit
import RxSwift
25
import Reusable
26

27
class ConversationCell: UITableViewCell, NibReusable {
28

29
    @IBOutlet weak var avatarView: UIView!
30 31 32 33 34
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var newMessagesIndicator: UIView!
    @IBOutlet weak var newMessagesLabel: UILabel!
    @IBOutlet weak var lastMessageDateLabel: UILabel!
    @IBOutlet weak var lastMessagePreviewLabel: UILabel!
35
    @IBOutlet weak var presenceIndicator: UIView!
36 37

    override func setSelected(_ selected: Bool, animated: Bool) {
Quentin Muret's avatar
Quentin Muret committed
38
        self.backgroundColor = UIColor.jamiUITableViewCellSelection
39
        UIView.animate(withDuration: 0.35, animations: {
Quentin Muret's avatar
Quentin Muret committed
40
            self.backgroundColor = UIColor.jamiUITableViewCellSelection.lighten(byPercentage: 5.0)
41
        })
42 43 44
    }

    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
45
        if highlighted {
Quentin Muret's avatar
Quentin Muret committed
46
            self.backgroundColor = UIColor.jamiUITableViewCellSelection
47 48 49
        } else {
            self.backgroundColor = UIColor.clear
        }
50
    }
51 52 53 54 55 56

    var disposeBag = DisposeBag()

    override func prepareForReuse() {
        self.disposeBag = DisposeBag()
    }
57 58

    func configureFromItem(_ item: ConversationSection.Item) {
59 60
        // avatar
        Observable<(Data?, String)>.combineLatest(item.profileImageData.asObservable(),
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
61 62 63 64 65 66
                                                  item.userName.asObservable(),
                                                  item.displayName.asObservable()) { profileImage, username, displayName in
                                                    if let displayName = displayName, !displayName.isEmpty {
                                                        return (profileImage, displayName)
                                                    }
                                                    return (profileImage, username)
67 68
            }
            .observeOn(MainScheduler.instance)
69 70 71 72 73 74 75
            .startWith((item.profileImageData.value, item.userName.value))
            .subscribe({ [weak self] profileData -> Void in
                self?.avatarView.subviews.forEach({ $0.removeFromSuperview() })
                self?.avatarView.addSubview(AvatarView(profileImageData: profileData.element?.0,
                                                       username: (profileData.element?.1)!,
                                                       size: 40))
                return
76 77 78
            })
            .disposed(by: self.disposeBag)

79 80 81 82 83 84
        // unread messages
        self.newMessagesLabel.text = item.unreadMessages
        self.newMessagesIndicator.isHidden = item.hideNewMessagesLabel

        // presence
        item.contactPresence.asObservable()
85
            .observeOn(MainScheduler.instance)
86 87
            .map { value in !value }
            .bind(to: self.presenceIndicator.rx.isHidden)
88 89
            .disposed(by: self.disposeBag)

90
        // username
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
91
        item.bestName.asObservable()
92
            .observeOn(MainScheduler.instance)
93
            .bind(to: self.nameLabel.rx.text)
94
            .disposed(by: self.disposeBag)
95
        self.nameLabel.lineBreakMode = .byTruncatingTail
96

97
        // last message date
98
        self.lastMessageDateLabel.text = item.lastMessageReceivedDate
99 100

        // last message preview
101
        self.lastMessagePreviewLabel.text = item.lastMessage
102
        self.lastMessagePreviewLabel.lineBreakMode = .byTruncatingTail
103

104
        self.selectionStyle = .none
105
    }
106
}