page behavior protocol

This commit is contained in:
Kyle Matthew Hedden 2020-05-11 11:52:18 -04:00
parent cb9af131d5
commit 72f1d48eeb
8 changed files with 158 additions and 4 deletions

View File

@ -108,6 +108,8 @@
0AE98BB523FF18D2004C5109 /* Arrow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE98BB423FF18D2004C5109 /* Arrow.swift */; };
0AE98BB723FF18E9004C5109 /* ArrowModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE98BB623FF18E9004C5109 /* ArrowModel.swift */; };
279B1569242BBC2F00921D6C /* ActionModelAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 279B1568242BBC2F00921D6C /* ActionModelAdapter.swift */; };
27F973532466074500CAB5C5 /* PageBehavior.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27F973522466074500CAB5C5 /* PageBehavior.swift */; };
27F9736A246750BE00CAB5C5 /* ScreenBrightnessModifierBehavior.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27F97369246750BE00CAB5C5 /* ScreenBrightnessModifierBehavior.swift */; };
31BE15CB23D8924D00452370 /* CheckboxLabelModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31BE15C923D8924C00452370 /* CheckboxLabelModel.swift */; };
31BE15CC23D8924D00452370 /* CheckboxModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31BE15CA23D8924C00452370 /* CheckboxModel.swift */; };
522679C123FE886900906CBA /* ListLeftVariableCheckboxAllTextAndLinks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 522679BF23FE886900906CBA /* ListLeftVariableCheckboxAllTextAndLinks.swift */; };
@ -519,6 +521,8 @@
0AE98BB423FF18D2004C5109 /* Arrow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Arrow.swift; sourceTree = "<group>"; };
0AE98BB623FF18E9004C5109 /* ArrowModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrowModel.swift; sourceTree = "<group>"; };
279B1568242BBC2F00921D6C /* ActionModelAdapter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionModelAdapter.swift; sourceTree = "<group>"; };
27F973522466074500CAB5C5 /* PageBehavior.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageBehavior.swift; sourceTree = "<group>"; };
27F97369246750BE00CAB5C5 /* ScreenBrightnessModifierBehavior.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScreenBrightnessModifierBehavior.swift; sourceTree = "<group>"; };
31BE15C923D8924C00452370 /* CheckboxLabelModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CheckboxLabelModel.swift; sourceTree = "<group>"; };
31BE15CA23D8924C00452370 /* CheckboxModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CheckboxModel.swift; sourceTree = "<group>"; };
522679BF23FE886900906CBA /* ListLeftVariableCheckboxAllTextAndLinks.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ListLeftVariableCheckboxAllTextAndLinks.swift; sourceTree = "<group>"; };
@ -948,6 +952,15 @@
path = Adapters;
sourceTree = "<group>";
};
27F973512466071600CAB5C5 /* Behaviors */ = {
isa = PBXGroup;
children = (
27F973522466074500CAB5C5 /* PageBehavior.swift */,
27F97369246750BE00CAB5C5 /* ScreenBrightnessModifierBehavior.swift */,
);
path = Behaviors;
sourceTree = "<group>";
};
5206F150241144A900658DC5 /* Headers */ = {
isa = PBXGroup;
children = (
@ -1383,6 +1396,7 @@
D29DF0CE21E404D4003B2FB9 /* MVMCoreUI */ = {
isa = PBXGroup;
children = (
27F973512466071600CAB5C5 /* Behaviors */,
D2C78CD324252F4E00B69FDE /* Atomic */,
012A88EF23985E0100FE3DA1 /* CustomPrimitives */,
D2B18B7D236090D500A9AEDC /* BaseClasses */,
@ -2103,7 +2117,7 @@
0A7BAFA1232BE61800FB8E22 /* Checkbox.swift in Sources */,
011B58F023A2AA980085F53C /* ListItemModelProtocol.swift in Sources */,
D22479962316AF6E003FCCF9 /* HeadlineBodyLink.swift in Sources */,
8DE5BECD2456F7A200772E76 /* ListTwoColumnDropdownSelectorsModel.swift in Sources */,
8DE5BECD2456F7A200772E76 /* ListTwoColumnDropdownSelectorsModel.swift in Sources */,
0A41BA7F23453A6400D4C0BC /* TextEntryField.swift in Sources */,
BB55B51D244482C1002001AD /* ListRightVariablePriceChangeBodyText.swift in Sources */,
017BEB382360C6AC0024EF95 /* RadioButtonLabel.swift in Sources */,
@ -2128,6 +2142,7 @@
AA69AAF62445BF5700AF3D3B /* ListLeftVariableCheckboxBodyText.swift in Sources */,
D264FAA3243E632F00D98315 /* ProgrammaticCollectionViewController.swift in Sources */,
D29DF27A21E7A533003B2FB9 /* MVMCoreUISession.m in Sources */,
27F9736A246750BE00CAB5C5 /* ScreenBrightnessModifierBehavior.swift in Sources */,
D2A5146B2214905000345BFB /* ThreeLayerViewController.swift in Sources */,
526A265E240D200500B0D828 /* ListTwoColumnCompareChanges.swift in Sources */,
8D24041523E7FC0B009E23BE /* ListLeftVariableIconWithRightCaretModel.swift in Sources */,
@ -2220,6 +2235,7 @@
D2A5146122121FBF00345BFB /* MoleculeStackTemplate.swift in Sources */,
D2E2A9A323E096B1000B42E6 /* DisableableModelProtocol.swift in Sources */,
D29DF11821E6805F003B2FB9 /* NSLayoutConstraint+MFConvenience.m in Sources */,
27F973532466074500CAB5C5 /* PageBehavior.swift in Sources */,
94C2D9A323872C110006CF46 /* LabelAttributeStrikeThroughModel.swift in Sources */,
D28A838523CCCA8900DFE4FC /* ScrollerModel.swift in Sources */,
D29DF26C21E6AA0B003B2FB9 /* FLAnimatedImage.m in Sources */,

View File

@ -198,6 +198,9 @@ import Foundation
try? ModelRegistry.register(ActionTopAlertModel.self)
try? ModelRegistry.register(ActionCollapseNotificationModel.self)
try? ModelRegistry.register(ActionOpenPanelModel.self)
// Behaviors
try? ModelRegistry.register(ScreenBrightnessModifierBehavior.self)
}
/// Convenience function to get required modules for a give model

View File

@ -9,7 +9,7 @@
import Foundation
public protocol TemplateModelProtocol: PageModelProtocol, ModelProtocol {
public protocol TemplateModelProtocol: PageModelProtocol, ModelProtocol, PageBehaviorsTemplateProtocol {
var template: String { get }
}

View File

@ -9,6 +9,7 @@
import Foundation
@objcMembers public class TemplateModel: MVMControllerModelProtocol {
public class var identifier: String {
return ""
}
@ -21,7 +22,8 @@ import Foundation
public var screenHeading: String?
public var navigationItem: (NavigationItemModelProtocol & MoleculeModelProtocol)?
public var formRules: [FormGroupRule]?
public var behaviors: [PageBehaviorProtocol]?
public init(pageType: String) {
self.pageType = pageType
}
@ -32,6 +34,7 @@ import Foundation
case screenHeading
case backgroundColor
case formRules
case behaviors
case navigationItem
}
@ -41,6 +44,7 @@ import Foundation
screenHeading = try typeContainer.decodeIfPresent(String.self, forKey: .screenHeading)
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
formRules = try typeContainer.decodeIfPresent([FormGroupRule].self, forKey: .formRules)
behaviors = try typeContainer.decodeModelsIfPresent(codingKey: .behaviors)
navigationItem = try typeContainer.decodeModelIfPresent(codingKey: .navigationItem)
}

View File

@ -8,6 +8,6 @@
import Foundation
public protocol MVMControllerModelProtocol: TemplateModelProtocol, FormHolderModelProtocol {
public protocol MVMControllerModelProtocol: TemplateModelProtocol, FormHolderModelProtocol, PageBehaviorsTemplateProtocol {
}

View File

@ -306,6 +306,18 @@ import UIKit
MVMCoreUISession.sharedGlobal()?.currentPageType = pageType
MVMCoreUILoggingHandler.shared()?.defaultLogPageState(forController: self)
}
executeBehaviors { (behavior: PageVisibilityBehavior) in
behavior.onPageShown()
}
}
open override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
executeBehaviors { (behavior: PageVisibilityBehavior) in
behavior.onPageHidden()
}
}
deinit {
@ -420,4 +432,9 @@ import UIKit
selectedField = nil
}
}
// MARK: - Behavior Execution
func executeBehaviors<T>(_ behaviorBlock:(_ behavior:T)->Void) {
pageModel?.behaviors?.compactMap({ $0 as? T }).forEach({ behaviorBlock($0) })
}
}

