Skip to content

Commit c138ccb

Browse files
implemented gallery grid view
1 parent ae28fb8 commit c138ccb

File tree

7 files changed

+55
-43
lines changed

7 files changed

+55
-43
lines changed

Sources/StreamChatSwiftUI/ChatChannel/Gallery/GalleryView.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ struct GalleryView: View {
8383
}
8484
}
8585
.sheet(isPresented: $gridShown) {
86-
GridPhotosView(loadedImages: loadedImages)
86+
GridPhotosView(
87+
imageURLs: sources,
88+
isShown: $gridShown
89+
)
8790
}
8891
}
8992
}

Sources/StreamChatSwiftUI/ChatChannel/Gallery/GridPhotosView.swift

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,45 @@ import SwiftUI
77

88
struct GridPhotosView: View {
99

10-
var loadedImages: [Int: UIImage]
10+
var imageURLs: [URL]
11+
@Binding var isShown: Bool
1112

12-
let columns = [GridItem(.adaptive(minimum: 120), spacing: 2)]
13+
private static let spacing: CGFloat = 2
1314

14-
private var sorted: [UIImage] {
15-
let keys = loadedImages.keys.sorted()
16-
return keys.compactMap { index in
17-
loadedImages[index]
15+
private static var itemWidth: CGFloat {
16+
if UIDevice.current.userInterfaceIdiom == .phone {
17+
return (UIScreen.main.bounds.size.width / 3) - spacing * 3
18+
} else {
19+
return 120
1820
}
1921
}
2022

23+
private let columns = [GridItem(.adaptive(minimum: itemWidth), spacing: spacing)]
24+
2125
var body: some View {
22-
ScrollView {
23-
LazyVGrid(columns: columns, spacing: 2) {
24-
ForEach(sorted, id: \.self) { image in
25-
Image(uiImage: image)
26-
.resizable()
27-
.aspectRatio(1, contentMode: .fill)
26+
VStack {
27+
TitleWithCloseButton(
28+
title: L10n.Message.Gallery.photos,
29+
isShown: $isShown
30+
)
31+
ScrollView {
32+
LazyVGrid(columns: columns, spacing: 2) {
33+
ForEach(imageURLs, id: \.self) { url in
34+
LazyLoadingImage(
35+
source: url,
36+
width: Self.itemWidth
37+
)
38+
.frame(
39+
width: Self.itemWidth,
40+
height: Self.itemWidth
41+
)
2842
.clipped()
43+
}
2944
}
45+
.padding(.horizontal, 2)
46+
.animation(nil)
3047
}
31-
.padding(.horizontal, 2)
32-
.animation(nil)
48+
Spacer()
3349
}
3450
}
3551
}

Sources/StreamChatSwiftUI/ChatChannel/MessageList/ImageAttachmentView.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,6 @@ struct LazyLoadingImage: View {
280280

281281
let source: URL
282282
let width: CGFloat
283-
284283
var resize: Bool = true
285284
var imageTapped: ((Int) -> Void)? = nil
286285
var index: Int?
@@ -290,13 +289,15 @@ struct LazyLoadingImage: View {
290289
ZStack {
291290
if let image = image {
292291
if let imageTapped = imageTapped {
293-
Button {
294-
imageTapped(index ?? 0)
295-
} label: {
296-
imageView(for: image)
297-
}
292+
imageView(for: image, allowsHitTesting: true)
293+
.highPriorityGesture(
294+
TapGesture()
295+
.onEnded { _ in
296+
imageTapped(index ?? 0)
297+
}
298+
)
298299
} else {
299-
imageView(for: image)
300+
imageView(for: image, allowsHitTesting: false)
300301
}
301302
} else if error != nil {
302303
Color(.secondarySystemBackground)
@@ -331,13 +332,16 @@ struct LazyLoadingImage: View {
331332
.clipped()
332333
}
333334

334-
func imageView(for image: UIImage) -> some View {
335+
func imageView(
336+
for image: UIImage,
337+
allowsHitTesting: Bool
338+
) -> some View {
335339
Image(uiImage: image)
336340
.resizable()
337341
.scaledToFill()
338342
.aspectRatio(contentMode: .fill)
339343
.clipped()
340-
.allowsHitTesting(false)
344+
.allowsHitTesting(allowsHitTesting)
341345
}
342346
}
343347

Sources/StreamChatSwiftUI/CommonViews/TitleWithCloseButton.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ struct TitleWithCloseButton: View {
2525
Text(title)
2626
.font(fonts.bodyBold)
2727
}
28+
.padding()
2829
}
2930
}

Sources/StreamChatSwiftUI/Generated/L10n.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ internal enum L10n {
225225
/// Error occured while previewing the file.
226226
internal static var errorPreview: String { L10n.tr("Localizable", "message.file-attachment.error-preview") }
227227
}
228+
internal enum Gallery {
229+
/// Photos
230+
internal static var photos: String { L10n.tr("Localizable", "message.gallery.photos") }
231+
}
228232
internal enum GiphyAttachment {
229233
/// GIPHY
230234
internal static var title: String { L10n.tr("Localizable", "message.giphy-attachment.title") }

Sources/StreamChatSwiftUI/Resources/en.lproj/Localizable.strings

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
"message.threads.replyWith" = "with %@";
4242
"message.threads.subtitle" = "with messages";
4343

44+
"message.gallery.photos" = "Photos";
45+
4446
"alert.actions.cancel" = "Cancel";
4547
"alert.actions.delete" = "Delete";
4648
"alert.actions.ok" = "Ok";

StreamChatSwiftUI.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 1 addition & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)