changed behavior to align with android.

This commit is contained in:
Kevin G Christiano 2021-06-29 13:11:40 -04:00
parent ada936eff6
commit f8dce8503b
3 changed files with 52 additions and 53 deletions

View File

@ -7,7 +7,7 @@
// //
@objcMembers public class CheckboxModel: NSObject, MoleculeModelProtocol, SelectableMoleculeModelProtocol, FormFieldProtocol { @objcMembers public class CheckboxModel: MoleculeModelProtocol, SelectableMoleculeModelProtocol, FormFieldProtocol {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Properties // MARK: - Properties
//-------------------------------------------------- //--------------------------------------------------
@ -15,7 +15,7 @@
public static var identifier: String = "checkbox" public static var identifier: String = "checkbox"
public var backgroundColor: Color? public var backgroundColor: Color?
public var accessibilityIdentifier: String? public var accessibilityIdentifier: String?
public dynamic var selected: Bool = false public var selected: Bool = false
public var enabled: Bool = true public var enabled: Bool = true
public var animated: Bool = true public var animated: Bool = true
public var inverted: Bool = false public var inverted: Bool = false
@ -80,6 +80,14 @@
baseValue = isChecked baseValue = isChecked
} }
//--------------------------------------------------
// MARK: - Behavior
//--------------------------------------------------
public func getRequiredBehaviors() -> [PageBehaviorModelProtocol] {
[SelectAllBoxesBehaviorModel()]
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Codec // MARK: - Codec
//-------------------------------------------------- //--------------------------------------------------

View File

@ -7,7 +7,7 @@
// //
public class ToggleModel: NSObject, MoleculeModelProtocol, FormFieldProtocol, EnableableModelProtocol, SelectableMoleculeModelProtocol { public class ToggleModel: MoleculeModelProtocol, FormFieldProtocol, EnableableModelProtocol, SelectableMoleculeModelProtocol {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Properties // MARK: - Properties
//-------------------------------------------------- //--------------------------------------------------
@ -15,7 +15,7 @@ public class ToggleModel: NSObject, MoleculeModelProtocol, FormFieldProtocol, En
public static var identifier: String = "toggle" public static var identifier: String = "toggle"
public var accessibilityIdentifier: String? public var accessibilityIdentifier: String?
public var backgroundColor: Color? public var backgroundColor: Color?
public dynamic var selected: Bool = false public var selected: Bool = false
public var animated: Bool = true public var animated: Bool = true
public var enabled: Bool = true public var enabled: Bool = true
public var action: ActionModelProtocol? public var action: ActionModelProtocol?

View File

@ -9,8 +9,8 @@
/// Protocol to apply to any model of a UI Control with a binary on/off nature. /// Protocol to apply to any model of a UI Control with a binary on/off nature.
/// ///
/// Example classes: Checkbox or Toggle. /// Example classes: Checkbox or Toggle.
@objc public protocol SelectableMoleculeModelProtocol: AnyObject { @objc public protocol SelectableMoleculeModelProtocol {
@objc dynamic var selected: Bool { get set } var selected: Bool { get set }
} }
public class SelectAllBoxesBehaviorModel: PageBehaviorModelProtocol { public class SelectAllBoxesBehaviorModel: PageBehaviorModelProtocol {
@ -34,9 +34,6 @@ public class SelectAllBoxesBehavior: PageCustomActionHandlerBehavior, PageMolecu
/// Reference to the general PageBehaviorModel. /// Reference to the general PageBehaviorModel.
var model: PageBehaviorModelProtocol var model: PageBehaviorModelProtocol
/// Dictionary of KVOs to observing the selected property of each `SelectableMoleculeModel`.
private var observers = [String: NSKeyValueObservation?]()
/// A store representing the values of the `SelectableMoleculeModel`. /// A store representing the values of the `SelectableMoleculeModel`.
private var valuesMirror = [String: Bool]() private var valuesMirror = [String: Bool]()
@ -57,45 +54,17 @@ public class SelectAllBoxesBehavior: PageCustomActionHandlerBehavior, PageMolecu
public func onPageNew(rootMolecules: [MoleculeModelProtocol], _ delegateObject: MVMCoreUIDelegateObject) { public func onPageNew(rootMolecules: [MoleculeModelProtocol], _ delegateObject: MVMCoreUIDelegateObject) {
let selectableModels: [(NSObject & SelectableMoleculeModelProtocol)] = rootMolecules.allMoleculesOfType() let selectableModels: [SelectableMoleculeModelProtocol] = rootMolecules.allMoleculesOfType()
guard !selectableModels.isEmpty else { return } guard !selectableModels.isEmpty else { return }
for model in selectableModels { for model in selectableModels {
if let key = (model as? FormFieldProtocol)?.fieldKey { if let key = (model as? FormFieldProtocol)?.fieldKey {
valuesMirror[key] = model.selected valuesMirror[key] = model.selected
setObserver(model, fieldKey: key)
} }
} }
} }
func setObserver<T>(_ model: T, fieldKey: String) where T: (NSObject & SelectableMoleculeModelProtocol) {
observers[fieldKey] = model.observe(\.selected, options: [.new]) { [weak self] model, change in
guard let self = self,
let isChecked = change.newValue
else { return }
self.valuesMirror[fieldKey] = isChecked
// If all are models are in the opposite state of the behavior, then realign.
if self.selectAllIsMisaligned() {
self.realignPageBehaviorAs(willSelectAll: false)
} else if self.deselectAllIsMisaligned() {
self.realignPageBehaviorAs(willSelectAll: true)
}
}
}
//--------------------------------------------------
// MARK: - Deinit
//--------------------------------------------------
deinit {
observers.values.forEach { $0?.invalidate() }
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Custom Action // MARK: - Custom Action
//-------------------------------------------------- //--------------------------------------------------
@ -110,23 +79,45 @@ public class SelectAllBoxesBehavior: PageCustomActionHandlerBehavior, PageMolecu
public func handleAction(type actionType: String?, information: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?) -> Bool { public func handleAction(type actionType: String?, information: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?) -> Bool {
// Verify we have the correct action type and necessary values. // Verify we have the correct action type and necessary values.
guard actionType == "selectAllBoxes", if actionType == "selectAllBoxes" {
let selectableModels: [SelectableMoleculeModelProtocol] = delegate?.moleculeDelegate?.getRootMolecules().allMoleculesOfType(), guard let selectableModels: [SelectableMoleculeModelProtocol] = delegate?.moleculeDelegate?.getRootMolecules().allMoleculesOfType(),
!selectableModels.isEmpty !selectableModels.isEmpty
else { return false } else { return false }
// Hold value dues to asynch behavior of page refreshing. // Hold value dues to asynch behavior of page refreshing.
let newSelectedState = willSelectAllState let newSelectedState = willSelectAllState
// Iterate through selectable molecules. // Iterate through selectable molecules.
for selectableModel in selectableModels { for selectableModel in selectableModels {
if toSelect(model: selectableModel) || toDeselect(model: selectableModel) { if toSelect(model: selectableModel) || toDeselect(model: selectableModel) {
selectableModel.selected = newSelectedState selectableModel.selected = newSelectedState
}
} }
willSelectAllState.toggle()
updatePageNavigationUI()
return true
} else if actionType == "boxSelected" {
guard let checkboxModel = (additionalData?["sourceModel"] as? CheckboxModel),
let fieldKey = checkboxModel.fieldKey
else { return false }
self.valuesMirror[fieldKey] = checkboxModel.selected
// If all are models are in the opposite state of the behavior, then realign.
if self.selectAllIsMisaligned() {
self.realignPageBehaviorAs(willSelectAll: false)
} else if self.deselectAllIsMisaligned() {
self.realignPageBehaviorAs(willSelectAll: true)
}
return true
} else {
return false
} }
updatePageNavigationUI()
return true
} }
//-------------------------------------------------- //--------------------------------------------------