Skip to content

Commit

Permalink
Merge pull request #100 from lucasnlm/migrate-to-hilt
Browse files Browse the repository at this point in the history
Migrate to hilt
  • Loading branch information
lucasnlm authored Jun 20, 2020
2 parents 2d6c4fc + 2f141f1 commit 2135744
Show file tree
Hide file tree
Showing 41 changed files with 251 additions and 580 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
- name: Build with Gradle
run: |
./gradlew assembleDebug
./gradlew test --stacktrace
./gradlew testGoogleDebugUnitTest --stacktrace
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Where `ANDROID_JRE` is the Java runtime provided by Android Studio.
- [Android SDK 29](https://developer.android.com/about/versions/10)
- [AndroidX](https://developer.android.com/jetpack/androidx)
- [Lifecycle](https://developer.android.com/topic/libraries/architecture/lifecycle)
- [Dagger](https://dagger.dev/android.html)
- [Dagger Hilt](https://dagger.dev/hilt/)
- [Room](https://developer.android.com/training/data-storage/room)
- [Robolectric](http://robolectric.org/)
- [Coroutines](https://kotlinlang.org/docs/reference/coroutines-overview.html)
Expand Down
29 changes: 21 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'kotlin-kapt'

android {
Expand Down Expand Up @@ -58,6 +59,14 @@ android {
}
}

kapt {
correctErrorTypes true
}

hilt {
enableTransformForLocalTests true
}

dependencies {
// Dependencies must be hardcoded to support F-droid

Expand All @@ -69,9 +78,10 @@ dependencies {

// AndroidX
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.preference:preference:1.1.0'
implementation 'androidx.preference:preference:1.1.1'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'androidx.activity:activity-ktx:1.1.0'

// Constraint
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
Expand All @@ -80,26 +90,29 @@ dependencies {
implementation 'com.google.android.material:material:1.1.0'

// Dagger
api 'com.google.dagger:dagger-android:2.21'
api 'com.google.dagger:dagger-android-support:2.21'
kapt 'com.google.dagger:dagger-android-processor:2.21'
kapt 'com.google.dagger:dagger-compiler:2.21'
testImplementation 'com.google.dagger:dagger-android:2.21'
testImplementation 'com.google.dagger:dagger-android-support:2.21'
implementation 'com.google.dagger:hilt-android:2.28-alpha'
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01'
kapt 'com.google.dagger:hilt-android-compiler:2.28-alpha'
testImplementation 'com.google.dagger:hilt-android-testing:2.28-alpha'
kaptTest 'com.google.dagger:hilt-android-compiler:2.28-alpha'

// Kotlin
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.5'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.5'

// Tests
testImplementation 'junit:junit:4.12'
testImplementation 'junit:junit:4.13'
testImplementation 'androidx.core:core:1.1.0'
testImplementation 'androidx.test:core:1.2.0'
testImplementation 'androidx.test:core-ktx:1.2.0'
testImplementation 'androidx.test:rules:1.2.0'
testImplementation 'androidx.test:runner:1.2.0'
testImplementation 'androidx.test.espresso:espresso-core:3.2.0'
testImplementation 'androidx.test.espresso:espresso-contrib:3.2.0'
testImplementation 'androidx.fragment:fragment-testing:1.1.0'
testImplementation 'org.robolectric:robolectric:4.3.1'
testImplementation 'androidx.test.ext:junit:1.1.1'
}

tasks.withType(Test) {
Expand Down
20 changes: 7 additions & 13 deletions app/src/main/java/dev/lucasnlm/antimine/GameActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.WindowManager
import androidx.activity.viewModels
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.HandlerCompat.postDelayed
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.preference.PreferenceManager
import dagger.android.support.DaggerAppCompatActivity
import dagger.hilt.android.AndroidEntryPoint
import dev.lucasnlm.antimine.about.AboutActivity
import dev.lucasnlm.antimine.common.level.models.Difficulty
import dev.lucasnlm.antimine.common.level.models.Event
import dev.lucasnlm.antimine.common.level.models.Score
import dev.lucasnlm.antimine.common.level.models.Status
import dev.lucasnlm.antimine.common.level.repository.ISavesRepository
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModel
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModelFactory
import dev.lucasnlm.antimine.core.analytics.AnalyticsManager
import dev.lucasnlm.antimine.core.analytics.models.Analytics
import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository
Expand All @@ -45,11 +45,8 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import javax.inject.Inject

class GameActivity : DaggerAppCompatActivity() {

@Inject
lateinit var viewModelFactory: GameViewModelFactory

@AndroidEntryPoint
class GameActivity : AppCompatActivity() {
@Inject
lateinit var preferencesRepository: IPreferencesRepository

Expand All @@ -62,8 +59,8 @@ class GameActivity : DaggerAppCompatActivity() {
@Inject
lateinit var savesRepository: ISavesRepository

private lateinit var viewModel: GameViewModel
private lateinit var shareViewModel: ShareViewModel
private val viewModel: GameViewModel by viewModels()
private val shareViewModel: ShareViewModel by viewModels()

private var status: Status = Status.PreGame
private val usingLargeArea by lazy { preferencesRepository.useLargeAreas() }
Expand All @@ -78,9 +75,6 @@ class GameActivity : DaggerAppCompatActivity() {
setContentView(R.layout.activity_game)
PreferenceManager.setDefaultValues(this, R.xml.preferences, false)

viewModel = ViewModelProviders.of(this, viewModelFactory).get(GameViewModel::class.java)
shareViewModel = ViewModelProviders.of(this).get(ShareViewModel::class.java)

bindViewModel()
bindToolbarAndDrawer()
loadGameFragment()
Expand Down
36 changes: 8 additions & 28 deletions app/src/main/java/dev/lucasnlm/antimine/MainApplication.kt
Original file line number Diff line number Diff line change
@@ -1,41 +1,21 @@
package dev.lucasnlm.antimine

import android.app.Application
import android.content.Context
import androidx.multidex.MultiDex
import dagger.android.AndroidInjector
import dagger.android.support.DaggerApplication
import dev.lucasnlm.antimine.common.level.di.LevelModule
import androidx.multidex.MultiDexApplication
import dagger.hilt.android.HiltAndroidApp
import dev.lucasnlm.antimine.core.analytics.AnalyticsManager
import dev.lucasnlm.antimine.core.analytics.models.Analytics
import dev.lucasnlm.antimine.di.AppModule
import dev.lucasnlm.antimine.di.DaggerAppComponent
import javax.inject.Inject

open class MainApplication : DaggerApplication() {

@HiltAndroidApp
open class MainApplication : MultiDexApplication() {
@Inject
lateinit var analyticsManager: AnalyticsManager

protected open fun appModule(application: Application) = AppModule(application)

protected open fun levelModule(application: Application) = LevelModule(application)

override fun applicationInjector(): AndroidInjector<out DaggerApplication> =
DaggerAppComponent.builder()
.application(this)
.appModule(appModule(this))
.levelModule(levelModule(this))
.build()

override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)
MultiDex.install(this)
}

override fun onCreate() {
super.onCreate()
analyticsManager.setup(applicationContext, mapOf())
analyticsManager.sentEvent(Analytics.Open())
analyticsManager.apply {
setup(applicationContext, mapOf())
sentEvent(Analytics.Open())
}
}
}
21 changes: 6 additions & 15 deletions app/src/main/java/dev/lucasnlm/antimine/TvGameActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,31 @@ import android.text.format.DateUtils
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.HandlerCompat
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.preference.PreferenceManager
import dagger.android.support.DaggerAppCompatActivity
import dagger.hilt.android.AndroidEntryPoint
import dev.lucasnlm.antimine.about.AboutActivity
import dev.lucasnlm.antimine.common.level.models.Difficulty
import dev.lucasnlm.antimine.common.level.models.Event
import dev.lucasnlm.antimine.common.level.models.Score
import dev.lucasnlm.antimine.common.level.models.Status
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModel
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModelFactory
import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository
import dev.lucasnlm.antimine.level.view.CustomLevelDialogFragment
import dev.lucasnlm.antimine.level.view.LevelFragment
import dev.lucasnlm.antimine.preferences.PreferencesActivity
import kotlinx.android.synthetic.main.activity_tv_game.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import javax.inject.Inject

class TvGameActivity : DaggerAppCompatActivity() {

@Inject
lateinit var viewModelFactory: GameViewModelFactory

@Inject
lateinit var preferencesRepository: IPreferencesRepository

private lateinit var viewModel: GameViewModel
@AndroidEntryPoint
class TvGameActivity : AppCompatActivity() {
private val viewModel: GameViewModel by viewModels()

private var status: Status = Status.PreGame
private var totalMines: Int = 0
Expand All @@ -52,7 +44,6 @@ class TvGameActivity : DaggerAppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tv_game)
viewModel = ViewModelProviders.of(this, viewModelFactory).get(GameViewModel::class.java)
bindViewModel()

PreferenceManager.setDefaultValues(this, R.xml.preferences, false)
Expand Down
28 changes: 0 additions & 28 deletions app/src/main/java/dev/lucasnlm/antimine/di/ActivityModule.kt

This file was deleted.

36 changes: 0 additions & 36 deletions app/src/main/java/dev/lucasnlm/antimine/di/AppComponent.kt

This file was deleted.

16 changes: 8 additions & 8 deletions app/src/main/java/dev/lucasnlm/antimine/di/AppModule.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package dev.lucasnlm.antimine.di

import android.app.Application
import android.content.Context
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import dagger.hilt.android.qualifiers.ApplicationContext
import dev.lucasnlm.antimine.instant.InstantAppManager

@Module
class AppModule(
private val application: Application
) {
@InstallIn(ApplicationComponent::class)
class AppModule {
@Provides
fun provideContext(): Context = application.applicationContext

@Provides
fun provideInstantAppManager(): InstantAppManager = InstantAppManager(application.applicationContext)
fun provideInstantAppManager(
@ApplicationContext context: Context
): InstantAppManager = InstantAppManager(context)
}
23 changes: 0 additions & 23 deletions app/src/main/java/dev/lucasnlm/antimine/di/FragmentModule.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import dagger.android.support.DaggerFragment
import dagger.hilt.android.AndroidEntryPoint
import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.common.level.repository.ISavesRepository
import dev.lucasnlm.antimine.history.viewmodel.HistoryViewModel
Expand All @@ -17,7 +18,8 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import javax.inject.Inject

class HistoryFragment : DaggerFragment() {
@AndroidEntryPoint
class HistoryFragment : Fragment() {
@Inject
lateinit var savesRepository: ISavesRepository

Expand Down
Loading

0 comments on commit 2135744

Please sign in to comment.