Skip to content

Commit

Permalink
fix: prevent lateinit property exception
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Jun 17, 2024
1 parent 6f71413 commit 2c66dc1
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ internal class EmbraceNetworkConnectivityService(
private val networkConnectivityListeners = mutableListOf<NetworkConnectivityListener>()
override val ipAddress by lazy { calculateIpAddress() }

init {
registerConnectivityActionReceiver()
}

override fun onReceive(context: Context, intent: Intent) = handleNetworkStatus(true)

override fun networkStatusOnSessionStarted(startTime: Long) = handleNetworkStatus(false, startTime)
Expand Down Expand Up @@ -94,7 +90,7 @@ internal class EmbraceNetworkConnectivityService(
private fun didNetworkStatusChange(newNetworkStatus: NetworkStatus) =
lastNetworkStatus == null || lastNetworkStatus != newNetworkStatus

private fun registerConnectivityActionReceiver() {
override fun register() {
backgroundWorker.submit {
try {
context.registerReceiver(this, intentFilter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ internal interface NetworkConnectivityService : Closeable {
* Calculate the device's IP address
*/
val ipAddress: String?

/**
* Start listening for network connectivity changes
*/
fun register()
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ internal class NoOpNetworkConnectivityService : NetworkConnectivityService {
return NetworkStatus.UNKNOWN
}

override fun register() {}

override val ipAddress: String? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ internal class ModuleInitBootstrapper(
anrModule
)
}
Systrace.traceSynchronous("network-connectivity-registration") {
essentialServiceModule.networkConnectivityService.register()
}
initModule.internalErrorService.internalErrorDataSource = { dataSourceModule.internalErrorDataSource.dataSource }

dataCaptureServiceModule = init(DataCaptureServiceModule::class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ internal class EmbraceNetworkConnectivityServiceTest {
@Test
@Throws(InterruptedException::class)
fun `test connectivity broadcast receiver can register and unregister`() {
service.register()
verify { context.registerReceiver(service, any()) }
service.close()
verify { context.unregisterReceiver(service) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class FakeNetworkConnectivityService(
notifyListeners()
}

public var networkStatusOnSessionStartedCount = 0
var networkStatusOnSessionStartedCount = 0

override fun networkStatusOnSessionStarted(startTime: Long) {
notifyListeners()
Expand All @@ -37,6 +37,9 @@ internal class FakeNetworkConnectivityService(
override fun close() {
}

override fun register() {
}

private fun notifyListeners() {
networkConnectivityListeners.forEach {
it.onNetworkConnectivityStatusChanged(networkStatus)
Expand Down

0 comments on commit 2c66dc1

Please sign in to comment.