View File

@ -0,0 +1,44 @@
//
// PageBehaviors.swift
// MVMCoreUI
//
// Created by Kyle on 5/8/20.
// Copyright © 2020 Verizon Wireless. All rights reserved.
//
import Foundation
public protocol PageBehaviorProtocol: ModelProtocol {
// The type of rule
var behaviorName: String { get }
}
public extension PageBehaviorProtocol {
var behaviorName: String {
get { return Self.identifier }
}
static var categoryCodingKey: String {
return "behaviorName"
}
static var categoryName: String {
return "\(PageBehaviorProtocol.self)"
}
}
public protocol PageVisibilityBehavior: PageBehaviorProtocol {
func onPageShown()
func onPageHidden()
}
public protocol PageBehaviorsTemplateProtocol {
var behaviors: [PageBehaviorProtocol]? { get }
}

View File

@ -0,0 +1,70 @@
//
// ScreenBrightnessModifierBehavior.swift
// MVMCoreUI
//
// Created by Kyle on 5/9/20.
// Copyright © 2020 Verizon Wireless. All rights reserved.
//
class ScreenBrightnessModifierBehavior: PageVisibilityBehavior {
static var identifier = "screenBrightnessModifier"
@Clamping(range: 0...1) var screenBrightness: CGFloat
var originalScreenBrightness: CGFloat?
//MARK:- PageVisibilityBehavior
func onPageShown() {
changeScreenBrightness()
}
func onPageHidden() {
restoreScreenBrightness()
}
//MARK:- Behavior
func changeScreenBrightness() {
if originalScreenBrightness == nil {
originalScreenBrightness = UIScreen.main.brightness
UIScreen.main.brightness = screenBrightness
NotificationCenter.default.addObserver(self, selector: #selector(didEnterBackground), name: UIApplication.willResignActiveNotification, object: nil)
}
}
func restoreScreenBrightness() {
if let originalScreenBrightness = originalScreenBrightness {
UIScreen.main.brightness = originalScreenBrightness
self.originalScreenBrightness = nil
NotificationCenter.default.removeObserver(self, name: UIApplication.willResignActiveNotification, object: nil)
}
}
@objc func didEnterBackground() {
restoreScreenBrightness()
NotificationCenter.default.addObserver(self, selector: #selector(didEnterForeground), name: UIApplication.didBecomeActiveNotification, object: nil)
}
@objc func didEnterForeground() {
NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
changeScreenBrightness()
}
//MARK:- Codable
enum CodingKeys: String, CodingKey {
case screenBrightness
}
required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
screenBrightness = try typeContainer.decode(CGFloat.self, forKey: .screenBrightness)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(screenBrightness, forKey: .screenBrightness)
}
}