|
| 1 | +[⏎ Overview](../Overview.md) |
| 2 | + |
| 3 | +# Advanced Usage |
| 4 | + |
| 5 | +## How it works |
| 6 | + |
| 7 | +The main logic is located in `TableAccessibilityPlugin` and `CollectionAccessibilityPlugin`. It automatically added to all builders to be ready work with `AccessibilityItems`. It's a common plugin that works with delgate events. |
| 8 | + |
| 9 | +**Only each `AccessibilityItem` will be processed by these plugins.** |
| 10 | + |
| 11 | +On `willDisplay` event every time 2 steps are happens: |
| 12 | + |
| 13 | +1. Element will be passed to its defined modifier through corresponding methods `modify(item: accessibilityItem)` or `modify(item: accessibilityItem, generator: generator)` if generator implements `AccessibilityStrategyProvider`. |
| 14 | +2. A new invalidator instance is setted with provided `indexPath` if supports invalidation mechanism. Its invalidate method calls delegate to pass the new event `invalidatedAccessibility` which will execute step 1. |
| 15 | + |
| 16 | +On `didEndDisplay` event the plugin will remove invalidator if it exists. |
| 17 | + |
| 18 | +<img src="https://i.ibb.co/SfbfzZJ/2023-06-16-18-43-17.png" alt="Plugin Example"> |
| 19 | + |
| 20 | +<br> |
| 21 | + |
| 22 | +This is the essence how accessibility works in **RDDM**, so you don't have access to its functionality. Instead, you may have full control under modify and invalidate processes. |
| 23 | + |
| 24 | +*** |
| 25 | + |
| 26 | +<br> |
| 27 | + |
| 28 | +## Modify custom accessibility parameters |
| 29 | + |
| 30 | +If you need to set a parameter which is not presented or change the way how they setting, you need to define your own strategy provider (or custom accessibility item) and modifier. You can combine your custom logic with base functionality. |
| 31 | + |
| 32 | +*Example:* |
| 33 | +```swift |
| 34 | +enum CustomAccessibilityModifier: AccessibilityModifier { |
| 35 | + static func modify(item: AccessibilityItem) { |
| 36 | + // set base parameters |
| 37 | + BaseAccessibilityModifier.modify(item) |
| 38 | + |
| 39 | + // additional logic for custom item |
| 40 | + guard let item = item as? CustomAccessibilityItem else { return } |
| 41 | + item.accessibilityIdentifier = item.identifier.value |
| 42 | + item.accessibilityHint = item.hintStrategy.value |
| 43 | + } |
| 44 | + |
| 45 | + // corresponding method implementation with generator |
| 46 | +} |
| 47 | + |
| 48 | +protocol CustomAccessibilityItem: AccessibilityItem { |
| 49 | + var modifierType: AccessibilityModifierType { CustomAccessibilityModifier.self } |
| 50 | + |
| 51 | + // expand base accessibility item with new parameters using string strategy |
| 52 | + var identifier: AccessibilityStringStrategy { get } |
| 53 | + var hintStrategy: AccessibilityStringStrategy { get } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +By this way you can add any other functionality to accessibility items. |
| 58 | + |
| 59 | +*** |
| 60 | + |
| 61 | +<br> |
| 62 | + |
| 63 | +## Customize invalidation |
| 64 | + |
| 65 | + |
| 66 | +### in table or collection |
| 67 | +If you want to perform additional logic on invalidate, you can implement your own `AccessibilityItemInvalidator` and set `AccessibilityInvalidatorCreationBlock` |
| 68 | + |
| 69 | +**For example** |
| 70 | +```swift |
| 71 | + |
| 72 | + private lazy var adapter = collectionView.rddm.baseBuilder |
| 73 | + .add(plugin: .accessibility({ item, kind, delegate in |
| 74 | + return CustomInvalidator(item, kind, delegate) |
| 75 | + })) |
| 76 | + .build() |
| 77 | +``` |
| 78 | + |
| 79 | +*Note that we have `DelegatedAccessibilityItemInvalidator` which could be used as base for your custom invalidator. This class is needed to combine strategies from item and generator.* |
| 80 | + |
| 81 | +### in other view |
| 82 | + |
| 83 | +Any view can implement `AccessibilityItem`, but you need adittionaly call `AccessibilityItem.modifySelf()` to setup accessibility-properties for real. |
| 84 | + |
| 85 | +`AcessibilityInvalidatable.setBasicInvalidator` is also available for invalidation. |
| 86 | + |
| 87 | +*Note that is recomended to call* |
| 88 | +- `setBasicInvalidator` when view become **visible** |
| 89 | +- `removeInvalidator` when view become **invisible** |
0 commit comments