Skip to content

Commit f8981aa

Browse files
committed
Add makeGalleryHeaderView for customising header view in GalleryView and VideoPlayerView
1 parent 68ecf4f commit f8981aa

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

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.makeGalleryHeaderView(
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,20 +460,30 @@ 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

Sources/StreamChatSwiftUI/ViewFactory.swift

Lines changed: 13 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:

0 commit comments

Comments
 (0)