Skip to content

Commit

Permalink
Nickname setting and "Delete all favorites" button (#1473)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjmarf authored Dec 7, 2024
1 parent 8295865 commit 194e355
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 5 deletions.
8 changes: 8 additions & 0 deletions Mlem.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@
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 */; };
Expand Down Expand Up @@ -609,6 +611,8 @@
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>"; };
Expand Down Expand Up @@ -903,6 +907,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 @@ -2110,6 +2116,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 @@ -2149,6 +2156,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
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
4 changes: 3 additions & 1 deletion Mlem/App/Views/Shared/Navigation/SettingsPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI
enum SettingsPage: Hashable {
case root
case accounts, account
case accountGeneral, accountAdvanced, accountSignIn, accountChangeEmail
case accountGeneral, accountAdvanced, accountSignIn, accountChangeEmail, accountLocal
case general, links, sorting
case importExportSettings
case theme, icon
Expand All @@ -37,6 +37,8 @@ enum SettingsPage: Hashable {
AccountAdvancedSettingsView()
case .accountChangeEmail:
AccountEmailSettingsView()
case .accountLocal:
AccountLocalSettingsView()
case .accounts:
AccountListSettingsView()
case .general:
Expand Down
82 changes: 82 additions & 0 deletions Mlem/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@
},
"Apply to All Interaction Bars" : {

},
"Are you sure you want to delete all community favorites for this account? This cannot be undone." : {
"localizations" : {
"en-GB" : {
"stringUnit" : {
"state" : "translated",
"value" : "Are you sure you want to delete all community favourites for this account? This cannot be undone."
}
}
}
},
"Ask to confirm every time" : {

Expand Down Expand Up @@ -455,6 +465,16 @@
},
"Delete Account" : {

},
"Delete Community Favorites" : {
"localizations" : {
"en-GB" : {
"stringUnit" : {
"state" : "translated",
"value" : "Delete Community Favourites"
}
}
}
},
"Delete posts and comments" : {

Expand Down Expand Up @@ -820,6 +840,9 @@
},
"Local" : {

},
"Local Options" : {

},
"Lock" : {

Expand Down Expand Up @@ -919,6 +942,9 @@
},
"Next" : {

},
"Nickname" : {

},
"No" : {

Expand Down Expand Up @@ -1451,6 +1477,9 @@
}
}
}
},
"The name shown in the account switcher." : {

},
"The number of child comments that are shown in a chain before the \"More Replies\" button is shown." : {

Expand Down Expand Up @@ -1482,6 +1511,59 @@
},
"There were no recorded incidents today." : {

},
"These options are stored locally in Mlem and not on your Lemmy account." : {

},
"This account has %lld favorite communities." : {
"localizations" : {
"en" : {
"variations" : {
"plural" : {
"one" : {
"stringUnit" : {
"state" : "translated",
"value" : "This account has %lld favorite community."
}
},
"other" : {
"stringUnit" : {
"state" : "new",
"value" : "This account has %lld favorite communities."
}
}
}
}
},
"en-GB" : {
"variations" : {
"plural" : {
"one" : {
"stringUnit" : {
"state" : "translated",
"value" : "This account has %lld favourite community."
}
},
"other" : {
"stringUnit" : {
"state" : "translated",
"value" : "This account has %lld favourite communities."
}
}
}
}
}
}
},
"This account has no favorite communities." : {
"localizations" : {
"en-GB" : {
"stringUnit" : {
"state" : "translated",
"value" : "This account has no favourite communities."
}
}
}
},
"This community likely contains graphic or explicit content." : {

Expand Down

0 comments on commit 194e355

Please sign in to comment.