Skip to content

Commit

Permalink
- Add scroll to top as auxiliary action to ExpandedPost/FeedView/User…
Browse files Browse the repository at this point in the history
…View.
  • Loading branch information
boscojwho committed Oct 7, 2023
1 parent e339faa commit db5834e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
20 changes: 19 additions & 1 deletion Mlem/Views/Shared/Posts/Expanded Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ struct ExpandedPost: View {
@State var commentSortingType: CommentSortType = .appStorageValue()
@State private var postLayoutMode: LargePost.LayoutMode = .maximize

@State private var scrollToTopAppeared = false
@Namespace var scrollToTop

var body: some View {
contentView
.environmentObject(commentTracker)
.navigationBarTitle(post.community.name, displayMode: .inline)
.hoistNavigation(dismiss: dismiss)
.toolbar {
ToolbarItemGroup(placement: .navigationBarTrailing) { toolbarMenu }
}
Expand All @@ -100,6 +102,9 @@ struct ExpandedPost: View {
GeometryReader { proxy in
ScrollViewReader { (scrollProxy: ScrollViewProxy) in
ScrollView {
ScrollToView(appeared: $scrollToTopAppeared)
.id(scrollToTop)

VStack(spacing: 0) {
postView
.id(0)
Expand Down Expand Up @@ -136,6 +141,19 @@ struct ExpandedPost: View {
.onPreferenceChange(AnchorsKey.self) { anchors in
topVisibleCommentId = topCommentRow(of: anchors, in: proxy)
}
.hoistNavigation(
dismiss: dismiss,
auxiliaryAction: {
if scrollToTopAppeared {
return false
} else {
withAnimation {
scrollProxy.scrollTo(scrollToTop)
}
return true
}
}
)
}
}
.overlay {
Expand Down
11 changes: 9 additions & 2 deletions Mlem/Views/Tabs/Feeds/Feed View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,15 @@ struct FeedView: View {
return true
}
} else {
print("exhausted auxiliary actions, perform dismiss action instead...")
return false
if scrollToTopAppeared {
print("exhausted auxiliary actions, perform dismiss action instead...")
return false
} else {
withAnimation {
scrollViewProxy?.scrollTo(scrollToTop, anchor: .top)
}
return true
}
}
}
)
Expand Down
9 changes: 8 additions & 1 deletion Mlem/Views/Tabs/Profile/User View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ struct UserView: View {
}
return true
} else {
return false
if scrollToTopAppeared {
return false
} else {
withAnimation {
scrollViewProxy?.scrollTo(scrollToTop)
}
return true
}
}
}
)
Expand Down

0 comments on commit db5834e

Please sign in to comment.