Skip to content

Commit

Permalink
#1274 add Hilt modules WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sds100 committed Oct 16, 2024
1 parent a5ba747 commit 1a1e6a5
Show file tree
Hide file tree
Showing 20 changed files with 333 additions and 26 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ dependencies {
implementation "dev.rikka.shizuku:provider:$shizuku_version"
implementation "org.lsposed.hiddenapibypass:hiddenapibypass:4.3"
implementation("com.google.dagger:hilt-android:2.51")
implementation("androidx.hilt:hilt-navigation-fragment:1.2.0")
kapt("com.google.dagger:hilt-android-compiler:2.51")
proImplementation 'com.revenuecat.purchases:purchases:8.8.1'

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
</service>

<service
android:name="io.github.sds100.keymapper.system.notifications.NotificationReceiver"
android:name="io.github.sds100.keymapper.system.notifications.NotificationReceiverService"
android:exported="true"
android:label="Key Mapper Notification Receiver"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/io/github/sds100/keymapper/AppHiltModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.sds100.keymapper

import android.content.Context
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
class AppHiltModule {
@Singleton
@Provides
fun provideCoroutineScope(@ApplicationContext ctx: Context): CoroutineScope = MainScope()
}
11 changes: 8 additions & 3 deletions app/src/main/java/io/github/sds100/keymapper/KeyMapperApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import io.github.sds100.keymapper.system.nfc.AndroidNfcAdapter
import io.github.sds100.keymapper.system.notifications.AndroidNotificationAdapter
import io.github.sds100.keymapper.system.notifications.ManageNotificationsUseCaseImpl
import io.github.sds100.keymapper.system.notifications.NotificationController
import io.github.sds100.keymapper.system.notifications.NotificationReceiverAdapter
import io.github.sds100.keymapper.system.notifications.NotificationReceiverServiceAdapter
import io.github.sds100.keymapper.system.permissions.AndroidPermissionAdapter
import io.github.sds100.keymapper.system.permissions.AutoGrantPermissionController
import io.github.sds100.keymapper.system.permissions.Permission
Expand Down Expand Up @@ -108,14 +108,19 @@ class KeyMapperApp : MultiDexApplication() {
this,
appCoroutineScope,
suAdapter,
notificationReceiverAdapter,
notificationReceiverServiceAdapter,
ServiceLocator.settingsRepository(this),
)
}

