Skip to content
Open
13 changes: 13 additions & 0 deletions FormEditor/Classes/FECustom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ public class FECustom: PFEParam {
self.configureCell = configureCell
}

public func copy(from: PFEParam) {
guard let from = from as? FECustom else {
return
}

self.id = from.id
self.cellReuseId = from.cellReuseId
self.cellNibName = from.cellNibName
self.visible = from.visible
self.onSelect = from.onSelect
self.configureCell = from.configureCell
}

public func configure(cell: UITableViewCell, facade: FormParamFacade) {
configureCell?(cell)
}
Expand Down
17 changes: 17 additions & 0 deletions FormEditor/Classes/FEDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ public class FEDate: PFEParam {
self.valueChangeListener = listener
}

public func copy(from: PFEParam) {
guard let from = from as? FEDate else {
return
}

self.id = from.id
self.title = from.title
self.value = from.value
self.displayableValueFormat = from.displayableValueFormat
self.readOnly = from.readOnly
self.visible = from.visible
self.accessibilityIdentifier = from.accessibilityIdentifier
self.minDate = from.minDate
self.maxDate = from.maxDate
self.valueChangeListener = from.valueChangeListener
}

public func configure(cell: UITableViewCell, facade: FormParamFacade) {
if let paramCell = cell as? FEDateCell {
paramCell.configure(facade: facade)
Expand Down
9 changes: 7 additions & 2 deletions FormEditor/Classes/FEDateCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,17 @@ class FEDateCell: UITableViewCell, UITextFieldDelegate, FormParamFacadeDelegate
}

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
let result = enableNavigationToolbar()
dateTextField.inputView = datePicker()
return result
}

internal func enableNavigationToolbar() -> Bool {
guard let facade = self.facade else {
return false
}

dateTextField.enableParamsNavigationToolbar(preferences: facade.preferences, moveNextClosure: facade.editNextParam, movePreviousClosure: facade.editPreviousParam)
dateTextField.inputView = datePicker()
dateTextField.enableParamsNavigationToolbar(preferences: facade.preferences, moveNextClosure: facade.functionForMoveToNextParam, movePreviousClosure: facade.functionForMoveToPreviousParam)
return true
}

Expand Down
15 changes: 15 additions & 0 deletions FormEditor/Classes/FELabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ public class FELabel: PFEParam {
self.onSelect = onSelect
}

public func copy(from: PFEParam) {
guard let from = from as? FELabel else {
return
}

self.id = from.id
self.title = from.title
self.value = from.value
self.mask = from.mask
self.alwaysShowTitle = from.alwaysShowTitle
self.visible = from.visible
self.accessibilityIdentifier = from.accessibilityIdentifier
self.onSelect = from.onSelect
}

public var canReceiveFocus: Bool {
return false
}
Expand Down
19 changes: 19 additions & 0 deletions FormEditor/Classes/FESelector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ public class FESelector: PFEParam {
}
}

public func copy(from: PFEParam) {
guard let from = from as? FESelector else {
return
}

self.id = from.id
self.title = from.title
self.value = from.value
self.emptyVisibleValue = from.emptyVisibleValue
self.displayableValueFormat = from.displayableValueFormat
self.readOnly = from.readOnly
self.visible = from.visible
self.accessibilityIdentifier = from.accessibilityIdentifier
self.valueChangeListener = from.valueChangeListener

self.items = []
self.items?.append(contentsOf: (from.items ?? []))
}

public func configure(cell: UITableViewCell, facade: FormParamFacade) {
if let paramCell = cell as? FESelectorCell {
paramCell.configure(facade: facade)
Expand Down
2 changes: 1 addition & 1 deletion FormEditor/Classes/FESelectorCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class FESelectorCell: UITableViewCell, UITextFieldDelegate, UIPickerViewDelegate
return false
}
valueTextField.inputView = pickerView()
valueTextField.enableParamsNavigationToolbar(preferences: facade.preferences, moveNextClosure: facade.editNextParam, movePreviousClosure: facade.editPreviousParam)
valueTextField.enableParamsNavigationToolbar(preferences: facade.preferences, moveNextClosure: facade.functionForMoveToNextParam, movePreviousClosure: facade.functionForMoveToPreviousParam)
return true
}

