Further development of the select all behavior.

This commit is contained in:
Kevin G Christiano 2021-06-21 14:07:37 -04:00
parent 975f3fa012
commit 76787545ac
4 changed files with 67 additions and 34 deletions

View File

@ -6,10 +6,8 @@
// Copyright © 2020 Verizon Wireless. All rights reserved. // Copyright © 2020 Verizon Wireless. All rights reserved.
// //
import Foundation
@objcMembers public class CheckboxModel: MoleculeModelProtocol, SelectableModel, FormFieldProtocol {
@objcMembers public class CheckboxModel: MoleculeModelProtocol, FormFieldProtocol {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Properties // MARK: - Properties
//-------------------------------------------------- //--------------------------------------------------
@ -68,11 +66,23 @@ import Foundation
} }
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Methods // MARK: - Form Validation
//-------------------------------------------------- //--------------------------------------------------
public func formFieldValue() -> AnyHashable? { checked } public func formFieldValue() -> AnyHashable? { checked }
//--------------------------------------------------
// MARK: - Selectable Protocol
//--------------------------------------------------
public func select(as isSelected: Bool) {
checked = isSelected
}
public var selectedValue: Bool {
checked
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Initializer // MARK: - Initializer
//-------------------------------------------------- //--------------------------------------------------

View File

@ -6,14 +6,14 @@
// Copyright © 2019 Verizon Wireless. All rights reserved. // Copyright © 2019 Verizon Wireless. All rights reserved.
// //
import Foundation
public protocol MoleculeDelegateProtocol: AnyObject { public protocol MoleculeDelegateProtocol: AnyObject {
func getRootMolecules() -> [MoleculeModelProtocol] func getRootMolecules() -> [MoleculeModelProtocol]
/// returns a module for the corresponding module name. /// returns a module for the corresponding module name.
func getModuleWithName(_ name: String?) -> [AnyHashable : Any]? func getModuleWithName(_ name: String?) -> [AnyHashable: Any]?
func getModuleWithName(_ moleculeName: String) -> MoleculeModelProtocol? func getModuleWithName(_ moleculeName: String) -> MoleculeModelProtocol?
/// Notifies the delegate that the molecule layout update. Should be called when the layout may change due to an async method. Mainly used for list or collections. /// Notifies the delegate that the molecule layout update. Should be called when the layout may change due to an async method. Mainly used for list or collections.
@ -21,15 +21,19 @@ public protocol MoleculeDelegateProtocol: AnyObject {
/// Asks the delegate to add or remove molecules. Mainly used for list or collections. /// Asks the delegate to add or remove molecules. Mainly used for list or collections.
func getIndexPath(for molecule: ListItemModelProtocol & MoleculeModelProtocol) -> IndexPath? func getIndexPath(for molecule: ListItemModelProtocol & MoleculeModelProtocol) -> IndexPath?
func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], indexPath: IndexPath, animation: UITableView.RowAnimation) func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], indexPath: IndexPath, animation: UITableView.RowAnimation)
func removeMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], animation: UITableView.RowAnimation) func removeMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], animation: UITableView.RowAnimation)
} }
extension MoleculeDelegateProtocol { extension MoleculeDelegateProtocol {
public func moleculeLayoutUpdated(_ molecule: MoleculeViewProtocol) {} public func moleculeLayoutUpdated(_ molecule: MoleculeViewProtocol) { }
public func getIndexPath(for molecule: ListItemModelProtocol & MoleculeModelProtocol) -> IndexPath? { return nil } public func getIndexPath(for molecule: ListItemModelProtocol & MoleculeModelProtocol) -> IndexPath? { nil }
public func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], indexPath: IndexPath, animation: UITableView.RowAnimation) {}
public func removeMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], animation: UITableView.RowAnimation) {} public func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], indexPath: IndexPath, animation: UITableView.RowAnimation) { }
public func removeMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], animation: UITableView.RowAnimation) { }
} }

