Use Hilt lifecycle viewmodel and remove ViewModelFactory
This commit is contained in:
parent
63fd0599e7
commit
51927617ec
15 changed files with 59 additions and 208 deletions
|
@ -78,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'
|
||||
|
@ -90,6 +91,7 @@ dependencies {
|
|||
|
||||
// Dagger
|
||||
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'
|
||||
|
|
|
@ -10,6 +10,7 @@ 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
|
||||
|
@ -18,7 +19,6 @@ 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.hilt.android.AndroidEntryPoint
|
||||
import dev.lucasnlm.antimine.about.AboutActivity
|
||||
|
@ -28,7 +28,6 @@ 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
|
||||
|
@ -48,9 +47,6 @@ import javax.inject.Inject
|
|||
|
||||
@AndroidEntryPoint
|
||||
class GameActivity : AppCompatActivity() {
|
||||
@Inject
|
||||
lateinit var viewModelFactory: GameViewModelFactory
|
||||
|
||||
@Inject
|
||||
lateinit var preferencesRepository: IPreferencesRepository
|
||||
|
||||
|
@ -63,8 +59,8 @@ class GameActivity : AppCompatActivity() {
|
|||
@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() }
|
||||
|
@ -79,9 +75,6 @@ class GameActivity : AppCompatActivity() {
|
|||
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()
|
||||
|
|
|
@ -7,12 +7,12 @@ 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.hilt.android.AndroidEntryPoint
|
||||
import dev.lucasnlm.antimine.about.AboutActivity
|
||||
|
@ -21,8 +21,6 @@ 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
|
||||
|
@ -30,17 +28,10 @@ import kotlinx.android.synthetic.main.activity_tv_game.*
|
|||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class TvGameActivity : AppCompatActivity() {
|
||||
@Inject
|
||||
lateinit var viewModelFactory: GameViewModelFactory
|
||||
|
||||
@Inject
|
||||
lateinit var preferencesRepository: IPreferencesRepository
|
||||
|
||||
private lateinit var viewModel: GameViewModel
|
||||
private val viewModel: GameViewModel by viewModels()
|
||||
|
||||
private var status: Status = Status.PreGame
|
||||
private var totalMines: Int = 0
|
||||
|
@ -53,7 +44,6 @@ class TvGameActivity : AppCompatActivity() {
|
|||
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)
|
||||
|
|
|
@ -2,17 +2,15 @@ package dev.lucasnlm.antimine.level.view
|
|||
|
||||
import android.app.Dialog
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import android.widget.TextView
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatDialogFragment
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import dev.lucasnlm.antimine.R
|
||||
import dev.lucasnlm.antimine.common.level.models.Difficulty
|
||||
import dev.lucasnlm.antimine.common.level.models.Minefield
|
||||
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModel
|
||||
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModelFactory
|
||||
import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
|
@ -21,9 +19,6 @@ import javax.inject.Inject
|
|||
|
||||
@AndroidEntryPoint
|
||||
class CustomLevelDialogFragment : AppCompatDialogFragment() {
|
||||
@Inject
|
||||
lateinit var viewModelFactory: GameViewModelFactory
|
||||
|
||||
@Inject
|
||||
lateinit var preferencesRepository: IPreferencesRepository
|
||||
|
||||
|
@ -33,7 +28,7 @@ class CustomLevelDialogFragment : AppCompatDialogFragment() {
|
|||
super.onCreate(savedInstanceState)
|
||||
|
||||
activity?.let {
|
||||
viewModel = ViewModelProviders.of(it, viewModelFactory).get(GameViewModel::class.java)
|
||||
viewModel = it.viewModels<GameViewModel>().value
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,7 +47,7 @@ class CustomLevelDialogFragment : AppCompatDialogFragment() {
|
|||
}
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
return AlertDialog.Builder(context!!, R.style.MyDialog).apply {
|
||||
return AlertDialog.Builder(requireContext(), R.style.MyDialog).apply {
|
||||
setTitle(R.string.new_game)
|
||||
setView(R.layout.dialog_custom_game)
|
||||
setNegativeButton(R.string.cancel, null)
|
||||
|
|
|
@ -5,14 +5,13 @@ import android.app.Dialog
|
|||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.widget.TextView
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatDialogFragment
|
||||
import androidx.fragment.app.FragmentManager
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import dev.lucasnlm.antimine.R
|
||||
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModel
|
||||
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModelFactory
|
||||
import dev.lucasnlm.antimine.instant.InstantAppManager
|
||||
import dev.lucasnlm.antimine.level.viewmodel.EngGameDialogViewModel
|
||||
import dev.lucasnlm.antimine.share.viewmodel.ShareViewModel
|
||||
|
@ -22,9 +21,6 @@ import javax.inject.Inject
|
|||
|
||||
@AndroidEntryPoint
|
||||
class EndGameDialogFragment : AppCompatDialogFragment() {
|
||||
@Inject
|
||||
lateinit var viewModelFactory: GameViewModelFactory
|
||||
|
||||
@Inject
|
||||
lateinit var instantAppManager: InstantAppManager
|
||||
|
||||
|
@ -43,9 +39,9 @@ class EndGameDialogFragment : AppCompatDialogFragment() {
|
|||
super.onCreate(savedInstanceState)
|
||||
|
||||
activity?.let {
|
||||
viewModel = ViewModelProviders.of(it, viewModelFactory).get(GameViewModel::class.java)
|
||||
endGameViewModel = ViewModelProviders.of(this).get(EngGameDialogViewModel::class.java)
|
||||
shareViewModel = ViewModelProviders.of(this).get(ShareViewModel::class.java)
|
||||
viewModel = it.viewModels<GameViewModel>().value
|
||||
endGameViewModel = it.viewModels<EngGameDialogViewModel>().value
|
||||
shareViewModel = it.viewModels<ShareViewModel>().value
|
||||
}
|
||||
|
||||
arguments?.run {
|
||||
|
@ -66,7 +62,7 @@ class EndGameDialogFragment : AppCompatDialogFragment() {
|
|||
|
||||
@SuppressLint("InflateParams")
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog =
|
||||
AlertDialog.Builder(context!!, R.style.MyDialog).apply {
|
||||
AlertDialog.Builder(requireContext(), R.style.MyDialog).apply {
|
||||
val view = LayoutInflater
|
||||
.from(context)
|
||||
.inflate(R.layout.dialog_end_game, null, false)
|
||||
|
|
|
@ -16,7 +16,6 @@ import dev.lucasnlm.antimine.common.level.repository.MemorySavesRepository
|
|||
import dev.lucasnlm.antimine.common.level.repository.MemoryStatsRepository
|
||||
import dev.lucasnlm.antimine.common.level.utils.Clock
|
||||
import dev.lucasnlm.antimine.common.level.utils.IHapticFeedbackInteractor
|
||||
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModelFactory
|
||||
import dev.lucasnlm.antimine.core.analytics.AnalyticsManager
|
||||
import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository
|
||||
import dev.lucasnlm.antimine.mocks.DisabledHapticFeedbackInteractor
|
||||
|
@ -32,31 +31,6 @@ class TestLevelModule {
|
|||
@Provides
|
||||
fun provideClock(): Clock = Clock()
|
||||
|
||||
@Provides
|
||||
fun provideGameViewModelFactory(
|
||||
@ApplicationContext context: Context,
|
||||
eventObserver: MutableLiveData<Event>,
|
||||
savesRepository: ISavesRepository,
|
||||
statsRepository: IStatsRepository,
|
||||
dimensionRepository: IDimensionRepository,
|
||||
preferencesRepository: IPreferencesRepository,
|
||||
hapticFeedbackInteractor: IHapticFeedbackInteractor,
|
||||
minefieldRepository: IMinefieldRepository,
|
||||
analyticsManager: AnalyticsManager,
|
||||
clock: Clock
|
||||
) = GameViewModelFactory(
|
||||
context,
|
||||
eventObserver,
|
||||
savesRepository,
|
||||
statsRepository,
|
||||
dimensionRepository,
|
||||
preferencesRepository,
|
||||
hapticFeedbackInteractor,
|
||||
minefieldRepository,
|
||||
analyticsManager,
|
||||
clock
|
||||
)
|
||||
|
||||
@Provides
|
||||
fun provideDimensionRepository(): IDimensionRepository = FixedDimensionRepository()
|
||||
|
||||
|
|
|
@ -16,6 +16,15 @@ android {
|
|||
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
|
||||
resConfigs 'en', 'cs', 'de', 'el', 'es', 'fr', 'ja', 'pt', 'ru', 'tr', 'uk', 'vi', 'zh'
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
targetCompatibility 1.8
|
||||
sourceCompatibility 1.8
|
||||
}
|
||||
|
||||
kotlinOptions {
|
||||
jvmTarget = JavaVersion.VERSION_1_8.toString()
|
||||
}
|
||||
}
|
||||
|
||||
kapt {
|
||||
|
@ -33,8 +42,9 @@ 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.activity:activity-ktx:1.1.0'
|
||||
|
||||
// Constraint
|
||||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
|
||||
|
@ -45,6 +55,7 @@ dependencies {
|
|||
|
||||
// Dagger
|
||||
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'
|
||||
|
|
|
@ -8,10 +8,10 @@ import dagger.Provides
|
|||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.components.ActivityComponent
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dev.lucasnlm.antimine.common.level.models.Event
|
||||
import dev.lucasnlm.antimine.common.level.database.AppDataBase
|
||||
import dev.lucasnlm.antimine.common.level.database.dao.SaveDao
|
||||
import dev.lucasnlm.antimine.common.level.database.dao.StatsDao
|
||||
import dev.lucasnlm.antimine.common.level.models.Event
|
||||
import dev.lucasnlm.antimine.common.level.repository.DimensionRepository
|
||||
import dev.lucasnlm.antimine.common.level.repository.IDimensionRepository
|
||||
import dev.lucasnlm.antimine.common.level.repository.IMinefieldRepository
|
||||
|
@ -23,8 +23,6 @@ import dev.lucasnlm.antimine.common.level.repository.StatsRepository
|
|||
import dev.lucasnlm.antimine.common.level.utils.Clock
|
||||
import dev.lucasnlm.antimine.common.level.utils.HapticFeedbackInteractor
|
||||
import dev.lucasnlm.antimine.common.level.utils.IHapticFeedbackInteractor
|
||||
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModelFactory
|
||||
import dev.lucasnlm.antimine.core.analytics.AnalyticsManager
|
||||
import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository
|
||||
|
||||
@Module
|
||||
|
@ -55,31 +53,6 @@ open class LevelModule {
|
|||
@Provides
|
||||
open fun provideClock(): Clock = Clock()
|
||||
|
||||
@Provides
|
||||
open fun provideGameViewModelFactory(
|
||||
@ApplicationContext context: Context,
|
||||
eventObserver: MutableLiveData<Event>,
|
||||
savesRepository: ISavesRepository,
|
||||
statsRepository: IStatsRepository,
|
||||
dimensionRepository: IDimensionRepository,
|
||||
preferencesRepository: IPreferencesRepository,
|
||||
hapticFeedbackInteractor: IHapticFeedbackInteractor,
|
||||
minefieldRepository: IMinefieldRepository,
|
||||
analyticsManager: AnalyticsManager,
|
||||
clock: Clock
|
||||
) = GameViewModelFactory(
|
||||
context,
|
||||
eventObserver,
|
||||
savesRepository,
|
||||
statsRepository,
|
||||
dimensionRepository,
|
||||
preferencesRepository,
|
||||
hapticFeedbackInteractor,
|
||||
minefieldRepository,
|
||||
analyticsManager,
|
||||
clock
|
||||
)
|
||||
|
||||
@Provides
|
||||
open fun provideDimensionRepository(
|
||||
@ApplicationContext context: Context,
|
||||
|
|
|
@ -30,7 +30,7 @@ class AreaAdapter(
|
|||
|
||||
init {
|
||||
setHasStableIds(true)
|
||||
paintSettings = createAreaPaintSettings(context, viewModel.useAccessibilityMode())
|
||||
paintSettings = createAreaPaintSettings(context.applicationContext, viewModel.useAccessibilityMode())
|
||||
}
|
||||
|
||||
fun setAmbientMode(isAmbientMode: Boolean, isLowBitAmbient: Boolean) {
|
||||
|
|
|
@ -6,19 +6,16 @@ import android.util.TypedValue
|
|||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.activity.viewModels
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import dev.lucasnlm.antimine.common.level.repository.IDimensionRepository
|
||||
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModel
|
||||
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModelFactory
|
||||
import dev.lucasnlm.antimine.common.level.widget.FixedGridLayoutManager
|
||||
import javax.inject.Inject
|
||||
|
||||
abstract class CommonLevelFragment : Fragment() {
|
||||
@Inject
|
||||
lateinit var viewModelFactory: GameViewModelFactory
|
||||
|
||||
@Inject
|
||||
lateinit var dimensionRepository: IDimensionRepository
|
||||
|
||||
|
@ -38,7 +35,7 @@ abstract class CommonLevelFragment : Fragment() {
|
|||
super.onCreate(savedInstanceState)
|
||||
|
||||
activity?.let {
|
||||
viewModel = ViewModelProviders.of(it, viewModelFactory).get(GameViewModel::class.java)
|
||||
viewModel = it.viewModels<GameViewModel>().value
|
||||
areaAdapter = AreaAdapter(it.applicationContext, viewModel)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package dev.lucasnlm.antimine.common.level.viewmodel
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Handler
|
||||
import androidx.hilt.lifecycle.ViewModelInject
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import dev.lucasnlm.antimine.common.level.LevelFacade
|
||||
|
@ -26,8 +26,7 @@ import kotlinx.coroutines.delay
|
|||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class GameViewModel(
|
||||
val context: Context,
|
||||
class GameViewModel @ViewModelInject constructor(
|
||||
val eventObserver: MutableLiveData<Event>,
|
||||
private val savesRepository: ISavesRepository,
|
||||
private val statsRepository: IStatsRepository,
|
||||
|
|
|
@ -1,49 +0,0 @@
|
|||
package dev.lucasnlm.antimine.common.level.viewmodel
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import dev.lucasnlm.antimine.common.level.models.Event
|
||||
import dev.lucasnlm.antimine.common.level.repository.IDimensionRepository
|
||||
import dev.lucasnlm.antimine.common.level.repository.IMinefieldRepository
|
||||
import dev.lucasnlm.antimine.common.level.repository.ISavesRepository
|
||||
import dev.lucasnlm.antimine.common.level.repository.IStatsRepository
|
||||
import dev.lucasnlm.antimine.common.level.utils.Clock
|
||||
import dev.lucasnlm.antimine.common.level.utils.IHapticFeedbackInteractor
|
||||
import dev.lucasnlm.antimine.core.analytics.AnalyticsManager
|
||||
import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository
|
||||
import javax.inject.Inject
|
||||
|
||||
class GameViewModelFactory @Inject constructor(
|
||||
private val context: Context,
|
||||
private val eventObserver: MutableLiveData<Event>,
|
||||
private val savesRepository: ISavesRepository,
|
||||
private val statsRepository: IStatsRepository,
|
||||
private val dimensionRepository: IDimensionRepository,
|
||||
private val preferencesRepository: IPreferencesRepository,
|
||||
private val hapticFeedbackInteractor: IHapticFeedbackInteractor,
|
||||
private val minefieldRepository: IMinefieldRepository,
|
||||
private val analyticsManager: AnalyticsManager,
|
||||
private val clock: Clock
|
||||
) : ViewModelProvider.Factory {
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T : ViewModel?> create(modelClass: Class<T>): T =
|
||||
if (modelClass.isAssignableFrom(GameViewModel::class.java)) {
|
||||
GameViewModel(
|
||||
context,
|
||||
eventObserver,
|
||||
savesRepository,
|
||||
statsRepository,
|
||||
dimensionRepository,
|
||||
preferencesRepository,
|
||||
hapticFeedbackInteractor,
|
||||
minefieldRepository,
|
||||
analyticsManager,
|
||||
clock
|
||||
) as T
|
||||
} else {
|
||||
throw IllegalArgumentException("ViewModel Not Found")
|
||||
}
|
||||
}
|
|
@ -1,12 +1,10 @@
|
|||
package dev.lucasnlm.antimine.common.level.di
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
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.common.level.mocks.FixedDimensionRepository
|
||||
import dev.lucasnlm.antimine.common.level.models.Event
|
||||
import dev.lucasnlm.antimine.common.level.repository.IDimensionRepository
|
||||
|
@ -15,13 +13,9 @@ import dev.lucasnlm.antimine.common.level.repository.ISavesRepository
|
|||
import dev.lucasnlm.antimine.common.level.repository.IStatsRepository
|
||||
import dev.lucasnlm.antimine.common.level.repository.MemorySavesRepository
|
||||
import dev.lucasnlm.antimine.common.level.repository.MemoryStatsRepository
|
||||
import dev.lucasnlm.antimine.common.level.repository.MinefieldRepository
|
||||
import dev.lucasnlm.antimine.common.level.utils.Clock
|
||||
import dev.lucasnlm.antimine.common.level.utils.DisabledIHapticFeedbackInteractor
|
||||
import dev.lucasnlm.antimine.common.level.utils.IHapticFeedbackInteractor
|
||||
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModelFactory
|
||||
import dev.lucasnlm.antimine.core.analytics.AnalyticsManager
|
||||
import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository
|
||||
|
||||
@Module
|
||||
@InstallIn(ApplicationComponent::class)
|
||||
|
@ -32,31 +26,6 @@ class TestLevelModule {
|
|||
@Provides
|
||||
fun provideClock(): Clock = Clock()
|
||||
|
||||
@Provides
|
||||
fun provideGameViewModelFactory(
|
||||
@ApplicationContext context: Context,
|
||||
eventObserver: MutableLiveData<Event>,
|
||||
savesRepository: ISavesRepository,
|
||||
statsRepository: IStatsRepository,
|
||||
dimensionRepository: IDimensionRepository,
|
||||
preferencesRepository: IPreferencesRepository,
|
||||
hapticFeedbackInteractor: IHapticFeedbackInteractor,
|
||||
minefieldRepository: MinefieldRepository,
|
||||
analyticsManager: AnalyticsManager,
|
||||
clock: Clock
|
||||
) = GameViewModelFactory(
|
||||
context,
|
||||
eventObserver,
|
||||
savesRepository,
|
||||
statsRepository,
|
||||
dimensionRepository,
|
||||
preferencesRepository,
|
||||
hapticFeedbackInteractor,
|
||||
minefieldRepository,
|
||||
analyticsManager,
|
||||
clock
|
||||
)
|
||||
|
||||
@Provides
|
||||
fun provideDimensionRepository(): IDimensionRepository = FixedDimensionRepository()
|
||||
|
||||
|
|
|
@ -24,6 +24,15 @@ android {
|
|||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
targetCompatibility 1.8
|
||||
sourceCompatibility 1.8
|
||||
}
|
||||
|
||||
kotlinOptions {
|
||||
jvmTarget = JavaVersion.VERSION_1_8.toString()
|
||||
}
|
||||
}
|
||||
|
||||
kapt {
|
||||
|
@ -45,7 +54,7 @@ dependencies {
|
|||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
|
||||
implementation 'androidx.recyclerview:recyclerview:1.1.0'
|
||||
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
|
||||
|
||||
implementation 'androidx.activity:activity-ktx:1.1.0'
|
||||
|
||||
// Wear OS
|
||||
implementation 'androidx.wear:wear:1.0.0'
|
||||
|
@ -55,6 +64,7 @@ dependencies {
|
|||
|
||||
// Dagger
|
||||
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'
|
||||
|
|
|
@ -2,28 +2,26 @@ package dev.lucasnlm.antimine.wear
|
|||
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.view.View
|
||||
import androidx.fragment.app.FragmentTransaction
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import androidx.wear.widget.SwipeDismissFrameLayout
|
||||
import dev.lucasnlm.antimine.common.level.models.Status
|
||||
import dev.lucasnlm.antimine.common.level.utils.Clock
|
||||
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModel
|
||||
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModelFactory
|
||||
import kotlinx.android.synthetic.main.activity_level.*
|
||||
import javax.inject.Inject
|
||||
import android.text.format.DateFormat
|
||||
import android.text.format.DateUtils
|
||||
import android.view.View
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.os.HandlerCompat
|
||||
import androidx.fragment.app.FragmentTransaction
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.wear.ambient.AmbientModeSupport
|
||||
import androidx.wear.ambient.AmbientModeSupport.AmbientCallback
|
||||
import androidx.wear.ambient.AmbientModeSupport.EXTRA_LOWBIT_AMBIENT
|
||||
import androidx.wear.widget.SwipeDismissFrameLayout
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import dev.lucasnlm.antimine.R
|
||||
import dev.lucasnlm.antimine.common.level.models.AmbientSettings
|
||||
import dev.lucasnlm.antimine.common.level.models.Event
|
||||
import dev.lucasnlm.antimine.common.level.models.Status
|
||||
import dev.lucasnlm.antimine.common.level.utils.Clock
|
||||
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModel
|
||||
import kotlinx.android.synthetic.main.activity_level.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
|
@ -31,11 +29,11 @@ import kotlinx.coroutines.launch
|
|||
@AndroidEntryPoint
|
||||
class WatchGameActivity : AppCompatActivity(), AmbientModeSupport.AmbientCallbackProvider {
|
||||
|
||||
@Inject
|
||||
lateinit var viewModelFactory: GameViewModelFactory
|
||||
private val viewModel: GameViewModel by viewModels()
|
||||
|
||||
private lateinit var viewModel: GameViewModel
|
||||
private lateinit var ambientController: AmbientModeSupport.AmbientController
|
||||
private val ambientController: AmbientModeSupport.AmbientController by lazy {
|
||||
AmbientModeSupport.attach(this)
|
||||
}
|
||||
|
||||
private var currentLevelFragment: WatchLevelFragment? = null
|
||||
|
||||
|
@ -69,16 +67,9 @@ class WatchGameActivity : AppCompatActivity(), AmbientModeSupport.AmbientCallbac
|
|||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
viewModel =
|
||||
ViewModelProviders.of(this, viewModelFactory).get(GameViewModel::class.java)
|
||||
|
||||
ambientController = AmbientModeSupport.attach(this)
|
||||
|
||||
setContentView(R.layout.activity_level)
|
||||
|
||||
bindViewModel()
|
||||
|
||||
loadGameFragment()
|
||||
|
||||
swipe.addCallback(object : SwipeDismissFrameLayout.Callback() {
|
||||
|
|
Loading…
Reference in a new issue