yattee/Shared/Settings/InstancesSettingsView.swift

74 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
@EnvironmentObject<InvidiousAPI> private var api
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 {
2021-09-29 10:14:43 +00:00
Section(header: Text("Instances"), footer: DefaultAccountHint()) {
ForEach(instances) { instance in
NavigationLink(instance.description) {
AccountsSettingsView(instanceID: instance.id)
}
#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) {
instancesModel.remove(instance)
}
2021-09-26 22:03:33 +00:00
}
2021-09-29 10:14:43 +00:00
private func resetDefaultAccount() {
instancesModel.resetDefaultAccount()
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
}
}