diff --git a/IceCubesApp.xcodeproj/project.pbxproj b/IceCubesApp.xcodeproj/project.pbxproj index ffeb0336..68b817a4 100644 --- a/IceCubesApp.xcodeproj/project.pbxproj +++ b/IceCubesApp.xcodeproj/project.pbxproj @@ -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; diff --git a/IceCubesApp/App/Tabs/Settings/AddAccountsView.swift b/IceCubesApp/App/Tabs/Settings/AddAccountsView.swift index 68a29c55..fd520f6d 100644 --- a/IceCubesApp/App/Tabs/Settings/AddAccountsView.swift +++ b/IceCubesApp/App/Tabs/Settings/AddAccountsView.swift @@ -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) } diff --git a/IceCubesApp/App/Tabs/Settings/SettingsTab.swift b/IceCubesApp/App/Tabs/Settings/SettingsTab.swift index 32afd21d..f39915a6 100644 --- a/IceCubesApp/App/Tabs/Settings/SettingsTab.swift +++ b/IceCubesApp/App/Tabs/Settings/SettingsTab.swift @@ -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) diff --git a/IceCubesNotifications/NotificationService.swift b/IceCubesNotifications/NotificationService.swift index 512c0386..e37c3c82 100644 --- a/IceCubesNotifications/NotificationService.swift +++ b/IceCubesNotifications/NotificationService.swift @@ -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")) diff --git a/Packages/AppAccount/Sources/AppAccount/AppAccount.swift b/Packages/AppAccount/Sources/AppAccount/AppAccount.swift index 10016514..40040020 100644 --- a/Packages/AppAccount/Sources/AppAccount/AppAccount.swift +++ b/Packages/AppAccount/Sources/AppAccount/AppAccount.swift @@ -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 } diff --git a/Packages/AppAccount/Sources/AppAccount/AppAccountViewModel.swift b/Packages/AppAccount/Sources/AppAccount/AppAccountViewModel.swift index 21d093d1..fd2a13f6 100644 --- a/Packages/AppAccount/Sources/AppAccount/AppAccountViewModel.swift +++ b/Packages/AppAccount/Sources/AppAccount/AppAccountViewModel.swift @@ -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) } diff --git a/Packages/AppAccount/Sources/AppAccount/AppAccountsManager.swift b/Packages/AppAccount/Sources/AppAccount/AppAccountsManager.swift index a5c9afa9..9d64dc64 100644 --- a/Packages/AppAccount/Sources/AppAccount/AppAccountsManager.swift +++ b/Packages/AppAccount/Sources/AppAccount/AppAccountsManager.swift @@ -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) } } } diff --git a/Packages/Env/Sources/Env/PushNotificationsService.swift b/Packages/Env/Sources/Env/PushNotificationsService.swift index 3b23d3f2..376bc89a 100644 --- a/Packages/Env/Sources/Env/PushNotificationsService.swift +++ b/Packages/Env/Sources/Env/PushNotificationsService.swift @@ -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