rxObservableCollectionWithObservableElement 0.1.2

rxObservableCollectionWithObservableElement 0.1.2

Maintained by Ismael Otero.



 
Depends on:
RxSwift>= 0
RxCocoa>= 0
 

rxObservableCollectionWithObservableElement

CI Status Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Observable Collection

This observable collection works just likes any other providing collection changed events on insertions and deletions. Where this differs is that each element must implement the NotifyChanged protocol discussed below. This allows not only to received event's if elements are inserted but receive events for object's that implement the NotifyChanged Protocol any time a Variable is modified.

NotifyChanged Protocol

protocol NotifyChanged: class {
func raiseChangeFor<A>(keyPath: AnyKeyPath, old:A, new:A)
func set<A>(keyPath: ReferenceWritableKeyPath<Self, A>, value: A)
var elementChanged: PublishSubject<(keyPath: AnyKeyPath, old:Any, new:Any)>{ get }
}

This protocol really only has one thing of note, and that is this generic set method that can be used as below.

elements.set(keyPath: \something.money, value: 100) 
/// you can use the set method to set any variable in your class this method emits an event on the obeservable collection letting the subscriber that an element in the observable collection has changed.
///

NOTE:
elements.money = 100 //does not emit collection changed event

Full Example

final class something:NotifyChanged { //any class that inherits this must be a final class probably because im still a n0_ob
var elementChanged: PublishSubject<(keyPath: AnyKeyPath, old: Any, new: Any)>

var string:String
var int:Int
var money: Double
init(string:String,int:Int, money:Double) {
self.string = string
self.int = int

self.money = money


elementChanged = PublishSubject<(keyPath: AnyKeyPath, old: Any, new: Any)>()
}
}
var t = ObservableCollection<something>()

t.subscribe(onNext: { [weak self] (tuple) in
			// tuple is a combination of the following
            // event: which is an enum holding the changed indices
            // element: an array of the updated objects in your observable collection
            guard let  strongSelf = self else { return }

			switch tuple.event {
				case .deletedIndices(let indices):
					DispatchQueue.main.async {
					//TODO: Some UI stuff can go here 
					}
					break
				case .updatedIndices(let indices):
					break
				case .insertedIndices(let indices):
					break
			}
		}).disposed(by: disposeBag)


var elements = something(string: "", int: 0, money: 0.0)

t.append(elements)

elements.set(keyPath: \something.money, value: 100) 
/// you can use the set method to set any variable in your class this method emits an event on the obeservable collection letting the subscriber that an element in the observable collection has changed.
///

t.append(something(string: "", int: 0, money: 100.0))
t[1].money = 0 // or set the variable directly note this does not emit an collectionchanged event.

Requirements

Installation

rxObservableCollectionWithObservableElement is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'rxObservableCollectionWithObservableElement'

Author

[email protected]

License

rxObservableCollectionWithObservableElement is available under the MIT license. See the LICENSE file for more info.