val systemFeatureAdapter by lazy { AndroidSystemFeatureAdapter(this) }
val accessibilityServiceAdapter by lazy { AccessibilityServiceAdapter(this, appCoroutineScope) }
val notificationReceiverAdapter by lazy { NotificationReceiverAdapter(this, appCoroutineScope) }
val notificationReceiverServiceAdapter by lazy {
NotificationReceiverServiceAdapter(
this,
appCoroutineScope,
)
}
val appShortcutAdapter by lazy { AndroidAppShortcutAdapter(this) }
val fileAdapter by lazy { AndroidFileAdapter(this) }
val popupMessageAdapter by lazy { AndroidToastAdapter(this) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import io.github.sds100.keymapper.system.network.NetworkAdapter
import io.github.sds100.keymapper.system.nfc.NfcAdapter
import io.github.sds100.keymapper.system.notifications.AndroidNotificationAdapter
import io.github.sds100.keymapper.system.notifications.NotificationController
import io.github.sds100.keymapper.system.notifications.NotificationReceiverAdapter
import io.github.sds100.keymapper.system.notifications.NotificationReceiverServiceAdapter
import io.github.sds100.keymapper.system.permissions.AndroidPermissionAdapter
import io.github.sds100.keymapper.system.permissions.SystemFeatureAdapter
import io.github.sds100.keymapper.system.phone.PhoneAdapter
Expand Down Expand Up @@ -189,8 +189,8 @@ object ServiceLocator {
fun accessibilityServiceAdapter(context: Context): AccessibilityServiceAdapter =
(context.applicationContext as KeyMapperApp).accessibilityServiceAdapter

fun notificationReceiverAdapter(context: Context): NotificationReceiverAdapter =
(context.applicationContext as KeyMapperApp).notificationReceiverAdapter
fun notificationReceiverAdapter(context: Context): NotificationReceiverServiceAdapter =
(context.applicationContext as KeyMapperApp).notificationReceiverServiceAdapter

fun appShortcutAdapter(context: Context): AppShortcutAdapter =
(context.applicationContext as KeyMapperApp).appShortcutAdapter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.sds100.keymapper.actions

import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ViewModelComponent

@Module
@InstallIn(ViewModelComponent::class)
abstract class ActionsHiltModule {
@Binds
abstract fun provideTestActionUseCase(impl: TestActionUseCaseImpl): TestActionUseCase
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package io.github.sds100.keymapper.actions
import io.github.sds100.keymapper.system.accessibility.ServiceAdapter
import io.github.sds100.keymapper.util.Result
import io.github.sds100.keymapper.util.ServiceEvent
import javax.inject.Inject

/**
* Created by sds100 on 20/02/2021.
*/

class TestActionUseCaseImpl(
class TestActionUseCaseImpl @Inject constructor(
private val serviceAdapter: ServiceAdapter,
) : TestActionUseCase {
override suspend fun invoke(action: ActionData): Result<*> =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.github.sds100.keymapper.data

import android.content.Context
import androidx.datastore.preferences.preferencesDataStore
import androidx.room.Room
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import io.github.sds100.keymapper.data.db.AppDatabase
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object DataHiltModule {
private val Context.legacyFingerprintMapDataStore by preferencesDataStore("fingerprint_gestures")

@Singleton
@Provides
fun provideAppDatabase(@ApplicationContext ctx: Context): AppDatabase {
return Room.databaseBuilder(
ctx,
AppDatabase::class.java,
AppDatabase.DATABASE_NAME,
).addMigrations(
AppDatabase.MIGRATION_1_2,
AppDatabase.MIGRATION_2_3,
AppDatabase.MIGRATION_3_4,
AppDatabase.MIGRATION_4_5,
AppDatabase.MIGRATION_5_6,
AppDatabase.MIGRATION_6_7,
AppDatabase.MIGRATION_7_8,
AppDatabase.MIGRATION_8_9,
AppDatabase.MIGRATION_9_10,
AppDatabase.MIGRATION_10_11,
AppDatabase.RoomMigration11To12(ctx.legacyFingerprintMapDataStore),
AppDatabase.MIGRATION_12_13,
).build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import java.util.UUID
import javax.inject.Inject
import javax.inject.Singleton

/**
* Created by sds100 on 18/03/2021.
*/
class RoomKeyMapRepository(
@Singleton
class RoomKeyMapRepository @Inject constructor(
private val dao: KeyMapDao,
private val coroutineScope: CoroutineScope,
private val dispatchers: DispatcherProvider = DefaultDispatcherProvider(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.github.sds100.keymapper.mappings

import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ViewModelComponent
import io.github.sds100.keymapper.mappings.fingerprintmaps.AreFingerprintGesturesSupportedUseCase
import io.github.sds100.keymapper.mappings.fingerprintmaps.AreFingerprintGesturesSupportedUseCaseImpl
import io.github.sds100.keymapper.mappings.fingerprintmaps.DetectFingerprintMapsUseCase
import io.github.sds100.keymapper.mappings.fingerprintmaps.DetectFingerprintMapsUseCaseImpl
import io.github.sds100.keymapper.mappings.keymaps.ConfigKeyMapUseCase
import io.github.sds100.keymapper.mappings.keymaps.ConfigKeyMapUseCaseImpl
import io.github.sds100.keymapper.mappings.keymaps.CreateKeyMapShortcutUseCase
import io.github.sds100.keymapper.mappings.keymaps.CreateKeyMapShortcutUseCaseImpl
import io.github.sds100.keymapper.mappings.keymaps.DisplayKeyMapUseCase
import io.github.sds100.keymapper.mappings.keymaps.DisplayKeyMapUseCaseImpl

@Module
@InstallIn(ViewModelComponent::class)
abstract class MappingsHiltModule {
@Binds
abstract fun provideConfigKeyMapUseCase(impl: ConfigKeyMapUseCaseImpl): ConfigKeyMapUseCase

@Binds
abstract fun provideDisplaySimpleMappingUseCase(impl: DisplaySimpleMappingUseCaseImpl): DisplaySimpleMappingUseCase

@Binds
abstract fun providePauseMappingsUseCase(impl: PauseMappingsUseCaseImpl): PauseMappingsUseCase

@Binds
abstract fun provideDetectFingerprintMapsUseCase(impl: DetectFingerprintMapsUseCaseImpl): DetectFingerprintMapsUseCase

@Binds
abstract fun provideAreFingerprintGesturesSupportedUseCase(impl: AreFingerprintGesturesSupportedUseCaseImpl): AreFingerprintGesturesSupportedUseCase

@Binds
abstract fun provideCreateKeyMapShortcutUseCase(impl: CreateKeyMapShortcutUseCaseImpl): CreateKeyMapShortcutUseCase

@Binds
abstract fun provideDisplayKeyMapUseCase(impl: DisplayKeyMapUseCaseImpl): DisplayKeyMapUseCase
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package io.github.sds100.keymapper.mappings.keymaps
import android.os.Bundle
import android.view.View
import androidx.fragment.app.setFragmentResultListener
import androidx.hilt.navigation.fragment.hiltNavGraphViewModels
import androidx.lifecycle.Lifecycle
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.navigation.navGraphViewModels
import io.github.sds100.keymapper.NavAppDirections
import io.github.sds100.keymapper.R
import io.github.sds100.keymapper.constraints.ChooseConstraintFragment
Expand All @@ -17,7 +17,6 @@ import io.github.sds100.keymapper.mappings.keymaps.trigger.ConfigTriggerOptionsF
import io.github.sds100.keymapper.mappings.keymaps.trigger.TriggerFragment
import io.github.sds100.keymapper.ui.utils.getJsonSerializable
import io.github.sds100.keymapper.util.FragmentInfo
import io.github.sds100.keymapper.util.Inject
import io.github.sds100.keymapper.util.int
import io.github.sds100.keymapper.util.intArray
import io.github.sds100.keymapper.util.launchRepeatOnLifecycle
Expand All @@ -34,9 +33,7 @@ class ConfigKeyMapFragment : ConfigMappingFragment() {

private val args by navArgs<ConfigKeyMapFragmentArgs>()

override val viewModel: ConfigKeyMapViewModel by navGraphViewModels(R.id.nav_config_keymap) {
Inject.configKeyMapViewModel(requireContext())
}
override val viewModel: ConfigKeyMapViewModel by hiltNavGraphViewModels(R.id.nav_config_keymap)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ import io.github.sds100.keymapper.util.ifIsData
import io.github.sds100.keymapper.util.moveElement
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

/**
* Created by sds100 on 16/02/2021.
*/
class ConfigKeyMapUseCaseImpl(
class ConfigKeyMapUseCaseImpl @Inject constructor(
private val keyMapRepository: KeyMapRepository,
private val devicesAdapter: DevicesAdapter,
private val preferenceRepository: PreferenceRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Bundle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import io.github.sds100.keymapper.actions.ConfigActionsViewModel
import io.github.sds100.keymapper.actions.CreateActionUseCase
import io.github.sds100.keymapper.actions.TestActionUseCase
Expand All @@ -24,12 +25,14 @@ import io.github.sds100.keymapper.util.ui.ResourceProvider
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
* Created by sds100 on 22/11/20.
*/

class ConfigKeyMapViewModel(
@HiltViewModel
class ConfigKeyMapViewModel @Inject constructor(
private val config: ConfigKeyMapUseCase,
private val testAction: TestActionUseCase,
private val onboarding: OnboardingUseCase,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.sds100.keymapper.onboarding

import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ViewModelComponent

@Module
@InstallIn(ViewModelComponent::class)
abstract class OnboardingHiltModule {
@Binds
abstract fun provideOnboardingUseCase(impl: OnboardingUseCaseImpl): OnboardingUseCase
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import javax.inject.Inject

/**
* Created by sds100 on 14/02/21.
*/
class OnboardingUseCaseImpl(
class OnboardingUseCaseImpl @Inject constructor(
private val preferenceRepository: PreferenceRepository,
private val fileAdapter: FileAdapter,
private val leanbackAdapter: LeanbackAdapter,
Expand Down Expand Up @@ -74,7 +75,7 @@ class OnboardingUseCaseImpl(
)

override val showWhatsNew = get(Keys.lastInstalledVersionCodeHomeScreen)
.map { it ?: -1 < Constants.VERSION_CODE }
.map { (it ?: -1) < Constants.VERSION_CODE }

override fun showedWhatsNew() {
set(Keys.lastInstalledVersionCodeHomeScreen, Constants.VERSION_CODE)
Expand Down
Loading

0 comments on commit 1a1e6a5

Please sign in to comment.