ContactRequestItem.swift 2.51 KB
Newer Older
1
/*
2
 *  Copyright (C) 2017-2019 Savoir-faire Linux Inc.
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
 *
 *  Author: Silbino Gonçalves Matado <silbino.gmatado@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 RxSwift
import Contacts
import SwiftyBeaver

class ContactRequestItem {

    let contactRequest: ContactRequestModel

    let userName = Variable("")
30
    let profileName = Variable("")
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
31
    let profileImageData = Variable<Data?>(nil)
32 33 34 35 36 37 38 39 40 41 42
    lazy var bestName: Observable<String> = {
        return Observable
            .combineLatest(userName.asObservable(),
                           profileName.asObservable()) {(userName, displayname) in
                            if displayname.isEmpty {
                                return userName
                            }
                            return displayname
        }
    }()

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
43
    let disposeBag = DisposeBag()
44

45
    init(withContactRequest contactRequest: ContactRequestModel, profileService: ProfilesService,
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
46
         contactService: ContactsService) {
47
        self.contactRequest = contactRequest
48
        self.userName.value = contactRequest.ringId
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
49
        self.profileImageData.value = self.contactRequest.vCard?.imageData
50
        self.profileName.value = VCardUtils.getName(from: self.contactRequest.vCard)
51
        profileService.getProfile(ringId: contactRequest.ringId, createIfNotexists: false)
52
            .subscribe(onNext: { [weak self] profile in
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
53
                if let photo = profile.photo,
54 55
                    let data = NSData(base64Encoded: photo,
                                      options: NSData.Base64DecodingOptions.ignoreUnknownCharacters) as Data? {
56
                    self?.profileImageData.value = data
57 58 59
                }
                if let name = profile.alias, !name.isEmpty {
                    self?.profileName.value = name
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
60
                }
61
            }).disposed(by: self.disposeBag)
62 63
    }
}