ConversationCell.swift 3.92 KB
Newer Older
1 2 3 4
/*
 *  Copyright (C) 2017 Savoir-faire Linux Inc.
 *
 *  Author: Silbino Gonçalves Matado <silbino.gmatado@savoirfairelinux.com>
5
 *  Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 *  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
24
import Reusable
25

26
class ConversationCell: UITableViewCell, NibReusable {
27

28
    @IBOutlet weak var avatarView: UIView!
29 30 31 32 33
    @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!
34
    @IBOutlet weak var presenceIndicator: UIView!
35 36

    override func setSelected(_ selected: Bool, animated: Bool) {
37 38 39 40
        self.backgroundColor = UIColor.ringUITableViewCellSelection
        UIView.animate(withDuration: 0.35, animations: {
            self.backgroundColor = UIColor.ringUITableViewCellSelection.lighten(byPercentage: 5.0)
        })
41 42 43
    }

    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
44 45 46 47 48
        if highlighted {
            self.backgroundColor = UIColor.ringUITableViewCellSelection
        } else {
            self.backgroundColor = UIColor.clear
        }
49
    }
50 51 52 53 54 55

    var disposeBag = DisposeBag()

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

    func configureFromItem(_ item: ConversationSection.Item) {
58 59 60 61
        // avatar
        Observable<(Data?, String)>.combineLatest(item.profileImageData.asObservable(),
                                                  item.userName.asObservable()) { profileImage, username in
                                                            return (profileImage, username)
62 63
            }
            .observeOn(MainScheduler.instance)
64 65 66 67 68 69 70
            .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
71 72 73
            })
            .disposed(by: self.disposeBag)

74 75 76 77 78 79
        // unread messages
        self.newMessagesLabel.text = item.unreadMessages
        self.newMessagesIndicator.isHidden = item.hideNewMessagesLabel

        // presence
        item.contactPresence.asObservable()
80
            .observeOn(MainScheduler.instance)
81 82
            .map { value in !value }
            .bind(to: self.presenceIndicator.rx.isHidden)
83 84
            .disposed(by: self.disposeBag)

85
        // username
86 87
        item.userName.asObservable()
            .observeOn(MainScheduler.instance)
88
            .bind(to: self.nameLabel.rx.text)
89
            .disposed(by: self.disposeBag)
90
        self.nameLabel.lineBreakMode = .byTruncatingTail
91

92
        // last message date
93
        self.lastMessageDateLabel.text = item.lastMessageReceivedDate
94 95

        // last message preview
96
        self.lastMessagePreviewLabel.text = item.lastMessage
97
        self.lastMessagePreviewLabel.lineBreakMode = .byTruncatingTail
98

99
        self.selectionStyle = .none
100
    }
101
}