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

Eric/uikit scroll to section #1789

Merged
merged 2 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions Mlem/App/Views/Root/Tabs/Feeds/SectionIndexTitles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ struct SectionIndexTitles: View {
var id: String { label }
}

let proxy: ScrollViewProxy
let sections: [Section]
@Binding var sectionScroller: Int

init(sections: [SubscriptionListSection], sectionScroller: Binding<Int>) {
self.sections = sections.map {
.init(label: $0.label, systemImage: $0.systemImage)
}
self._sectionScroller = sectionScroller
}

@GestureState private var dragLocation: CGPoint = .zero

// Track which sidebar label we picked last so we
Expand Down Expand Up @@ -57,9 +65,7 @@ struct SectionIndexTitles: View {
if sectionLabel != lastSelectedLabel {
Task { @MainActor in
lastSelectedLabel = sectionLabel
proxy.scrollTo(sectionLabel, anchor: .top)

// Play nice tappy taps
sectionScroller = sectionIndex
HapticManager.main.play(haptic: .rigidInfo, priority: .low)
}
}
Expand Down
132 changes: 69 additions & 63 deletions Mlem/App/Views/Root/Tabs/Feeds/SubscriptionListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import MlemMiddleware
import SwiftUI
@_spi(Advanced) import SwiftUIIntrospect

struct SubscriptionListView: View {
@Environment(AppState.self) private var appState
Expand All @@ -22,6 +23,10 @@ struct SubscriptionListView: View {
FeedSelection.cases(for: appState.firstAccount.accountType)
}

@State var sectionScroller: Int = 0

@Weak var form: UICollectionView?

var body: some View {
content
.listStyle(.sidebar)
Expand All @@ -35,87 +40,88 @@ struct SubscriptionListView: View {
navigation.path.isEmpty
}
}

@ViewBuilder
var content: some View {
let sections = subscriptions?.visibleSections(sort: sort) ?? []

ScrollViewReader { proxy in
Form {
Section {
ForEach(feedOptions, id: \.hashValue) { feedOption in
SubscriptionListNavigationButton(.feeds(feedOption)) {
HStack(spacing: 15) {
FeedIconView(
feedDescription: feedOption.description,
size: appState.firstSession is GuestSession ? 36 : 28
)
VStack(alignment: .leading) {
Text(feedOption.description.label)
if appState.firstSession is GuestSession {
Text(feedOption.description.subtitle)
.font(.footnote)
.foregroundStyle(palette.secondary)
}
Form {
Section {
ForEach(feedOptions, id: \.hashValue) { feedOption in
SubscriptionListNavigationButton(.feeds(feedOption)) {
HStack(spacing: 15) {
FeedIconView(
feedDescription: feedOption.description,
size: appState.firstSession is GuestSession ? 36 : 28
)
VStack(alignment: .leading) {
Text(feedOption.description.label)
if appState.firstSession is GuestSession {
Text(feedOption.description.subtitle)
.font(.footnote)
.foregroundStyle(palette.secondary)
}
}
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(.rect)
}
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(.rect)
}
}

if AccountsTracker.main.isEmpty {
Section {
signedOutInfoView
.listRowBackground(Color.clear)
}
}

ForEach(sections) { section in
SubscriptionListSectionView(section: section, sectionIndicesShown: sectionIndicesShown)
.id(section.label)
}
.scrollTargetLayout()
}
.foregroundStyle(palette.primary)
.overlay(alignment: .trailing) {
if sectionIndicesShown {
SectionIndexTitles(
proxy: proxy,
sections: [.init(label: String(localized: "Favorites"), systemImage: "star.fill")]
+ "ABCDEFGHIJKLMNOPQRSTUVWYZ#".map { .init(label: String($0)) }
)

if AccountsTracker.main.isEmpty {
Section {
signedOutInfoView
.listRowBackground(Color.clear)
}
}
.toolbar {
if !(subscriptions?.communities.isEmpty ?? true) {
Picker("Sort", selection: $sort) {
ForEach(SubscriptionListSort.allCases, id: \.self) { item in
Label(item.label, systemImage: item.systemImage)
}
}
}

ForEach(sections) { section in
SubscriptionListSectionView(section: section, sectionIndicesShown: sectionIndicesShown)
.id(section.label)
}
.scrollTargetLayout()
}
.introspect(.form, on: .iOS(.v17, .v18)) { introspectedForm in
self.form = introspectedForm
}
.onChange(of: sectionScroller) {
form?.scrollToItem(at: .init(row: 0, section: sectionScroller), at: .centeredVertically, animated: false)
}
.foregroundStyle(palette.primary)
.overlay(alignment: .trailing) {
if sectionIndicesShown {
SectionIndexTitles(
sections: sections,
sectionScroller: $sectionScroller
)
}
.onChange(of: tabReselectTracker.flag) {
// normal reselect tracker does not work here thanks to NavigationSplitView, so we need to implement a custom one
if detailDisplayed, tabReselectTracker.flag {
tabReselectTracker.reset()
withAnimation {
proxy.scrollTo(sections.first?.label)
}
.toolbar {
if !(subscriptions?.communities.isEmpty ?? true) {
Picker("Sort", selection: $sort) {
ForEach(SubscriptionListSort.allCases, id: \.self) { item in
Label(item.label, systemImage: item.systemImage)
}
}
}
.scrollIndicators(sectionIndicesShown ? .hidden : .visible)
.refreshable {
do {
try await subscriptions?.refresh()
} catch {
handleError(error)
}
}
.onChange(of: tabReselectTracker.flag) {
// normal reselect tracker does not work here thanks to NavigationSplitView, so we need to implement a custom one
if detailDisplayed, tabReselectTracker.flag {
tabReselectTracker.reset()
form?.scrollToItem(at: .init(row: 0, section: 0), at: .bottom, animated: true)
}
}
.scrollIndicators(sectionIndicesShown ? .hidden : .visible)
.refreshable {
do {
try await subscriptions?.refresh()
} catch {
handleError(error)
}
.background(palette.background)
}
.background(palette.background)
}

@ViewBuilder
Expand Down