Fixed requesting contact authorization status in several places

This commit is contained in:
Ilya Laktyushin 2018-12-20 23:10:21 +04:00
parent f65369f308
commit a63a38ca84
5 changed files with 53 additions and 38 deletions

View File

@ -80,7 +80,7 @@ public final class DeviceAccess {
return AVAudioSession.sharedInstance().recordPermission() == .granted
}
public static func authorizationStatus(account: Account, subject: DeviceAccessSubject) -> Signal<AccessType, NoError> {
public static func authorizationStatus(account: Account? = nil, subject: DeviceAccessSubject) -> Signal<AccessType, NoError> {
switch subject {
case .notifications:
let status = Signal<AccessType, NoError> { subscriber in
@ -116,9 +116,13 @@ public final class DeviceAccess {
return .complete()
}
})
return account.telegramApplicationContext.applicationBindings.applicationInForeground
|> distinctUntilChanged
|> mapToSignal { inForeground -> Signal<AccessType, NoError> in
if let account = account {
return account.telegramApplicationContext.applicationBindings.applicationInForeground
|> distinctUntilChanged
|> mapToSignal { inForeground -> Signal<AccessType, NoError> in
return status
}
} else {
return status
}
case .contacts:
@ -184,20 +188,24 @@ public final class DeviceAccess {
return EmptyDisposable
}
case .siri:
return Signal { subscriber in
let status = account.telegramApplicationContext.applicationBindings.siriAuthorization()
subscriber.putNext(status)
subscriber.putCompletion()
return EmptyDisposable
if let account = account {
return Signal { subscriber in
let status = account.telegramApplicationContext.applicationBindings.siriAuthorization()
subscriber.putNext(status)
subscriber.putCompletion()
return EmptyDisposable
}
|> then(self.siri
|> mapToSignal { authorized -> Signal<AccessType, NoError> in
if let authorized = authorized {
return .single(authorized ? .allowed : .denied)
} else {
return .complete()
}
})
} else {
return .single(.denied)
}
|> then(self.siri
|> mapToSignal { authorized -> Signal<AccessType, NoError> in
if let authorized = authorized {
return .single(authorized ? .allowed : .denied)
} else {
return .complete()
}
})
default:
return .single(.notDetermined)
}

View File

@ -57,20 +57,23 @@ final class ContactsControllerNode: ASDisplayNode {
})
inviteImpl = { [weak self] in
let _ = (DeviceAccess.contacts
let _ = (DeviceAccess.authorizationStatus(account: account, subject: .contacts)
|> take(1)
|> deliverOnMainQueue).start(next: { value in
guard let strongSelf = self else {
return
}
if let value = value, value {
strongSelf.openInvite?()
} else {
let presentationData = strongSelf.presentationData
present(standardTextAlertController(theme: AlertControllerTheme(presentationTheme: presentationData.theme), title: presentationData.strings.AccessDenied_Title, text: presentationData.strings.Contacts_AccessDeniedError, actions: [TextAlertAction(type: .defaultAction, title: presentationData.strings.Common_NotNow, action: {}), TextAlertAction(type: .genericAction, title: presentationData.strings.AccessDenied_Settings, action: {
self?.account.telegramApplicationContext.applicationBindings.openSettings()
})]), nil)
switch value {
case .allowed:
strongSelf.openInvite?()
case .notDetermined:
DeviceAccess.authorizeAccess(to: .contacts)
default:
let presentationData = strongSelf.presentationData
present(standardTextAlertController(theme: AlertControllerTheme(presentationTheme: presentationData.theme), title: presentationData.strings.AccessDenied_Title, text: presentationData.strings.Contacts_AccessDeniedError, actions: [TextAlertAction(type: .defaultAction, title: presentationData.strings.Common_NotNow, action: {}), TextAlertAction(type: .genericAction, title: presentationData.strings.AccessDenied_Settings, action: {
self?.account.telegramApplicationContext.applicationBindings.openSettings()
})]), nil)
}
})
}

View File

@ -413,13 +413,13 @@ private final class DeviceContactDataManagerImpl {
init(queue: Queue) {
self.queue = queue
self.accessDisposable = (DeviceAccess.contacts
self.accessDisposable = (DeviceAccess.authorizationStatus(subject: .contacts)
|> deliverOn(self.queue)).start(next: { [weak self] authorizationStatus in
guard let strongSelf = self, let authorizationStatus = authorizationStatus else {
guard let strongSelf = self, authorizationStatus != .notDetermined else {
return
}
strongSelf.accessInitialized = true
if authorizationStatus {
if authorizationStatus == .allowed {
if #available(iOSApplicationExtension 9.0, *) {
let dataContext = DeviceContactDataModernContext(queue: strongSelf.queue, updated: { stableIdToBasicContactData in
guard let strongSelf = self else {

View File

@ -107,6 +107,7 @@ final class InstantPageNavigationBar: ASDisplayNode {
self.titleNode = ASTextNode()
self.titleNode.maximumNumberOfLines = 1
self.titleNode.truncationMode = .byTruncatingTail
self.progressNode = InstantPageProgressNode()
@ -209,7 +210,7 @@ final class InstantPageNavigationBar: ASDisplayNode {
if let title = title {
self.titleNode.transform = CATransform3DIdentity
self.titleNode.attributedText = NSAttributedString(string: title, font: Font.semibold(17.0), textColor: .white, paragraphAlignment: .center)
let titleSize = self.titleNode.measure(CGSize(width: size.width - leftInset - rightInset - 30.0 - 88.0, height: size.height))
let titleSize = self.titleNode.measure(CGSize(width: size.width - leftInset - rightInset - 170.0, height: size.height))
self.titleNode.frame = CGRect(origin: CGPoint(x: (size.width - titleSize.width) / 2.0, y: size.height - 30.0), size: titleSize)
}
}

View File

@ -4,17 +4,20 @@ import TelegramCore
import Display
func openAddContact(account: Account, firstName: String = "", lastName: String = "", phoneNumber: String, label: String = "_$!<Mobile>!$_", present: @escaping (ViewController, Any?) -> Void, completed: @escaping () -> Void = {}) {
let _ = (DeviceAccess.contacts
let _ = (DeviceAccess.authorizationStatus(account: account, subject: .contacts)
|> take(1)
|> deliverOnMainQueue).start(next: { value in
if let value = value, value {
let contactData = DeviceContactExtendedData(basicData: DeviceContactBasicData(firstName: firstName, lastName: lastName, phoneNumbers: [DeviceContactPhoneNumberData(label: label, value: phoneNumber)]), middleName: "", prefix: "", suffix: "", organization: "", jobTitle: "", department: "", emailAddresses: [], urls: [], addresses: [], birthdayDate: nil, socialProfiles: [], instantMessagingProfiles: [])
present(deviceContactInfoController(account: account, subject: .create(peer: nil, contactData: contactData, completion: { _, _,_ in }), completed: completed), ViewControllerPresentationArguments(presentationAnimation: .modalSheet))
} else {
let presentationData = account.telegramApplicationContext.currentPresentationData.with { $0 }
present(standardTextAlertController(theme: AlertControllerTheme(presentationTheme: presentationData.theme), title: presentationData.strings.AccessDenied_Title, text: presentationData.strings.Contacts_AccessDeniedError, actions: [TextAlertAction(type: .defaultAction, title: presentationData.strings.Common_NotNow, action: {}), TextAlertAction(type: .genericAction, title: presentationData.strings.AccessDenied_Settings, action: {
account.telegramApplicationContext.applicationBindings.openSettings()
})]), nil)
switch value {
case .allowed:
let contactData = DeviceContactExtendedData(basicData: DeviceContactBasicData(firstName: firstName, lastName: lastName, phoneNumbers: [DeviceContactPhoneNumberData(label: label, value: phoneNumber)]), middleName: "", prefix: "", suffix: "", organization: "", jobTitle: "", department: "", emailAddresses: [], urls: [], addresses: [], birthdayDate: nil, socialProfiles: [], instantMessagingProfiles: [])
present(deviceContactInfoController(account: account, subject: .create(peer: nil, contactData: contactData, completion: { _, _,_ in }), completed: completed), ViewControllerPresentationArguments(presentationAnimation: .modalSheet))
case .notDetermined:
DeviceAccess.authorizeAccess(to: .contacts)
default:
let presentationData = account.telegramApplicationContext.currentPresentationData.with { $0 }
present(standardTextAlertController(theme: AlertControllerTheme(presentationTheme: presentationData.theme), title: presentationData.strings.AccessDenied_Title, text: presentationData.strings.Contacts_AccessDeniedError, actions: [TextAlertAction(type: .defaultAction, title: presentationData.strings.Common_NotNow, action: {}), TextAlertAction(type: .genericAction, title: presentationData.strings.AccessDenied_Settings, action: {
account.telegramApplicationContext.applicationBindings.openSettings()
})]), nil)
}
})
}