Expand Down
14 changes: 14 additions & 0 deletions FormEditor/Classes/FESwitch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ public class FESwitch: PFEParam {
self.listener = listener
}

public func copy(from: PFEParam) {
guard let from = from as? FESwitch else {
return
}

self.id = from.id
self.title = from.title
self.value = from.value
self.readOnly = from.readOnly
self.visible = from.visible
self.accessibilityIdentifier = from.accessibilityIdentifier
self.listener = from.listener
}

public func configure(cell: UITableViewCell, facade: FormParamFacade) {
if let paramCell = cell as? FESwitchCell {
paramCell.configure(facade: facade)
Expand Down
20 changes: 20 additions & 0 deletions FormEditor/Classes/FEText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ public class FEText: PFEParam {
self.accessoryImageNames = accessoryImageNames
}

public func copy(from: PFEParam) {
guard let from = from as? FEText else {
return
}

self.id = from.id
self.title = from.title
self.value = from.value
self.keyboardType = from.keyboardType
self.autocapitalizationType = from.autocapitalizationType
self.inputMask = from.inputMask
self.inputMaskForwardDecoration = from.inputMaskForwardDecoration
self.maxLength = from.maxLength
self.readOnly = from.readOnly
self.visible = from.visible
self.accessibilityIdentifier = from.accessibilityIdentifier
self.valueChangeListener = from.valueChangeListener
self.accessoryImageNames = from.accessoryImageNames
}

public var canReceiveFocus: Bool {
return !readOnly && isVisible()
}
Expand Down
14 changes: 14 additions & 0 deletions FormEditor/Classes/FETextArea.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ public class FETextArea: PFEParam {
self.valueChangeListener = listener
}

public func copy(from: PFEParam) {
guard let from = from as? FETextArea else {
return
}

self.id = from.id
self.title = from.title
self.value = from.value
self.readOnly = from.readOnly
self.visible = from.visible
self.accessibilityIdentifier = from.accessibilityIdentifier
self.valueChangeListener = from.valueChangeListener
}

public var canReceiveFocus: Bool {
return !readOnly && isVisible()
}
Expand Down
8 changes: 7 additions & 1 deletion FormEditor/Classes/FETextAreaCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@ class FETextAreaCell: UITableViewCell, UITextViewDelegate, FormParamFacadeDelega
}

func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
let result = enableNavigationToolbar()
return result
}

internal func enableNavigationToolbar() -> Bool {
guard let facade = self.facade else {
return false
}
valueTextView.enableParamsNavigationToolbar(preferences: facade.preferences, moveNextClosure: facade.editNextParam, movePreviousClosure: facade.editPreviousParam)

valueTextView.enableParamsNavigationToolbar(preferences: facade.preferences, moveNextClosure: facade.functionForMoveToNextParam, movePreviousClosure: facade.functionForMoveToPreviousParam)
return true
}

Expand Down
8 changes: 7 additions & 1 deletion FormEditor/Classes/FETextCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,16 @@ class FETextCell: UITableViewCell, UITextFieldDelegate, FormParamFacadeDelegate
}

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
let result = enableNavigationToolbar()
return result
}

internal func enableNavigationToolbar() -> Bool {
guard let facade = self.facade else {
return false
}
valueTextField.enableParamsNavigationToolbar(preferences: facade.preferences, moveNextClosure: facade.editNextParam, movePreviousClosure: facade.editPreviousParam)

valueTextField.enableParamsNavigationToolbar(preferences: facade.preferences, moveNextClosure: facade.functionForMoveToNextParam, movePreviousClosure: facade.functionForMoveToPreviousParam)
return true
}

