-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor "Devices" tab from List to Table from SwiftUI (#84)
* refactor: migrate from List to Table * fix: formatting * fix: formatting * chore: apply suggestions * chore: address comments
- Loading branch information
Showing
6 changed files
with
268 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// ParametersTable.swift | ||
// MiniSim | ||
// | ||
// Created by Oskar Kwasniewski on 12/03/2023. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct ParametersTable: View { | ||
@StateObject private var viewModel: ViewModel = ViewModel() | ||
|
||
var body: some View { | ||
VStack { | ||
SectionHeader( | ||
title: "Android additional launch parameters", | ||
subTitle: "These parameters are passed to every android launch command. \nFor example: Cold boot, Run without audio etc." | ||
) | ||
Table(viewModel.parameters, selection: $viewModel.selection) { | ||
TableColumn("Title", value: \.title) | ||
.width(80) | ||
TableColumn("Command", value: \.command) | ||
TableColumn("Enabled") { | ||
Text("\(String($0.enabled))") | ||
} | ||
} | ||
.contextMenu { | ||
if viewModel.selection != nil { | ||
Button(viewModel.selectedParameter?.enabled == true ? "Disable" : "Enable") { | ||
viewModel.toggleEnabled(item: viewModel.selectedParameter?.id) | ||
} | ||
Divider() | ||
Button("Edit") { | ||
viewModel.showForm.toggle() | ||
} | ||
Divider() | ||
Button("Delete") { | ||
viewModel.deleteParameter(item: viewModel.selection) | ||
} | ||
} | ||
} | ||
|
||
HStack { | ||
Spacer() | ||
Button("Add New") { | ||
viewModel.selection = nil | ||
viewModel.showForm.toggle() | ||
} | ||
.buttonStyle(.borderedProminent) | ||
.keyboardShortcut("n", modifiers: .command) | ||
} | ||
} | ||
.onAppear { | ||
viewModel.loadData() | ||
} | ||
.sheet(isPresented: $viewModel.showForm) { | ||
ParametersTableForm(parameter: viewModel.selectedParameter, allParameters: viewModel.parameters, onSubmit: viewModel.handleForm) | ||
} | ||
} | ||
} | ||
|
||
struct ParametersTable_Previews: PreviewProvider { | ||
static var previews: some View { | ||
ParametersTable() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// | ||
// ParametersTableForm.swift | ||
// MiniSim | ||
// | ||
// Created by Jan Jaworski on 27/10/2023. | ||
// | ||
|
||
import SwiftUI | ||
import SymbolPicker | ||
import CodeEditor | ||
|
||
|
||
struct ParametersTableForm: View { | ||
var parameter: Parameter? | ||
var allParameters: [Parameter] | ||
var onSubmit: (_ parameter: Parameter, _ prevParameter: Parameter?) -> Void | ||
|
||
@Environment(\.dismiss) var dismiss | ||
@Environment(\.colorScheme) var colorScheme | ||
|
||
@StateObject private var viewModel: ViewModel = ViewModel() | ||
|
||
var body: some View { | ||
Form { | ||
TextField("Title", text: $viewModel.title) | ||
HStack(alignment: .top) { | ||
CodeEditor( | ||
source: $viewModel.command, | ||
language: .bash, | ||
theme: colorScheme == .dark ? .atelierSavannaDark : .atelierSavannaLight, | ||
flags: [.selectable, .editable, .smartIndent] | ||
) | ||
.cornerRadius(6) | ||
} | ||
Toggle("Enabled", isOn: $viewModel.enabled) | ||
.help("Determines if command is enabled.") | ||
.toggleStyle(.switch) | ||
|
||
HStack { | ||
HStack { | ||
Button("Cancel") { | ||
dismiss() | ||
viewModel.clearForm() | ||
} | ||
} | ||
Spacer() | ||
Button(parameter != nil ? "Update" : "Add") { | ||
onSubmit( | ||
Parameter( | ||
title: viewModel.title, | ||
command: viewModel.command, | ||
enabled: viewModel.enabled | ||
), | ||
parameter | ||
) | ||
viewModel.clearForm() | ||
} | ||
.buttonStyle(.borderedProminent) | ||
.disabled(viewModel.disableForm) | ||
.keyboardShortcut("s", modifiers: .command) | ||
} | ||
.padding(.top) | ||
} | ||
.padding() | ||
.frame(minWidth: 550, minHeight: 250) | ||
.onAppear { | ||
if let parameter { | ||
viewModel.onAppear( | ||
title: parameter.title, | ||
command: parameter.command, | ||
enabled: parameter.enabled | ||
) | ||
} | ||
viewModel.onAppear(allCommands: allParameters, isUpdating: parameter != nil) | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
MiniSim/Views/ParametersTable/ParametersTableFormViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// ParametersTableFormViewModel.swift | ||
// MiniSim | ||
// | ||
// Created by Jan Jaworski on 27/10/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
extension ParametersTableForm { | ||
class ViewModel: ObservableObject { | ||
@Published var title = "" | ||
@Published var command = "" | ||
@Published var enabled = true | ||
|
||
var allParameters: [Parameter] = [] | ||
var isUpdating: Bool = false | ||
|
||
func onAppear(title: String = "", command: String = "", enabled: Bool = true) { | ||
self.title = title | ||
self.command = command | ||
self.enabled = enabled | ||
} | ||
|
||
func onAppear(allCommands: [Parameter], isUpdating: Bool = false) { | ||
self.allParameters = allCommands | ||
self.isUpdating = isUpdating | ||
} | ||
|
||
var disableForm: Bool { | ||
let hasDuplicatedName = !isUpdating && allParameters.contains(where: { $0.title.lowercased() == title.lowercased() }) | ||
return title.isEmpty || command.isEmpty || hasDuplicatedName | ||
} | ||
|
||
func clearForm() { | ||
title = "" | ||
command = "" | ||
enabled = true | ||
} | ||
} | ||
} |
Oops, something went wrong.