1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 14:50:32 +05:30
yattee/Shared/Settings/InstancesSettings.swift

71 lines
2.1 KiB
Swift
Raw Normal View History

2021-09-25 13:48:22 +05:30
import Defaults
import SwiftUI
2021-10-23 17:21:02 +05:30
struct InstancesSettings: View {
2021-09-25 13:48:22 +05:30
@Default(.instances) private var instances
2021-10-18 04:36:00 +05:30
@EnvironmentObject<AccountsModel> private var accounts
2021-09-25 13:48:22 +05:30
@State private var selectedInstanceID: Instance.ID?
2021-10-21 03:51:50 +05:30
@State private var selectedAccount: Account?
2021-09-25 13:48:22 +05:30
@State private var presentingInstanceForm = false
@State private var savedFormInstanceID: Instance.ID?
var body: some View {
Group {
Section(header: Text("Instances")) {
2021-09-29 15:44:43 +05:30
ForEach(instances) { instance in
2021-10-17 04:18:58 +05:30
Group {
NavigationLink(instance.longDescription) {
2021-10-23 17:21:02 +05:30
AccountsSettings(instanceID: instance.id)
2021-10-17 04:18:58 +05:30
}
2021-09-29 15:44:43 +05:30
}
#if os(iOS)
2021-09-25 13:48:22 +05:30
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
2021-09-29 15:44:43 +05:30
removeInstanceButton(instance)
2021-09-25 13:48:22 +05:30
}
.buttonStyle(.plain)
2021-09-29 15:44:43 +05:30
#else
.contextMenu {
2021-09-29 15:44:43 +05:30
removeInstanceButton(instance)
2021-09-25 13:48:22 +05:30
}
2021-09-29 15:44:43 +05:30
#endif
2021-09-25 13:48:22 +05:30
}
2021-09-29 15:44:43 +05:30
addInstanceButton
}
#if os(iOS)
.listStyle(.insetGrouped)
2021-09-25 13:48:22 +05:30
#endif
}
2021-09-29 15:44:43 +05:30
.sheet(isPresented: $presentingInstanceForm) {
2021-10-23 17:21:02 +05:30
InstanceForm(savedInstanceID: $savedFormInstanceID)
2021-09-25 13:48:22 +05:30
}
}
private var addInstanceButton: some View {
Button("Add Instance...") {
presentingInstanceForm = true
2021-09-27 03:33:33 +05:30
}
}
2021-09-29 15:44:43 +05:30
private func removeInstanceButton(_ instance: Instance) -> some View {
Button("Remove", role: .destructive) {
if accounts.current?.instance == instance {
accounts.setCurrent(nil)
2021-10-18 04:36:00 +05:30
}
InstancesModel.remove(instance)
2021-09-29 15:44:43 +05:30
}
2021-09-27 03:33:33 +05:30
}
2021-09-25 13:48:22 +05:30
}
struct InstancesSettingsView_Previews: PreviewProvider {
static var previews: some View {
2021-09-27 03:33:33 +05:30
VStack {
2021-10-23 17:21:02 +05:30
InstancesSettings()
2021-09-27 03:33:33 +05:30
}
.frame(width: 400, height: 270)
2021-09-25 13:48:22 +05:30
}
}