Skip to content

Commit

Permalink
Combine AuthenticationService into AuthenticationServiceProvider
Browse files Browse the repository at this point in the history
Also, AppContext, APIService, and AuthenticationServiceProvider are now more obviously singletons.

And AuthenticationServiceProvider can now be asked for the current active user, instead of every caller assuming the first element of a list of users is the active user.
  • Loading branch information
whattherestimefor committed Nov 14, 2024
1 parent 5dcc64b commit 77f3c5a
Show file tree
Hide file tree
Showing 32 changed files with 183 additions and 300 deletions.
6 changes: 3 additions & 3 deletions Mastodon/Coordinator/SceneCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ final public class SceneCoordinator {
}
let domain = authentication.domain
let userID = authentication.userID
let isSuccess = try await appContext.authenticationService.activeMastodonUser(domain: domain, userID: userID)
let isSuccess = try await AuthenticationServiceProvider.shared.activeMastodonUser(domain: domain, userID: userID)
guard isSuccess else { return }

self.setup()
Expand Down Expand Up @@ -648,8 +648,8 @@ extension SceneCoordinator: SettingsCoordinatorDelegate {
self.appContext.notificationService.clearNotificationCountForActiveUser()

Task { @MainActor in
try await self.appContext.authenticationService.signOutMastodonUser(
authenticationBox: authenticationBox
try await AuthenticationServiceProvider.shared.signOutMastodonUser(
authentication: authenticationBox.authentication
)
let userIdentifier = authenticationBox
FileManager.default.invalidateHomeTimelineCache(for: userIdentifier)
Expand Down
2 changes: 1 addition & 1 deletion Mastodon/Protocol/Provider/DataSourceFacade+Follow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension DataSourceFacade {
authenticationBox: dependency.authenticationBox
).value

dependency.context.authenticationService.fetchFollowingAndBlockedAsync()
AuthenticationServiceProvider.shared.fetchFollowingAndBlockedAsync()


NotificationCenter.default.post(name: .relationshipChanged, object: nil, userInfo: [
Expand Down
8 changes: 4 additions & 4 deletions Mastodon/Scene/Account/AccountViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ extension AccountListViewController: UITableViewDelegate {

Task { @MainActor in
do {
try await self.viewModel.context.authenticationService.signOutMastodonUser(authentication: record)
try await AuthenticationServiceProvider.shared.signOutMastodonUser(authentication: record)

let userIdentifier = record
FileManager.default.invalidateHomeTimelineCache(for: userIdentifier)
Expand Down Expand Up @@ -144,7 +144,7 @@ extension AccountListViewController: UITableViewDelegate {
case .authentication(let record):
assert(Thread.isMainThread)
Task { @MainActor in
let isActive = try await context.authenticationService.activeMastodonUser(domain: record.domain, userID: record.userID)
let isActive = try await AuthenticationServiceProvider.shared.activeMastodonUser(domain: record.domain, userID: record.userID)
guard isActive else { return }
self.coordinator.setup()
} // end Task
Expand All @@ -157,8 +157,8 @@ extension AccountListViewController: UITableViewDelegate {
let logoutAction = UIAlertAction(title: L10n.Scene.AccountList.logoutAllAccounts, style: .destructive) { _ in
Task { @MainActor in
self.coordinator.showLoading()
for authenticationBox in self.context.authenticationService.mastodonAuthenticationBoxes {
try? await self.context.authenticationService.signOutMastodonUser(authenticationBox: authenticationBox)
for authenticationBox in AuthenticationServiceProvider.shared.mastodonAuthenticationBoxes {
try? await AuthenticationServiceProvider.shared.signOutMastodonUser(authentication: authenticationBox.authentication)
}
self.coordinator.hideLoading()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ extension HomeTimelineViewController {
guard let authContext = viewModel?.authenticationBox else { return }

Task { @MainActor in
try await context.authenticationService.signOutMastodonUser(authenticationBox: authenticationBox)
try await AuthenticationServiceProvider.shared.signOutMastodonUser(authentication: authenticationBox.authentication)
let userIdentifier = authenticationBox
FileManager.default.invalidateHomeTimelineCache(for: userIdentifier)
FileManager.default.invalidateNotificationsAll(for: userIdentifier)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class NotificationPolicyViewController: UIViewController {
// MARK: - Action

@objc private func save(_ sender: UIButton) {
guard let authenticationBox = viewModel.appContext.authenticationService.mastodonAuthenticationBoxes.first else { return }
guard let authenticationBox = AuthenticationServiceProvider.shared.activeAuthentication else { return }

Task { [weak self] in
guard let self else { return }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class MastodonLoginViewController: UIViewController, NeedsDependency {
.authenticated.sink { (domain, account) in
Task { @MainActor in
do {
_ = try await self.context.authenticationService.activeMastodonUser(domain: domain, userID: account.id)
_ = try await AuthenticationServiceProvider.shared.activeMastodonUser(domain: domain, userID: account.id)
FileManager.default.store(account: account, forUserID: MastodonUserIdentifier(domain: domain, userID: account.id))

self.coordinator.setup()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ extension MastodonPickServerViewController {
.authenticated
.asyncMap { domain, user -> Result<Bool, Error> in
do {
let result = try await self.context.authenticationService.activeMastodonUser(domain: domain, userID: user.id)
let result = try await AuthenticationServiceProvider.shared.activeMastodonUser(domain: domain, userID: user.id)
return .success(result)
} catch {
return .failure(error)
Expand Down
2 changes: 1 addition & 1 deletion Mastodon/Scene/Onboarding/Welcome/WelcomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class WelcomeViewModel {
init(context: AppContext) {
self.context = context

context.authenticationService.$mastodonAuthenticationBoxes
AuthenticationServiceProvider.shared.$mastodonAuthenticationBoxes
.map { !$0.isEmpty }
.assign(to: &$needsShowDismissEntry)
}
Expand Down
2 changes: 1 addition & 1 deletion Mastodon/Scene/Root/MainTab/MainTabBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ extension MainTabBarController {
guard let profileTabItem = _profileTabItem else { return }
profileTabItem.accessibilityHint = L10n.Scene.AccountList.tabBarHint(account.displayNameWithFallback)

self.context.authenticationService.updateActiveUserAccountPublisher
AuthenticationServiceProvider.shared.updateActiveUserAccountPublisher
.sink { [weak self] in
self?.updateUserAccount()
}
Expand Down
6 changes: 3 additions & 3 deletions Mastodon/Supporting Files/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import MastodonUI

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

let appContext = AppContext()

var appContext: AppContext { return AppContext.shared }
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
AuthenticationServiceProvider.shared.prepareForUse()

Expand Down
4 changes: 2 additions & 2 deletions Mastodon/Supporting Files/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
AppContext.shared.statusFilterService.filterUpdatePublisher.send()

// trigger authenticated user account update
AppContext.shared.authenticationService.updateActiveUserAccountPublisher.send()
AuthenticationServiceProvider.shared.updateActiveUserAccountPublisher.send()

if let shortcutItem = savedShortCutItem {
Task {
Expand Down Expand Up @@ -208,7 +208,7 @@ extension SceneDelegate {
return false
}

let _isActive = try? await coordinator.appContext.authenticationService.activeMastodonUser(
let _isActive = try? await AuthenticationServiceProvider.shared.activeMastodonUser(
domain: authentication.domain,
userID: authentication.userID
)
Expand Down
7 changes: 2 additions & 5 deletions MastodonIntent/Handler/FollowersCountIntentHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ class FollowersCountIntentHandler: INExtension, FollowersCountIntentHandling {
func provideAccountOptionsCollection(for intent: FollowersCountIntent, searchTerm: String?) async throws -> INObjectCollection<NSString> {
guard
let searchTerm = searchTerm,
let authenticationBox = WidgetExtension.appContext
.authenticationService
.mastodonAuthenticationBoxes
.first
let authenticationBox = AuthenticationServiceProvider.shared.activeAuthentication
else {
return INObjectCollection(items: [])
}

let results = try await WidgetExtension.appContext
let results = try await AppContext.shared
.apiService
.search(query: .init(q: searchTerm), authenticationBox: authenticationBox)

Expand Down
11 changes: 4 additions & 7 deletions MastodonIntent/Handler/HashtagIntentHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
import Foundation
import Intents
import MastodonSDK
import MastodonCore

class HashtagIntentHandler: INExtension, HashtagIntentHandling {
func provideHashtagOptionsCollection(for intent: HashtagIntent, searchTerm: String?) async throws -> INObjectCollection<NSString> {

guard let authenticationBox = WidgetExtension.appContext
.authenticationService
.mastodonAuthenticationBoxes
.first
guard let authenticationBox = AuthenticationServiceProvider.shared.activeAuthentication
else {
return INObjectCollection(items: [])
}

var results: [NSString] = []

if let searchTerm, searchTerm.isEmpty == false {
let searchResults = try await WidgetExtension.appContext
.apiService
let searchResults = try await AppContext.shared.apiService
.search(query: .init(q: searchTerm, type: .hashtags), authenticationBox: authenticationBox)
.value
.hashtags
Expand All @@ -28,7 +25,7 @@ class HashtagIntentHandler: INExtension, HashtagIntentHandling {
results = searchResults

} else {
let followedTags = try await WidgetExtension.appContext.apiService.getFollowedTags(
let followedTags = try await AppContext.shared.apiService.getFollowedTags(
domain: authenticationBox.domain,
query: Mastodon.API.Account.FollowedTagsQuery(limit: nil),
authenticationBox: authenticationBox)
Expand Down
7 changes: 2 additions & 5 deletions MastodonIntent/Handler/MultiFollowersCountIntentHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ class MultiFollowersCountIntentHandler: INExtension, MultiFollowersCountIntentHa
func provideAccountsOptionsCollection(for intent: MultiFollowersCountIntent, searchTerm: String?) async throws -> INObjectCollection<NSString> {
guard
let searchTerm = searchTerm,
let authenticationBox = WidgetExtension.appContext
.authenticationService
.mastodonAuthenticationBoxes
.first
let authenticationBox = AuthenticationServiceProvider.shared.activeAuthentication
else {
return INObjectCollection(items: [])
}

let results = try await WidgetExtension.appContext
let results = try await AppContext.shared
.apiService
.search(query: .init(q: searchTerm), authenticationBox: authenticationBox)

Expand Down
32 changes: 13 additions & 19 deletions MastodonSDK/Sources/MastodonCore/AppContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import CoreDataStack
import AlamofireImage

public class AppContext: ObservableObject {
public static let shared = AppContext()

public var disposeBag = Set<AnyCancellable>()

Expand All @@ -21,7 +22,6 @@ public class AppContext: ObservableObject {
public let backgroundManagedObjectContext: NSManagedObjectContext

public let apiService: APIService
public let authenticationService: AuthenticationService
public let emojiService: EmojiService

public let publisherService: PublisherService
Expand All @@ -45,7 +45,7 @@ public class AppContext: ObservableObject {
.share()
.eraseToAnyPublisher()

public init() {
private init() {

let authProvider = AuthenticationServiceProvider.shared
let _coreDataStack = CoreDataStack()
Expand All @@ -65,45 +65,39 @@ public class AppContext: ObservableObject {
let _apiService = APIService(backgroundManagedObjectContext: _backgroundManagedObjectContext)
apiService = _apiService

let _authenticationService = AuthenticationService(
managedObjectContext: _managedObjectContext,
backgroundManagedObjectContext: _backgroundManagedObjectContext,
apiService: _apiService
)
authenticationService = _authenticationService
// let _authenticationService = AuthenticationService(
// managedObjectContext: _managedObjectContext,
// backgroundManagedObjectContext: _backgroundManagedObjectContext,
// apiService: _apiService
// )
// authenticationService = _authenticationService

emojiService = EmojiService(
apiService: apiService,
authenticationService: _authenticationService
apiService: apiService
)

publisherService = .init(apiService: _apiService)

let _notificationService = NotificationService(
apiService: _apiService,
authenticationService: _authenticationService
apiService: _apiService
)
notificationService = _notificationService

settingService = SettingService(
apiService: _apiService,
authenticationService: _authenticationService,
notificationService: _notificationService
)

instanceService = InstanceService(
apiService: _apiService,
authenticationService: _authenticationService
apiService: _apiService
)

blockDomainService = BlockDomainService(
backgroundManagedObjectContext: _backgroundManagedObjectContext,
authenticationService: _authenticationService
backgroundManagedObjectContext: _backgroundManagedObjectContext
)

statusFilterService = StatusFilterService(
apiService: _apiService,
authenticationService: _authenticationService
apiService: _apiService
)

documentStore = DocumentStore()
Expand Down
Loading

0 comments on commit 77f3c5a

Please sign in to comment.