Skip to content

Commit

Permalink
New Comments Indicator (#858)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjmarf authored Jan 17, 2024
1 parent 74ee409 commit 51ce887
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Mlem/Icons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ struct Icons {

// misc post
static let posts: String = "doc.plaintext"
static let replies: String = "bubble.right"
static let replies: String = "bubble"
static let unreadReplies: String = "text.bubble"
static let textPost: String = "text.book.closed"
static let titleOnlyPost: String = "character.bubble"
static let pinned: String = "pin.fill"
Expand Down
12 changes: 8 additions & 4 deletions Mlem/Models/Content/Post Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ struct PostModel {
let creator: UserModel
let community: CommunityModel
var votes: VotesModel
let numReplies: Int
let commentCount: Int
let unreadCommentCount: Int
let saved: Bool
let read: Bool
let published: Date
Expand All @@ -32,7 +33,8 @@ struct PostModel {
self.creator = UserModel(from: apiPostView.creator)
self.community = CommunityModel(from: apiPostView.community, subscribed: apiPostView.subscribed.isSubscribed)
self.votes = VotesModel(from: apiPostView.counts, myVote: apiPostView.myVote)
self.numReplies = apiPostView.counts.comments
self.commentCount = apiPostView.counts.comments
self.unreadCommentCount = apiPostView.unreadComments
self.saved = apiPostView.saved
self.read = apiPostView.read
self.published = apiPostView.post.published
Expand Down Expand Up @@ -60,7 +62,8 @@ struct PostModel {
creator: UserModel? = nil,
community: CommunityModel? = nil,
votes: VotesModel? = nil,
numReplies: Int? = nil,
commentCount: Int? = nil,
unreadCommentCount: Int? = nil,
saved: Bool? = nil,
read: Bool? = nil,
published: Date? = nil,
Expand All @@ -71,7 +74,8 @@ struct PostModel {
self.creator = creator ?? other.creator
self.community = community ?? other.community
self.votes = votes ?? other.votes
self.numReplies = numReplies ?? other.numReplies
self.commentCount = commentCount ?? other.commentCount
self.unreadCommentCount = unreadCommentCount ?? other.unreadCommentCount
self.saved = saved ?? other.saved
self.read = read ?? other.read
self.published = published ?? other.published
Expand Down
2 changes: 1 addition & 1 deletion Mlem/Views/Shared/Comments/Comment Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ struct CommentItem: View {
votes: VotesModel(from: hierarchicalComment.commentView.counts, myVote: hierarchicalComment.commentView.myVote),
published: hierarchicalComment.commentView.comment.published,
updated: hierarchicalComment.commentView.comment.updated,
numReplies: hierarchicalComment.commentView.counts.childCount,
commentCount: hierarchicalComment.commentView.counts.childCount,
saved: hierarchicalComment.commentView.saved,
accessibilityContext: "comment",
widgets: layoutWidgetTracker.groups.comment,
Expand Down
26 changes: 22 additions & 4 deletions Mlem/Views/Shared/Components/Components/InfoStackView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ struct InfoStackView: View {
let published: Date?
let updated: Date?
let commentCount: Int?
let unreadCommentCount: Int?

let saved: Bool?
let alignment: HorizontalAlignment
let colorizeVotes: Bool
Expand Down Expand Up @@ -52,7 +54,11 @@ struct InfoStackView: View {
}

if let commentCount {
repliesView(numReplies: commentCount)
if let unreadCommentCount, unreadCommentCount > 0, unreadCommentCount != commentCount {
unreadRepliesView(commentCount: commentCount, unreadCommentCount: unreadCommentCount)
} else {
repliesView(commentCount: commentCount)
}
}
}
.fixedSize()
Expand Down Expand Up @@ -112,13 +118,25 @@ struct InfoStackView: View {
}

@ViewBuilder
func repliesView(numReplies: Int) -> some View {
func repliesView(commentCount: Int) -> some View {
HStack(spacing: AppConstants.iconToTextSpacing) {
Image(systemName: Icons.replies)
Text(numReplies.description)
Text(String(describing: commentCount))
}
.accessibilityAddTraits(.isStaticText)
.accessibilityElement(children: .ignore)
.accessibilityLabel("\(commentCount) comments")
}

@ViewBuilder
func unreadRepliesView(commentCount: Int, unreadCommentCount: Int) -> some View {
HStack(spacing: AppConstants.iconToTextSpacing) {
Image(systemName: Icons.unreadReplies)
Text("\(commentCount)")
+ Text(" +\(unreadCommentCount)").foregroundColor(.green)
}
.accessibilityAddTraits(.isStaticText)
.accessibilityElement(children: .ignore)
.accessibilityLabel("\(numReplies) comments")
.accessibilityLabel("\(commentCount) comments, \(unreadCommentCount) new")
}
}
6 changes: 4 additions & 2 deletions Mlem/Views/Shared/Components/InteractionBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ struct InteractionBarView: View {
let votes: VotesModel
let published: Date
let updated: Date?
let numReplies: Int
let commentCount: Int
var unreadCommentCount: Int = 0
let saved: Bool

let accessibilityContext: String
Expand Down Expand Up @@ -111,7 +112,8 @@ struct InteractionBarView: View {
: nil,
published: shouldShowTime ? published : nil,
updated: shouldShowTime ? updated : nil,
commentCount: shouldShowReplies ? numReplies : nil,
commentCount: shouldShowReplies ? commentCount : nil,
unreadCommentCount: unreadCommentCount,
saved: shouldShowSaved ? saved : nil,
alignment: infoStackAlignment(offset),
colorizeVotes: false
Expand Down
3 changes: 2 additions & 1 deletion Mlem/Views/Shared/Posts/Expanded Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ struct ExpandedPost: View {
votes: post.votes,
published: post.published,
updated: post.updated,
numReplies: post.numReplies,
commentCount: post.commentCount,
unreadCommentCount: post.unreadCommentCount,
saved: post.saved,
accessibilityContext: "post",
widgets: layoutWidgetTracker.groups.post,
Expand Down
4 changes: 4 additions & 0 deletions Mlem/Views/Shared/Posts/ExpandedPostLogic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ extension ExpandedPost {
isLoading = true

do {
// Making this request marks unread comments as read.
post = PostModel(from: try await postRepository.loadPost(postId: post.postId))
postTracker.update(with: post)

let comments = try await commentRepository.comments(for: post.post.id)
let sorted = sortComments(comments, by: commentSortingType)
commentTracker.comments = sorted
Expand Down
3 changes: 2 additions & 1 deletion Mlem/Views/Shared/Posts/Feed Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ struct FeedPost: View {
votes: post.votes,
published: post.published,
updated: post.updated,
numReplies: post.numReplies,
commentCount: post.commentCount,
unreadCommentCount: post.unreadCommentCount,
saved: post.saved,
accessibilityContext: "post",
widgets: layoutWidgetTracker.groups.post,
Expand Down
3 changes: 2 additions & 1 deletion Mlem/Views/Shared/Posts/Post Sizes/Compact Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ struct CompactPost: View {
),
published: post.published,
updated: post.updated,
commentCount: post.numReplies,
commentCount: post.commentCount,
unreadCommentCount: post.unreadCommentCount,
saved: post.saved,
alignment: .center,
colorizeVotes: true
Expand Down

0 comments on commit 51ce887

Please sign in to comment.