View File

@ -507,7 +507,7 @@ import UIKit
//-------------------------------------------------- //--------------------------------------------------
open func getRootMolecules() -> [MoleculeModelProtocol] { open func getRootMolecules() -> [MoleculeModelProtocol] {
return model?.rootMolecules ?? [] model?.rootMolecules ?? []
} }
open func getModuleWithName(_ name: String?) -> [AnyHashable: Any]? { open func getModuleWithName(_ name: String?) -> [AnyHashable: Any]? {

View File

@ -6,9 +6,13 @@
// Copyright © 2021 Verizon Wireless. All rights reserved. // Copyright © 2021 Verizon Wireless. All rights reserved.
// //
public protocol SelectableModel {
var selectedValue: Bool { get }
func select(as isSelected: Bool)
}
public class SelectAllBoxesBehaviorModel: PageBehaviorModelProtocol { public class SelectAllBoxesBehaviorModel: PageBehaviorModelProtocol {
public class var identifier: String { "selectAllBoxesBehavior" } public class var identifier: String { "pageSelectAllBoxesBehavior" }
public var shouldAllowMultipleInstances: Bool { false } public var shouldAllowMultipleInstances: Bool { false }
public init() { } public init() { }
} }
@ -16,9 +20,11 @@ public class SelectAllBoxesBehaviorModel: PageBehaviorModelProtocol {
public class SelectAllBoxesBehavior: PageCustomActionHandlerBehavior { public class SelectAllBoxesBehavior: PageCustomActionHandlerBehavior {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Active Model // MARK: - Properties
//-------------------------------------------------- //--------------------------------------------------
var didSelectAll = false
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Delegate // MARK: - Delegate
//-------------------------------------------------- //--------------------------------------------------
@ -37,31 +43,44 @@ public class SelectAllBoxesBehavior: PageCustomActionHandlerBehavior {
// MARK: - Custom Action // MARK: - Custom Action
//-------------------------------------------------- //--------------------------------------------------
// Either play or pause // To select or deselect all controls adhereing to
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 {
guard actionType == "selectAllBoxes" else { return false } guard actionType == "selectAllBoxes",
let selectableModels: [SelectableModel] = delegate?.moleculeDelegate?.getRootMolecules().allMoleculesOfType(),
!selectableModels.isEmpty
else { return false }
let currentVC = MVMCoreUIUtility.getCurrentVisibleController() didSelectAll.toggle()
let navButtonTitle: String? = (didSelectAll ? information!["deSelectAllTitle"] : information!["selectAllTitle"]) as? String
traverseAndSelectCheckboxes(view: currentVC.view) for selectableModel in selectableModels {
if toSelect(model: selectableModel) || toDeselect(model: selectableModel) {
selectableModel.select(as: didSelectAll)
}
}
// TODO: A) select all boxes and the change the nav button title to "deselect all" MVMCoreDispatchUtility.performBlock(onMainThread: {
// TODO: B) deselect all boxes and change nav nutton title to "select all" // TODO: move to protocol function instead
guard let controller = self.delegate?.moleculeDelegate as? ViewController else { return }
controller.handleNewDataAndUpdateUI()
if MVMCoreUIUtility.getCurrentVisibleController() == controller {
// Update navigation bar if showing.
controller.navigationItem.rightBarButtonItem?.title = navButtonTitle
controller.manager?.refreshNavigationUI()
}
})
return true return true
} }
private func traverseAndSelectCheckboxes(view: UIView) { func toSelect(model: SelectableModel) -> Bool {
didSelectAll && !model.selectedValue
}
if let checkbox = view as? Checkbox { func toDeselect(model: SelectableModel) -> Bool {
checkbox.isSelected = true !didSelectAll && model.selectedValue
return
}
for subview in view.subviews {
traverseAndSelectCheckboxes(view: subview)
}
} }
} }