Push notifications: display account name if multi account fix #344

This commit is contained in:
Thomas Ricouard 2023-01-24 13:38:26 +01:00
parent 4927b12e90
commit d05afd93c4
8 changed files with 39 additions and 11 deletions

View file

@ -46,6 +46,7 @@
9F7335EF29674F7100AFF0BA /* QuickLook.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9F7335EE29674F7100AFF0BA /* QuickLook.framework */; };
9F7335F22967608F00AFF0BA /* AddRemoteTimelineView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F7335F12967608F00AFF0BA /* AddRemoteTimelineView.swift */; };
9F7335F92968576500AFF0BA /* DisplaySettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F7335F82968576500AFF0BA /* DisplaySettingsView.swift */; };
9F7D93942980063100EE6B7A /* AppAccount in Frameworks */ = {isa = PBXBuildFile; productRef = 9F7D93932980063100EE6B7A /* AppAccount */; };
9F8CA5972979B61100481E8E /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = E9B576C529743F4C00BCE646 /* Localizable.strings */; };
9F8CA5982979B63D00481E8E /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = E9B576C529743F4C00BCE646 /* Localizable.strings */; };
9FAD85832971BF7200496AB1 /* Secret.plist in Resources */ = {isa = PBXBuildFile; fileRef = 9FAD85822971BF7200496AB1 /* Secret.plist */; };
@ -178,6 +179,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
9F7D93942980063100EE6B7A /* AppAccount in Frameworks */,
9F2A5428296AB683009B2D7C /* Models in Frameworks */,
9F2A5426296AB67E009B2D7C /* KeychainSwift in Frameworks */,
9F2A5424296AB67A009B2D7C /* Env in Frameworks */,
@ -401,6 +403,7 @@
9F2A5423296AB67A009B2D7C /* Env */,
9F2A5425296AB67E009B2D7C /* KeychainSwift */,
9F2A5427296AB683009B2D7C /* Models */,
9F7D93932980063100EE6B7A /* AppAccount */,
);
productName = IceCubesNotifications;
productReference = 9F2A5416296AB631009B2D7C /* IceCubesNotifications.appex */;
@ -1103,6 +1106,10 @@
isa = XCSwiftPackageProductDependency;
productName = Conversations;
};
9F7D93932980063100EE6B7A /* AppAccount */ = {
isa = XCSwiftPackageProductDependency;
productName = AppAccount;
};
9FAD85972974405D00496AB1 /* Status */ = {
isa = XCSwiftPackageProductDependency;
productName = Status;

View file

@ -225,7 +225,11 @@ struct AddAccountView: View {
do {
oauthURL = nil
let oauthToken = try await client.continueOauthFlow(url: url)
appAccountsManager.add(account: AppAccount(server: client.server, oauthToken: oauthToken))
let client = Client(server: client.server, oauthToken: oauthToken)
let account: Account = try await client.get(endpoint: Accounts.verifyCredentials)
appAccountsManager.add(account: AppAccount(server: client.server,
accountName: "\(account.acct)@\(client.server)",
oauthToken: oauthToken))
Task {
await pushNotifications.updateSubscriptions(accounts: appAccountsManager.pushAccounts)
}

View file

@ -64,7 +64,9 @@ struct SettingsTabs: View {
let account = appAccountsManager.availableAccounts[index]
if let token = account.oauthToken {
Task {
await pushNotifications.deleteSubscriptions(accounts: [.init(server: account.server, token: token)])
await pushNotifications.deleteSubscriptions(accounts: [.init(server: account.server,
token: token,
accountName: account.accountName)])
}
}
appAccountsManager.delete(account: account)

View file

@ -4,6 +4,7 @@ import KeychainSwift
import Models
import UIKit
import UserNotifications
import AppAccount
@MainActor
class NotificationService: UNNotificationServiceExtension {
@ -50,9 +51,11 @@ class NotificationService: UNNotificationServiceExtension {
contentHandler(bestAttemptContent)
return
}
bestAttemptContent.title = notification.title
bestAttemptContent.subtitle = ""
if AppAccountsManager.shared.availableAccounts.count > 1 {
bestAttemptContent.subtitle = bestAttemptContent.userInfo["i"] as? String ?? ""
}
bestAttemptContent.body = notification.body.escape()
bestAttemptContent.userInfo["plaintext"] = plaintextData
bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "glass.wav"))

View file

@ -6,6 +6,7 @@ import SwiftUI
public struct AppAccount: Codable, Identifiable {
public let server: String
public var accountName: String?
public let oauthToken: OauthToken?
public var id: String {
@ -28,8 +29,11 @@ public struct AppAccount: Codable, Identifiable {
}
}
public init(server: String, oauthToken: OauthToken? = nil) {
public init(server: String,
accountName: String?,
oauthToken: OauthToken? = nil) {
self.server = server
self.accountName = accountName
self.oauthToken = oauthToken
}

View file

@ -4,7 +4,7 @@ import SwiftUI
@MainActor
public class AppAccountViewModel: ObservableObject {
let appAccount: AppAccount
var appAccount: AppAccount
let client: Client
let isCompact: Bool
@ -23,6 +23,10 @@ public class AppAccountViewModel: ObservableObject {
func fetchAccount() async {
do {
account = try await client.get(endpoint: Accounts.verifyCredentials)
if appAccount.accountName == nil, let account {
appAccount.accountName = "\(account.acct)@\(appAccount.server)"
try appAccount.save()
}
} catch {
print(error)
}

View file

@ -21,13 +21,13 @@ public class AppAccountsManager: ObservableObject {
public var pushAccounts: [PushNotificationsService.PushAccounts] {
availableAccounts.filter { $0.oauthToken != nil }
.map { .init(server: $0.server, token: $0.oauthToken!) }
.map { .init(server: $0.server, token: $0.oauthToken!, accountName: $0.accountName) }
}
public static var shared = AppAccountsManager()
internal init() {
var defaultAccount = AppAccount(server: AppInfo.defaultServer, oauthToken: nil)
var defaultAccount = AppAccount(server: AppInfo.defaultServer, accountName: nil, oauthToken: nil)
let keychainAccounts = AppAccount.retrieveAll()
availableAccounts = keychainAccounts
if let currentAccount = keychainAccounts.first(where: { $0.id == Self.latestCurrentAccountKey }) {
@ -51,7 +51,9 @@ public class AppAccountsManager: ObservableObject {
availableAccounts.removeAll(where: { $0.id == account.id })
account.delete()
if currentAccount.id == account.id {
currentAccount = availableAccounts.first ?? AppAccount(server: AppInfo.defaultServer, oauthToken: nil)
currentAccount = availableAccounts.first ?? AppAccount(server: AppInfo.defaultServer,
accountName: nil,
oauthToken: nil)
}
}
}

View file

@ -17,10 +17,12 @@ public class PushNotificationsService: ObservableObject {
public struct PushAccounts {
public let server: String
public let token: OauthToken
public let accountName: String?
public init(server: String, token: OauthToken) {
public init(server: String, token: OauthToken, accountName: String?) {
self.server = server
self.token = token
self.accountName = accountName
}
}
@ -85,7 +87,7 @@ public class PushNotificationsService: ObservableObject {
var listenerURL = Constants.endpoint
listenerURL += "/push/"
listenerURL += pushToken.hexString
listenerURL += "/\(account.server)"
listenerURL += "/\(account.accountName ?? account.server)"
#if DEBUG
listenerURL += "?sandbox=true"
#endif