Initial public API refactoring experiment

This commit is contained in:
Ali 2021-03-23 02:16:48 +04:00
parent e6eff16df1
commit 8d5a945763
32 changed files with 82 additions and 11 deletions

View File

@ -713,6 +713,7 @@ public protocol AccountGroupCallContextCache: class {
public protocol AccountContext: class {
var sharedContext: SharedAccountContext { get }
var account: Account { get }
var engine: TelegramEngine { get }
var liveLocationManager: LiveLocationManager? { get }
var peersNearbyManager: PeersNearbyManager? { get }

View File

@ -127,7 +127,7 @@ public final class SecureIdAuthController: ViewController, StandalonePresentable
if let strongSelf = self {
let storedPassword = context.getStoredSecureIdPassword()
if data.currentPasswordDerivation != nil, let storedPassword = storedPassword {
strongSelf.authenthicateDisposable.set((accessSecureId(network: strongSelf.context.account.network, password: storedPassword)
strongSelf.authenthicateDisposable.set((strongSelf.context.engine.secureId.accessSecureId(password: storedPassword)
|> deliverOnMainQueue).start(next: { context in
guard let strongSelf = self, strongSelf.state.verificationState == nil else {
return
@ -441,7 +441,7 @@ public final class SecureIdAuthController: ViewController, StandalonePresentable
state.verificationState = .passwordChallenge(hint: hint, state: .checking, hasRecoveryEmail: hasRecoveryEmail)
return state
})
self.challengeDisposable.set((accessSecureId(network: self.context.account.network, password: password)
self.challengeDisposable.set((self.context.engine.secureId.accessSecureId(password: password)
|> deliverOnMainQueue).start(next: { [weak self] context in
guard let strongSelf = self, let verificationState = strongSelf.state.verificationState, case .passwordChallenge(_, .checking, _) = verificationState else {
return

View File

@ -478,14 +478,14 @@ public func peersNearbyController(context: AccountContext) -> ViewController {
let _ = (coordinatePromise.get()
|> deliverOnMainQueue).start(next: { coordinate in
if let coordinate = coordinate {
let _ = updatePeersNearbyVisibility(account: context.account, update: .visible(latitude: coordinate.latitude, longitude: coordinate.longitude), background: false).start()
let _ = context.engine.peersNearby.updatePeersNearbyVisibility(update: .visible(latitude: coordinate.latitude, longitude: coordinate.longitude), background: false).start()
}
})
})]), nil)
} else {
let _ = updatePeersNearbyVisibility(account: context.account, update: .invisible, background: false).start()
let _ = context.engine.peersNearby.updatePeersNearbyVisibility(update: .invisible, background: false).start()
}
}, openProfile: { peer, distance in
navigateToProfileImpl?(peer, distance)

View File

@ -25,7 +25,7 @@ public enum PeerNearbyVisibilityUpdate {
case invisible
}
public func updatePeersNearbyVisibility(account: Account, update: PeerNearbyVisibilityUpdate, background: Bool) -> Signal<Void, NoError> {
func _internal_updatePeersNearbyVisibility(account: Account, update: PeerNearbyVisibilityUpdate, background: Bool) -> Signal<Void, NoError> {
var flags: Int32 = 0
var geoPoint: Api.InputGeoPoint
var selfExpires: Int32?
@ -138,7 +138,6 @@ public final class PeersNearbyContext {
}
|> restartIfError
|> `catch` { _ -> Signal<[PeerNearby], NoError> in
return .single([])
}
self.disposable.set((combined

View File

@ -0,0 +1,15 @@
import SwiftSignalKit
public extension TelegramEngine {
final class PeersNearby {
private let account: Account
init(account: Account) {
self.account = account
}
public func updatePeersNearbyVisibility(update: PeerNearbyVisibilityUpdate, background: Bool) -> Signal<Void, NoError> {
return _internal_updatePeersNearbyVisibility(account: self.account, update: update, background: background)
}
}
}

View File

@ -153,7 +153,7 @@ public enum SecureIdAccessError {
case secretPasswordMismatch
}
public func accessSecureId(network: Network, password: String) -> Signal<(context: SecureIdAccessContext, settings: TwoStepVerificationSettings), SecureIdAccessError> {
func _internal_accessSecureId(network: Network, password: String) -> Signal<(context: SecureIdAccessContext, settings: TwoStepVerificationSettings), SecureIdAccessError> {
return requestTwoStepVerifiationSettings(network: network, password: password)
|> mapError { error -> SecureIdAccessError in
return .passwordError(error)

View File

@ -0,0 +1,15 @@
import SwiftSignalKit
public extension TelegramEngine {
final class SecureId {
private let account: Account
init(account: Account) {
self.account = account
}
public func accessSecureId(password: String) -> Signal<(context: SecureIdAccessContext, settings: TwoStepVerificationSettings), SecureIdAccessError> {
return _internal_accessSecureId(network: self.account.network, password: password)
}
}
}

View File

@ -0,0 +1,18 @@
import SwiftSignalKit
import Postbox
public final class TelegramEngine {
public let account: Account
public init(account: Account) {
self.account = account
}
public lazy var secureId: SecureId = {
return SecureId(account: self.account)
}()
public lazy var peersNearby: PeersNearby = {
return PeersNearby(account: self.account)
}()
}

View File

@ -109,6 +109,7 @@ public final class AccountContextImpl: AccountContext {
return self.sharedContextImpl
}
public let account: Account
public let engine: TelegramEngine
public let fetchManager: FetchManager
private let prefetchManager: PrefetchManager?
@ -160,6 +161,7 @@ public final class AccountContextImpl: AccountContext {
{
self.sharedContextImpl = sharedContext
self.account = account
self.engine = TelegramEngine(account: account)
self.downloadedMediaStoreManager = DownloadedMediaStoreManagerImpl(postbox: account.postbox, accountManager: sharedContext.accountManager)
@ -180,7 +182,7 @@ public final class AccountContextImpl: AccountContext {
}
if let locationManager = self.sharedContextImpl.locationManager, sharedContext.applicationBindings.isMainApp && !temp {
self.peersNearbyManager = PeersNearbyManagerImpl(account: account, locationManager: locationManager, inForeground: sharedContext.applicationBindings.applicationInForeground)
self.peersNearbyManager = PeersNearbyManagerImpl(account: account, engine: self.engine, locationManager: locationManager, inForeground: sharedContext.applicationBindings.applicationInForeground)
} else {
self.peersNearbyManager = nil
}

View File

@ -7,23 +7,27 @@ import TelegramApi
import DeviceLocationManager
import CoreLocation
import AccountContext
import DeviceAccess
private let locationUpdateTimePeriod: Double = 1.0 * 60.0 * 60.0
private let locationDistanceUpdateThreshold: Double = 1000
final class PeersNearbyManagerImpl: PeersNearbyManager {
private let account: Account
private let engine: TelegramEngine
private let locationManager: DeviceLocationManager
private let inForeground: Signal<Bool, NoError>
private var preferencesDisposable: Disposable?
private var locationDisposable = MetaDisposable()
private var updateDisposable = MetaDisposable()
private var accessDisposable: Disposable?
private var previousLocation: CLLocation?
init(account: Account, locationManager: DeviceLocationManager, inForeground: Signal<Bool, NoError>) {
init(account: Account, engine: TelegramEngine, locationManager: DeviceLocationManager, inForeground: Signal<Bool, NoError>) {
self.account = account
self.engine = engine
self.locationManager = locationManager
self.inForeground = inForeground
@ -32,17 +36,34 @@ final class PeersNearbyManagerImpl: PeersNearbyManager {
let state = view.values[PreferencesKeys.peersNearby] as? PeersNearbyState ?? .default
return state.visibilityExpires
}
|> deliverOnMainQueue
|> distinctUntilChanged).start(next: { [weak self] visibility in
if let strongSelf = self {
strongSelf.visibilityUpdated(visible: visibility != nil)
}
})
self.accessDisposable = (DeviceAccess.authorizationStatus(applicationInForeground: nil, siriAuthorization: nil, subject: .location(.live))
|> deliverOnMainQueue).start(next: { [weak self] status in
guard let strongSelf = self else {
return
}
switch status {
case .denied:
let _ = strongSelf.engine.peersNearby.updatePeersNearbyVisibility(update: .invisible, background: false).start()
strongSelf.locationDisposable.set(nil)
strongSelf.updateDisposable.set(nil)
default:
break
}
})
}
deinit {
self.preferencesDisposable?.dispose()
self.locationDisposable.dispose()
self.updateDisposable.dispose()
self.accessDisposable?.dispose()
}
private func visibilityUpdated(visible: Bool) {
@ -77,9 +98,9 @@ final class PeersNearbyManagerImpl: PeersNearbyManager {
}
private func updateLocation(_ location: CLLocation) {
self.updateDisposable.set(updatePeersNearbyVisibility(account: self.account, update: .location(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude), background: true).start(error: { [weak self] _ in
self.updateDisposable.set(self.engine.peersNearby.updatePeersNearbyVisibility(update: .location(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude), background: true).start(error: { [weak self] _ in
if let strongSelf = self {
let _ = updatePeersNearbyVisibility(account: strongSelf.account, update: .invisible, background: false).start()
let _ = strongSelf.engine.peersNearby.updatePeersNearbyVisibility(update: .invisible, background: false).start()
strongSelf.locationDisposable.set(nil)
strongSelf.updateDisposable.set(nil)
}