Skip to content

Commit

Permalink
Merge branch 'dev' into eric/inbox-feed-loader
Browse files Browse the repository at this point in the history
  • Loading branch information
EricBAndrews committed Dec 8, 2024
2 parents 53971b4 + 194e355 commit a5a5363
Show file tree
Hide file tree
Showing 17 changed files with 342 additions and 38 deletions.
12 changes: 12 additions & 0 deletions Mlem.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@
03AB48552CBC0B8000567FF9 /* AccountAdvancedSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03AB48542CBC0B8000567FF9 /* AccountAdvancedSettingsView.swift */; };
03AB48572CBC0DFC00567FF9 /* AccountSignInSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03AB48562CBC0DFC00567FF9 /* AccountSignInSettingsView.swift */; };
03AB48592CBC14CE00567FF9 /* AccountEmailSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03AB48582CBC14CE00567FF9 /* AccountEmailSettingsView.swift */; };
03AD0A822CFDBFA0001EF9F7 /* AccountLocalSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03AD0A812CFDBFA0001EF9F7 /* AccountLocalSettingsView.swift */; };
03AD0A842CFDC557001EF9F7 /* AccountNicknameFieldView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03AD0A832CFDC557001EF9F7 /* AccountNicknameFieldView.swift */; };
03AD09E82CF88007001EF9F7 /* MoreRepliesButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03AD09E72CF88007001EF9F7 /* MoreRepliesButton.swift */; };
03AF91DD2C1B23E500E56644 /* ImageViewer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03AF91DC2C1B23E500E56644 /* ImageViewer.swift */; };
03AF91DF2C1B243D00E56644 /* ZoomableContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03AF91DE2C1B243D00E56644 /* ZoomableContainer.swift */; };
03AF91E12C1B25DE00E56644 /* UIDevice+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03AF91E02C1B25DE00E56644 /* UIDevice+Extensions.swift */; };
Expand Down Expand Up @@ -607,6 +610,9 @@
03AB48542CBC0B8000567FF9 /* AccountAdvancedSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountAdvancedSettingsView.swift; sourceTree = "<group>"; };
03AB48562CBC0DFC00567FF9 /* AccountSignInSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountSignInSettingsView.swift; sourceTree = "<group>"; };
03AB48582CBC14CE00567FF9 /* AccountEmailSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountEmailSettingsView.swift; sourceTree = "<group>"; };
03AD0A812CFDBFA0001EF9F7 /* AccountLocalSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountLocalSettingsView.swift; sourceTree = "<group>"; };
03AD0A832CFDC557001EF9F7 /* AccountNicknameFieldView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountNicknameFieldView.swift; sourceTree = "<group>"; };
03AD09E72CF88007001EF9F7 /* MoreRepliesButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoreRepliesButton.swift; sourceTree = "<group>"; };
03AF91DC2C1B23E500E56644 /* ImageViewer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageViewer.swift; sourceTree = "<group>"; };
03AF91DE2C1B243D00E56644 /* ZoomableContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZoomableContainer.swift; sourceTree = "<group>"; };
03AF91E02C1B25DE00E56644 /* UIDevice+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIDevice+Extensions.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -899,6 +905,8 @@
03AB48542CBC0B8000567FF9 /* AccountAdvancedSettingsView.swift */,
03AB48562CBC0DFC00567FF9 /* AccountSignInSettingsView.swift */,
03AB48582CBC14CE00567FF9 /* AccountEmailSettingsView.swift */,
03AD0A812CFDBFA0001EF9F7 /* AccountLocalSettingsView.swift */,
03AD0A832CFDC557001EF9F7 /* AccountNicknameFieldView.swift */,
03134A572BEC1C46002662CC /* AccountListSettingsView.swift */,
031E2D5A2BEFC9460003BC45 /* ThemeSettingsView.swift */,
03B72B6A2C28A0190023A6C4 /* SubscriptionListSettingsView.swift */,
Expand Down Expand Up @@ -1166,6 +1174,7 @@
03E0EF442CA74036002CB66C /* CommentPage.swift */,
03B0EB6E2C87827A00F79FDF /* ExpandedPostView.swift */,
033EF40F2CB9AEF7004D8A3F /* ExpandedPostView+Views.swift */,
03AD09E72CF88007001EF9F7 /* MoreRepliesButton.swift */,
039F58852C7A810100C61658 /* ExpandedPostView+Logic.swift */,
03E0EF422CA73D7A002CB66C /* PostPage.swift */,
);
Expand Down Expand Up @@ -2104,6 +2113,7 @@
03AB48552CBC0B8000567FF9 /* AccountAdvancedSettingsView.swift in Sources */,
039F588A2C7B54FE00C61658 /* GeneralSettingsView.swift in Sources */,
03F967272CE218110081C9A3 /* PersonBanEditorView.swift in Sources */,
03AD0A822CFDBFA0001EF9F7 /* AccountLocalSettingsView.swift in Sources */,
039F58992C7B697D00C61658 /* Bundle+Extensions.swift in Sources */,
033F84D92C2B61FB002E3EDF /* ToastType.swift in Sources */,
03A82FA12C0D1E8500D01A5C /* ApiClient+Extensions.swift in Sources */,
Expand Down Expand Up @@ -2143,6 +2153,7 @@
03C93CF02BEFFB1A00327BFE /* LoginCredentialsView.swift in Sources */,
CD332D7C2CA71E6F00A53988 /* GifView.swift in Sources */,
CD13CC652C5D2B9D001AF428 /* CircleCroppedImageView.swift in Sources */,
03AD0A842CFDC557001EF9F7 /* AccountNicknameFieldView.swift in Sources */,
CDAA02DD2C81792500D75633 /* SolarizedPalette.swift in Sources */,
035EDF032C2ED0DE00F51144 /* PersonListRowBody.swift in Sources */,
03FD6CB02C9B719100500FD6 /* View+PopupAnchor.swift in Sources */,
Expand Down Expand Up @@ -2263,6 +2274,7 @@
CD317D4F2BE983ED008F63E2 /* MonochromePalette.swift in Sources */,
03AF91EA2C1CE96600E56644 /* Counter.swift in Sources */,
03B431C22C45BA00001A1EB5 /* MarkdownEditorToolbarView.swift in Sources */,
03AD09E82CF88007001EF9F7 /* MoreRepliesButton.swift in Sources */,
034B948E2C0937BA00039AF4 /* FancyScrollView.swift in Sources */,
CD332D7E2CA7486000A53988 /* String+Extensions.swift in Sources */,
CD64A91E2CA62592007CA7E6 /* MediaView.swift in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions Mlem/App/Models/Account/Account.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ protocol Account: AnyObject, Codable, ActorIdentifiable, Profile1Providing, Hash
var nicknameSortKey: String { get }
var instanceSortKey: String { get }
var isActive: Bool { get }

func setNickname(_ newValue: String)
}

// Profile1Providing conformance
Expand Down
5 changes: 5 additions & 0 deletions Mlem/App/Models/Account/GuestAccount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ class GuestAccount: Account {
AccountsTracker.main.saveAccounts(ofType: .guest)
}
}

func setNickname(_ newValue: String) {
storedNickname = newValue.isEmpty ? nil : newValue
AccountsTracker.main.saveAccounts(ofType: .guest)
}
}

extension GuestAccount: CacheIdentifiable {
Expand Down
5 changes: 5 additions & 0 deletions Mlem/App/Models/Account/UserAccount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ class UserAccount: Account, CommunityOrPersonStub {
guard let host else { return nil }
return "@\(name)@\(host)"
}

func setNickname(_ newValue: String) {
storedNickname = newValue.isEmpty ? nil : newValue
AccountsTracker.main.saveAccounts(ofType: .user)
}
}

private func getKeychainId(actorId: URL) -> String {
Expand Down
48 changes: 48 additions & 0 deletions Mlem/App/Views/Root/Tabs/Settings/AccountLocalSettingsView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// AccountLocalSettingsView.swift
// Mlem
//
// Created by Sjmarf on 2024-12-02.
//

import SwiftUI

struct AccountLocalSettingsView: View {
@Environment(AppState.self) var appState
@Environment(Palette.self) var palette

@State var isShowingFavoriteDeletionWarning: Bool = false

var body: some View {
Form {
AccountNicknameFieldView()
if let userSession = AppState.main.firstSession as? UserSession {
Section {
Button("Delete Community Favorites", systemImage: Icons.delete, role: .destructive) {
isShowingFavoriteDeletionWarning = true
}
.disabled(userSession.account.favorites.isEmpty)
.tint(palette.warning)
.confirmationDialog(
"Delete Community Favorites",
isPresented: $isShowingFavoriteDeletionWarning
) {
Button("Delete", role: .destructive) {
for community in userSession.subscriptions.favorites {
community.updateFavorite(false)
}
}
} message: {
Text("Are you sure you want to delete all community favorites for this account? This cannot be undone.")
}
} footer: {
if userSession.account.favorites.isEmpty {
Text("This account has no favorite communities.")
} else {
Text("This account has \(userSession.account.favorites.count) favorite communities.")
}
}
}
}
}
}
33 changes: 33 additions & 0 deletions Mlem/App/Views/Root/Tabs/Settings/AccountNicknameFieldView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// AccountNicknameFieldView.swift
// Mlem
//
// Created by Sjmarf on 2024-12-02.
//

import SwiftUI

struct AccountNicknameFieldView: View {
@Environment(AppState.self) var appState

@State var nickname: String

init() {
self.nickname = AppState.main.firstAccount.storedNickname ?? ""
}

var body: some View {
Section("Nickname") {
TextField(
"Nickname",
text: $nickname,
prompt: Text(appState.firstAccount.name)
)
.onSubmit {
AppState.main.firstAccount.setNickname(nickname)
}
} footer: {
Text("The name shown in the account switcher.")
}
}
}
24 changes: 20 additions & 4 deletions Mlem/App/Views/Root/Tabs/Settings/AccountSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,28 @@ struct AccountSettingsView: View {
)
.tint(palette.negative)
}
Section {
NavigationLink(
"Local Options",
systemImage: "iphone",
destination: .settings(.accountLocal)
)
.tint(palette.colorfulAccent(2))
} footer: {
Text("These options are stored locally in Mlem and not on your Lemmy account.")
}
} else {
AccountNicknameFieldView()
}

Group {
Section {
Button(signOutLabel) {
Button {
appState.firstAccount.signOut()
} label: {
Text(signOutLabel)
.frame(maxWidth: .infinity)
}
.frame(maxWidth: .infinity)
.confirmationDialog(signOutPrompt, isPresented: $showingSignOutConfirmation) {
Button(signOutLabel, role: .destructive) {
appState.firstAccount.signOut()
Expand All @@ -79,10 +93,12 @@ struct AccountSettingsView: View {

if let account = appState.firstAccount as? UserAccount {
Section {
Button("Delete Account", role: .destructive) {
Button(role: .destructive) {
navigation.openSheet(.deleteAccount(account))
} label: {
Text("Delete Account")
.frame(maxWidth: .infinity)
}
.frame(maxWidth: .infinity)
}
}
}
Expand Down
20 changes: 17 additions & 3 deletions Mlem/App/Views/Shared/ExpandedPost/CommentPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ struct CommentPage: View {
@Environment(\.dismiss) var dismiss

let comment: AnyComment
let initialComments: [Comment2]?
@State var tracker: CommentTreeTracker?
let showViewPostButton: Bool

@State var post: Post3?

init(comment: AnyComment, showViewPostButton: Bool = false) {
init(comment: AnyComment, initialComments: [Comment2]?, showViewPostButton: Bool = false) {
self.comment = comment
self.showViewPostButton = showViewPostButton
self.initialComments = initialComments
if let comment = comment.wrappedValue as? any Comment {
self._tracker = .init(wrappedValue: .init(root: .comment(comment, parentCount: 1)))
} else {
Expand Down Expand Up @@ -85,9 +87,14 @@ struct CommentPage: View {
if let comment = model.wrappedValue as? any Comment {
if let tracker {
tracker.root = .comment(comment, parentCount: 1)
tracker.loadingState = .idle
Task {
await tracker.load()
if let initialComments {
await tracker.insertAdditionalComments(comments: initialComments)
tracker.loadingState = .done
} else {
tracker.loadingState = .idle
await tracker.load()
}
}
} else {
tracker = .init(root: .comment(comment, parentCount: 1))
Expand All @@ -106,6 +113,13 @@ struct CommentPage: View {
}
}
}
.onAppear {
if comment.isUpgraded, let tracker {
Task {
await tracker.load()
}
}
}
}

var currentDepth: Int {
Expand Down
17 changes: 13 additions & 4 deletions Mlem/App/Views/Shared/ExpandedPost/CommentTreeTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class CommentTreeTracker: Hashable {
sort: sort,
includedParentCount: parentCount,
page: page,
maxDepth: Settings.main.maxCommentDepth,
maxDepth: min(8, Settings.main.maxCommentDepth) + parentCount,
limit: 999
)
}
Expand Down Expand Up @@ -155,10 +155,15 @@ class CommentTreeTracker: Hashable {
}

@MainActor
private func buildCommentTree(comments newComments: [Comment2]) async {
var output: [CommentWrapper] = []
func insertAdditionalComments(comments newComments: [Comment2]) async {
await buildCommentTree(comments: newComments, clear: false)
}

@MainActor
private func buildCommentTree(comments newComments: [Comment2], clear: Bool = true) async {
var output: [CommentWrapper] = clear ? [] : comments
var commentsKeyedById: [Int: CommentWrapper] = [:]
var commentsKeyedByActorId: [URL: CommentWrapper] = [:]
var commentsKeyedByActorId: [URL: CommentWrapper] = clear ? [:] : commentsKeyedByActorId

// From 0.19.0 onwards, a comment's parent is guaranteed to precede it in the array.
//
Expand All @@ -182,6 +187,10 @@ class CommentTreeTracker: Hashable {
}

for comment in sortedComments {
if commentsKeyedByActorId.keys.contains(comment.actorId) {
commentsKeyedById[comment.id] = commentsKeyedByActorId[comment.actorId]
continue
}
let wrapper: CommentWrapper = .init(comment)
commentsKeyedById[comment.id] = wrapper
commentsKeyedByActorId[comment.actorId] = wrapper
Expand Down
21 changes: 1 addition & 20 deletions Mlem/App/Views/Shared/ExpandedPost/ExpandedPostView+Views.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,7 @@ extension ExpandedPostView {
.padding(.leading, CGFloat(comment.depth - tracker.proposedDepthOffset) * 10)
.id(comment.actorId)
case let .unloadedComments(comment, _):
Button {
navigation.push(.comment(comment, showViewPostButton: false))
} label: {
HStack {
CommentBarView(depth: comment.depth + 1)
HStack {
Text("More Replies")
Image(systemName: Icons.forward)
}
.frame(maxWidth: .infinity)
.padding(.vertical, 8)
.foregroundStyle(palette.accent)
}
.background(
palette.secondaryGroupedBackground,
in: .rect(cornerRadius: Constants.main.standardSpacing)
)
}
.padding(.leading, CGFloat(comment.depth + 1 - tracker.proposedDepthOffset) * 10)
.buttonStyle(.plain)
MoreRepliesButton(tracker: tracker, comment: comment)
}
}
.padding(.horizontal, Constants.main.standardSpacing)
Expand Down
1 change: 0 additions & 1 deletion Mlem/App/Views/Shared/ExpandedPost/ExpandedPostView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ struct ExpandedPostView<Content: View>: View {
.task(id: tracker == nil) {
if let tracker, post.api == appState.firstApi, tracker.loadingState == .idle {
post.markRead()
await tracker.load()
}
}
} else {
Expand Down
Loading

0 comments on commit a5a5363

Please sign in to comment.