mirror of
https://github.com/yattee/yattee.git
synced 2025-08-05 02:04:07 +00:00
New account selection menu
This commit is contained in:
@@ -3,6 +3,7 @@ import SwiftUI
|
||||
|
||||
struct AccountsMenuView: View {
|
||||
@ObservedObject private var model = AccountsModel.shared
|
||||
private var navigation = NavigationModel.shared
|
||||
|
||||
@Default(.accounts) private var accounts
|
||||
@Default(.instances) private var instances
|
||||
@@ -11,22 +12,8 @@ struct AccountsMenuView: View {
|
||||
|
||||
@ViewBuilder var body: some View {
|
||||
if !instances.isEmpty {
|
||||
Menu {
|
||||
ForEach(allAccounts, id: \.id) { account in
|
||||
Button {
|
||||
model.setCurrent(account)
|
||||
} label: {
|
||||
HStack {
|
||||
Text(accountButtonTitle(account: account))
|
||||
|
||||
Spacer()
|
||||
|
||||
if model.current == account {
|
||||
Image(systemName: "checkmark")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Button {
|
||||
navigation.presentingAccounts = true
|
||||
} label: {
|
||||
HStack {
|
||||
if !accountPickerDisplaysUsername || !(model.current?.isPublic ?? true) {
|
||||
@@ -39,7 +26,6 @@ struct AccountsMenuView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
.disabled(allAccounts.isEmpty)
|
||||
.transaction { t in t.animation = .none }
|
||||
}
|
||||
}
|
||||
@@ -47,13 +33,4 @@ struct AccountsMenuView: View {
|
||||
private var label: some View {
|
||||
Label(model.current?.description ?? "Select Account", systemImage: "globe")
|
||||
}
|
||||
|
||||
private var allAccounts: [Account] {
|
||||
let anonymousAccounts = accountPickerDisplaysAnonymousAccounts ? instances.map(\.anonymousAccount) : []
|
||||
return accounts + anonymousAccounts + [model.publicAccount].compactMap { $0 }
|
||||
}
|
||||
|
||||
private func accountButtonTitle(account: Account) -> String {
|
||||
account.isPublic ? account.description : "\(account.description) — \(account.instance.shortDescription)"
|
||||
}
|
||||
}
|
||||
|
112
Shared/Navigation/AccountsView.swift
Normal file
112
Shared/Navigation/AccountsView.swift
Normal file
@@ -0,0 +1,112 @@
|
||||
import SwiftUI
|
||||
|
||||
struct AccountsView: View {
|
||||
@StateObject private var model = AccountsViewModel()
|
||||
@Environment(\.presentationMode) private var presentationMode
|
||||
|
||||
var body: some View {
|
||||
#if os(macOS)
|
||||
VStack(alignment: .leading) {
|
||||
closeButton
|
||||
.padding([.leading, .top])
|
||||
|
||||
list
|
||||
}
|
||||
.frame(minWidth: 500, maxWidth: 800, minHeight: 350, maxHeight: 700)
|
||||
|
||||
#else
|
||||
NavigationView {
|
||||
list
|
||||
#if os(iOS)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .navigationBarLeading) {
|
||||
closeButton
|
||||
}
|
||||
}
|
||||
#endif
|
||||
.navigationTitle("Accounts")
|
||||
}
|
||||
#if os(tvOS)
|
||||
.frame(maxWidth: 1000)
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
var list: some View {
|
||||
List {
|
||||
if !model.accounts.isEmpty {
|
||||
Section(header: Text("Your Accounts")) {
|
||||
ForEach(model.sortedAccounts) { account in
|
||||
accountButton(account)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !model.instances.isEmpty {
|
||||
Section(header: Text("Browse without account")) {
|
||||
ForEach(model.instances) { instance in
|
||||
accountButton(instance.anonymousAccount)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let account = model.publicAccount {
|
||||
Section(header: Text("Public account")) {
|
||||
accountButton(account)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func accountButton(_ account: Account) -> some View {
|
||||
Button {
|
||||
presentationMode.wrappedValue.dismiss()
|
||||
AccountsModel.shared.setCurrent(account)
|
||||
} label: {
|
||||
HStack {
|
||||
instanceImage(account.instance)
|
||||
|
||||
if !account.anonymous {
|
||||
Text(account.description)
|
||||
}
|
||||
|
||||
Text((account.anonymous ? "" : "@ ") + account.urlHost)
|
||||
.lineLimit(1)
|
||||
.truncationMode(.head)
|
||||
.foregroundColor(account.anonymous ? .primary : .secondary)
|
||||
|
||||
Spacer()
|
||||
|
||||
Image(systemName: "checkmark")
|
||||
.foregroundColor(.accentColor)
|
||||
.opacity(account == model.currentAccount ? 1 : 0)
|
||||
}
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
|
||||
var closeButton: some View {
|
||||
Button(action: { presentationMode.wrappedValue.dismiss() }) {
|
||||
Label("Done", systemImage: "xmark")
|
||||
}
|
||||
#if os(macOS)
|
||||
.labelStyle(.titleOnly)
|
||||
#endif
|
||||
#if !os(tvOS)
|
||||
.keyboardShortcut(.cancelAction)
|
||||
#endif
|
||||
}
|
||||
|
||||
func instanceImage(_ instance: Instance) -> some View {
|
||||
Image(instance.app.rawValue.capitalized)
|
||||
.resizable()
|
||||
.frame(width: 30, height: 30)
|
||||
}
|
||||
}
|
||||
|
||||
struct AccountsView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
AccountsView()
|
||||
}
|
||||
}
|
36
Shared/Navigation/AccountsViewModel.swift
Normal file
36
Shared/Navigation/AccountsViewModel.swift
Normal file
@@ -0,0 +1,36 @@
|
||||
import Foundation
|
||||
|
||||
final class AccountsViewModel: ObservableObject {
|
||||
typealias AreInIncreasingOrder = (Account, Account) -> Bool
|
||||
|
||||
var accounts: [Account] { AccountsModel.shared.all }
|
||||
|
||||
var sortedAccounts: [Account] {
|
||||
accounts.sorted { lhs, rhs in
|
||||
let predicates: [AreInIncreasingOrder] = [
|
||||
{ ($0.app ?? .local).rawValue < ($1.app ?? .local).rawValue },
|
||||
{ $0.urlHost < $1.urlHost },
|
||||
{ $0.description < $1.description }
|
||||
]
|
||||
|
||||
for predicate in predicates {
|
||||
if !predicate(lhs, rhs), !predicate(rhs, lhs) {
|
||||
continue
|
||||
}
|
||||
|
||||
return predicate(lhs, rhs)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
var publicAccount: Account? { AccountsModel.shared.publicAccount }
|
||||
var currentAccount: Account? { AccountsModel.shared.current }
|
||||
|
||||
var instances: [Instance] { InstancesModel.shared.all }
|
||||
|
||||
func accountsOfInstance(_ instance: Instance) -> [Account] {
|
||||
accounts.filter { $0.instance.apiURL == instance.apiURL }.sorted { $0.name < $1.name }
|
||||
}
|
||||
}
|
@@ -77,12 +77,6 @@ struct AppSidebarNavigation: View {
|
||||
|
||||
ToolbarItemGroup(placement: accountsMenuToolbarItemPlacement) {
|
||||
AccountsMenuView()
|
||||
.help(
|
||||
"Switch Instances and Accounts\n" +
|
||||
"Current Instance: \n" +
|
||||
"\(accounts.current?.urlString ?? "Not Set")\n" +
|
||||
"Current User: \(accounts.current?.description ?? "Not set")"
|
||||
)
|
||||
}
|
||||
|
||||
#if os(macOS)
|
||||
|
@@ -81,7 +81,6 @@ struct AppTabNavigation: View {
|
||||
private var subscriptionsNavigationView: some View {
|
||||
NavigationView {
|
||||
LazyView(SubscriptionsView())
|
||||
.toolbar { toolbarContent }
|
||||
}
|
||||
.tabItem {
|
||||
Label("Subscriptions", systemImage: "star.circle.fill")
|
||||
@@ -115,7 +114,6 @@ struct AppTabNavigation: View {
|
||||
private var trendingNavigationView: some View {
|
||||
NavigationView {
|
||||
LazyView(TrendingView())
|
||||
.toolbar { toolbarContent }
|
||||
}
|
||||
.tabItem {
|
||||
Label("Trending", systemImage: "chart.bar.fill")
|
||||
@@ -127,7 +125,6 @@ struct AppTabNavigation: View {
|
||||
private var playlistsNavigationView: some View {
|
||||
NavigationView {
|
||||
LazyView(PlaylistsView())
|
||||
.toolbar { toolbarContent }
|
||||
}
|
||||
.tabItem {
|
||||
Label("Playlists", systemImage: "list.and.film")
|
||||
|
@@ -69,6 +69,11 @@ struct ContentView: View {
|
||||
SettingsView()
|
||||
}
|
||||
)
|
||||
.background(
|
||||
EmptyView().sheet(isPresented: $navigation.presentingAccounts) {
|
||||
AccountsView()
|
||||
}
|
||||
)
|
||||
#if !os(tvOS)
|
||||
.fileImporter(
|
||||
isPresented: $navigation.presentingFileImporter,
|
||||
|
Reference in New Issue
Block a user