Skip to content

Commit 16affbe

Browse files
wip on the gallery screen
1 parent da6a584 commit 16affbe

File tree

5 files changed

+88
-16
lines changed

5 files changed

+88
-16
lines changed

Sources/StreamChatSwiftUI/ChatChannel/Gallery/GalleryView.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct GalleryView: View {
1212
var message: ChatMessage
1313
@Binding var isShown: Bool
1414
@State private var selected: Int
15+
@State private var loadedImages = [Int: UIImage]()
1516

1617
init(
1718
message: ChatMessage,
@@ -41,19 +42,33 @@ struct GalleryView: View {
4142
LazyLoadingImage(
4243
source: url,
4344
width: reader.size.width,
44-
resize: true
45+
resize: true,
46+
onImageLoaded: { image in
47+
loadedImages[index] = image
48+
}
4549
)
4650
.frame(width: reader.size.width)
4751
.aspectRatio(contentMode: .fit)
4852
Spacer()
4953
}
5054
}
51-
.background(Color(colors.background1))
5255
.tag(index)
5356
}
5457
}
55-
.tabViewStyle(.page)
56-
.indexViewStyle(.page(backgroundDisplayMode: .never))
58+
.tabViewStyle(.page(indexDisplayMode: .never))
59+
60+
if let image = loadedImages[selected] {
61+
HStack {
62+
ShareButtonView(content: [image])
63+
.standardPadding()
64+
65+
Spacer()
66+
67+
Text("\(selected + 1) of \(sources.count)")
68+
69+
Spacer()
70+
}
71+
}
5772
}
5873
}
5974
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// Copyright © 2021 Stream.io Inc. All rights reserved.
3+
//
4+
5+
import SwiftUI
6+
7+
/// View for presenting a button which has a share action.
8+
struct ShareButtonView: View {
9+
@Injected(\.colors) var colors
10+
11+
var content: [Any]
12+
@State var isSharePresented = false
13+
14+
var body: some View {
15+
Button(action: {
16+
self.isSharePresented = true
17+
}, label: {
18+
Image(systemName: "square.and.arrow.up")
19+
})
20+
.foregroundColor(Color(colors.text))
21+
.sheet(isPresented: $isSharePresented) {
22+
ShareActivityView(activityItems: content)
23+
}
24+
}
25+
}
26+
27+
/// View controller reprensetable which wraps up the activity view controller.
28+
struct ShareActivityView: UIViewControllerRepresentable {
29+
30+
var activityItems: [Any]
31+
var applicationActivities: [UIActivity]? = nil
32+
33+
func makeUIViewController(
34+
context: UIViewControllerRepresentableContext<ShareActivityView>
35+
) -> UIActivityViewController {
36+
let controller = UIActivityViewController(
37+
activityItems: activityItems,
38+
applicationActivities: applicationActivities
39+
)
40+
controller.popoverPresentationController?.sourceView = UIApplication.shared.windows.first?.rootViewController?.view
41+
42+
return controller
43+
}
44+
45+
func updateUIViewController(
46+
_ uiViewController: UIActivityViewController,
47+
context: UIViewControllerRepresentableContext<ShareActivityView>
48+
) {}
49+
}

Sources/StreamChatSwiftUI/ChatChannel/Gallery/ZoomableScrollView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ private struct ZoomableScrollViewImpl<Content: View>: UIViewControllerRepresenta
4444
// MARK: - ZoomableScrollViewController
4545

