From 826fbcd9360bc5dae7b97fbf238a7b748900b03b Mon Sep 17 00:00:00 2001 From: Sjmarf <78750526+Sjmarf@users.noreply.github.com> Date: Sun, 28 Jan 2024 10:11:01 +0000 Subject: [PATCH] Custom Account Sort Order (#870) --- Mlem/Enums/Settings/AccountSortMode.swift | 6 +++++- .../Components/AccountListView+Logic.swift | 15 +++++++++++---- .../Settings/Components/AccountListView.swift | 13 ++++++++++++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/Mlem/Enums/Settings/AccountSortMode.swift b/Mlem/Enums/Settings/AccountSortMode.swift index 3f0adfbed..51a259899 100644 --- a/Mlem/Enums/Settings/AccountSortMode.swift +++ b/Mlem/Enums/Settings/AccountSortMode.swift @@ -8,7 +8,7 @@ import SwiftUI enum AccountSortMode: String, CaseIterable { - case name, instance, mostRecent + case custom, name, instance, mostRecent var label: String { switch self { @@ -18,6 +18,8 @@ enum AccountSortMode: String, CaseIterable { return "Instance" case .mostRecent: return "Most Recent" + case .custom: + return "Custom Order" } } @@ -29,6 +31,8 @@ enum AccountSortMode: String, CaseIterable { return "at" case .mostRecent: return "clock" + case .custom: + return "line.3.horizontal.decrease" } } } diff --git a/Mlem/Views/Tabs/Settings/Components/AccountListView+Logic.swift b/Mlem/Views/Tabs/Settings/Components/AccountListView+Logic.swift index d57ea5a78..c836ff4f5 100644 --- a/Mlem/Views/Tabs/Settings/Components/AccountListView+Logic.swift +++ b/Mlem/Views/Tabs/Settings/Components/AccountListView+Logic.swift @@ -9,8 +9,10 @@ import SwiftUI extension AccountListView { var accounts: [SavedAccount] { - let accountSort = accountsTracker.savedAccounts.count == 2 ? .name : accountSort + let accountSort = accountsTracker.savedAccounts.count == 2 ? .custom : accountSort switch accountSort { + case .custom: + return accountsTracker.savedAccounts case .name: return accountsTracker.savedAccounts.sorted { $0.nicknameSortKey < $1.nicknameSortKey } case .instance: @@ -29,16 +31,17 @@ extension AccountListView { func getNameCategory(account: SavedAccount) -> String { guard let first = account.nickname.first else { return "Unknown" } - if "abcdefghijklmnopqrstuvwxyz".contains(first) { - return String(first) + if first.isLetter { + return String(first.lowercased()) } return "*" } var accountGroups: [AccountGroup] { switch accountSort { + case .custom: + return [.init(header: "Custom", accounts: accountsTracker.savedAccounts)] case .name: - return Dictionary( grouping: accountsTracker.savedAccounts, by: { getNameCategory(account: $0) } @@ -122,4 +125,8 @@ extension AccountListView { return groups } } + + func reorderAccount(fromOffsets: IndexSet, toOffset: Int) { + accountsTracker.savedAccounts.move(fromOffsets: fromOffsets, toOffset: toOffset) + } } diff --git a/Mlem/Views/Tabs/Settings/Components/AccountListView.swift b/Mlem/Views/Tabs/Settings/Components/AccountListView.swift index d4c73ec33..2b6a71576 100644 --- a/Mlem/Views/Tabs/Settings/Components/AccountListView.swift +++ b/Mlem/Views/Tabs/Settings/Components/AccountListView.swift @@ -23,7 +23,7 @@ struct QuickSwitcherView: View { struct AccountListView: View { @Environment(\.setAppFlow) var setFlow - @AppStorage("accountSort") var accountSort: AccountSortMode = .name + @AppStorage("accountSort") var accountSort: AccountSortMode = .custom @AppStorage("groupAccountSort") var groupAccountSort: Bool = false @EnvironmentObject var appState: AppState @@ -47,6 +47,10 @@ struct AccountListView: View { self.isQuickSwitcher = isQuickSwitcher } + var shouldAllowReordering: Bool { + (accountSort == .custom || accountsTracker.savedAccounts.count == 2) && !isQuickSwitcher + } + var body: some View { Group { if !isSwitching { @@ -73,6 +77,7 @@ struct AccountListView: View { ForEach(accounts, id: \.self) { account in AccountButtonView(account: account, isSwitching: $isSwitching) } + .onMove(perform: shouldAllowReordering ? reorderAccount : nil) } } Section { @@ -111,11 +116,17 @@ struct AccountListView: View { Label(sortMode.label, systemImage: sortMode.systemImage).tag(sortMode) } } + .onChange(of: accountSort) { newValue in + if newValue == .custom { + groupAccountSort = false + } + } if accountsTracker.savedAccounts.count > 3 { Divider() Toggle(isOn: $groupAccountSort) { Label("Grouped", systemImage: "square.stack.3d.up.fill") } + .disabled(accountSort == .custom) } } label: { HStack(alignment: .center, spacing: 2) {