Skip to content

Commit

Permalink
"Max Comment Depth" setting (#1461)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjmarf authored Nov 26, 2024
1 parent facc29c commit 119442c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 13 deletions.
3 changes: 3 additions & 0 deletions Mlem/App/Configuration/User Settings/CodableSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct CodableSettings: Codable {
var comment_gestures_tapToCollapse: Bool
var comment_jumpButton: CommentJumpButtonLocation
var comment_showCreatorInstance: Bool
var comment_maxDepth: Int
var community_showAvatar: Bool
var community_showBanner: Bool
var community_showInstance: Bool
Expand Down Expand Up @@ -111,6 +112,7 @@ struct CodableSettings: Codable {
self.comment_gestures_tapToCollapse = try container.decodeIfPresent(Bool.self, forKey: .comment_gestures_tapToCollapse) ?? true
self.comment_jumpButton = try container.decodeIfPresent(CommentJumpButtonLocation.self, forKey: .comment_jumpButton) ?? .bottomTrailing
self.comment_showCreatorInstance = try container.decodeIfPresent(Bool.self, forKey: .comment_showCreatorInstance) ?? true
self.comment_maxDepth = try container.decodeIfPresent(Int.self, forKey: .comment_maxDepth) ?? 8
self.community_showAvatar = try container.decodeIfPresent(Bool.self, forKey: .community_showAvatar) ?? true
self.community_showBanner = try container.decodeIfPresent(Bool.self, forKey: .community_showBanner) ?? true
self.community_showInstance = try container.decodeIfPresent(Bool.self, forKey: .community_showInstance) ?? true
Expand Down Expand Up @@ -187,6 +189,7 @@ struct CodableSettings: Codable {
self.comment_gestures_tapToCollapse = true
self.comment_jumpButton = settings.jumpButton
self.comment_showCreatorInstance = true
self.comment_maxDepth = 8
self.community_showAvatar = settings.showCommunityAvatar
self.community_showBanner = true
self.community_showInstance = true
Expand Down
2 changes: 2 additions & 0 deletions Mlem/App/Configuration/User Settings/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class Settings: ObservableObject {
@AppStorage("comment.compact") var compactComments: Bool = false
@AppStorage("comment.jumpButton") var jumpButton: CommentJumpButtonLocation = .bottomTrailing
@AppStorage("comment.sort") var commentSort: ApiCommentSortType = .top
@AppStorage("comment.maxDepth") var maxCommentDepth: Int = 8

@AppStorage("status.bypassImageProxyShown") var bypassImageProxyShown: Bool = false

Expand Down Expand Up @@ -138,6 +139,7 @@ class Settings: ObservableObject {
compactComments = settings.comment_compact
jumpButton = settings.comment_jumpButton
commentSort = settings.comment_defaultSort
maxCommentDepth = settings.comment_maxDepth
bypassImageProxyShown = settings.status_bypassImageProxyShown
autoBypassImageProxy = settings.privacy_autoBypassImageProxy
sidebarVisibleByDefault = settings.navigation_sidebarVisibleByDefault
Expand Down
27 changes: 27 additions & 0 deletions Mlem/App/Views/Root/Tabs/Settings/CommentSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import SwiftUI

struct CommentSettingsView: View {
@Environment(Palette.self) var palette

@Setting(\.compactComments) var compactComments
@Setting(\.maxCommentDepth) var maxCommentDepth

var body: some View {
Form {
Expand All @@ -22,7 +25,31 @@ struct CommentSettingsView: View {
destination: .settings(.commentInteractionBar)
)
}
maxDepthSection
}
.navigationTitle("Comments")
}

@ViewBuilder
var maxDepthSection: some View {
Section {
HStack {
Text("Maximum Comment Depth")
Spacer()
Text(String(maxCommentDepth))
.foregroundStyle(palette.secondary)
.monospaced()
}
Slider(
value: .init(
get: { Double(maxCommentDepth) },
set: { maxCommentDepth = Int($0) }
),
in: 1.0 ... 12.0,
step: 1
)
} footer: {
Text("The number of child comments that are shown in a chain before the \"More Replies\" button is shown.")
}
}
}
35 changes: 22 additions & 13 deletions Mlem/App/Views/Shared/ExpandedPost/CommentTreeTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,7 @@ class CommentTreeTracker: Hashable {
guard loadingState == .idle else { return }
loadingState = .loading
do {
var newComments: [Comment2]
switch root {
case let .post(post):
newComments = try await post.getComments(sort: sort, page: 1, maxDepth: 8, limit: 50)
case let .comment(comment, parentCount):
newComments = try await comment.getChildren(
sort: sort,
includedParentCount: parentCount,
page: 1,
maxDepth: 8,
limit: 999
)
}
var newComments = try await fetchComments(page: 1)
if let ensuredComment, !commentsKeyedByActorId.keys.contains(ensuredComment.actorId) {
let comment: any Comment
let api = root.wrappedValue.api
Expand Down Expand Up @@ -112,6 +100,27 @@ class CommentTreeTracker: Hashable {
}
}

@MainActor
private func fetchComments(page: Int) async throws -> [Comment2] {
switch root {
case let .post(post):
return try await post.getComments(
sort: sort,
page: page,
maxDepth: Settings.main.maxCommentDepth,
limit: 50
)
case let .comment(comment, parentCount):
return try await comment.getChildren(
sort: sort,
includedParentCount: parentCount,
page: page,
maxDepth: min(8, Settings.main.maxCommentDepth),
limit: 999
)
}
}

private func handleFailure(_ error: Error) {
var details = handleErrorWithDetails(error)
details?.refresh = {
Expand Down
6 changes: 6 additions & 0 deletions Mlem/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,9 @@
},
"Matrix Room" : {

},
"Maximum Comment Depth" : {

},
"Medium" : {

Expand Down Expand Up @@ -1439,6 +1442,9 @@
}
}
}
},
"The number of child comments that are shown in a chain before the \"More Replies\" button is shown." : {

},
"Theme" : {

Expand Down

0 comments on commit 119442c

Please sign in to comment.