https://github.com/osfunapps/android-lib-broadcast-receiver
An android library for broadcast receviers
https://github.com/osfunapps/android-lib-broadcast-receiver
android application broadcastreceiver dagger2-mvp kotlin library mvp-architecture observer-pattern
Last synced: 3 months ago
JSON representation
An android library for broadcast receviers
- Host: GitHub
- URL: https://github.com/osfunapps/android-lib-broadcast-receiver
- Owner: osfunapps
- Created: 2018-05-09T15:35:27.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-05-27T18:39:13.000Z (about 7 years ago)
- Last Synced: 2025-01-28T11:32:52.258Z (4 months ago)
- Topics: android, application, broadcastreceiver, dagger2-mvp, kotlin, library, mvp-architecture, observer-pattern
- Language: Kotlin
- Homepage:
- Size: 81.1 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# An easy Broadcast Receiver library for Android.
## Introduction
This library meant to handle all of the Broadcast Receiver operations using the Observer design pattern, to keep updating all of the broadcast listeners.
For example, by using this library you can keep track of the app network status, no matter where are you in your app.
Just inject the NetworkReceiver object to your instance and you good to go!This library works on Google's Dagger 2.1 android design, the recommended app architecture by Google.
## Installing
### Gradle:
add the gradle dependency:
```groovy
dependencies {
implementation 'com.github.osfunapps:android-lib-broadcast-receiver:v1.0.2'
}
```## Usage
1) add the library module **BroadcastReceiverLibraryBuilder::class** to your core component (singleton one).
Example:
```
@Singleton
@Component(modules = [
AppModule::class,
NetworkModule::class,
...
BroadcastReceiverLibraryBuilder::class])
```
2) add the desired broadcast receiver to your instance:We will add the network receiver to the MainPresenter.kt, for example:
```
@Module
class MainActivityModule {//the main activity view
@Provides
fun provideMainView(mainActivity: MainActivity) : MainActivityView = mainActivity//the main presenter
@Provides
@PerActivity
fun provideMainPresenter(mainActivityView: MainActivityView, networkReceiver: NetworkReceiver): MainActivityPresenterImpl {
return MainActivityPresenterImpl(mainActivityView, networkReceiver)
}}
```
3) inject the receiver to your presenter:```
class MainActivityPresenterImpl @Inject constructor(private var view: MainActivityView,
private var networkReceiver: NetworkReceiver) : NetworkObserver {
```4) subscribe your instance to get calls:
```
fun viewReady() {
//view.onPresenterReady()
networkReceiver.create()
networkReceiver.register(view.getActivity())
networkReceiver.subscribeObserver(this)
}override fun onNetworkChanged(networkStateObj: NewNetworkStateObj) {
println(networkStateObj.state.name)
}```
And that's it.Don't forget to register and unregister your receiver in the activity onResume/onStop.
## Develop
Any contributions for this repository is blessed, just make sure to:
* Design in DI style with Dagger 2.1
* Write your code in Kotlin
* Seperate views from controllers and modules
* Stick to generics
* Work with the Observer design pattern
* Do not use reflections