Skip to content
Snippets Groups Projects
Commit 3499fcc8 authored by Bruno Muniz's avatar Bruno Muniz :apple:
Browse files

Merge branch 'fix-nickname-sent' into 'development'

Fixes bug where setting nickname would mess authStatus

See merge request elixxir/client-ios!49
parents fc508cfd 046a3749
No related branches found
No related tags found
2 merge requests!54Releasing 1.1.4,!49Fixes bug where setting nickname would mess authStatus
......@@ -118,29 +118,41 @@ extension Session {
}
public func add(_ contact: Contact) throws {
/// Make sure we are not adding ourselves
///
guard contact.username != username else {
throw NSError.create("You can't add yourself")
}
var contactToOperate: Contact!
var contact = contact
if [.requestFailed, .confirmationFailed, .stranger].contains(contact.authStatus) {
contactToOperate = contact
/// Check if this contact is actually
/// being requested/confirmed after failing
///
if [.requestFailed, .confirmationFailed].contains(contact.authStatus) {
/// If it is being re-requested or
/// re-confirmed, no need to save again
///
contact.createdAt = Date()
if contact.authStatus == .confirmationFailed {
try confirm(contact)
return
}
} else {
/// If its not failed, lets make sure that
/// this is an actual new contact
///
if let _ = try? dbManager.fetchContacts(.init(id: [contact.id])).first {
/// Found a user w/ that id already stored
///
throw NSError.create("This user has already been requested")
}
contactToOperate = try dbManager.saveContact(contact)
contact.authStatus = .requesting
}
guard contactToOperate.authStatus != .confirmationFailed else {
contactToOperate.createdAt = Date()
try confirm(contact)
return
}
contactToOperate.authStatus = .requesting
contact = try dbManager.saveContact(contact)
let myself = client.bindings.meMarshalled(
username!,
......@@ -148,22 +160,23 @@ extension Session {
phone: isSharingPhone ? phone : nil
)
client.bindings.add(contactToOperate.marshaled!, from: myself) { [weak self, contactToOperate] in
guard let self = self, var contactToOperate = contactToOperate else { return }
client.bindings.add(contact.marshaled!, from: myself) { [weak self, contact] in
guard let self = self else { return }
var contact = contact
do {
switch $0 {
case .success(let success):
contactToOperate.authStatus = success ? .requested : .requestFailed
contactToOperate = try self.dbManager.saveContact(contactToOperate)
contact.authStatus = success ? .requested : .requestFailed
contact = try self.dbManager.saveContact(contact)
case .failure:
contactToOperate.authStatus = .requestFailed
contactToOperate.createdAt = Date()
contactToOperate = try self.dbManager.saveContact(contactToOperate)
contact.createdAt = Date()
contact.authStatus = .requestFailed
contact = try self.dbManager.saveContact(contact)
self.toastController.enqueueToast(model: .init(
title: Localized.Requests.Failed.toast(contactToOperate.nickname ?? contact.username!),
title: Localized.Requests.Failed.toast(contact.nickname ?? contact.username!),
color: Asset.accentDanger.color,
leftImage: Asset.requestFailedToaster.image
))
......
......@@ -3,6 +3,7 @@ import UIKit
import Models
import Shared
import Combine
import XXModels
import Integration
import ToastFeature
import CombineSchedulers
......@@ -32,7 +33,12 @@ final class RequestsSentViewModel {
var backgroundScheduler: AnySchedulerOf<DispatchQueue> = DispatchQueue.global().eraseToAnyScheduler()
init() {
session.dbManager.fetchContactsPublisher(.init(authStatus: [.requested]))
let query = Contact.Query(authStatus: [
.requested,
.requesting
])
session.dbManager.fetchContactsPublisher(query)
.assertNoFailure()
.removeDuplicates()
.map { data -> NSDiffableDataSourceSnapshot<Section, RequestSent> in
......
......@@ -163,12 +163,9 @@ final class SearchViewModel {
}
func didSet(nickname: String, for contact: Contact) {
var contact = contact
contact.nickname = nickname
backgroundScheduler.schedule { [weak self] in
guard let self = self else { return }
_ = try? self.session.dbManager.saveContact(contact)
if var contact = try? session.dbManager.fetchContacts(.init(id: [contact.id])).first {
contact.nickname = nickname
_ = try? session.dbManager.saveContact(contact)
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment