yattee/Shared/Settings/InstancesSettingsView.swift

75 lines
2.3 KiB
Swift
Raw Normal View History

2021-09-25 08:18:22 +00:00
import Defaults
import SwiftUI
struct InstancesSettingsView: View {
@Default(.instances) private var instances
2021-10-17 23:06:00 +00:00
@EnvironmentObject<AccountsModel> private var accounts
2021-09-29 10:14:43 +00:00
@EnvironmentObject<InstancesModel> private var instancesModel
2021-09-25 08:18:22 +00:00
@EnvironmentObject<SubscriptionsModel> private var subscriptions
@EnvironmentObject<PlaylistsModel> private var playlists
@State private var selectedInstanceID: Instance.ID?
@State private var selectedAccount: Instance.Account?
@State private var presentingInstanceForm = false
@State private var savedFormInstanceID: Instance.ID?
var body: some View {
Group {
Section(header: Text("Instances")) {
2021-09-29 10:14:43 +00:00
ForEach(instances) { instance in
2021-10-16 22:48:58 +00:00
Group {
NavigationLink(instance.longDescription) {
AccountsSettingsView(instanceID: instance.id)
}
2021-09-29 10:14:43 +00:00
}
#if os(iOS)
2021-09-25 08:18:22 +00:00
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
2021-09-29 10:14:43 +00:00
removeInstanceButton(instance)
2021-09-25 08:18:22 +00:00
}
.buttonStyle(.plain)
2021-09-29 10:14:43 +00:00
#else
.contextMenu {
2021-09-29 10:14:43 +00:00
removeInstanceButton(instance)
2021-09-25 08:18:22 +00:00
}
2021-09-29 10:14:43 +00:00
#endif
2021-09-25 08:18:22 +00:00
}
2021-09-29 10:14:43 +00:00
addInstanceButton
}
#if os(iOS)
.listStyle(.insetGrouped)
2021-09-25 08:18:22 +00:00
#endif
}
2021-09-29 10:14:43 +00:00
.sheet(isPresented: $presentingInstanceForm) {
2021-09-25 08:18:22 +00:00
InstanceFormView(savedInstanceID: $savedFormInstanceID)
}
}
private var addInstanceButton: some View {
Button("Add Instance...") {
presentingInstanceForm = true
2021-09-26 22:03:33 +00:00
}
}
2021-09-29 10:14:43 +00:00
private func removeInstanceButton(_ instance: Instance) -> some View {
Button("Remove", role: .destructive) {
if accounts.current?.instance == instance {
accounts.setCurrent(nil)
2021-10-17 23:06:00 +00:00
}
InstancesModel.remove(instance)
2021-09-29 10:14:43 +00:00
}
2021-09-26 22:03:33 +00:00
}
2021-09-25 08:18:22 +00:00
}
struct InstancesSettingsView_Previews: PreviewProvider {
static var previews: some View {
2021-09-26 22:03:33 +00:00
VStack {
InstancesSettingsView()
}
.frame(width: 400, height: 270)
.environmentObject(InstancesModel())
2021-09-25 08:18:22 +00:00
}
}