Commit 9fd7d04b authored by Kateryna Kostiuk's avatar Kateryna Kostiuk Committed by Andreas Traczyk

contact request: send invitation

Add send request button in conversation controller

Change-Id: I17deaaa24a94a6d18f9ddd3169805e65ee2b7ebe
Reviewed-by: Andreas Traczyk's avatarAndreas Traczyk <andreas.traczyk@savoirfairelinux.com>
parent cd1a85f5
......@@ -45,6 +45,7 @@ struct ColorAsset {
// swiftlint:disable identifier_name line_length nesting type_body_length type_name
enum Asset {
static let addPerson = ImageAsset(name: "add_person")
static let backgroundRing = ImageAsset(name: "background_ring")
static let icContactPicture = ImageAsset(name: "ic_contact_picture")
static let logoRingBeta2Blanc = ImageAsset(name: "logo-ring-beta2-blanc")
......@@ -53,6 +54,7 @@ enum Asset {
static let allColors: [ColorAsset] = [
]
static let allImages: [ImageAsset] = [
addPerson,
backgroundRing,
icContactPicture,
logoRingBeta2Blanc,
......
......@@ -77,6 +77,21 @@ class ConversationViewController: UIViewController, UITextFieldDelegate, Storybo
self.tableView.contentInset.bottom = messageAccessoryView.frame.size.height
self.tableView.scrollIndicatorInsets.bottom = messageAccessoryView.frame.size.height
//invite button
let inviteItem = UIBarButtonItem()
inviteItem.image = UIImage(named: "add_person")
inviteItem.rx.tap.subscribe(onNext: { [unowned self] in
self.inviteItemTapped()
}).disposed(by: self.disposeBag)
self.navigationItem.rightBarButtonItem = inviteItem
self.viewModel.inviteButtonIsAvailable.asObservable().bind(to: inviteItem.rx.isEnabled).disposed(by: disposeBag)
}
func inviteItemTapped() {
self.viewModel?.sendContactRequest()
}
override func viewDidAppear(_ animated: Bool) {
......
......@@ -50,6 +50,13 @@ class ConversationViewModel: ViewModel {
let contact = self.contactsService.contact(withRingId: self.conversation.recipientRingId)
if let contact = contact {
self.inviteButtonIsAvailable.onNext(!contact.confirmed)
}
self.contactsService.contactStatus.subscribe(onNext: { contact in
self.inviteButtonIsAvailable.onNext(!contact.confirmed)
}).disposed(by: self.disposeBag)
if let contactUserName = contact?.userName {
self.userName.onNext(contactUserName)
} else {
......@@ -94,6 +101,7 @@ class ConversationViewModel: ViewModel {
var messages: Observable<[MessageViewModel]>!
var userName = BehaviorSubject(value: "")
var inviteButtonIsAvailable = BehaviorSubject(value: true)
//Services
private let conversationsService: ConversationsService
......@@ -202,4 +210,12 @@ class ConversationViewModel: ViewModel {
return message.status != .read && message.author != accountHelper.ringId!
}).count
}
func sendContactRequest() {
self.contactsService.sendContactRequest(toContactRingId: self.conversation.recipientRingId, vCard: nil, withAccount: self.accountService.currentAccount!)
.subscribe(onCompleted: {
self.log.info("request sent")
})
.disposed(by: disposeBag)
}
}
......@@ -18,7 +18,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
class ContactModel {
class ContactModel: Equatable {
var ringId: String = ""
var userName: String?
......
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic_person_add.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "ic_person_add_2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "ic_person_add_3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
......@@ -38,6 +38,8 @@ class ContactsService {
let contactRequests = Variable([ContactRequestModel]())
let contacts = Variable([ContactModel]())
let contactStatus = PublishSubject<ContactModel>()
init(withContactsAdapter contactsAdapter: ContactsAdapter) {
self.contactsAdapter = contactsAdapter
ContactsAdapter.delegate = self
......@@ -118,10 +120,14 @@ class ContactsService {
}
}
func sendContactRequest(toContactRingId ringId: String, vCard: CNContact, withAccount account: AccountModel) -> Completable {
func sendContactRequest(toContactRingId ringId: String, vCard: CNContact?, withAccount account: AccountModel) -> Completable {
return Completable.create { [unowned self] completable in
do {
let payload = try CNContactVCardSerialization.data(with: [vCard])
var payload: Data?
if let vCard = vCard {
payload = try CNContactVCardSerialization.data(with: [vCard])
}
self.contactsAdapter.sendTrustRequest(toContact: ringId, payload: payload, withAccountId: account.id)
completable(.completed)
} catch {
......@@ -234,6 +240,36 @@ extension ContactsService: ContactsAdapterDelegate {
func contactAdded(contact uri: String, withAccountId accountId: String, confirmed: Bool) {
//Update trust request list
self.removeContactRequest(withRingId: uri)
//if contact list is empty thats mean app just starts and we receive all contacts was added
if self.contacts.value.isEmpty {
return
}
// update contact status
if let contact = self.contact(withRingId: uri) {
if contact.confirmed != confirmed {
contact.confirmed = confirmed
contactStatus.onNext(contact)
}
}
//sync contacts with daemon contacts
else {
let contactsDictionaries = self.contactsAdapter.contacts(withAccountId: accountId)
//Serialize them
if let contacts = contactsDictionaries?.map({ contactDict in
return ContactModel(withDictionary: contactDict)
}) {
for contact in contacts {
if self.contacts.value.index(of: contact) == nil {
self.contacts.value.append(contact)
contactStatus.onNext(contact)
}
}
}
}
log.debug("Contact added :\(uri)")
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment