Skip to content

Commit

Permalink
Classes, methods and variables names refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
LilianaFaustinoDev committed Sep 28, 2022
1 parent a932145 commit 0ce7b0b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 39 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/tech/relaycorp/courier/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tech.relaycorp.courier
import android.app.Application
import android.os.Build
import android.os.StrictMode
import tech.relaycorp.courier.background.WifiHotspotStateReceiver
import tech.relaycorp.courier.background.WifiHotspotStateWatcher
import tech.relaycorp.courier.common.Logging
import tech.relaycorp.courier.common.di.AppComponent
import tech.relaycorp.courier.common.di.DaggerAppComponent
Expand All @@ -14,7 +14,7 @@ import javax.inject.Inject
open class App : Application() {

@Inject
lateinit var wifiHotspotStateReceiver: WifiHotspotStateReceiver
lateinit var wifiHotspotStateWatcher: WifiHotspotStateWatcher

open val component: AppComponent by lazy {
DaggerAppComponent.builder()
Expand All @@ -36,12 +36,12 @@ open class App : Application() {
component.inject(this)
setupLogger()
setupStrictMode()
wifiHotspotStateReceiver.register()
wifiHotspotStateWatcher.start()
}

override fun onTerminate() {
super.onTerminate()
wifiHotspotStateReceiver.unregister()
wifiHotspotStateWatcher.stop()
}

private fun setupLogger() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import android.net.wifi.WifiManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.distinctUntilChanged
Expand All @@ -25,18 +24,17 @@ import javax.inject.Singleton
import kotlin.time.Duration.Companion.seconds

@Singleton
class WifiHotspotStateReceiver
class WifiHotspotStateWatcher
@Inject constructor(
private val context: Context
) {

private val state = BehaviorChannel(WifiHotspotState.Disabled)
fun state() = state.asFlow().distinctUntilChanged()

private val coroutineContext = SupervisorJob() + Dispatchers.IO
private var pollingGatewayAddressesJob: Job? = null

fun register() {
fun start() {
if (isWifiApStateChangeAvailable) {
context.registerReceiver(
wifiApStateChangeReceiver,
Expand All @@ -47,7 +45,7 @@ class WifiHotspotStateReceiver
}
}

fun unregister() {
fun stop() {
if (isWifiApStateChangeAvailable) {
context.unregisterReceiver(wifiApStateChangeReceiver)
} else {
Expand All @@ -56,7 +54,7 @@ class WifiHotspotStateReceiver
}

private fun startPollingGatewayAddresses() {
pollingGatewayAddressesJob = tickerFlow(delayDuration)
pollingGatewayAddressesJob = tickerFlow(POLLING_GATEWAY_ADDRESS_INTERVAL)
.map {
try {
Networking.getGatewayIpAddress()
Expand All @@ -65,44 +63,44 @@ class WifiHotspotStateReceiver
WifiHotspotState.Disabled
}
}
.distinctUntilChanged { oldWifiHotspotState, newWifiHotspotState ->
oldWifiHotspotState == newWifiHotspotState
}
.distinctUntilChanged()
.onEach {
logger.info("Hotspot State $it")
state.send(it)
}
.launchIn(CoroutineScope(coroutineContext))
.launchIn(CoroutineScope(Dispatchers.IO))
}

private fun stopPollingGatewayAddresses() {
pollingGatewayAddressesJob?.cancel()
}

private val wifiApStateChangeReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action != WIFI_AP_STATE_CHANGED_ACTION) return
private val wifiApStateChangeReceiver by lazy {
object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action != WIFI_AP_STATE_CHANGED_ACTION) return

val stateFlag = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0)
logger.info("Wifi State $stateFlag")
state.trySendBlocking(
if (stateFlag == WIFI_AP_STATE_ENABLED) {
WifiHotspotState.Enabled
} else {
WifiHotspotState.Disabled
}
)
val stateFlag = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0)
logger.info("Wifi State $stateFlag")
state.trySendBlocking(
if (stateFlag == WIFI_AP_STATE_ENABLED) {
WifiHotspotState.Enabled
} else {
WifiHotspotState.Disabled
}
)
}
}
}

companion object {
// From WifiManager documentation
private const val WIFI_AP_STATE_CHANGED_ACTION = "android.net.wifi.WIFI_AP_STATE_CHANGED"
private const val WIFI_AP_STATE_ENABLED = 13

private val POLLING_GATEWAY_ADDRESS_INTERVAL = 2.seconds
}

private val isWifiApStateChangeAvailable =
android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.TIRAMISU

private val delayDuration = 2.seconds
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlinx.coroutines.launch
import tech.relaycorp.courier.background.InternetConnection
import tech.relaycorp.courier.background.InternetConnectionObserver
import tech.relaycorp.courier.background.WifiHotspotState
import tech.relaycorp.courier.background.WifiHotspotStateReceiver
import tech.relaycorp.courier.background.WifiHotspotStateWatcher
import tech.relaycorp.courier.common.BehaviorChannel
import tech.relaycorp.courier.data.model.StorageSize
import tech.relaycorp.courier.data.model.StorageUsage
Expand All @@ -22,7 +22,7 @@ import javax.inject.Inject
class MainViewModel
@Inject constructor(
internetConnectionObserver: InternetConnectionObserver,
hotspotStateReceiver: WifiHotspotStateReceiver,
hotspotStateReceiver: WifiHotspotStateWatcher,
getStorageUsage: GetStorageUsage,
observeCCACount: ObserveCCACount,
deleteExpiredMessages: DeleteExpiredMessages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package tech.relaycorp.courier.ui.sync.people

import kotlinx.coroutines.flow.map
import tech.relaycorp.courier.background.WifiHotspotState
import tech.relaycorp.courier.background.WifiHotspotStateReceiver
import tech.relaycorp.courier.background.WifiHotspotStateWatcher
import tech.relaycorp.courier.ui.BaseViewModel
import javax.inject.Inject

class HotspotInstructionsViewModel
@Inject constructor(
private val wifiHotspotStateReceiver: WifiHotspotStateReceiver
private val wifiHotspotStateWatcher: WifiHotspotStateWatcher
) : BaseViewModel() {

fun state() =
wifiHotspotStateReceiver
wifiHotspotStateWatcher
.state()
.map { it.toState() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.take
import tech.relaycorp.courier.background.WifiHotspotState
import tech.relaycorp.courier.background.WifiHotspotStateReceiver
import tech.relaycorp.courier.background.WifiHotspotStateWatcher
import tech.relaycorp.courier.common.BehaviorChannel
import tech.relaycorp.courier.common.PublishChannel
import tech.relaycorp.courier.domain.PrivateSync
Expand All @@ -21,7 +21,7 @@ import javax.inject.Inject
class PeopleSyncViewModel
@Inject constructor(
private val privateSync: PrivateSync,
wifiHotspotStateReceiver: WifiHotspotStateReceiver
wifiHotspotStateWatcher: WifiHotspotStateWatcher
) : BaseViewModel() {

// Inputs
Expand Down Expand Up @@ -49,7 +49,7 @@ class PeopleSyncViewModel
private var hadFirstClient = false

init {
wifiHotspotStateReceiver
wifiHotspotStateWatcher
.state()
.take(1)
.onEach {
Expand All @@ -63,7 +63,7 @@ class PeopleSyncViewModel
}
.launchIn(scope)

wifiHotspotStateReceiver
wifiHotspotStateWatcher
.state()
.drop(1)
.filter { it == WifiHotspotState.Disabled }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.junit.jupiter.api.Test
import tech.relaycorp.courier.background.InternetConnection
import tech.relaycorp.courier.background.InternetConnectionObserver
import tech.relaycorp.courier.background.WifiHotspotState
import tech.relaycorp.courier.background.WifiHotspotStateReceiver
import tech.relaycorp.courier.background.WifiHotspotStateWatcher
import tech.relaycorp.courier.data.model.StorageSize
import tech.relaycorp.courier.data.model.StorageUsage
import tech.relaycorp.courier.domain.DeleteExpiredMessages
Expand All @@ -27,7 +27,7 @@ import tech.relaycorp.courier.test.factory.StoredMessageFactory
internal class MainViewModelTest {

private val connectionObserver = mock<InternetConnectionObserver>()
private val hotspotStateReceiver = mock<WifiHotspotStateReceiver>()
private val hotspotStateReceiver = mock<WifiHotspotStateWatcher>()
private val getStorageUsage = mock<GetStorageUsage>()
private val observeCCACount = mock<ObserveCCACount>()
private val deleteExpiredMessages = mock<DeleteExpiredMessages> {
Expand Down

0 comments on commit 0ce7b0b

Please sign in to comment.