Latest | 1.0.1 |
---|---|
Homepage | https://github.com/limadeveloper/ObservableKit |
License | MIT |
Platforms | ios 9.3, requires ARC |
Authors |
ObservableKit is the easiest way to observe values in Swift.
Requirements
- iOS 9.3+
- Swift 4.0+
Installation
CocoaPods
ObservableKit is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod 'ObservableKit', '~> 1.0'
and run pod install
Manual
Just copy source folder to your project.
Framework > ObservableKit > Source
How to use
Import library in your swift file:
import ObservableKit
Download image example
Setup ObservableKit in your ViewModel:
let observable: OKObservable<OKState<Data>> = OKObservable(OKState.loading)
func fetchImage() {
let url = URL(string: model.image)!
observable.value = .loading
URLSession.shared.dataTask(with: url, completionHandler: { data, _, error in
if let error = error {
self.observable.value = .errored(error: error)
return
}
self.observable.value = data != nil ? .load(data: data!) : .empty
}).resume()
}
Than, in your view controller call the observer:
@IBOutlet weak var imageView: UIImageView!
private let viewModel = ViewModel()
override func viewDidLoad() {
super.viewDidLoad()
addObservers()
}
private func addObservers() {
viewModel.observable.didChange = { [weak self] status in
DispatchQueue.main.async {
switch status {
case .load(data: let data):
print("✅ fetch image with succss")
let image = UIImage(data: data)
self?.imageView.image = image
case .loading:
print("🚀 loading data...")
case .errored(error: let error):
print("❌ get an error: (error)")
case .empty:
print("❌ data not found")
}
}
}
}
private func loadImage() {
viewModel.fetchImage()
}
Communication
- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request. 👨🏻💻
License
ObservableKit is under MIT license. See the LICENSE file for more info.
Latest podspec
{ "name": "ObservableKit", "version": "1.0.1", "summary": "ObservableKit is the easiest way to observe values in Swift", "requires_arc": true, "homepage": "https://github.com/limadeveloper/ObservableKit", "license": "MIT", "authors": { "John Lima": "[email protected]" }, "social_media_url": "https://twitter.com/johncarloslima", "platforms": { "ios": "9.3" }, "source": { "git": "https://github.com/limadeveloper/ObservableKit.git", "tag": "1.0.1" }, "source_files": "Framework/ObservableKit/Source/*.{swift}", "swift_versions": "4.0" }
Sun, 03 Mar 2019 11:00:07 +0000