Expand Down
55 changes: 55 additions & 0 deletions FormEditor/Classes/FormEditorFacade.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ class FormEditorFacade {
let addedSections = self.addedSections(oldSections: oldSections, newSections: newSections)
let deletedSections = self.deletedSections(oldSections: oldSections, newSections: newSections)

replaceParams(fromOldSections: oldSections, toNewSections: newSections)

visibleSections = newSections

delegate.beginUpdates()
Expand All @@ -242,10 +244,45 @@ class FormEditorFacade {
}
updatedItems.forEach({delegate.reload(indexPath: $0)})

// if the first item in the list was changed, then update NavigationBar's buttons
if let firstVisibleParamOld = firstVisibleFocusableParam(fromSections: oldSections), let firstVisibleParamNew = firstVisibleFocusableParam(fromSections: newSections), firstVisibleParamOld.id != firstVisibleParamNew.id {
updateNavigationBar( forParam: firstVisibleParamOld, sections: oldSections)
updateNavigationBar( forParam: firstVisibleParamNew, sections: newSections)
}
// if the last item in the list was changed, then update NavigationBar's buttons
if let lastVisibleParamOld = lastVisibleFocusableParam(fromSections: oldSections), let lastVisibleParamNew = lastVisibleFocusableParam(fromSections: newSections), lastVisibleParamOld.id != lastVisibleParamNew.id {
updateNavigationBar( forParam: lastVisibleParamOld, sections: oldSections)
updateNavigationBar( forParam: lastVisibleParamNew, sections: newSections)
}

delegate.endUpdates()
}

private func updateNavigationBar( forParam: PFEParam, sections: [FESection]) {
if let indexPath = indexPath(param: forParam, sections: sections) {
if let cell = (form as! UITableViewController).tableView.cellForRow(at: indexPath) {
if let cell = (cell as? FEDateCell) {
let _ = cell.enableNavigationToolbar()
}

if let cell = (cell as? FETextCell) {
let _ = cell.enableNavigationToolbar()
}

if let cell = (cell as? FETextAreaCell) {
let _ = cell.enableNavigationToolbar()
}
}
}
}

private func firstVisibleFocusableParam( fromSections sections: [FESection]) -> PFEParam? {
return sections.first?.params?.first(where: { $0.isVisible() && $0.canReceiveFocus })
}

private func lastVisibleFocusableParam( fromSections sections: [FESection]) -> PFEParam? {
return sections.last?.params?.filter({ $0.isVisible() && $0.canReceiveFocus }).last
}

private func addedSections(oldSections: [FESection], newSections: [FESection]) -> [Int] {
guard newSections.count > oldSections.count else {
Expand Down Expand Up @@ -359,6 +396,24 @@ class FormEditorFacade {
}
return updatedItems
}

private func replaceParams(fromOldSections oldSections: [FESection], toNewSections newSections: [FESection]) {
for (newSectionIndex, newSection) in newSections.enumerated() {
for (newParamIndex, newParam) in (newSection.params ?? []).enumerated() {
for oldSection in oldSections {
for oldParam in oldSection.params ?? [] {
if newParam.id == oldParam.id {
if !newParam.equals(other: oldParam) {
oldParam.copy(from: newParam)
}
newSections[newSectionIndex].params?[newParamIndex] = oldParam
}
}
}
}
}
}

}


Expand Down
24 changes: 24 additions & 0 deletions FormEditor/Classes/FormParamFacade.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ public class FormParamFacade {

// MARK: Взаимодействие с другими параметрами

var isPreviousParamExists: Bool {
return formEditorFacade?.previous(param: param) != nil
}

var isNextParamExists: Bool {
return formEditorFacade?.next(param: param) != nil
}

public var functionForMoveToNextParam: (()-> Void)? {
guard isNextParamExists else {
return nil
}

return editNextParam
}

public var functionForMoveToPreviousParam: (()-> Void)? {
guard isPreviousParamExists else {
return nil
}

return editPreviousParam
}

public func editPreviousParam() {
if let previousParam = formEditorFacade?.previous(param: param) {
formEditorFacade?.select(param: previousParam, scrollToPosition: true)
Expand Down
2 changes: 2 additions & 0 deletions FormEditor/Classes/PFEParam.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public protocol PFEParam: class {
func isVisible() -> Bool

func equals(other: PFEParam) -> Bool

func copy(from: PFEParam)
}
Loading