Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimsn98 committed May 16, 2020
1 parent 5817666 commit 520e018
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 93 deletions.
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ allprojects {
}
dependencies {
implementation 'com.github.ibrahimsn98:android-live-broadcasts:1.0'
implementation 'com.github.ibrahimsn98:android-live-broadcasts:1.1'
}
```

## Usage
```kotlin
LiveBroadcasts.init().subscribe(context, arrayOf(Intent.ACTION_TIME_TICK)).observe(this, Observer {
if (it != null)
Log.d("MainActivity", it.action)
LiveBroadcast(this, arrayOf(Intent.ACTION_TIME_TICK)).observe(this, Observer {
Log.d("MainActivity", it.action)
})
```

Expand Down Expand Up @@ -49,7 +48,4 @@ License
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
© 2018 Git


> Follow me on Twitter [@ibrahimsn98](https://twitter.com/ibrahimsn98)
© 2018 Git
7 changes: 3 additions & 4 deletions app/src/main/java/me/ibrahimsn/livebroadcasts/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import android.os.Bundle
import android.util.Log
import androidx.lifecycle.Observer
import kotlinx.android.synthetic.main.activity_main.*
import me.ibrahimsn.lib.LiveBroadcasts
import me.ibrahimsn.lib.LiveBroadcast

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

LiveBroadcasts.init().subscribe(this, arrayOf(Intent.ACTION_TIME_TICK)).observe(this, Observer {
if (it != null)
Log.d("MainActivity", it.action)
LiveBroadcast(this, arrayOf(Intent.ACTION_TIME_TICK)).observe(this, Observer {
Log.d("MainActivity", it.action)
})

textView.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.Observer
import me.ibrahimsn.lib.LiveBroadcasts
import me.ibrahimsn.lib.LiveBroadcast

class SecondActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)

LiveBroadcasts.init().subscribe(this, arrayOf(Intent.ACTION_TIME_TICK)).observe(this, Observer {
if (it != null)
Log.d("MainActivity", it.action)
LiveBroadcast(this, arrayOf(Intent.ACTION_TIME_TICK)).observe(this, Observer {
Log.d("SecondActivity", it.action)
})
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath 'com.android.tools.build:gradle:3.6.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Jul 23 16:27:22 EET 2019
#Sat May 16 18:34:51 EET 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
54 changes: 19 additions & 35 deletions lib/src/main/java/me/ibrahimsn/lib/LiveBroadcast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,34 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.lifecycle.MutableLiveData
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
import androidx.lifecycle.LiveData

class LiveBroadcast(private val receiver: BroadcastReceiver,
private val context: Context,
private val updates: Observable<Intent>,
private val filters: Array<String>) : MutableLiveData<Intent>() {
class LiveBroadcast(
private val context: Context,
private val filters: Array<String>
) : LiveData<Intent>() {

private var disposable: Disposable? = null
private val intentFilter = IntentFilter().apply {
for (filter in filters) addAction(filter)
}

override fun onActive() {
super.onActive()
context.registerReceiver(broadcastReceiver, intentFilter)
}

disposable = updates.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribeWith(object: DisposableObserver<Intent>() {
override fun onComplete() {

}

override fun onNext(t: Intent) {
postValue(t)
}

override fun onError(e: Throwable) {

}
})

context.registerReceiver(receiver, IntentFilter().apply {
for (filter in filters)
this.addAction(filter)
})
private val broadcastReceiver = object: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
postValue(intent)
}
}

override fun onInactive() {
super.onInactive()
disposable?.dispose()

try {
context.unregisterReceiver(receiver)
} catch (e: IllegalArgumentException) {}
context.unregisterReceiver(broadcastReceiver)
} catch (e: IllegalArgumentException) {
e.printStackTrace()
}
}
}
}
39 changes: 0 additions & 39 deletions lib/src/main/java/me/ibrahimsn/lib/LiveBroadcasts.kt

This file was deleted.

0 comments on commit 520e018

Please sign in to comment.