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 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 { switch subject {
case .notifications: case .notifications:
let status = Signal<AccessType, NoError> { subscriber in let status = Signal<AccessType, NoError> { subscriber in
@ -116,11 +116,15 @@ public final class DeviceAccess {
return .complete() return .complete()
} }
}) })
if let account = account {
return account.telegramApplicationContext.applicationBindings.applicationInForeground return account.telegramApplicationContext.applicationBindings.applicationInForeground
|> distinctUntilChanged |> distinctUntilChanged
|> mapToSignal { inForeground -> Signal<AccessType, NoError> in |> mapToSignal { inForeground -> Signal<AccessType, NoError> in
return status return status
} }
} else {
return status
}
case .contacts: case .contacts:
let status = Signal<AccessType, NoError> { subscriber in let status = Signal<AccessType, NoError> { subscriber in
if #available(iOSApplicationExtension 9.0, *) { if #available(iOSApplicationExtension 9.0, *) {
@ -184,6 +188,7 @@ public final class DeviceAccess {
return EmptyDisposable return EmptyDisposable
} }
case .siri: case .siri:
if let account = account {
return Signal { subscriber in return Signal { subscriber in
let status = account.telegramApplicationContext.applicationBindings.siriAuthorization() let status = account.telegramApplicationContext.applicationBindings.siriAuthorization()
subscriber.putNext(status) subscriber.putNext(status)
@ -198,6 +203,9 @@ public final class DeviceAccess {
return .complete() return .complete()
} }
}) })
} else {
return .single(.denied)
}
default: default:
return .single(.notDetermined) return .single(.notDetermined)
} }

View File

@ -57,16 +57,19 @@ final class ContactsControllerNode: ASDisplayNode {
}) })
inviteImpl = { [weak self] in inviteImpl = { [weak self] in
let _ = (DeviceAccess.contacts let _ = (DeviceAccess.authorizationStatus(account: account, subject: .contacts)
|> take(1) |> take(1)
|> deliverOnMainQueue).start(next: { value in |> deliverOnMainQueue).start(next: { value in
guard let strongSelf = self else { guard let strongSelf = self else {
return return
} }
if let value = value, value { switch value {
case .allowed:
strongSelf.openInvite?() strongSelf.openInvite?()
} else { case .notDetermined:
DeviceAccess.authorizeAccess(to: .contacts)
default:
let presentationData = strongSelf.presentationData 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: { 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() self?.account.telegramApplicationContext.applicationBindings.openSettings()

View File

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

View File

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

View File

@ -4,13 +4,16 @@ import TelegramCore
import Display import Display
func openAddContact(account: Account, firstName: String = "", lastName: String = "", phoneNumber: String, label: String = "_$!<Mobile>!$_", present: @escaping (ViewController, Any?) -> Void, completed: @escaping () -> Void = {}) { 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) |> take(1)
|> deliverOnMainQueue).start(next: { value in |> deliverOnMainQueue).start(next: { value in
if let value = value, value { 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: []) 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)) present(deviceContactInfoController(account: account, subject: .create(peer: nil, contactData: contactData, completion: { _, _,_ in }), completed: completed), ViewControllerPresentationArguments(presentationAnimation: .modalSheet))
} else { case .notDetermined:
DeviceAccess.authorizeAccess(to: .contacts)
default:
let presentationData = account.telegramApplicationContext.currentPresentationData.with { $0 } 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: { 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() account.telegramApplicationContext.applicationBindings.openSettings()