Skip to content

Commit

Permalink
Make view models @mainactor
Browse files Browse the repository at this point in the history
  • Loading branch information
whattherestimefor committed Nov 15, 2024
1 parent c4442fe commit 35f1280
Show file tree
Hide file tree
Showing 45 changed files with 89 additions and 39 deletions.
1 change: 1 addition & 0 deletions Mastodon/Coordinator/SceneCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import MastodonAsset
import MastodonLocalization
import MBProgressHUD

@MainActor
final public class SceneCoordinator {

private var disposeBag = Set<AnyCancellable>()
Expand Down
2 changes: 1 addition & 1 deletion Mastodon/Protocol/Provider/DataSourceFacade+URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension DataSourceFacade {
url.pathComponents[1] == "web",
url.pathComponents[2] == "statuses" {
let statusID = url.pathComponents[3]
let threadViewModel = RemoteThreadViewModel(context: provider.context, authenticationBox: provider.authenticationBox, statusID: statusID)
let threadViewModel = await RemoteThreadViewModel(context: provider.context, authenticationBox: provider.authenticationBox, statusID: statusID)
_ = await provider.coordinator.present(scene: .thread(viewModel: threadViewModel), from: nil, transition: .show)
} else {
_ = await provider.coordinator.present(scene: .safari(url: url), from: nil, transition: .safariPresent(animated: true, completion: nil))
Expand Down
1 change: 1 addition & 0 deletions Mastodon/Scene/Account/AccountListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import MastodonMeta
import MastodonCore
import MastodonUI

@MainActor
final class AccountListViewModel: NSObject {

var disposeBag = Set<AnyCancellable>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ extension HomeTimelineViewModel.LoadLatestState {

override func didEnter(from previousState: GKState?) {
guard let viewModel else { return }
guard let diffableDataSource = viewModel.diffableDataSource else {
assertionFailure()
return
}

OperationQueue.main.addOperation {
Task { @MainActor in
guard let diffableDataSource = viewModel.diffableDataSource else {
assertionFailure()
return
}
viewModel.dataController.records = []
var snapshot = NSDiffableDataSourceSnapshot<StatusSection, StatusItem>()
snapshot.appendSections([.main])
Expand All @@ -104,10 +104,10 @@ extension HomeTimelineViewModel.LoadLatestState {
super.didEnter(from: previousState)

guard let viewModel else { return }

viewModel.timelineIsEmpty.send(nil)

Task { @MainActor in
viewModel.timelineIsEmpty.send(nil)

let latestFeedRecords = viewModel.dataController.records

let latestStatusIDs: [Status.ID] = latestFeedRecords.compactMap { record in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extension HomeTimelineViewModel.LoadOldestState {
}
}

@MainActor
class Loading: HomeTimelineViewModel.LoadOldestState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
return stateClass == Fail.self || stateClass == Idle.self || stateClass == NoMore.self
Expand Down Expand Up @@ -125,11 +126,11 @@ extension HomeTimelineViewModel.LoadOldestState {

override func didEnter(from previousState: GKState?) {
guard let viewModel = viewModel else { return }
guard let diffableDataSource = viewModel.diffableDataSource else {
assertionFailure()
return
}
DispatchQueue.main.async {
guard let diffableDataSource = viewModel.diffableDataSource else {
assertionFailure()
return
}
var snapshot = diffableDataSource.snapshot()
snapshot.deleteItems([.bottomLoader])
diffableDataSource.apply(snapshot)
Expand Down
1 change: 1 addition & 0 deletions Mastodon/Scene/HomeTimeline/HomeTimelineViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import MastodonCore
import MastodonUI
import MastodonSDK

@MainActor
final class HomeTimelineViewModel: NSObject {
var disposeBag = Set<AnyCancellable>()
var observations = Set<NSKeyValueObservation>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ extension NotificationTimelineViewModel.LoadOldestState {

override func didEnter(from previousState: GKState?) {
guard let viewModel = viewModel else { return }
guard let diffableDataSource = viewModel.diffableDataSource else {
assertionFailure()
return
}
DispatchQueue.main.async {
guard let diffableDataSource = viewModel.diffableDataSource else {
assertionFailure()
return
}
var snapshot = diffableDataSource.snapshot()
snapshot.deleteItems([.bottomLoader])
diffableDataSource.apply(snapshot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import MastodonSDK
import MastodonCore
import MastodonLocalization

@MainActor
final class NotificationTimelineViewModel {

var disposeBag = Set<AnyCancellable>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ protocol MastodonLoginViewModelDelegate: AnyObject {
func serversUpdated(_ viewModel: MastodonLoginViewModel)
}

@MainActor
class MastodonLoginViewModel {

private var serverList: [Mastodon.Entity.Server] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extension MastodonPickServerViewModel.LoadIndexedServerState {
}
}

@MainActor
class Loading: MastodonPickServerViewModel.LoadIndexedServerState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
return stateClass == Fail.self || stateClass == Idle.self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import MastodonCore
import MastodonUI
import MastodonLocalization

@MainActor
class MastodonPickServerViewModel: NSObject {

enum EmptyStateViewState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import MastodonAsset
import MastodonCore
import MastodonLocalization

@MainActor
final class MastodonRegisterViewModel: ObservableObject {
var disposeBag = Set<AnyCancellable>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Combine
import MastodonSDK
import MastodonCore

@MainActor
final class AuthenticationViewModel {

var disposeBag = Set<AnyCancellable>()
Expand Down
1 change: 1 addition & 0 deletions Mastodon/Scene/Onboarding/Welcome/WelcomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Combine
import MastodonCore
import MastodonSDK

@MainActor
final class WelcomeViewModel {

var disposeBag = Set<AnyCancellable>()
Expand Down
1 change: 1 addition & 0 deletions Mastodon/Scene/Profile/ProfileViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import MastodonLocalization
import MastodonUI

// please override this base class
@MainActor
class ProfileViewModel: NSObject {

typealias UserID = String
Expand Down
1 change: 1 addition & 0 deletions Mastodon/Scene/Root/Sidebar/SidebarViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import MastodonAsset
import MastodonCore
import MastodonLocalization

@MainActor
final class SidebarViewModel {
var disposeBag = Set<AnyCancellable>()

Expand Down
1 change: 1 addition & 0 deletions Mastodon/Scene/Settings/SettingsCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ protocol SettingsCoordinatorDelegate: AnyObject {
func openProfileSettingsURL(_ settingsCoordinator: SettingsCoordinator)
}

@MainActor
class SettingsCoordinator: NSObject, Coordinator {

let navigationController: UINavigationController
Expand Down
8 changes: 4 additions & 4 deletions Mastodon/Scene/Thread/ThreadViewModel+LoadThreadState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ extension ThreadViewModel.LoadThreadState {

guard let viewModel, let stateMachine else { return }

guard let threadContext = viewModel.threadContext else {
stateMachine.enter(Fail.self)
return
}

Task { @MainActor in
guard let threadContext = viewModel.threadContext else {
stateMachine.enter(Fail.self)
return
}
do {
let response = try await APIService.shared.statusContext(
statusID: threadContext.statusID,
Expand Down
1 change: 1 addition & 0 deletions Mastodon/Scene/Thread/ThreadViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import MastodonAsset
import MastodonCore
import MastodonLocalization

@MainActor
class ThreadViewModel {

var disposeBag = Set<AnyCancellable>()
Expand Down
1 change: 1 addition & 0 deletions MastodonIntent/Handler/FollowersCountIntentHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import MastodonCore
import MastodonSDK
import MastodonLocalization

@MainActor
class FollowersCountIntentHandler: INExtension, FollowersCountIntentHandling {
func resolveShowChart(for intent: FollowersCountIntent) async -> INBooleanResolutionResult {
return .success(with: intent.showChart?.boolValue ?? false)
Expand Down
2 changes: 1 addition & 1 deletion MastodonIntent/Handler/HashtagIntentHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Foundation
import Intents
import MastodonSDK
import MastodonCore

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import MastodonCore
import MastodonSDK
import MastodonLocalization

@MainActor
class MultiFollowersCountIntentHandler: INExtension, MultiFollowersCountIntentHandling {
func provideAccountsOptionsCollection(for intent: MultiFollowersCountIntent, searchTerm: String?) async throws -> INObjectCollection<NSString> {
guard
Expand Down
5 changes: 3 additions & 2 deletions MastodonIntent/Handler/SendPostIntentHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import CoreDataStack
import MastodonSDK
import MastodonCore

@MainActor
final class SendPostIntentHandler: NSObject {

var disposeBag = Set<AnyCancellable>()
Expand Down Expand Up @@ -118,7 +119,7 @@ extension SendPostIntentHandler: SendPostIntentHandling {
} // end func

// content
func resolveContent(for intent: SendPostIntent, with completion: @escaping (INStringResolutionResult) -> Void) {
nonisolated func resolveContent(for intent: SendPostIntent, with completion: @escaping (INStringResolutionResult) -> Void) {
guard let content = intent.content, !content.isEmpty else {
completion(.needsValue())
return
Expand Down Expand Up @@ -146,7 +147,7 @@ extension SendPostIntentHandler: SendPostIntentHandling {
}

// visibility
func resolveVisibility(for intent: SendPostIntent, with completion: @escaping (PostVisibilityResolutionResult) -> Void) {
nonisolated func resolveVisibility(for intent: SendPostIntent, with completion: @escaping (PostVisibilityResolutionResult) -> Void) {
completion(.success(with: intent.visibility))
}

Expand Down
1 change: 1 addition & 0 deletions MastodonIntent/Model/Account+Fetch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ extension Account {
}

extension Array where Element == Account {
@MainActor
func mastodonAuthentication() throws -> [MastodonAuthentication] {
let identifiers = self
.compactMap { $0.identifier }
Expand Down
1 change: 1 addition & 0 deletions MastodonSDK/Sources/MastodonCore/AppContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class PersistenceManager {
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import KeychainAccess
import MastodonCommon
import os.log

@MainActor
public class AuthenticationServiceProvider: ObservableObject {
private let logger = Logger(subsystem: "AuthenticationServiceProvider", category: "Authentication")

Expand All @@ -21,6 +22,9 @@ public class AuthenticationServiceProvider: ObservableObject {
public let updateActiveUserAccountPublisher = PassthroughSubject<Void, Never>()

private init() {
prepareForUse()
authentications = authenticationSortedByActivation()

$mastodonAuthenticationBoxes
.throttle(for: 3, scheduler: DispatchQueue.main, latest: true)
.sink { [weak self] boxes in
Expand Down Expand Up @@ -52,8 +56,6 @@ public class AuthenticationServiceProvider: ObservableObject {
in: PersistenceManager.shared.managedObjectContext
)
}
await prepareForUse()
authentications = authenticationSortedByActivation()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import MastodonSDK
import AlamofireImage
// import AlamofireNetworkActivityIndicator

@MainActor
public final class APIService {

@MainActor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import MastodonSDK
import OSLog
import UIKit

@MainActor
public final class BlockDomainService {

// input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ extension EmojiService.CustomEmojiViewModel.LoadState {
return stateClass == Fail.self || stateClass == Finish.self
}

@MainActor
override func didEnter(from previousState: GKState?) {
super.didEnter(from: previousState)
guard let viewModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation
import Combine
import MastodonSDK

@MainActor
public final class EmojiService {
public static let shared = { EmojiService() }()
let workingQueue = DispatchQueue(label: "org.joinmastodon.app.EmojiService.working-queue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import CoreData
import CoreDataStack
import MastodonSDK

@MainActor
public final class InstanceService {

var disposeBag = Set<AnyCancellable>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extension MastodonAttachmentService {
self.service = service
}

@MainActor
public override func didEnter(from previousState: GKState?) {
service?.uploadStateMachineSubject.send(self)
}
Expand Down Expand Up @@ -51,6 +52,7 @@ extension MastodonAttachmentService.UploadState {
|| stateClass == Processing.self
}

@MainActor
public override func didEnter(from previousState: GKState?) {
super.didEnter(from: previousState)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import MastodonSDK
import MastodonCommon
import MastodonLocalization

@MainActor
public final class NotificationService {

public static let shared = { NotificationService() }()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Photos
import Alamofire
import AlamofireImage

@MainActor
public final class PhotoLibraryService: NSObject {
public static let shared = { PhotoLibraryService() }()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import UIKit
import AlamofireImage

@MainActor
public final class PlaceholderImageCacheService {

let cache = NSCache<Key, UIImage>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import UIKit
import Combine

@MainActor
public final class PublisherService {

public static let shared = { PublisherService() }()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation
import Combine

@MainActor
public protocol StatusPublisher: ProgressReporting {
var state: Published<StatusPublisherState>.Publisher { get }
var reactor: StatusPublisherReactor? { get set }
Expand Down
Loading

0 comments on commit 35f1280

Please sign in to comment.