Skip to content

Commit d2b15c7

Browse files
authored
refactor(di): Introduce @ProcessLifecycle qualifier for di (#3978)
Signed-off-by: James Rich <[email protected]>
1 parent 1bbb4ac commit d2b15c7

File tree

7 files changed

+44
-17
lines changed

7 files changed

+44
-17
lines changed

app/src/main/java/com/geeksville/mesh/ApplicationModule.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import dagger.Provides
2727
import dagger.hilt.InstallIn
2828
import dagger.hilt.components.SingletonComponent
2929
import org.meshtastic.core.common.BuildConfigProvider
30+
import org.meshtastic.core.di.ProcessLifecycle
3031
import org.meshtastic.core.service.MeshServiceNotifications
3132
import javax.inject.Singleton
3233

@@ -37,10 +38,13 @@ interface ApplicationModule {
3738
@Binds fun bindMeshServiceNotifications(impl: MeshServiceNotificationsImpl): MeshServiceNotifications
3839

3940
companion object {
40-
@Provides fun provideProcessLifecycleOwner(): LifecycleOwner = ProcessLifecycleOwner.get()
41+
@Provides @ProcessLifecycle
42+
fun provideProcessLifecycleOwner(): LifecycleOwner = ProcessLifecycleOwner.get()
4143

4244
@Provides
43-
fun provideProcessLifecycle(processLifecycleOwner: LifecycleOwner): Lifecycle = processLifecycleOwner.lifecycle
45+
@ProcessLifecycle
46+
fun provideProcessLifecycle(@ProcessLifecycle processLifecycleOwner: LifecycleOwner): Lifecycle =
47+
processLifecycleOwner.lifecycle
4448

4549
@Singleton
4650
@Provides

app/src/main/java/com/geeksville/mesh/MeshServiceClient.kt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package com.geeksville.mesh
1919

20-
import android.app.Activity
20+
import android.content.Context
2121
import androidx.appcompat.app.AppCompatActivity.BIND_ABOVE_CLIENT
2222
import androidx.appcompat.app.AppCompatActivity.BIND_AUTO_CREATE
2323
import androidx.lifecycle.DefaultLifecycleObserver
@@ -28,6 +28,7 @@ import com.geeksville.mesh.android.ServiceClient
2828
import com.geeksville.mesh.concurrent.handledLaunch
2929
import com.geeksville.mesh.service.MeshService
3030
import com.geeksville.mesh.service.startService
31+
import dagger.hilt.android.qualifiers.ActivityContext
3132
import dagger.hilt.android.scopes.ActivityScoped
3233
import kotlinx.coroutines.Job
3334
import org.meshtastic.core.service.IMeshService
@@ -40,18 +41,12 @@ import javax.inject.Inject
4041
class MeshServiceClient
4142
@Inject
4243
constructor(
43-
/**
44-
* Ideally, this would be broken up into Context and LifecycleOwner. However, ApplicationModule defines its own
45-
* LifecycleOwner which overrides the default binding for @ActivityScoped. The solution to this is to add a
46-
* qualifier to the LifecycleOwner provider in ApplicationModule.
47-
*/
48-
private val activity: Activity,
44+
@ActivityContext private val context: Context,
4945
private val serviceRepository: ServiceRepository,
5046
) : ServiceClient<IMeshService>(IMeshService.Stub::asInterface),
5147
DefaultLifecycleObserver {
5248

53-
// TODO Use the default binding for @ActivityScoped
54-
private val lifecycleOwner: LifecycleOwner = activity as LifecycleOwner
49+
private val lifecycleOwner: LifecycleOwner = context as LifecycleOwner
5550

5651
// TODO Inject this for ease of testing
5752
private var serviceSetupJob: Job? = null
@@ -106,11 +101,11 @@ constructor(
106101
private fun bindMeshService() {
107102
Timber.d("Binding to mesh service!")
108103
try {
109-
MeshService.startService(activity)
104+
MeshService.startService(context)
110105
} catch (ex: Exception) {
111106
Timber.e("Failed to start service from activity - but ignoring because bind will work: ${ex.message}")
112107
}
113108

114-
connect(activity, MeshService.createIntent(activity), BIND_AUTO_CREATE + BIND_ABOVE_CLIENT)
109+
connect(context, MeshService.createIntent(context), BIND_AUTO_CREATE + BIND_ABOVE_CLIENT)
115110
}
116111
}

app/src/main/java/com/geeksville/mesh/repository/bluetooth/BluetoothRepository.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import no.nordicsemi.kotlin.ble.client.distinctByPeripheral
4141
import no.nordicsemi.kotlin.ble.core.Manager
4242
import org.meshtastic.core.common.hasBluetoothPermission
4343
import org.meshtastic.core.di.CoroutineDispatchers
44+
import org.meshtastic.core.di.ProcessLifecycle
4445
import timber.log.Timber
4546
import javax.inject.Inject
4647
import javax.inject.Singleton
@@ -56,7 +57,7 @@ constructor(
5657
private val application: Application,
5758
private val bluetoothBroadcastReceiverLazy: Lazy<BluetoothBroadcastReceiver>,
5859
private val dispatchers: CoroutineDispatchers,
59-
private val processLifecycle: Lifecycle,
60+
@ProcessLifecycle private val processLifecycle: Lifecycle,
6061
private val centralManager: CentralManager,
6162
) {
6263
private val _state =

app/src/main/java/com/geeksville/mesh/repository/radio/RadioInterfaceService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import kotlinx.coroutines.flow.onEach
4545
import kotlinx.coroutines.launch
4646
import org.meshtastic.core.analytics.platform.PlatformAnalytics
4747
import org.meshtastic.core.di.CoroutineDispatchers
48+
import org.meshtastic.core.di.ProcessLifecycle
4849
import org.meshtastic.core.model.util.anonymize
4950
import org.meshtastic.core.prefs.radio.RadioPrefs
5051
import org.meshtastic.core.service.ConnectionState
@@ -71,7 +72,7 @@ constructor(
7172
private val dispatchers: CoroutineDispatchers,
7273
private val bluetoothRepository: BluetoothRepository,
7374
private val networkRepository: NetworkRepository,
74-
private val processLifecycle: Lifecycle,
75+
@ProcessLifecycle private val processLifecycle: Lifecycle,
7576
private val radioPrefs: RadioPrefs,
7677
private val interfaceFactory: InterfaceFactory,
7778
private val analytics: PlatformAnalytics,

app/src/main/java/com/geeksville/mesh/repository/usb/UsbRepository.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import kotlinx.coroutines.flow.stateIn
3636
import kotlinx.coroutines.launch
3737
import kotlinx.coroutines.withContext
3838
import org.meshtastic.core.di.CoroutineDispatchers
39+
import org.meshtastic.core.di.ProcessLifecycle
3940
import javax.inject.Inject
4041
import javax.inject.Singleton
4142

@@ -47,7 +48,7 @@ class UsbRepository
4748
constructor(
4849
private val application: Application,
4950
private val dispatchers: CoroutineDispatchers,
50-
private val processLifecycle: Lifecycle,
51+
@ProcessLifecycle private val processLifecycle: Lifecycle,
5152
private val usbBroadcastReceiverLazy: dagger.Lazy<UsbBroadcastReceiver>,
5253
private val usbManagerLazy: dagger.Lazy<UsbManager?>,
5354
private val usbSerialProberLazy: dagger.Lazy<UsbSerialProber>,

core/data/src/main/kotlin/org/meshtastic/core/data/repository/NodeRepository.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import org.meshtastic.core.database.entity.NodeEntity
4040
import org.meshtastic.core.database.model.Node
4141
import org.meshtastic.core.database.model.NodeSortOption
4242
import org.meshtastic.core.di.CoroutineDispatchers
43+
import org.meshtastic.core.di.ProcessLifecycle
4344
import org.meshtastic.core.model.DataPacket
4445
import org.meshtastic.core.model.util.onlineTimeThreshold
4546
import org.meshtastic.proto.MeshProtos
@@ -51,7 +52,7 @@ import javax.inject.Singleton
5152
class NodeRepository
5253
@Inject
5354
constructor(
54-
processLifecycle: Lifecycle,
55+
@ProcessLifecycle processLifecycle: Lifecycle,
5556
private val nodeInfoReadDataSource: NodeInfoReadDataSource,
5657
private val nodeInfoWriteDataSource: NodeInfoWriteDataSource,
5758
private val dispatchers: CoroutineDispatchers,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2025 Meshtastic LLC
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package org.meshtastic.core.di
19+
20+
import javax.inject.Qualifier
21+
22+
@Qualifier
23+
@Retention(AnnotationRetention.BINARY)
24+
annotation class ProcessLifecycle

0 commit comments

Comments
 (0)