diff --git a/README.md b/README.md index f733ecc..ead220e 100644 --- a/README.md +++ b/README.md @@ -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) }) ``` @@ -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) \ No newline at end of file + © 2018 Git \ No newline at end of file diff --git a/app/src/main/java/me/ibrahimsn/livebroadcasts/MainActivity.kt b/app/src/main/java/me/ibrahimsn/livebroadcasts/MainActivity.kt index 7f3848c..a91d9b1 100644 --- a/app/src/main/java/me/ibrahimsn/livebroadcasts/MainActivity.kt +++ b/app/src/main/java/me/ibrahimsn/livebroadcasts/MainActivity.kt @@ -6,7 +6,7 @@ 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() { @@ -14,9 +14,8 @@ class MainActivity : AppCompatActivity() { 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 { diff --git a/app/src/main/java/me/ibrahimsn/livebroadcasts/SecondActivity.kt b/app/src/main/java/me/ibrahimsn/livebroadcasts/SecondActivity.kt index 86d05ce..8bc24a2 100644 --- a/app/src/main/java/me/ibrahimsn/livebroadcasts/SecondActivity.kt +++ b/app/src/main/java/me/ibrahimsn/livebroadcasts/SecondActivity.kt @@ -5,7 +5,7 @@ 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() { @@ -13,9 +13,8 @@ class SecondActivity : AppCompatActivity() { 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) }) } } diff --git a/build.gradle b/build.gradle index 438d280..a5d5183 100644 --- a/build.gradle +++ b/build.gradle @@ -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 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index ee68b33..76af51b 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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 diff --git a/lib/src/main/java/me/ibrahimsn/lib/LiveBroadcast.kt b/lib/src/main/java/me/ibrahimsn/lib/LiveBroadcast.kt index 1f2b232..552d466 100644 --- a/lib/src/main/java/me/ibrahimsn/lib/LiveBroadcast.kt +++ b/lib/src/main/java/me/ibrahimsn/lib/LiveBroadcast.kt @@ -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, - private val filters: Array) : MutableLiveData() { +class LiveBroadcast( + private val context: Context, + private val filters: Array +) : LiveData() { - 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() { - 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() + } } -} \ No newline at end of file +} diff --git a/lib/src/main/java/me/ibrahimsn/lib/LiveBroadcasts.kt b/lib/src/main/java/me/ibrahimsn/lib/LiveBroadcasts.kt deleted file mode 100644 index a82ad86..0000000 --- a/lib/src/main/java/me/ibrahimsn/lib/LiveBroadcasts.kt +++ /dev/null @@ -1,39 +0,0 @@ -package me.ibrahimsn.lib - -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import io.reactivex.subjects.PublishSubject - -class LiveBroadcasts { - - private val publisher = PublishSubject.create() - - companion object { - private var liveBroadcasts: LiveBroadcasts? = null - - fun init(): LiveBroadcasts { - if (liveBroadcasts == null) - liveBroadcasts = LiveBroadcasts() - - return liveBroadcasts!! - } - } - - init { - if (liveBroadcasts != null){ - throw RuntimeException("Use getInstance() method to get the single instance of this class.") - } - } - - private val receiver = object: BroadcastReceiver() { - override fun onReceive(context: Context?, intent: Intent?) { - if (intent != null) - publisher.onNext(intent) - } - } - - fun subscribe(context: Context, filters: Array): LiveBroadcast { - return LiveBroadcast(receiver, context, publisher, filters) - } -} \ No newline at end of file