Skip to content

Commit 39d8e05

Browse files
committed
Add view factory methods for customising headers in GalleryView and VideoPlayerView
1 parent 68ecf4f commit 39d8e05

File tree

5 files changed

+59
-6
lines changed

5 files changed

+59
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
99
- Add `MessageViewModel` to `MessageContainerView` to make it easier to customise presentation logic [#815](https://github.com/GetStream/stream-chat-swiftui/pull/815)
1010
- Add `MessageListConfig.messaeDisplayOptions.showOriginalTranslatedButton` to enable showing original text in translated message [#815](https://github.com/GetStream/stream-chat-swiftui/pull/815)
1111
- Add `Utils.originalTranslationsStore` to keep track of messages that should show the original text [#815](https://github.com/GetStream/stream-chat-swiftui/pull/815)
12+
- Add `ViewFactory.makeGalleryHeaderView(title:subtitle:shown:)` for customising header views in `GalleryView` and `VideoPlayerView` [#837](https://github.com/GetStream/stream-chat-swiftui/pull/837)
1213
### 🐞 Fixed
1314
- Fix swipe to reply enabled when quoting a message is disabled [#824](https://github.com/GetStream/stream-chat-swiftui/pull/824)
1415
- Fix mark unread action not removed when read events are disabled [#823](https://github.com/GetStream/stream-chat-swiftui/pull/823)

Sources/StreamChatSwiftUI/ChatChannel/Gallery/GalleryView.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import StreamChat
77
import SwiftUI
88

99
/// View used for displaying image attachments in a gallery.
10-
public struct GalleryView: View {
10+
public struct GalleryView<Factory: ViewFactory>: View {
1111

1212
@Environment(\.presentationMode) var presentationMode
1313

1414
@Injected(\.colors) private var colors
1515
@Injected(\.fonts) private var fonts
1616
@Injected(\.images) private var images
1717

18+
private let viewFactory: Factory
1819
var mediaAttachments: [MediaAttachment]
1920
var author: ChatUser
2021
@Binding var isShown: Bool
@@ -23,13 +24,15 @@ public struct GalleryView: View {
2324
@State private var gridShown = false
2425

2526
public init(
27+
viewFactory: Factory = DefaultViewFactory.shared,
2628
imageAttachments: [ChatMessageImageAttachment],
2729
author: ChatUser,
2830
isShown: Binding<Bool>,
2931
selected: Int
3032
) {
3133
let mediaAttachments = imageAttachments.map { MediaAttachment(from: $0) }
3234
self.init(
35+
viewFactory: viewFactory,
3336
mediaAttachments: mediaAttachments,
3437
author: author,
3538
isShown: isShown,
@@ -38,11 +41,13 @@ public struct GalleryView: View {
3841
}
3942

4043
public init(
44+
viewFactory: Factory = DefaultViewFactory.shared,
4145
mediaAttachments: [MediaAttachment],
4246
author: ChatUser,
4347
isShown: Binding<Bool>,
4448
selected: Int
4549
) {
50+
self.viewFactory = viewFactory
4651
self.mediaAttachments = mediaAttachments
4752
self.author = author
4853
_isShown = isShown
@@ -52,10 +57,10 @@ public struct GalleryView: View {
5257
public var body: some View {
5358
GeometryReader { reader in
5459
VStack {
55-
GalleryHeaderView(
60+
viewFactory.makeGalleryHeaderView(
5661
title: author.name ?? "",
5762
subtitle: author.onlineText,
58-
isShown: $isShown
63+
shown: $isShown
5964
)
6065

6166
TabView(selection: $selected) {

Sources/StreamChatSwiftUI/ChatChannel/Gallery/VideoPlayerView.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import StreamChat
77
import SwiftUI
88

99
/// View used for displaying videos.
10-
public struct VideoPlayerView: View {
10+
public struct VideoPlayerView<Factory: ViewFactory>: View {
1111
@Environment(\.presentationMode) var presentationMode
1212

1313
@Injected(\.fonts) private var fonts
@@ -18,6 +18,7 @@ public struct VideoPlayerView: View {
1818
utils.fileCDN
1919
}
2020

21+
private let viewFactory: Factory
2122
let attachment: ChatMessageVideoAttachment
2223
let author: ChatUser
2324
@Binding var isShown: Bool
@@ -26,21 +27,23 @@ public struct VideoPlayerView: View {
2627
@State private var error: Error?
2728

2829
public init(
30+
viewFactory: Factory = DefaultViewFactory.shared,
2931
attachment: ChatMessageVideoAttachment,
3032
author: ChatUser,
3133
isShown: Binding<Bool>
3234
) {
35+
self.viewFactory = viewFactory
3336
self.attachment = attachment
3437
self.author = author
3538
_isShown = isShown
3639
}
3740

3841
public var body: some View {
3942
VStack {
40-
GalleryHeaderView(
43+
viewFactory.makeVideoPlayerHeaderView(
4144
title: author.name ?? "",
4245
subtitle: author.onlineText,
43-
isShown: $isShown
46+
shown: $isShown
4447
)
4548
if let avPlayer {
4649
VideoPlayer(player: avPlayer)

Sources/StreamChatSwiftUI/DefaultViewFactory.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,26 +460,44 @@ extension ViewFactory {
460460
options: MediaViewsOptions
461461
) -> some View {
462462
GalleryView(
463+
viewFactory: self,
463464
mediaAttachments: mediaAttachments,
464465
author: message.author,
465466
isShown: isShown,
466467
selected: options.selectedIndex
467468
)
468469
}
469470

471+
public func makeGalleryHeaderView(
472+
title: String,
473+
subtitle: String,
474+
shown: Binding<Bool>
475+
) -> some View {
476+
GalleryHeaderView(title: title, subtitle: subtitle, isShown: shown)
477+
}
478+
470479
public func makeVideoPlayerView(
471480
attachment: ChatMessageVideoAttachment,
472481
message: ChatMessage,
473482
isShown: Binding<Bool>,
474483
options: MediaViewsOptions
475484
) -> some View {
476485
VideoPlayerView(
486+
viewFactory: self,
477487
attachment: attachment,
478488
author: message.author,
479489
isShown: isShown
480490
)
481491
}
482492

493+
public func makeVideoPlayerHeaderView(
494+
title: String,
495+
subtitle: String,
496+
shown: Binding<Bool>
497+
) -> some View {
498+
GalleryHeaderView(title: title, subtitle: subtitle, isShown: shown)
499+
}
500+
483501
public func makeDeletedMessageView(
484502
for message: ChatMessage,
485503
isFirst: Bool,

Sources/StreamChatSwiftUI/ViewFactory.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,19 @@ public protocol ViewFactory: AnyObject {
464464
options: MediaViewsOptions
465465
) -> GalleryViewType
466466

467+
associatedtype GalleryHeaderViewType: View
468+
/// Creates the gallery header view presented with a sheet.
469+
/// - Parameters:
470+
/// - title: The title displayed in the header.
471+
/// - subtitle: The subtitle displayed in the header.
472+
/// - shown: Binding controlling whether the gallery is shown.
473+
/// - Returns: View displayed in the gallery header slot.
474+
func makeGalleryHeaderView(
475+
title: String,
476+
subtitle: String,
477+
shown: Binding<Bool>
478+
) -> GalleryHeaderViewType
479+
467480
associatedtype VideoPlayerViewType: View
468481
/// Creates the video player view.
469482
/// - Parameters:
@@ -478,6 +491,19 @@ public protocol ViewFactory: AnyObject {
478491
isShown: Binding<Bool>,
479492
options: MediaViewsOptions
480493
) -> VideoPlayerViewType
494+
495+
associatedtype VideoPlayerHeaderViewType: View
496+
/// Creates the video player header view presented with a sheet.
497+
/// - Parameters:
498+
/// - title: The title displayed in the header.
499+
/// - subtitle: The subtitle displayed in the header.
500+
/// - shown: Binding controlling whether the video player is shown.
501+
/// - Returns: View displayed in the video player header slot.
502+
func makeVideoPlayerHeaderView(
503+
title: String,
504+
subtitle: String,
505+
shown: Binding<Bool>
506+
) -> VideoPlayerHeaderViewType
481507

482508
associatedtype DeletedMessageViewType: View
483509
/// Creates the deleted message view.

0 commit comments

Comments
 (0)