Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make bluetooth repository to emit initial bluetooth enabled state if … #243

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package at.roteskreuz.stopcorona.di

import android.bluetooth.BluetoothAdapter
import at.roteskreuz.stopcorona.model.managers.BluetoothManager
import at.roteskreuz.stopcorona.model.managers.ExposureNotificationManager
import at.roteskreuz.stopcorona.model.managers.ExposureNotificationManagerImpl
import at.roteskreuz.stopcorona.model.repositories.*
Expand Down Expand Up @@ -78,7 +79,7 @@ val repositoryModule = module {
}

single<BluetoothRepository> {
BluetoothRepositoryImpl(BluetoothAdapter.getDefaultAdapter())
BluetoothRepositoryImpl(BluetoothAdapter.getDefaultAdapter(), get<BluetoothManager>())
}

single<ExposureNotificationRepository> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package at.roteskreuz.stopcorona.model.managers

import at.roteskreuz.stopcorona.model.receivers.Registrable
import at.roteskreuz.stopcorona.model.repositories.BluetoothRepository
import at.roteskreuz.stopcorona.model.repositories.other.ContextInteractor
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject

/**
* Manager to start/stop listening for bluetooth enabled state.
Expand All @@ -17,6 +20,11 @@ interface BluetoothManager {
* Stop listening for bluetooth changes.
*/
fun stopListeningForChanges()

/**
* listening subject if bluetooth manager is starting to listen to bluetooth enabled changes
*/
val listeningSubject : Subject<Boolean>
}

class BluetoothManagerImpl(
Expand All @@ -26,13 +34,17 @@ class BluetoothManagerImpl(

override fun startListeningForChanges() {
bluetoothStateReceiver.register(contextInteractor.applicationContext)
listeningSubject.onNext(true)
}

override fun stopListeningForChanges() {
try {
bluetoothStateReceiver.unregister(contextInteractor.applicationContext)
listeningSubject.onNext(false)
} catch (e: Exception) {
// ignored, receiver is probably not registered yet
}
}

override val listeningSubject: Subject<Boolean> = PublishSubject.create()
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package at.roteskreuz.stopcorona.model.repositories

import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import at.roteskreuz.stopcorona.model.managers.BluetoothManager
import at.roteskreuz.stopcorona.utils.NonNullableBehaviorSubject
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers

/**
* Wrapper for Bluetooth related functionality.
Expand Down Expand Up @@ -32,10 +34,22 @@ interface BluetoothRepository {
fun updateEnabledState(enabled: Boolean)
}

@SuppressLint("CheckResult")
class BluetoothRepositoryImpl(
private val bluetoothAdapter: BluetoothAdapter?
private val bluetoothAdapter: BluetoothAdapter?,
bluetoothManager: BluetoothManager
) : BluetoothRepository {

init {
bluetoothManager.listeningSubject
.subscribeOn(Schedulers.single())
.observeOn(Schedulers.single())
.filter { it }
.subscribe {
enabledStateSubject.onNext(bluetoothEnabled)
}
}

private val enabledStateSubject = NonNullableBehaviorSubject(
bluetoothAdapter?.isEnabled == true
)
Expand Down