Skip to content

Commit 86083bb

Browse files
Reacting to orientation changes (#34)
1 parent 4592170 commit 86083bb

File tree

6 files changed

+40
-4
lines changed

6 files changed

+40
-4
lines changed

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelView.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ public struct ChatChannelView<Factory: ViewFactory>: View, KeyboardReadable {
1414
@State private var messageDisplayInfo: MessageDisplayInfo?
1515
@State private var keyboardShown = false
1616
@State private var tabBarAvailable: Bool = false
17+
@State private var orientation = UIDevice.current.orientation
1718

1819
private var factory: Factory
20+
21+
private let orientationChanged = NotificationCenter.default
22+
.publisher(for: UIDevice.orientationDidChangeNotification)
23+
.makeConnectable()
24+
.autoconnect()
1925

2026
public init(
2127
viewFactory: Factory,
@@ -61,6 +67,9 @@ public struct ChatChannelView<Factory: ViewFactory>: View, KeyboardReadable {
6167
factory.makeDateIndicatorView(dateString: viewModel.currentDateString!)
6268
: nil
6369
)
70+
.if(multipleOrientationsSupported) { view in
71+
view.id(orientation.rawValue)
72+
}
6473

6574
Divider()
6675
.navigationBarBackButtonHidden(viewModel.reactionsShown)
@@ -105,6 +114,11 @@ public struct ChatChannelView<Factory: ViewFactory>: View, KeyboardReadable {
105114
.onReceive(keyboardWillChangePublisher, perform: { visible in
106115
keyboardShown = visible
107116
})
117+
.onReceive(orientationChanged) { _ in
118+
if multipleOrientationsSupported {
119+
self.orientation = UIDevice.current.orientation
120+
}
121+
}
108122
.onAppear {
109123
viewModel.onViewAppear()
110124
}
@@ -129,4 +143,10 @@ public struct ChatChannelView<Factory: ViewFactory>: View, KeyboardReadable {
129143
let bottomPadding = topVC()?.view.safeAreaInsets.bottom ?? 0
130144
return bottomPadding
131145
}
146+
147+
private var multipleOrientationsSupported: Bool {
148+
let orientationsKey = "UISupportedInterfaceOrientations"
149+
let orientations = Bundle.main.infoDictionary?[orientationsKey] as? [String] ?? []
150+
return orientations.count > 1
151+
}
132152
}

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageContainerView.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,12 @@ struct MessageContainerView<Factory: ViewFactory>: View {
193193
}
194194

195195
private var spacerWidth: CGFloat {
196-
(width ?? 0) / 4
196+
let proposedViewWidth = width ?? 0
197+
if isIPad {
198+
return 2 * proposedViewWidth / 3
199+
} else {
200+
return proposedViewWidth / 4
201+
}
197202
}
198203

199204
private var reactionsShown: Bool {

Sources/StreamChatSwiftUI/ChatChannel/Utils/ChatChannelHelpers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct WidthPreferenceKey: PreferenceKey {
3030
static var defaultValue: CGFloat? = nil
3131

3232
static func reduce(value: inout CGFloat?, nextValue: () -> CGFloat?) {
33-
value = value ?? nextValue()
33+
value = nextValue() ?? value
3434
}
3535
}
3636

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelHelperViews.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,7 @@ struct TabBarAccessor: UIViewControllerRepresentable {
108108
var isIphone: Bool {
109109
UIDevice.current.userInterfaceIdiom == .phone
110110
}
111+
112+
var isIPad: Bool {
113+
UIDevice.current.userInterfaceIdiom == .pad
114+
}

Sources/StreamChatSwiftUI/ChatChannelList/ChatChannelListViewModel.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
265265
// access channels
266266
self.updateChannels()
267267
self.checkForDeeplinks()
268+
self.setInitialChannelIfSplitView()
268269
}
269270
}
270271
}
@@ -326,6 +327,12 @@ open class ChatChannelListViewModel: ObservableObject, ChatChannelListController
326327
private func updateChannels() {
327328
channels = controller?.channels ?? LazyCachedMapCollection<ChatChannel>()
328329
}
330+
331+
private func setInitialChannelIfSplitView() {
332+
if isIPad && deeplinkChannel == nil {
333+
selectedChannel = channels.first?.channelSelectionInfo
334+
}
335+
}
329336
}
330337

331338
/// Enum for the type of alert presented in the channel list view.

Sources/StreamChatSwiftUI/Utils/Common/ImageMerger.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ open class DefaultImageMerger: ImageMerging {
5050
dimensions.height += image.size.height
5151
}
5252

53-
UIGraphicsBeginImageContext(dimensions)
53+
UIGraphicsBeginImageContextWithOptions(dimensions, true, UIScreen.main.scale)
5454

5555
var lastY: CGFloat = 0
5656
for image in images {
@@ -75,7 +75,7 @@ open class DefaultImageMerger: ImageMerging {
7575
dimensions.height = max(dimensions.height, image.size.height)
7676
}
7777

78-
UIGraphicsBeginImageContext(dimensions)
78+
UIGraphicsBeginImageContextWithOptions(dimensions, true, UIScreen.main.scale)
7979

8080
var lastX: CGFloat = 0
8181
for image in images {

0 commit comments

Comments
 (0)