|
| 1 | +import Foundation |
| 2 | +import RiveRuntime |
| 3 | + |
| 4 | +/// Protocol for Rive property types that support listener management |
| 5 | +protocol RivePropertyWithListeners: AnyObject { |
| 6 | + func removeListener(_ id: UUID) |
| 7 | +} |
| 8 | + |
| 9 | +typealias BooleanPropertyType = RiveDataBindingViewModel.Instance.BooleanProperty |
| 10 | +typealias NumberPropertyType = RiveDataBindingViewModel.Instance.NumberProperty |
| 11 | +typealias StringPropertyType = RiveDataBindingViewModel.Instance.StringProperty |
| 12 | +typealias EnumPropertyType = RiveDataBindingViewModel.Instance.EnumProperty |
| 13 | +typealias ColorPropertyType = RiveDataBindingViewModel.Instance.ColorProperty |
| 14 | +typealias TriggerPropertyType = RiveDataBindingViewModel.Instance.TriggerProperty |
| 15 | +typealias ImagePropertyType = RiveDataBindingViewModel.Instance.ImageProperty |
| 16 | + |
| 17 | +// Make all Rive property types conform to the protocol |
| 18 | +extension BooleanPropertyType: RivePropertyWithListeners {} |
| 19 | +extension NumberPropertyType: RivePropertyWithListeners {} |
| 20 | +extension StringPropertyType: RivePropertyWithListeners {} |
| 21 | +extension EnumPropertyType: RivePropertyWithListeners {} |
| 22 | +extension ColorPropertyType: RivePropertyWithListeners {} |
| 23 | +extension TriggerPropertyType: RivePropertyWithListeners {} |
| 24 | +extension ImagePropertyType: RivePropertyWithListeners {} |
| 25 | + |
| 26 | +/// Helper class for managing ViewModel property listeners |
| 27 | +/// Similar to Android's BaseHybridViewModelPropertyImpl, provides reusable listener management |
| 28 | +class PropertyListenerHelper<PropertyType: RivePropertyWithListeners> { |
| 29 | + private var listenerIds: [UUID] = [] |
| 30 | + weak var property: PropertyType? |
| 31 | + |
| 32 | + init(property: PropertyType) { |
| 33 | + self.property = property |
| 34 | + } |
| 35 | + |
| 36 | + /// Adds a listener and automatically tracks its ID for later cleanup |
| 37 | + /// - Parameter addListener: Closure that adds the listener to the property and returns the listener ID |
| 38 | + func trackListener(_ addListener: (PropertyType) -> UUID) { |
| 39 | + guard let property = property else { return } |
| 40 | + let id = addListener(property) |
| 41 | + listenerIds.append(id) |
| 42 | + } |
| 43 | + |
| 44 | + func removeListeners() throws { |
| 45 | + guard let property = property else { return } |
| 46 | + for id in listenerIds { |
| 47 | + property.removeListener(id) |
| 48 | + } |
| 49 | + listenerIds.removeAll() |
| 50 | + } |
| 51 | + |
| 52 | + func dispose() throws { |
| 53 | + try? removeListeners() |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/// Protocol that provides common functionality for all hybrid ViewModel property classes |
| 58 | +/// Reduces boilerplate by providing default implementations for listener management |
| 59 | +protocol ViewModelPropertyProtocol { |
| 60 | + associatedtype PropertyType: RivePropertyWithListeners |
| 61 | + |
| 62 | + var helper: PropertyListenerHelper<PropertyType> { get } |
| 63 | + |
| 64 | + func removeListeners() throws |
| 65 | + func dispose() throws |
| 66 | +} |
| 67 | + |
| 68 | +/// Default implementations for common listener management methods |
| 69 | +extension ViewModelPropertyProtocol { |
| 70 | + func removeListeners() throws { |
| 71 | + try helper.removeListeners() |
| 72 | + } |
| 73 | + |
| 74 | + func dispose() throws { |
| 75 | + try helper.dispose() |
| 76 | + } |
| 77 | +} |
| 78 | + |
0 commit comments