VMConfirmActionModifier.swift 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // Copyright © 2020 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. import SwiftUI
  17. enum ConfirmAction: Int, Identifiable {
  18. case confirmCloneVM
  19. case confirmDeleteVM
  20. case confirmDeleteShortcut
  21. case confirmStopVM
  22. case confirmMoveVM
  23. var id: Int { rawValue }
  24. }
  25. struct VMConfirmActionModifier: ViewModifier {
  26. let vm: VMData
  27. @Binding var confirmAction: ConfirmAction?
  28. let onConfirm: () -> Void
  29. @EnvironmentObject private var data: UTMData
  30. func body(content: Content) -> some View {
  31. content.alert(item: $confirmAction) { action in
  32. switch action {
  33. case .confirmCloneVM:
  34. if vm.isShortcut {
  35. return Alert(title: Text("Do you want to copy this VM and all its data to internal storage?"), primaryButton: .cancel(), secondaryButton: .default(Text("Yes")) {
  36. data.busyWorkAsync {
  37. try await data.clone(vm: vm)
  38. }
  39. onConfirm()
  40. })
  41. } else {
  42. return Alert(title: Text("Do you want to duplicate this VM and all its data?"), primaryButton: .cancel(), secondaryButton: .default(Text("Yes")) {
  43. data.busyWorkAsync {
  44. try await data.clone(vm: vm)
  45. }
  46. onConfirm()
  47. })
  48. }
  49. case .confirmDeleteVM:
  50. return Alert(title: Text("Do you want to delete this VM and all its data?"), primaryButton: .cancel(), secondaryButton: .destructive(Text("Delete")) {
  51. data.busyWorkAsync {
  52. try await data.delete(vm: vm)
  53. }
  54. onConfirm()
  55. })
  56. case .confirmDeleteShortcut:
  57. return Alert(title: Text("Do you want to remove this shortcut? The data will not be deleted."), primaryButton: .cancel(), secondaryButton: .destructive(Text("Remove")) {
  58. data.busyWorkAsync {
  59. await data.listRemove(vm: vm)
  60. }
  61. onConfirm()
  62. })
  63. case .confirmStopVM:
  64. return Alert(title: Text("Do you want to force stop this VM and lose all unsaved data?"), primaryButton: .cancel(), secondaryButton: .destructive(Text("Stop")) {
  65. data.stop(vm: vm)
  66. onConfirm()
  67. })
  68. case .confirmMoveVM:
  69. return Alert(title: Text("Do you want to move this VM to another location? This will copy the data to the new location, delete the data from the original location, and then create a shortcut."), primaryButton: .cancel(), secondaryButton: .destructive(Text("Confirm")) {
  70. onConfirm()
  71. })
  72. }
  73. }
  74. }
  75. }