mirror of
https://github.com/element-hq/element-x-ios.git
synced 2025-03-10 21:39:12 +00:00
vector-im/element-x-ios/issues/53 - Adopt MainActors, dispatch heavy operations do detached tasks and ensure combine publisher call back on the right queue
This commit is contained in:
parent
dde7786152
commit
f5890b68a2
@ -19,6 +19,7 @@ import UIKit
|
||||
/// Protocol describing a [Coordinator](http://khanlou.com/2015/10/coordinators-redux/).
|
||||
/// Coordinators are the objects which control the navigation flow of the application.
|
||||
/// It helps to isolate and reuse view controllers and pass dependencies down the navigation hierarchy.
|
||||
@MainActor
|
||||
protocol Coordinator: AnyObject {
|
||||
|
||||
/// Starts job of the coordinator.
|
||||
|
@ -17,6 +17,7 @@
|
||||
import UIKit
|
||||
|
||||
/// `NavigationRouterStoreProtocol` describes a structure that enables to get a NavigationRouter from a UINavigationController instance.
|
||||
@MainActor
|
||||
protocol NavigationRouterStoreProtocol {
|
||||
|
||||
/// Gets the existing navigation router for the supplied controller, creating a new one if it doesn't yet exist.
|
||||
|
@ -18,6 +18,7 @@ import UIKit
|
||||
|
||||
/// Protocol describing a router that wraps a UINavigationController and add convenient completion handlers. Completions are called when a Presentable is removed.
|
||||
/// Routers are used to be passed between coordinators. They handles only `physical` navigation.
|
||||
@MainActor
|
||||
protocol NavigationRouterType: AnyObject, Presentable {
|
||||
|
||||
/// Present modally a view controller on the navigation controller
|
||||
|
@ -17,6 +17,7 @@
|
||||
import UIKit
|
||||
|
||||
/// Protocol used to pass UIViewControllers to routers
|
||||
@MainActor
|
||||
protocol Presentable {
|
||||
func toPresentable() -> UIViewController
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ import UIKit
|
||||
|
||||
/// Protocol describing a router that wraps the root navigation of the application.
|
||||
/// Routers are used to be passed between coordinators. They handles only `physical` navigation.
|
||||
@MainActor
|
||||
protocol RootRouterType: AnyObject {
|
||||
|
||||
/// Update the root view controller
|
||||
|
@ -30,6 +30,7 @@ import Combine
|
||||
/// It provides a nice layer of consistency and also safety. As we are not passing the `ViewModel` to the view directly, shortcuts/hacks
|
||||
/// can't be made into the `ViewModel`.
|
||||
@dynamicMemberLookup
|
||||
@MainActor
|
||||
class ViewModelContext<ViewState: BindableState, ViewAction>: ObservableObject {
|
||||
// MARK: - Properties
|
||||
|
||||
@ -70,6 +71,7 @@ class ViewModelContext<ViewState: BindableState, ViewAction>: ObservableObject {
|
||||
/// a specific portion of state that can be safely bound to.
|
||||
/// If we decide to add more features to our state management (like doing state processing off the main thread)
|
||||
/// we can do it in this centralised place.
|
||||
@MainActor
|
||||
class StateStoreViewModel<State: BindableState, ViewAction> {
|
||||
|
||||
typealias Context = ViewModelContext<State, ViewAction>
|
||||
|
@ -85,16 +85,12 @@ final class HomeScreenCoordinator: Coordinator, Presentable {
|
||||
Task {
|
||||
if case let .success(userAvatarURL) = await parameters.userSession.loadUserAvatarURL() {
|
||||
if case let .success(avatar) = await parameters.mediaProvider.loadImageFromURL(userAvatarURL) {
|
||||
await MainActor.run {
|
||||
self.viewModel.updateWithUserAvatar(avatar)
|
||||
}
|
||||
self.viewModel.updateWithUserAvatar(avatar)
|
||||
}
|
||||
}
|
||||
|
||||
if case let .success(userDisplayName) = await parameters.userSession.loadUserDisplayName() {
|
||||
await MainActor.run {
|
||||
self.viewModel.updateWithUserDisplayName(userDisplayName)
|
||||
}
|
||||
self.viewModel.updateWithUserDisplayName(userDisplayName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -47,11 +47,11 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
|
||||
override func process(viewAction: HomeScreenViewAction) {
|
||||
switch viewAction {
|
||||
case .logout:
|
||||
self.completion?(.logout)
|
||||
completion?(.logout)
|
||||
case .loadRoomData(let roomIdentifier):
|
||||
self.loadRoomDataForIdentifier(roomIdentifier)
|
||||
loadRoomDataForIdentifier(roomIdentifier)
|
||||
case .selectRoom(let roomIdentifier):
|
||||
self.completion?(.selectRoom(roomIdentifier: roomIdentifier))
|
||||
completion?(.selectRoom(roomIdentifier: roomIdentifier))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
@MainActor
|
||||
protocol HomeScreenViewModelProtocol {
|
||||
var completion: ((HomeScreenViewModelResult) -> Void)? { get set }
|
||||
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
@MainActor
|
||||
protocol LoginScreenViewModelProtocol {
|
||||
var completion: ((LoginScreenViewModelResult) -> Void)? { get set }
|
||||
var context: LoginScreenViewModelType.Context { get }
|
||||
|
@ -39,7 +39,7 @@ struct RoomScreenViewState: BindableState {
|
||||
var isBackPaginating = false
|
||||
var bindings: RoomScreenViewStateBindings
|
||||
|
||||
var contextMenuBuilder: ((_ itemId: String) -> TimelineItemContextMenu)?
|
||||
var contextMenuBuilder: (@MainActor (_ itemId: String) -> TimelineItemContextMenu)?
|
||||
|
||||
var sendButtonDisabled: Bool {
|
||||
bindings.composerText.count == 0
|
||||
|
@ -67,15 +67,11 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
|
||||
Task {
|
||||
switch viewAction {
|
||||
case .loadPreviousPage:
|
||||
await MainActor.run {
|
||||
state.isBackPaginating = true
|
||||
}
|
||||
state.isBackPaginating = true
|
||||
|
||||
switch await timelineController.paginateBackwards(Constants.backPaginationPageSize) {
|
||||
default:
|
||||
await MainActor.run {
|
||||
state.isBackPaginating = false
|
||||
}
|
||||
state.isBackPaginating = false
|
||||
}
|
||||
|
||||
case .itemAppeared(let id):
|
||||
@ -90,10 +86,7 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
|
||||
}
|
||||
|
||||
await timelineController.sendMessage(state.bindings.composerText)
|
||||
|
||||
await MainActor.run {
|
||||
state.bindings.composerText = ""
|
||||
}
|
||||
state.bindings.composerText = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
@MainActor
|
||||
protocol RoomScreenViewModelProtocol {
|
||||
var context: RoomScreenViewModelType.Context { get }
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ enum AuthenticationCoordinatorError: Error {
|
||||
case failedSettingUpSession
|
||||
}
|
||||
|
||||
@MainActor
|
||||
protocol AuthenticationCoordinatorDelegate: AnyObject {
|
||||
|
||||
func authenticationCoordinatorDidStartLoading(_ authenticationCoordinator: AuthenticationCoordinator)
|
||||
|
@ -27,12 +27,13 @@ private class WeakUserSessionWrapper: ClientDelegate {
|
||||
self.userSession = userSession
|
||||
}
|
||||
|
||||
func didReceiveSyncUpdate() {
|
||||
@MainActor func didReceiveSyncUpdate() {
|
||||
self.userSession?.didReceiveSyncUpdate()
|
||||
}
|
||||
}
|
||||
|
||||
class UserSession: ClientDelegate {
|
||||
@MainActor
|
||||
class UserSession {
|
||||
|
||||
private let client: Client
|
||||
|
||||
@ -74,26 +75,26 @@ class UserSession: ClientDelegate {
|
||||
}
|
||||
|
||||
func loadUserDisplayName() async -> Result<String, UserSessionError> {
|
||||
await withCheckedContinuation { continuation in
|
||||
await Task.detached { () -> Result<String, UserSessionError> in
|
||||
do {
|
||||
let displayName = try self.client.displayName()
|
||||
continuation.resume(returning: .success(displayName))
|
||||
return .success(displayName)
|
||||
} catch {
|
||||
continuation.resume(returning: .failure(.failedRetrievingDisplayName))
|
||||
return .failure(.failedRetrievingDisplayName)
|
||||
}
|
||||
|
||||
}
|
||||
}.value
|
||||
}
|
||||
|
||||
func loadUserAvatarURL() async -> Result<String, UserSessionError> {
|
||||
await withCheckedContinuation { continuation in
|
||||
await Task.detached { () -> Result<String, UserSessionError> in
|
||||
do {
|
||||
let avatarURL = try self.client.avatarUrl()
|
||||
continuation.resume(returning: .success(avatarURL))
|
||||
return .success(avatarURL)
|
||||
} catch {
|
||||
continuation.resume(returning: .failure(.failedRetrievingDisplayName))
|
||||
return .failure(.failedRetrievingDisplayName)
|
||||
}
|
||||
}
|
||||
}.value
|
||||
}
|
||||
|
||||
// MARK: ClientDelegate
|
||||
@ -101,8 +102,8 @@ class UserSession: ClientDelegate {
|
||||
func didReceiveSyncUpdate() {
|
||||
Benchmark.logElapsedDurationForIdentifier("ClientSync", message: "Received sync update")
|
||||
|
||||
Task {
|
||||
await updateRooms()
|
||||
Task.detached {
|
||||
await self.updateRooms()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,33 +46,33 @@ struct MediaProvider: MediaProviderProtocol {
|
||||
return .success(image)
|
||||
}
|
||||
|
||||
return await withCheckedContinuation { continuation in
|
||||
let cachedImageLoadResult = await withCheckedContinuation({ continuation in
|
||||
imageCache.retrieveImage(forKey: source.underlyingSource.url()) { result in
|
||||
if case let .success(cacheResult) = result,
|
||||
let image = cacheResult.image {
|
||||
continuation.resume(returning: .success(image))
|
||||
return
|
||||
continuation.resume(returning: result)
|
||||
}
|
||||
})
|
||||
|
||||
if case let .success(cacheResult) = cachedImageLoadResult,
|
||||
let image = cacheResult.image {
|
||||
return .success(image)
|
||||
}
|
||||
|
||||
return await Task.detached { () -> Result<UIImage, MediaProviderError> in
|
||||
do {
|
||||
let imageData = try client.getMediaContent(source: source.underlyingSource)
|
||||
|
||||
guard let image = UIImage(data: Data(bytes: imageData, count: imageData.count)) else {
|
||||
MXLog.error("Invalid image data")
|
||||
return .failure(.invalidImageData)
|
||||
}
|
||||
|
||||
processingQueue.async {
|
||||
do {
|
||||
let imageData = try client.getMediaContent(source: source.underlyingSource)
|
||||
|
||||
guard let image = UIImage(data: Data(bytes: imageData, count: imageData.count)) else {
|
||||
MXLog.error("Invalid image data")
|
||||
continuation.resume(returning: .failure(.invalidImageData))
|
||||
return
|
||||
}
|
||||
|
||||
imageCache.store(image, forKey: source.underlyingSource.url())
|
||||
|
||||
continuation.resume(returning: .success(image))
|
||||
} catch {
|
||||
MXLog.error("Failed retrieving image with error: \(error)")
|
||||
continuation.resume(returning: .failure(.failedRetrievingImage))
|
||||
}
|
||||
}
|
||||
imageCache.store(image, forKey: source.underlyingSource.url())
|
||||
|
||||
return .success(image)
|
||||
} catch {
|
||||
MXLog.error("Failed retrieving image with error: \(error)")
|
||||
return .failure(.failedRetrievingImage)
|
||||
}
|
||||
}
|
||||
}.value
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ enum MediaProviderError: Error {
|
||||
case invalidImageData
|
||||
}
|
||||
|
||||
@MainActor
|
||||
protocol MediaProviderProtocol {
|
||||
func imageFromSource(_ source: MediaSource?) -> UIImage?
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
@MainActor
|
||||
class MemberDetailProviderManager {
|
||||
|
||||
private var memberDetailProviders: [String: MemberDetailProviderProtocol] = [:]
|
||||
|
@ -14,6 +14,7 @@ enum MemberDetailProviderError: Error {
|
||||
case failedRetrievingUserDisplayName
|
||||
}
|
||||
|
||||
@MainActor
|
||||
protocol MemberDetailProviderProtocol {
|
||||
func avatarURLForUserId(_ userId: String) -> String?
|
||||
func loadAvatarURLForUserId(_ userId: String) async -> Result<String?, MemberDetailProviderError>
|
||||
|
@ -44,9 +44,7 @@ class RoomProxy: RoomProxyProtocol {
|
||||
|
||||
room.setDelegate(delegate: WeakRoomProxyWrapper(roomProxy: self))
|
||||
|
||||
Task {
|
||||
backwardStream = room.startLiveEventListener()
|
||||
}
|
||||
backwardStream = room.startLiveEventListener()
|
||||
}
|
||||
|
||||
var id: String {
|
||||
@ -86,78 +84,76 @@ class RoomProxy: RoomProxyProtocol {
|
||||
}
|
||||
|
||||
func loadAvatarURLForUserId(_ userId: String) async -> Result<String?, RoomProxyError> {
|
||||
await withCheckedContinuation({ continuation in
|
||||
await Task.detached { () -> Result<String?, RoomProxyError> in
|
||||
do {
|
||||
let avatarURL = try self.room.memberAvatarUrl(userId: userId)
|
||||
continuation.resume(returning: .success(avatarURL))
|
||||
return .success(avatarURL)
|
||||
} catch {
|
||||
continuation.resume(returning: .failure(.failedRetrievingMemberAvatarURL))
|
||||
return .failure(.failedRetrievingMemberAvatarURL)
|
||||
}
|
||||
})
|
||||
}.value
|
||||
}
|
||||
|
||||
func loadDisplayNameForUserId(_ userId: String) async -> Result<String?, RoomProxyError> {
|
||||
await withCheckedContinuation({ continuation in
|
||||
await Task.detached { () -> Result<String?, RoomProxyError> in
|
||||
do {
|
||||
let displayName = try self.room.memberDisplayName(userId: userId)
|
||||
continuation.resume(returning: .success(displayName))
|
||||
return .success(displayName)
|
||||
} catch {
|
||||
continuation.resume(returning: .failure(.failedRetrievingMemberDisplayName))
|
||||
return .failure(.failedRetrievingMemberDisplayName)
|
||||
}
|
||||
})
|
||||
}.value
|
||||
}
|
||||
|
||||
func loadDisplayName() async -> Result<String, RoomProxyError> {
|
||||
await withCheckedContinuation({ continuation in
|
||||
if let displayName = displayName {
|
||||
continuation.resume(returning: .success(displayName))
|
||||
return
|
||||
await Task.detached { () -> Result<String, RoomProxyError> in
|
||||
if let displayName = self.displayName {
|
||||
return .success(displayName)
|
||||
}
|
||||
|
||||
do {
|
||||
let displayName = try self.room.displayName()
|
||||
self.displayName = displayName
|
||||
|
||||
continuation.resume(returning: .success(displayName))
|
||||
return .success(displayName)
|
||||
} catch {
|
||||
continuation.resume(returning: .failure(.failedRetrievingDisplayName))
|
||||
return .failure(.failedRetrievingDisplayName)
|
||||
}
|
||||
})
|
||||
}.value
|
||||
}
|
||||
|
||||
func paginateBackwards(count: UInt) async -> Result<Void, RoomProxyError> {
|
||||
await withCheckedContinuation { continuation in
|
||||
await Task.detached { () -> Result<Void, RoomProxyError> in
|
||||
guard let backwardStream = self.backwardStream else {
|
||||
continuation.resume(returning: .failure(.backwardStreamNotAvailable))
|
||||
return
|
||||
return .failure(RoomProxyError.backwardStreamNotAvailable)
|
||||
}
|
||||
|
||||
|
||||
Benchmark.startTrackingForIdentifier("BackPagination \(self.id)", message: "Backpaginating \(count) message(s) in room \(self.id)")
|
||||
let sdkMessages = backwardStream.paginateBackwards(count: UInt64(count))
|
||||
Benchmark.endTrackingForIdentifier("BackPagination \(self.id)", message: "Finished backpaginating \(count) message(s) in room \(self.id)")
|
||||
|
||||
|
||||
let messages = sdkMessages.map { message in
|
||||
self.messageFactory.buildRoomMessageFrom(message)
|
||||
}.reversed()
|
||||
|
||||
|
||||
self.messages.insert(contentsOf: messages, at: 0)
|
||||
|
||||
continuation.resume(returning: .success(()))
|
||||
}
|
||||
return .success(())
|
||||
}.value
|
||||
}
|
||||
|
||||
func sendMessage(_ message: String) async -> Result<Void, RoomProxyError> {
|
||||
let messageContent = messageEventContentFromMarkdown(md: message)
|
||||
let transactionId = genTransactionId()
|
||||
|
||||
return await withCheckedContinuation { continuation in
|
||||
return await Task(priority: .high) { () -> Result<Void, RoomProxyError> in
|
||||
do {
|
||||
try self.room.send(msg: messageContent, txnId: transactionId)
|
||||
continuation.resume(returning: .success(()))
|
||||
return .success(())
|
||||
} catch {
|
||||
continuation.resume(returning: .failure(.failedSendingMessage))
|
||||
return .failure(.failedSendingMessage)
|
||||
}
|
||||
}
|
||||
}.value
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
@MainActor
|
||||
protocol EventBriefFactoryProtocol {
|
||||
func eventBriefForMessage(_ message: RoomMessageProtocol?) async -> EventBrief?
|
||||
}
|
||||
|
@ -98,13 +98,13 @@ class RoomSummary: RoomSummaryProtocol {
|
||||
}
|
||||
|
||||
await withTaskGroup(of: Void.self) { group in
|
||||
group.addTask {
|
||||
group.addTask(priority: .medium) {
|
||||
await self.loadDisplayName()
|
||||
}
|
||||
group.addTask {
|
||||
group.addTask(priority: .medium) {
|
||||
await self.loadAvatar()
|
||||
}
|
||||
group.addTask {
|
||||
group.addTask(priority: .medium) {
|
||||
await self.loadLastMessage()
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ enum RoomSummaryCallback {
|
||||
case updatedData
|
||||
}
|
||||
|
||||
@MainActor
|
||||
protocol RoomSummaryProtocol {
|
||||
var id: String { get }
|
||||
var name: String? { get }
|
||||
|
@ -31,7 +31,10 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
|
||||
self.mediaProvider = mediaProvider
|
||||
self.memberDetailProvider = memberDetailProvider
|
||||
|
||||
self.timelineProvider.callbacks.sink { [weak self] callback in
|
||||
self.timelineProvider
|
||||
.callbacks
|
||||
.receive(on: DispatchQueue.main)
|
||||
.sink { [weak self] callback in
|
||||
guard let self = self else { return }
|
||||
|
||||
switch callback {
|
||||
|
@ -18,6 +18,7 @@ enum RoomTimelineControllerError: Error {
|
||||
case generic
|
||||
}
|
||||
|
||||
@MainActor
|
||||
protocol RoomTimelineControllerProtocol {
|
||||
var timelineItems: [RoomTimelineItemProtocol] { get }
|
||||
var callbacks: PassthroughSubject<RoomTimelineControllerCallback, Never> { get }
|
||||
|
@ -18,6 +18,7 @@ enum RoomTimelineProviderError: Error {
|
||||
case generic
|
||||
}
|
||||
|
||||
@MainActor
|
||||
protocol RoomTimelineProviderProtocol {
|
||||
var callbacks: PassthroughSubject<RoomTimelineProviderCallback, Never> { get }
|
||||
|
||||
|
@ -9,6 +9,7 @@
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
@MainActor
|
||||
struct RoomTimelineItemFactory {
|
||||
private let mediaProvider: MediaProviderProtocol
|
||||
private let memberDetailProvider: MemberDetailProviderProtocol
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
@MainActor
|
||||
struct RoomTimelineViewFactory {
|
||||
func buildTimelineViewFor(_ timelineItem: RoomTimelineItemProtocol) -> RoomTimelineViewProvider {
|
||||
switch timelineItem {
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
@MainActor
|
||||
protocol TemplateSimpleScreenViewModelProtocol {
|
||||
var completion: ((TemplateSimpleScreenViewModelResult) -> Void)? { get set }
|
||||
var context: TemplateSimpleScreenViewModelType.Context { get }
|
||||
|
Loading…
x
Reference in New Issue
Block a user