Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix msg history #168

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
hide scrollbar
  • Loading branch information
12944qwerty committed Aug 4, 2023
commit 74219a6f27caa8faad7a8cd1158a370a7dc07441
84 changes: 41 additions & 43 deletions Swiftcord/Views/Message/MessagesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,56 +230,54 @@ struct MessagesView: View {
}

private var historyList: some View {
GeometryReader { reader in
ScrollViewReader { proxy in
ScrollView {
Group {
if viewModel.reachedTop {
MessagesViewHeader(chl: ctx.channel)
.zeroRowInsets()
.frame(maxWidth: .infinity, alignment: .leading)
} else {
loadingSkeleton
.zeroRowInsets()
.onAppear { if viewModel.fetchMessagesTask == nil { fetchMoreMessages() } }
.onDisappear {
if let loadTask = viewModel.fetchMessagesTask {
loadTask.cancel()
viewModel.fetchMessagesTask = nil
}
}
}

history(proxy: proxy)
.onAppear {
withAnimation {
// Already starts at very bottom, but just in case anyway
// Scroll to very bottom if read, otherwise scroll to message
if gateway.readState[ctx.channel?.id ?? "1"]?.last_message_id?.stringValue ?? "1" == viewModel.messages.first?.id ?? "1" {
proxy.scrollTo("1", anchor: .bottom)
} else {
proxy.scrollTo("unread", anchor: .bottom)
}
ScrollViewReader { proxy in
ScrollView {
Group {
if viewModel.reachedTop {
MessagesViewHeader(chl: ctx.channel)
.zeroRowInsets()
.frame(maxWidth: .infinity, alignment: .leading)
} else {
loadingSkeleton
.zeroRowInsets()
.onAppear { if viewModel.fetchMessagesTask == nil { fetchMoreMessages() } }
.onDisappear {
if let loadTask = viewModel.fetchMessagesTask {
loadTask.cancel()
viewModel.fetchMessagesTask = nil
}
}

Spacer(minLength: max(messageInputHeight-74, 10) + (viewModel.showingInfoBar ? 24 : 0)).zeroRowInsets()
.id("1")
}
.padding(.horizontal, 15)
.rotationEffect(Angle(degrees: 180))
}
.introspectScrollView { scrollView in
scrollView.drawsBackground = false

// Move to right side, scrollbar is between 15-20 wide
scrollView.scrollerInsets = NSEdgeInsets(top: 0, left: 0, bottom: 0, right: reader.size.width - 15)
history(proxy: proxy)
.onAppear {
withAnimation {
// Already starts at very bottom, but just in case anyway
// Scroll to very bottom if read, otherwise scroll to message
if gateway.readState[ctx.channel?.id ?? "1"]?.last_message_id?.stringValue ?? "1" == viewModel.messages.first?.id ?? "1" {
proxy.scrollTo("1", anchor: .bottom)
} else {
proxy.scrollTo("unread", anchor: .bottom)
}
}
}

Spacer(minLength: max(messageInputHeight-74, 10) + (viewModel.showingInfoBar ? 24 : 0)).zeroRowInsets()
.id("1")
}
.environment(\.defaultMinListRowHeight, 1) // By SwiftUI's logic, 0 is negative so we use 1 instead
.background(.clear)
.padding(.top, 74) // Ensure List doesn't go below text input field (and its border radius)
.padding(.horizontal, 15)
.rotationEffect(Angle(degrees: 180))
}
.introspectScrollView { scrollView in
scrollView.drawsBackground = false

// Hide scrollbar
scrollView.scrollerInsets = NSEdgeInsets(top: 0, left: 0, bottom: 0, right: -20)
}
.environment(\.defaultMinListRowHeight, 1) // By SwiftUI's logic, 0 is negative so we use 1 instead
.background(.clear)
.padding(.top, 74) // Ensure List doesn't go below text input field (and its border radius)
.rotationEffect(Angle(degrees: 180))
}
}

Expand Down