yattee/Shared/Settings/InstancesSettings.swift

71 lines
2.0 KiB
Swift
Raw Normal View History

2021-09-25 08:18:22 +00:00
import Defaults
import SwiftUI
2021-10-23 11:51:02 +00:00
struct InstancesSettings: View {
2021-09-25 08:18:22 +00:00
@Default(.instances) private var instances
2021-10-17 23:06:00 +00:00
@EnvironmentObject<AccountsModel> private var accounts
2021-09-25 08:18:22 +00:00
@State private var selectedInstanceID: Instance.ID?
2021-10-20 22:21:50 +00:00
@State private var selectedAccount: Account?
2021-09-25 08:18:22 +00:00
@State private var presentingInstanceForm = false
@State private var savedFormInstanceID: Instance.ID?
var body: some View {
Group {
2021-11-04 22:01:27 +00:00
Section(header: SettingsHeader(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) {
2021-10-23 11:51:02 +00:00
AccountsSettings(instanceID: instance.id)
2021-10-16 22:48:58 +00:00
}
2021-09-29 10:14:43 +00:00
}
#if os(iOS)
2021-11-08 16:29:35 +00:00
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
removeInstanceButton(instance)
}
.buttonStyle(.plain)
2021-09-29 10:14:43 +00:00
#else
2021-11-08 16:29:35 +00:00
.contextMenu {
removeInstanceButton(instance)
}
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)
2021-11-08 16:29:35 +00:00
.listStyle(.insetGrouped)
2021-09-25 08:18:22 +00:00
#endif
}
2021-09-29 10:14:43 +00:00
.sheet(isPresented: $presentingInstanceForm) {
2021-10-23 11:51:02 +00:00
InstanceForm(savedInstanceID: $savedFormInstanceID)
2021-09-25 08:18:22 +00:00
}
}
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 {
2021-10-23 11:51:02 +00:00
InstancesSettings()
2021-09-26 22:03:33 +00:00
}
.frame(width: 400, height: 270)
2021-09-25 08:18:22 +00:00
}
}