From 904d084e0543d709c6c12f32747264fa4cad5755 Mon Sep 17 00:00:00 2001 From: Jason Morley Date: Tue, 22 Nov 2022 23:06:16 +0000 Subject: [PATCH] fix: Separate fields for setting the rule destination folder and filename (#476) --- .../FileawayCore/Model/ComponentModel.swift | 2 +- .../FileawayCore/Model/RuleModel.swift | 22 +++++++++++++++++++ ios/Fileaway/Views/Editor/RuleView.swift | 12 ++++++---- .../Views/Settings/DestinationTable.swift | 9 +++----- .../Views/Settings/RuleDetailView.swift | 7 ++++++ .../Views/Settings/VariablesTable.swift | 2 +- 6 files changed, 42 insertions(+), 12 deletions(-) diff --git a/core/Sources/FileawayCore/Model/ComponentModel.swift b/core/Sources/FileawayCore/Model/ComponentModel.swift index 2b80582c..1925cf65 100644 --- a/core/Sources/FileawayCore/Model/ComponentModel.swift +++ b/core/Sources/FileawayCore/Model/ComponentModel.swift @@ -28,7 +28,7 @@ public class ComponentModel: ObservableObject, Identifiable, Hashable { @Published public var type: ComponentType public var variable: VariableModel? = nil - public init(value: String, type: ComponentType, variable: VariableModel?) { + public init(value: String, type: ComponentType, variable: VariableModel? = nil) { self.id = UUID() self.value = value self.type = type diff --git a/core/Sources/FileawayCore/Model/RuleModel.swift b/core/Sources/FileawayCore/Model/RuleModel.swift index 3fb0f0f7..6435f399 100644 --- a/core/Sources/FileawayCore/Model/RuleModel.swift +++ b/core/Sources/FileawayCore/Model/RuleModel.swift @@ -41,6 +41,28 @@ public class RuleModel: ObservableObject, Identifiable, CustomStringConvertible, return self.name } + public var folder: ComponentModel { + get { + return destination.first ?? ComponentModel(value: "", type: .text) + } + set { + if destination.isEmpty { + destination.append(newValue) + } else { + destination[0] = newValue + } + } + } + + public var filename: [ComponentModel] { + get { + return Array(destination[1...]) + } + set { + destination = [destination[0]] + newValue + } + } + public init(id: UUID, rootUrl: URL, name: String, variables: [VariableModel], destination: [ComponentModel]) { self.id = id self.rootUrl = rootUrl diff --git a/ios/Fileaway/Views/Editor/RuleView.swift b/ios/Fileaway/Views/Editor/RuleView.swift index bc3d411c..d44aac92 100644 --- a/ios/Fileaway/Views/Editor/RuleView.swift +++ b/ios/Fileaway/Views/Editor/RuleView.swift @@ -77,16 +77,20 @@ struct RuleView: View { ErrorText(text: editingRuleModel.validate() ? nil : "Variable names must be unique.") } + Section("Destination") { + ComponentItem(ruleModel: self.editingRuleModel, component: self.editingRuleModel.folder) + } + Section { - ForEach(editingRuleModel.destination) { component in + ForEach(editingRuleModel.filename) { component in ComponentItem(ruleModel: self.editingRuleModel, component: component) } .onMove { (fromOffsets, toOffset) in - self.editingRuleModel.destination.move(fromOffsets: fromOffsets, toOffset: toOffset) + self.editingRuleModel.filename.move(fromOffsets: fromOffsets, toOffset: toOffset) } - .onDelete { self.editingRuleModel.destination.remove(atOffsets: $0) } + .onDelete { self.editingRuleModel.filename.remove(atOffsets: $0) } } header: { - Text("Destination") + Text("Filename") } footer: { DestinationFooter(ruleModel: editingRuleModel) } diff --git a/macos/Fileaway/Views/Settings/DestinationTable.swift b/macos/Fileaway/Views/Settings/DestinationTable.swift index f326139b..fd1c24f6 100644 --- a/macos/Fileaway/Views/Settings/DestinationTable.swift +++ b/macos/Fileaway/Views/Settings/DestinationTable.swift @@ -41,10 +41,7 @@ struct DestinationTable: View { Table(selection: $selection) { TableColumn("Component") { componentModel in if componentModel.type == .text { - HStack { - ComponentValueTextField(componentModel: componentModel) - SetFolderButton(ruleModel: ruleModel, componentModel: componentModel) - } + ComponentValueTextField(componentModel: componentModel) } else { Text(ruleModel.name(for: componentModel)) .tokenAppearance() @@ -52,12 +49,12 @@ struct DestinationTable: View { } } } rows: { - ForEach(ruleModel.destination) { componentModel in + ForEach(ruleModel.filename) { componentModel in TableRow(componentModel) .itemProvider(componentModel.id, as: .component) } .onInsert(ComponentModel.ID.self, as: .component) { index, ids in - self.ruleModel.destination.move(ids: ids, toOffset: index) + self.ruleModel.filename.move(ids: ids, toOffset: index) } } .frame(minWidth: 500, minHeight: 200) diff --git a/macos/Fileaway/Views/Settings/RuleDetailView.swift b/macos/Fileaway/Views/Settings/RuleDetailView.swift index 6b9d3ef0..1b7d0e13 100644 --- a/macos/Fileaway/Views/Settings/RuleDetailView.swift +++ b/macos/Fileaway/Views/Settings/RuleDetailView.swift @@ -34,8 +34,15 @@ struct RuleDetailView: View { VariablesTable(ruleModel: ruleModel) Text("Destination") .font(.headline) + HStack { + TextField("", text: $ruleModel.folder.value) + SetFolderButton(ruleModel: ruleModel, componentModel: ruleModel.folder) + } + Text("Filename") + .font(.headline) DestinationTable(ruleModel: ruleModel) Spacer() + .layoutPriority(1) } .padding() } diff --git a/macos/Fileaway/Views/Settings/VariablesTable.swift b/macos/Fileaway/Views/Settings/VariablesTable.swift index 46215153..f4bbb780 100644 --- a/macos/Fileaway/Views/Settings/VariablesTable.swift +++ b/macos/Fileaway/Views/Settings/VariablesTable.swift @@ -47,7 +47,7 @@ struct VariablesTable: View { .onDeleteCommand { ruleModel.remove(variableIds: selection) } - .frame(minWidth: 500, minHeight: 200) + .frame(minWidth: 500, minHeight: 160) VStack { VStack {