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

Collapse post in ExpandedPostView #1378

Merged
merged 2 commits into from
Oct 7, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ struct CommentEditorView: View {
Spacer()
selectTextButton
}
LargePostBodyView(post: post, isExpanded: true, shouldBlur: false)
LargePostBodyView(post: post, isPostPage: true, shouldBlur: false)
FullyQualifiedLinkView(
entity: post.creator_,
labelStyle: .medium,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ struct FeedPostView: View {
content
.contentShape(.interaction, .rect)
.contentShape(.contextMenuPreview, .rect(cornerRadius: Constants.main.standardSpacing))
.clipShape(.rect(cornerRadius: Constants.main.standardSpacing))
.quickSwipes(post.swipeActions(behavior: postSize.swipeBehavior))
.contextMenu { post.allMenuActions() }
.shadow(color: postSize.tiled ? palette.primary.opacity(0.1) : .clear, radius: 3) // after quickSwipes to prevent clipping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ struct LargePostBodyView: View {
@Environment(\.communityContext) private var communityContext: (any Community1Providing)?

let post: any Post1Providing
let isExpanded: Bool
let isPostPage: Bool
let shouldBlur: Bool

var body: some View {
VStack(alignment: .leading, spacing: Constants.main.standardSpacing) {
post.taggedTitle(communityContext: communityContext)
.foregroundStyle((post.read_ ?? false) ? palette.secondary : palette.primary)
.foregroundStyle((post.read_ ?? false && !isPostPage) ? palette.secondary : palette.primary)
.font(.headline)
.imageScale(.small)

Expand All @@ -43,7 +43,7 @@ struct LargePostBodyView: View {
EmptyView()
}
if let content = post.content {
if isExpanded {
if isPostPage {
Markdown(content, configuration: .default)
} else {
// Cut down on compute time for very long text posts by only rendering the first 4 blocks
Expand Down
10 changes: 5 additions & 5 deletions Mlem/App/Views/Root/Tabs/Feeds/Feed Posts/LargePostView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct LargePostView: View {
@Environment(\.communityContext) private var communityContext

let post: any Post1Providing
var isExpanded: Bool = false
var isPostPage: Bool = false

var shouldBlur: Bool {
switch blurNsfw {
Expand All @@ -40,7 +40,7 @@ struct LargePostView: View {
var content: some View {
VStack(alignment: .leading, spacing: Constants.main.standardSpacing) {
HStack {
if communityContext == nil {
if communityContext == nil || isPostPage {
communityLink
} else {
personLink
Expand All @@ -53,16 +53,16 @@ struct LargePostView: View {
.foregroundStyle(palette.warning)
}

if !isExpanded {
if !isPostPage {
PostEllipsisMenus(post: post)
}
}
.padding(.horizontal, Constants.main.standardSpacing)

LargePostBodyView(post: post, isExpanded: isExpanded, shouldBlur: shouldBlur)
LargePostBodyView(post: post, isPostPage: isPostPage, shouldBlur: shouldBlur)
.padding(.horizontal, Constants.main.standardSpacing)

if showCreator || isExpanded, communityContext == nil {
if (showCreator && communityContext == nil) || isPostPage {
personLink
.padding(.horizontal, Constants.main.standardSpacing)
}
Expand Down
50 changes: 40 additions & 10 deletions Mlem/App/Views/Shared/ExpandedPost/ExpandedPostView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct ExpandedPostView<Content: View>: View {
@State var scrolledToscrollTargetedComment: Bool = false
@State var jumpButtonTarget: URL?
@State var topVisibleItem: URL?
@State var postCollapsed: Bool = false

init(
post: (any PostStubProviding)?,
Expand Down Expand Up @@ -95,19 +96,12 @@ struct ExpandedPostView<Content: View>: View {
FancyScrollView {
LazyVStack(
alignment: .leading,
spacing: compactComments ? Constants.main.halfSpacing : Constants.main.standardSpacing
spacing: 0
) {
LargePostView(post: post, isExpanded: true)
.clipShape(.rect(cornerRadius: Constants.main.standardSpacing))
.id(post.actorId)
.transition(.opacity)
.animation(.easeOut(duration: 0.1), value: type(of: post).tierNumber)
.anchorPreference(
key: AnchorsKey.self,
value: .center
) { [post.actorId: $0] }
postView(post)
.padding(.horizontal, Constants.main.standardSpacing)
content
.padding(.top, compactComments ? Constants.main.halfSpacing : Constants.main.standardSpacing)
if let tracker {
commentTree(tracker: tracker)
}
Expand Down Expand Up @@ -166,6 +160,41 @@ struct ExpandedPostView<Content: View>: View {
.environment(tracker)
}

@ViewBuilder
func postView(_ post: any Post) -> some View {
Group {
if postCollapsed {
HStack {
post.taggedTitle(communityContext: post.community_)
.font(.headline)
.background(palette.secondaryGroupedBackground)
Spacer()
Image(systemName: Icons.expandComment)
.frame(height: 10)
}
.imageScale(.small)
.padding(Constants.main.standardSpacing)
} else {
LargePostView(post: post, isPostPage: true)
}
}
.contentShape(.contextMenuPreview, .rect(cornerRadius: Constants.main.standardSpacing))
.quickSwipes(post.swipeActions(behavior: .standard, commentTreeTracker: tracker))
.contextMenu { post.allMenuActions() }
.onTapGesture {
withAnimation {
postCollapsed.toggle()
}
}
.id(post.actorId)
.transition(.opacity)
.animation(.easeOut(duration: 0.1), value: type(of: post).tierNumber)
.anchorPreference(
key: AnchorsKey.self,
value: .center
) { [post.actorId: $0] }
}

@ViewBuilder
func commentTree(tracker: CommentTreeTracker) -> some View {
ForEach(tracker.comments.itemTree(), id: \.hashValue) { item in
Expand Down Expand Up @@ -209,6 +238,7 @@ struct ExpandedPostView<Content: View>: View {
}
}
.padding(.horizontal, Constants.main.standardSpacing)
.padding(.top, compactComments ? Constants.main.halfSpacing : Constants.main.standardSpacing)
}
}

Expand Down
Loading