Skip to content

Commit 8edb22c

Browse files
authored
Use MainActor in StreamChat, Images, Fonts, Appearance, Utils (#984)
1 parent 4e4279c commit 8edb22c

39 files changed

+167
-135
lines changed

Sources/StreamChatSwiftUI/Appearance.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import SwiftUI
66

77
/// An object containing visual configuration for the whole application.
8-
public class Appearance {
8+
@MainActor public class Appearance {
99
public var colors: ColorPalette
1010
public var images: Images
1111
public var fonts: Fonts
@@ -21,21 +21,20 @@ public class Appearance {
2121
}
2222

2323
/// Provider for custom localization which is dependent on App Bundle.
24-
public nonisolated(unsafe)
25-
static var localizationProvider: @Sendable (_ key: String, _ table: String) -> String = { key, table in
24+
public static var localizationProvider: @Sendable (_ key: String, _ table: String) -> String = { key, table in
2625
Bundle.streamChatUI.localizedString(forKey: key, value: nil, table: table)
2726
}
2827
}
2928

3029
// MARK: - Appearance + Default
3130

3231
public extension Appearance {
33-
nonisolated(unsafe) static var `default`: Appearance = .init()
32+
static let `default`: Appearance = .init()
3433
}
3534

3635
/// Provides the default value of the `Appearance` class.
3736
public struct AppearanceKey: EnvironmentKey {
38-
public static var defaultValue: Appearance { Appearance() }
37+
public static var defaultValue: Appearance { StreamConcurrency.onMain { Appearance() } }
3938
}
4039

4140
extension EnvironmentValues {

Sources/StreamChatSwiftUI/ChatChannel/ChannelControllerFactory.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import StreamChat
66
import SwiftUI
77

88
/// Factory for creating channel controllers.
9-
class ChannelControllerFactory {
9+
@MainActor class ChannelControllerFactory {
1010
@Injected(\.chatClient) var chatClient
1111

1212
var currentChannelController: ChatChannelController?

Sources/StreamChatSwiftUI/ChatChannel/ChannelInfo/MediaAttachmentsViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public struct MediaItem: Identifiable {
128128
self.imageAttachment = imageAttachment
129129
}
130130

131-
public var mediaAttachment: MediaAttachment? {
131+
@MainActor public var mediaAttachment: MediaAttachment? {
132132
if let videoAttachment {
133133
return MediaAttachment(url: videoAttachment.videoURL, type: .video)
134134
} else if let imageAttachment {

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,11 +814,11 @@ import SwiftUI
814814
}
815815

816816
extension ChatMessage: Identifiable {
817-
public var scrollMessageId: String {
817+
@MainActor public var scrollMessageId: String {
818818
messageId
819819
}
820820

821-
var messageId: String {
821+
@MainActor var messageId: String {
822822
InjectedValues[\.utils].messageIdBuilder.makeMessageId(for: self)
823823
}
824824

Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ struct FileAddedAsset: Sendable {
938938
}
939939

940940
// The converter responsible to map attachments to assets and vice versa.
941-
class MessageAttachmentsConverter {
941+
@MainActor class MessageAttachmentsConverter {
942942
@Injected(\.utils) var utils
943943

944944
/// Converts the added assets to payloads.

Sources/StreamChatSwiftUI/ChatChannel/Composer/PhotoAssetsUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import StreamChat
77
import SwiftUI
88

99
/// Helper class that loads assets from the photo library.
10-
public class PhotoAssetLoader: NSObject, ObservableObject {
10+
@MainActor public class PhotoAssetLoader: NSObject, ObservableObject {
1111
@Injected(\.chatClient) private var chatClient
1212

1313
@Published var loadedImages = [String: UIImage]()

Sources/StreamChatSwiftUI/ChatChannel/MessageList/ImageAttachmentView.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,6 @@ extension ChatMessage {
423423
}
424424

425425
public struct MediaAttachment: Identifiable, Equatable {
426-
@Injected(\.utils) var utils
427-
428426
let url: URL
429427
let type: MediaAttachmentType
430428
var uploadingState: AttachmentUploadingState?
@@ -433,11 +431,12 @@ public struct MediaAttachment: Identifiable, Equatable {
433431
url.absoluteString
434432
}
435433

436-
func generateThumbnail(
434+
@MainActor func generateThumbnail(
437435
resize: Bool,
438436
preferredSize: CGSize,
439437
completion: @escaping @MainActor (Result<UIImage, Error>) -> Void
440438
) {
439+
let utils = InjectedValues[\.utils]
441440
if type == .image {
442441
utils.imageLoader.loadImage(
443442
url: url,

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageBubble.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ extension ChatMessage {
218218
/// - colors: The color pallete.
219219
/// - injectedBackgroundColor: If you need a custom background color injected.
220220
/// - Returns: The background colors (can be many for gradients) for the message cell.
221-
public func bubbleBackground(colors: ColorPalette, injectedBackgroundColor: UIColor? = nil) -> [Color] {
222-
if let injectedBackgroundColor = injectedBackgroundColor {
221+
@MainActor public func bubbleBackground(colors: ColorPalette, injectedBackgroundColor: UIColor? = nil) -> [Color] {
222+
if let injectedBackgroundColor {
223223
return [Color(injectedBackgroundColor)]
224224
}
225225
var colors = colors

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListConfig.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public struct MessageDisplayOptions {
153153
public let shouldAnimateReactions: Bool
154154
public let reactionsPlacement: ReactionsPlacement
155155
public let showOriginalTranslatedButton: Bool
156-
public let messageLinkDisplayResolver: (ChatMessage) -> [NSAttributedString.Key: Any]
156+
public let messageLinkDisplayResolver: @MainActor (ChatMessage) -> [NSAttributedString.Key: Any]
157157
public let spacerWidth: (CGFloat) -> CGFloat
158158
public let reactionsTopPadding: (ChatMessage) -> CGFloat
159159
public let dateSeparator: (ChatMessage, ChatMessage) -> Date?
@@ -173,7 +173,7 @@ public struct MessageDisplayOptions {
173173
shouldAnimateReactions: Bool = true,
174174
reactionsPlacement: ReactionsPlacement = .top,
175175
showOriginalTranslatedButton: Bool = false,
176-
messageLinkDisplayResolver: @escaping (ChatMessage) -> [NSAttributedString.Key: Any] = MessageDisplayOptions
176+
messageLinkDisplayResolver: @escaping @MainActor (ChatMessage) -> [NSAttributedString.Key: Any] = MessageDisplayOptions
177177
.defaultLinkDisplay,
178178
spacerWidth: @escaping (CGFloat) -> CGFloat = MessageDisplayOptions.defaultSpacerWidth,
179179
reactionsTopPadding: @escaping (ChatMessage) -> CGFloat = MessageDisplayOptions.defaultReactionsTopPadding,
@@ -216,7 +216,7 @@ public struct MessageDisplayOptions {
216216
}
217217
}
218218

219-
public static var defaultLinkDisplay: (ChatMessage) -> [NSAttributedString.Key: Any] {
219+
public static var defaultLinkDisplay: @MainActor (ChatMessage) -> [NSAttributedString.Key: Any] {
220220
{ _ in
221221
[
222222
NSAttributedString.Key.foregroundColor: UIColor(InjectedValues[\.colors].tintColor)

Sources/StreamChatSwiftUI/ChatChannel/Utils/ChatChannelExtensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extension ChatChannel {
4545
/// - Parameters:
4646
/// - currentUserId: the id of the current user.
4747
/// - Returns: the typing indicator string.
48-
public func typingIndicatorString(currentUserId: UserId?) -> String {
48+
@MainActor public func typingIndicatorString(currentUserId: UserId?) -> String {
4949
let chatUserNamer = InjectedValues[\.utils].chatUserNamer
5050
let typingUsers = currentlyTypingUsersFiltered(currentUserId: currentUserId)
5151
if let user = typingUsers.first(where: { user in user.name != nil }), let name = chatUserNamer.name(forUser: user) {

0 commit comments

Comments
 (0)