Skip to content

Commit

Permalink
refactor: remove redundant isKeyboardActive prop in ChatView
Browse files Browse the repository at this point in the history
  • Loading branch information
EnesKaraosman committed Apr 12, 2024
1 parent 55da302 commit ccff273
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions Sources/SwiftyChat/ChatView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public struct ChatView<Message: ChatMessage, User: ChatUser>: View {

@Binding private var scrollTo: UUID?
@Binding private var scrollToBottom: Bool
@State private var isKeyboardActive = false

public var body: some View {
GeometryReader { geometry in
Expand All @@ -52,8 +51,8 @@ public struct ChatView<Message: ChatMessage, User: ChatUser>: View {
}

@ViewBuilder private func chatView(in geometry: GeometryProxy) -> some View {
ScrollView(.vertical, showsIndicators: false) {
ScrollViewReader { proxy in
ScrollViewReader { proxy in
ScrollView(.vertical, showsIndicators: false) {
LazyVStack {
ForEach(messages) { message in
let showDateheader = shouldShowDateHeader(
Expand Down Expand Up @@ -90,7 +89,7 @@ public struct ChatView<Message: ChatMessage, User: ChatUser>: View {
}
}
Spacer()
.frame(height: inset.bottom)
.frame(height: 0)
.id("bottom")
}
.padding(inset)
Expand All @@ -103,7 +102,7 @@ public struct ChatView<Message: ChatMessage, User: ChatUser>: View {
}
}
.onChange(of: scrollTo) { value in
if let value = value {
if let value {
proxy.scrollTo(value, anchor: .top)
scrollTo = nil
}
Expand All @@ -116,18 +115,11 @@ public struct ChatView<Message: ChatMessage, User: ChatUser>: View {
.publisher(for: UIResponder.keyboardWillShowNotification)
.debounce(for: .milliseconds(400), scheduler: RunLoop.main),
perform: { _ in
if !isKeyboardActive {
isKeyboardActive = true
if !scrollToBottom {
scrollToBottom = true
}
}
)
.onReceive(
NotificationCenter
.default
.publisher(for: UIResponder.keyboardWillHideNotification),
perform: { _ in isKeyboardActive = false }
)
#endif
}
}
Expand Down

0 comments on commit ccff273

Please sign in to comment.