From 0ab78a43358ec473404e78401ecf5d8ac7ec0e50 Mon Sep 17 00:00:00 2001 From: Sjmarf <78750526+Sjmarf@users.noreply.github.com> Date: Sun, 21 Jan 2024 16:05:41 +0000 Subject: [PATCH] Update --- Mlem/Enums/Settings/AccountSortMode.swift | 6 +++++- .../Settings/Components/AccountListView+Logic.swift | 13 ++++++++++--- .../Tabs/Settings/Components/AccountListView.swift | 7 +++++++ 3 files changed, 22 insertions(+), 4 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 845d475b7..39bb6aae4 100644 --- a/Mlem/Views/Tabs/Settings/Components/AccountListView+Logic.swift +++ b/Mlem/Views/Tabs/Settings/Components/AccountListView+Logic.swift @@ -11,6 +11,8 @@ extension AccountListView { var accounts: [SavedAccount] { let accountSort = accountsTracker.savedAccounts.count == 2 ? .name : 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) } @@ -119,4 +122,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..f259d4e21 100644 --- a/Mlem/Views/Tabs/Settings/Components/AccountListView.swift +++ b/Mlem/Views/Tabs/Settings/Components/AccountListView.swift @@ -73,6 +73,7 @@ struct AccountListView: View { ForEach(accounts, id: \.self) { account in AccountButtonView(account: account, isSwitching: $isSwitching) } + .onMove(perform: accountSort == .custom && !isQuickSwitcher ? reorderAccount : nil) } } Section { @@ -111,11 +112,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) {