Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjmarf committed Jan 21, 2024
1 parent a41126b commit 0ab78a4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Mlem/Enums/Settings/AccountSortMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

enum AccountSortMode: String, CaseIterable {
case name, instance, mostRecent
case custom, name, instance, mostRecent

var label: String {
switch self {
Expand All @@ -18,6 +18,8 @@ enum AccountSortMode: String, CaseIterable {
return "Instance"
case .mostRecent:
return "Most Recent"
case .custom:
return "Custom Order"
}
}

Expand All @@ -29,6 +31,8 @@ enum AccountSortMode: String, CaseIterable {
return "at"
case .mostRecent:
return "clock"
case .custom:
return "line.3.horizontal.decrease"
}
}
}
13 changes: 10 additions & 3 deletions Mlem/Views/Tabs/Settings/Components/AccountListView+Logic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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) }
Expand Down Expand Up @@ -119,4 +122,8 @@ extension AccountListView {
return groups
}
}

func reorderAccount(fromOffsets: IndexSet, toOffset: Int) {
accountsTracker.savedAccounts.move(fromOffsets: fromOffsets, toOffset: toOffset)
}
}
7 changes: 7 additions & 0 deletions Mlem/Views/Tabs/Settings/Components/AccountListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 0ab78a4

Please sign in to comment.