MALINKA-912 - token push notifications

This commit is contained in:
Juraldinio
2022-12-19 13:55:38 +03:00
parent ce2de4f36c
commit 4d22d97b36
2 changed files with 30 additions and 32 deletions
@@ -11,6 +11,12 @@ import WalletFoundation
public struct AccountService: NetworkService {
public enum ServiceError: Error {
case notificationEmptyAccount
case notificationEmptyToken
case notificationEmptyLanguage
}
let environment: NetworkEnvironment
public init(environment: NetworkEnvironment) {
@@ -112,9 +118,13 @@ public struct AccountService: NetworkService {
}
}
public func updateNotification(token: String, accounts: [String], language: String) async {
public func updateNotification(token: String, accounts: [String], language: String) async throws {
guard !accounts.isEmpty, !token.isEmpty, !language.isEmpty else { return }
if accounts.isEmpty { throw ServiceError.notificationEmptyAccount }
if token.isEmpty { throw ServiceError.notificationEmptyToken }
if language.isEmpty { throw ServiceError.notificationEmptyLanguage }
let variables = AccountNotificationTokenRequest.Variables(
deviceToken: token,
@@ -39,6 +39,8 @@ extension Account {
// TODO: - Remove "!"
static var shared: Service!
private var cancellables = Set<AnyCancellable>()
let cityId = 16
var collection: [WalletKit.Wallet] { self.bank.wallets }
@@ -113,8 +115,9 @@ extension Account {
}
private func setup() {
self.updatePush()
self.quota.fetch(using: self.bank.active, clear: true)
self.accountsPublisher.sink { [weak self] _ in self?.updatePush() }.store(in: &self.cancellables)
}
}
}
@@ -257,44 +260,29 @@ extension Account.Service {
get { Settings.shared[isPushEnabledKey] }
set {
Settings.shared[isPushEnabledKey] = newValue
updatePush()
self.updatePush()
}
}
private func updatePush() {
let record = ApplicationEnvironment.shared().current
guard let token = Messaging.messaging().fcmToken else { return }
let collection = self.collection
guard let token = Messaging.messaging().fcmToken,
!collection.isEmpty else {
return
}
Task {
let service = try AccountService(environment: record.networkEnvironment())
await service.updateNotification(token: token,
accounts: self.collection.map { $0.name },
language: Common.Model.Language.current.rawValue)
}
}
}
// MARK: - Migration
extension Account.Service {
func migrateAccountIfNeeded(password: String) {
/*if !isAccountMigrated,
let data = UserDefaults.standard.value(forKey: collectionKey.rawValue) as? Data,
let objects = try? JSONDecoder().decode([Account.OldModel].self, from: data) {
collection = []
var accounts: [Account.Model] = []
objects.forEach { oldAccount in
let account = Account.Model(username: oldAccount.username,
publicKey: oldAccount.publicKey,
keyType: .active,
state: .accepted)
account.migrate(password: password)
accounts.append(account)
do {
try await service.updateNotification(token: token,
accounts: collection.map { $0.name },
language: Common.Model.Language.current.rawValue)
} catch {
print(error)
}
collection = accounts
}
isAccountMigrated = true*/
}
}