4646
class ZoomableScrollViewController: UIViewController, UIScrollViewDelegate {
47+
@Injected(\.colors) var colors
48+
4749
let coordinator: Coordinator
4850
let scrollView = UIScrollView()
4951

@@ -74,6 +76,7 @@ private struct ZoomableScrollViewImpl<Content: View>: UIViewControllerRepresenta
7476

7577
let hostedView = coordinator.hostingController.view!
7678
hostedView.translatesAutoresizingMaskIntoConstraints = false
79+
hostedView.backgroundColor = colors.background1
7780
scrollView.addSubview(hostedView)
7881
NSLayoutConstraint.activate([
7982
hostedView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor),

Sources/StreamChatSwiftUI/ChatChannel/MessageList/ImageAttachmentView.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ struct LazyLoadingImage: View {
284284
var resize: Bool = true
285285
var imageTapped: ((Int) -> Void)? = nil
286286
var index: Int?
287+
var onImageLoaded: (UIImage) -> Void = { _ in }
287288

288289
var body: some View {
289290
ZStack {
@@ -292,20 +293,10 @@ struct LazyLoadingImage: View {
292293
Button {
293294
imageTapped(index ?? 0)
294295
} label: {
295-
Image(uiImage: image)
296-
.resizable()
297-
.scaledToFill()
298-
.aspectRatio(contentMode: .fill)
299-
.clipped()
300-
.allowsHitTesting(false)
296+
imageView(for: image)
301297
}
302298
} else {
303-
Image(uiImage: image)
304-
.resizable()
305-
.scaledToFill()
306-
.aspectRatio(contentMode: .fill)
307-
.clipped()
308-
.allowsHitTesting(false)
299+
imageView(for: image)
309300
}
310301
} else if error != nil {
311302
Color(.secondarySystemBackground)
@@ -330,6 +321,7 @@ struct LazyLoadingImage: View {
330321
switch result {
331322
case let .success(image):
332323
self.image = image
324+
onImageLoaded(image)
333325
case let .failure(error):
334326
self.error = error
335327
}
@@ -338,6 +330,15 @@ struct LazyLoadingImage: View {
338330
}
339331
.clipped()
340332
}
333+
334+
func imageView(for image: UIImage) -> some View {
335+
Image(uiImage: image)
336+
.resizable()
337+
.scaledToFill()
338+
.aspectRatio(contentMode: .fill)
339+
.clipped()
340+
.allowsHitTesting(false)
341+
}
341342
}
342343

343344
extension ChatMessage {

StreamChatSwiftUI.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@
240240
84F2908A276B90610045472D /* GalleryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84F29089276B90610045472D /* GalleryView.swift */; };
241241
84F2908C276B91700045472D /* ZoomableScrollView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84F2908B276B91700045472D /* ZoomableScrollView.swift */; };
242242
84F2908E276B92A40045472D /* GalleryHeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84F2908D276B92A40045472D /* GalleryHeaderView.swift */; };
243+
84F29090276CC1280045472D /* ShareButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84F2908F276CC1280045472D /* ShareButtonView.swift */; };
243244
/* End PBXBuildFile section */
244245

245246
/* Begin PBXContainerItemProxy section */
@@ -516,6 +517,7 @@
516517
84F29089276B90610045472D /* GalleryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GalleryView.swift; sourceTree = "<group>"; };
517518
84F2908B276B91700045472D /* ZoomableScrollView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZoomableScrollView.swift; sourceTree = "<group>"; };
518519
84F2908D276B92A40045472D /* GalleryHeaderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GalleryHeaderView.swift; sourceTree = "<group>"; };
520+
84F2908F276CC1280045472D /* ShareButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareButtonView.swift; sourceTree = "<group>"; };
519521
/* End PBXFileReference section */
520522

521523
/* Begin PBXFrameworksBuildPhase section */
@@ -1066,6 +1068,7 @@
10661068
84F29089276B90610045472D /* GalleryView.swift */,
10671069
8465FD032746A95600AF091E /* VideoPlayerView.swift */,
10681070
84F2908B276B91700045472D /* ZoomableScrollView.swift */,
1071+
84F2908F276CC1280045472D /* ShareButtonView.swift */,
10691072
);
10701073
path = Gallery;
10711074
sourceTree = "<group>";
@@ -1347,6 +1350,7 @@
13471350
84A75FBB274EA29B00225CE8 /* GiphyAttachmentView.swift in Sources */,
13481351
8465FD802746A95700AF091E /* WebView.swift in Sources */,
13491352
8465FD942746A95700AF091E /* AddedFileAttachmentsView.swift in Sources */,
1353+
84F29090276CC1280045472D /* ShareButtonView.swift in Sources */,
13501354
8465FD852746A95700AF091E /* MessageView.swift in Sources */,
13511355
8465FDCA2746A95700AF091E /* MoreChannelActionsViewModel.swift in Sources */,
13521356
8465FDD12746A95700AF091E /* Images.swift in Sources */,

0 commit comments

Comments
 (0)