From 5da1aa7cf596b63d8b89d4d38bc590e59436feee Mon Sep 17 00:00:00 2001 From: Lucas Lima Date: Wed, 19 Aug 2020 20:10:34 -0300 Subject: [PATCH] Move hilt to koin view models --- app/build.gradle | 1 + .../dev/lucasnlm/antimine/GameActivity.kt | 3 ++- .../dev/lucasnlm/antimine/TvGameActivity.kt | 3 ++- .../lucasnlm/antimine/about/AboutActivity.kt | 8 +++---- .../antimine/about/viewmodel/AboutEvent.kt | 1 - .../views/{ => info}/AboutInfoFragment.kt | 5 +++-- .../about/views/licenses/LicensesFragment.kt | 5 ++--- .../views/translators/TranslatorsFragment.kt | 4 ++-- .../antimine/control/ControlDialogFragment.kt | 4 ++-- .../custom/CustomLevelDialogFragment.kt | 6 ++++-- .../lucasnlm/antimine/di/ViewModelModule.kt | 21 ++++++++++--------- .../antimine/history/views/HistoryFragment.kt | 3 ++- .../level/view/EndGameDialogFragment.kt | 6 ++++-- .../playgames/PlayGamesDialogFragment.kt | 3 ++- .../lucasnlm/antimine/stats/StatsActivity.kt | 3 ++- .../lucasnlm/antimine/text/TextActivity.kt | 3 ++- .../lucasnlm/antimine/theme/ThemeActivity.kt | 3 ++- common/build.gradle | 1 + .../common/level/view/CommonLevelFragment.kt | 3 ++- .../antimine/wear/WatchGameActivity.kt | 4 +--- 20 files changed, 50 insertions(+), 40 deletions(-) rename app/src/main/java/dev/lucasnlm/antimine/about/views/{ => info}/AboutInfoFragment.kt (86%) diff --git a/app/build.gradle b/app/build.gradle index 8da2d3b1..ad792911 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -117,6 +117,7 @@ dependencies { // Koin implementation 'org.koin:koin-android:2.1.6' + implementation 'org.koin:koin-androidx-viewmodel:2.1.6' testImplementation 'org.koin:koin-test:2.1.6' // Kotlin diff --git a/app/src/main/java/dev/lucasnlm/antimine/GameActivity.kt b/app/src/main/java/dev/lucasnlm/antimine/GameActivity.kt index 86a11c1a..f513cad4 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/GameActivity.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/GameActivity.kt @@ -54,6 +54,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import org.koin.android.ext.android.inject +import org.koin.androidx.viewmodel.ext.android.viewModel class GameActivity : ThematicActivity(R.layout.activity_game), DialogInterface.OnDismissListener { private val preferencesRepository: IPreferencesRepository by inject() @@ -68,7 +69,7 @@ class GameActivity : ThematicActivity(R.layout.activity_game), DialogInterface.O private val shareViewModel: ShareManager by inject() - val gameViewModel: GameViewModel by inject() + val gameViewModel by viewModel() override val noActionBar: Boolean = true diff --git a/app/src/main/java/dev/lucasnlm/antimine/TvGameActivity.kt b/app/src/main/java/dev/lucasnlm/antimine/TvGameActivity.kt index d003d625..fe90493e 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/TvGameActivity.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/TvGameActivity.kt @@ -25,9 +25,10 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import org.koin.android.ext.android.inject +import org.koin.androidx.viewmodel.ext.android.viewModel class TvGameActivity : AppCompatActivity() { - private val gameViewModel: GameViewModel by inject() + private val gameViewModel by viewModel() private var status: Status = Status.PreGame private var totalMines: Int = 0 diff --git a/app/src/main/java/dev/lucasnlm/antimine/about/AboutActivity.kt b/app/src/main/java/dev/lucasnlm/antimine/about/AboutActivity.kt index 1b10c775..ba0cdb4f 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/about/AboutActivity.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/about/AboutActivity.kt @@ -8,14 +8,14 @@ import dev.lucasnlm.antimine.R import dev.lucasnlm.antimine.ThematicActivity import dev.lucasnlm.antimine.about.viewmodel.AboutEvent import dev.lucasnlm.antimine.about.viewmodel.AboutViewModel -import dev.lucasnlm.antimine.about.views.AboutInfoFragment +import dev.lucasnlm.antimine.about.views.info.AboutInfoFragment import dev.lucasnlm.antimine.about.views.licenses.LicensesFragment import dev.lucasnlm.antimine.about.views.translators.TranslatorsFragment import kotlinx.coroutines.flow.collect -import org.koin.android.ext.android.inject +import org.koin.androidx.viewmodel.ext.android.viewModel class AboutActivity : ThematicActivity(R.layout.activity_empty) { - private val aboutViewModel: AboutViewModel by inject() + private val aboutViewModel: AboutViewModel by viewModel() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -23,8 +23,6 @@ class AboutActivity : ThematicActivity(R.layout.activity_empty) { replaceFragment(AboutInfoFragment(), null) lifecycleScope.launchWhenCreated { - aboutViewModel.sendEvent(AboutEvent.Info) - aboutViewModel.observeEvent().collect { event -> when (event) { AboutEvent.ThirdPartyLicenses -> { diff --git a/app/src/main/java/dev/lucasnlm/antimine/about/viewmodel/AboutEvent.kt b/app/src/main/java/dev/lucasnlm/antimine/about/viewmodel/AboutEvent.kt index 15b296cf..8026c4dc 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/about/viewmodel/AboutEvent.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/about/viewmodel/AboutEvent.kt @@ -1,7 +1,6 @@ package dev.lucasnlm.antimine.about.viewmodel enum class AboutEvent { - Info, ThirdPartyLicenses, SourceCode, Translators diff --git a/app/src/main/java/dev/lucasnlm/antimine/about/views/AboutInfoFragment.kt b/app/src/main/java/dev/lucasnlm/antimine/about/views/info/AboutInfoFragment.kt similarity index 86% rename from app/src/main/java/dev/lucasnlm/antimine/about/views/AboutInfoFragment.kt rename to app/src/main/java/dev/lucasnlm/antimine/about/views/info/AboutInfoFragment.kt index 19dc9365..7eaf493c 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/about/views/AboutInfoFragment.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/about/views/info/AboutInfoFragment.kt @@ -1,4 +1,4 @@ -package dev.lucasnlm.antimine.about.views +package dev.lucasnlm.antimine.about.views.info import android.os.Bundle import android.view.View @@ -9,9 +9,10 @@ import dev.lucasnlm.antimine.about.viewmodel.AboutEvent import dev.lucasnlm.antimine.about.viewmodel.AboutViewModel import kotlinx.android.synthetic.main.fragment_about_info.* import org.koin.android.ext.android.inject +import org.koin.androidx.viewmodel.ext.android.sharedViewModel class AboutInfoFragment : Fragment(R.layout.fragment_about_info) { - private val aboutViewModel: AboutViewModel by inject() + private val aboutViewModel: AboutViewModel by sharedViewModel() override fun onResume() { super.onResume() diff --git a/app/src/main/java/dev/lucasnlm/antimine/about/views/licenses/LicensesFragment.kt b/app/src/main/java/dev/lucasnlm/antimine/about/views/licenses/LicensesFragment.kt index b627e023..e4ec4833 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/about/views/licenses/LicensesFragment.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/about/views/licenses/LicensesFragment.kt @@ -3,7 +3,6 @@ package dev.lucasnlm.antimine.about.views.licenses import android.os.Bundle import android.view.View import androidx.fragment.app.Fragment -import androidx.fragment.app.activityViewModels import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager @@ -11,10 +10,10 @@ import dev.lucasnlm.antimine.R import dev.lucasnlm.antimine.about.viewmodel.AboutViewModel import kotlinx.android.synthetic.main.fragment_licenses.* import kotlinx.coroutines.flow.collect -import org.koin.android.ext.android.inject +import org.koin.androidx.viewmodel.ext.android.sharedViewModel class LicensesFragment : Fragment(R.layout.fragment_licenses) { - private val aboutViewModel: AboutViewModel by inject() + private val aboutViewModel: AboutViewModel by sharedViewModel() override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) diff --git a/app/src/main/java/dev/lucasnlm/antimine/about/views/translators/TranslatorsFragment.kt b/app/src/main/java/dev/lucasnlm/antimine/about/views/translators/TranslatorsFragment.kt index dd051009..9bdffd83 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/about/views/translators/TranslatorsFragment.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/about/views/translators/TranslatorsFragment.kt @@ -13,10 +13,10 @@ import dev.lucasnlm.antimine.about.viewmodel.AboutViewModel import kotlinx.android.synthetic.main.fragment_translators.* import kotlinx.android.synthetic.main.view_translator.view.* import kotlinx.coroutines.flow.collect -import org.koin.android.ext.android.inject +import org.koin.androidx.viewmodel.ext.android.sharedViewModel internal class TranslatorsFragment : Fragment(R.layout.fragment_translators) { - private val aboutViewModel: AboutViewModel by inject() + private val aboutViewModel: AboutViewModel by sharedViewModel() override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) diff --git a/app/src/main/java/dev/lucasnlm/antimine/control/ControlDialogFragment.kt b/app/src/main/java/dev/lucasnlm/antimine/control/ControlDialogFragment.kt index 86662578..e67e41fb 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/control/ControlDialogFragment.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/control/ControlDialogFragment.kt @@ -12,10 +12,10 @@ import dev.lucasnlm.antimine.R import dev.lucasnlm.antimine.control.view.ControlItemView import dev.lucasnlm.antimine.control.viewmodel.ControlEvent import dev.lucasnlm.antimine.control.viewmodel.ControlViewModel -import org.koin.android.ext.android.inject +import org.koin.androidx.viewmodel.ext.android.viewModel class ControlDialogFragment : AppCompatDialogFragment() { - private val controlViewModel: ControlViewModel by inject() + private val controlViewModel by viewModel() private val adapter by lazy { ControlListAdapter(controlViewModel) } override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { diff --git a/app/src/main/java/dev/lucasnlm/antimine/custom/CustomLevelDialogFragment.kt b/app/src/main/java/dev/lucasnlm/antimine/custom/CustomLevelDialogFragment.kt index 8694ac99..00bf343a 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/custom/CustomLevelDialogFragment.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/custom/CustomLevelDialogFragment.kt @@ -16,10 +16,12 @@ import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModel import dev.lucasnlm.antimine.custom.viewmodel.CreateGameViewModel import dev.lucasnlm.antimine.custom.viewmodel.CustomEvent import org.koin.android.ext.android.inject +import org.koin.androidx.viewmodel.ext.android.sharedViewModel +import org.koin.androidx.viewmodel.ext.android.viewModel class CustomLevelDialogFragment : AppCompatDialogFragment() { - private val gameViewModel: GameViewModel by inject() - private val createGameViewModel: CreateGameViewModel by inject() + private val gameViewModel by sharedViewModel() + private val createGameViewModel by viewModel() private lateinit var mapWidth: TextView private lateinit var mapHeight: TextView diff --git a/app/src/main/java/dev/lucasnlm/antimine/di/ViewModelModule.kt b/app/src/main/java/dev/lucasnlm/antimine/di/ViewModelModule.kt index b566089c..f2f1c6a8 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/di/ViewModelModule.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/di/ViewModelModule.kt @@ -10,19 +10,20 @@ import dev.lucasnlm.antimine.playgames.viewmodel.PlayGamesViewModel import dev.lucasnlm.antimine.stats.viewmodel.StatsViewModel import dev.lucasnlm.antimine.text.viewmodel.TextViewModel import dev.lucasnlm.antimine.theme.viewmodel.ThemeViewModel +import org.koin.androidx.viewmodel.dsl.viewModel import org.koin.dsl.module val ViewModelModule = module { - single { AboutViewModel(get()) } - factory { ControlViewModel(get()) } - factory { CreateGameViewModel(get()) } - factory { HistoryViewModel(get(), get()) } - factory { EndGameDialogViewModel(get()) } - factory { PlayGamesViewModel(get(), get()) } - factory { StatsViewModel(get(), get()) } - factory { TextViewModel(get()) } - factory { ThemeViewModel(get()) } - single { + viewModel { AboutViewModel(get()) } + viewModel { ControlViewModel(get()) } + viewModel { CreateGameViewModel(get()) } + viewModel { HistoryViewModel(get(), get()) } + viewModel { EndGameDialogViewModel(get()) } + viewModel { PlayGamesViewModel(get(), get()) } + viewModel { StatsViewModel(get(), get()) } + viewModel { TextViewModel(get()) } + viewModel { ThemeViewModel(get()) } + viewModel { GameViewModel(get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) } } diff --git a/app/src/main/java/dev/lucasnlm/antimine/history/views/HistoryFragment.kt b/app/src/main/java/dev/lucasnlm/antimine/history/views/HistoryFragment.kt index 8b85e68b..7be2b47d 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/history/views/HistoryFragment.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/history/views/HistoryFragment.kt @@ -12,9 +12,10 @@ import dev.lucasnlm.antimine.history.viewmodel.HistoryViewModel import kotlinx.android.synthetic.main.fragment_history.* import kotlinx.coroutines.flow.collect import org.koin.android.ext.android.inject +import org.koin.androidx.viewmodel.ext.android.viewModel class HistoryFragment : Fragment(R.layout.fragment_history) { - private val historyViewModel: HistoryViewModel by inject() + private val historyViewModel by viewModel() override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) diff --git a/app/src/main/java/dev/lucasnlm/antimine/level/view/EndGameDialogFragment.kt b/app/src/main/java/dev/lucasnlm/antimine/level/view/EndGameDialogFragment.kt index 41fb8c1e..4aea58c6 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/level/view/EndGameDialogFragment.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/level/view/EndGameDialogFragment.kt @@ -17,12 +17,14 @@ import dev.lucasnlm.antimine.level.viewmodel.EndGameDialogEvent import dev.lucasnlm.antimine.level.viewmodel.EndGameDialogViewModel import kotlinx.coroutines.flow.collect import org.koin.android.ext.android.inject +import org.koin.androidx.viewmodel.ext.android.sharedViewModel +import org.koin.androidx.viewmodel.ext.android.viewModel class EndGameDialogFragment : AppCompatDialogFragment() { private val instantAppManager: InstantAppManager by inject() - private val endGameViewModel: EndGameDialogViewModel by inject() - private val gameViewModel: GameViewModel by inject() + private val endGameViewModel by viewModel() + private val gameViewModel by sharedViewModel() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/app/src/main/java/dev/lucasnlm/antimine/playgames/PlayGamesDialogFragment.kt b/app/src/main/java/dev/lucasnlm/antimine/playgames/PlayGamesDialogFragment.kt index ab912fd5..e40744c4 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/playgames/PlayGamesDialogFragment.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/playgames/PlayGamesDialogFragment.kt @@ -18,9 +18,10 @@ import dev.lucasnlm.antimine.playgames.viewmodel.PlayGamesEvent import dev.lucasnlm.antimine.playgames.viewmodel.PlayGamesViewModel import kotlinx.coroutines.flow.collect import org.koin.android.ext.android.inject +import org.koin.androidx.viewmodel.ext.android.viewModel class PlayGamesDialogFragment : DialogFragment() { - private val playGamesViewModel: PlayGamesViewModel by inject() + private val playGamesViewModel by viewModel() private val adapter by lazy { PlayGamesAdapter(playGamesViewModel) } override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { diff --git a/app/src/main/java/dev/lucasnlm/antimine/stats/StatsActivity.kt b/app/src/main/java/dev/lucasnlm/antimine/stats/StatsActivity.kt index 5d28eb19..ade6ab54 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/stats/StatsActivity.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/stats/StatsActivity.kt @@ -15,9 +15,10 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch import org.koin.android.ext.android.inject +import org.koin.androidx.viewmodel.ext.android.viewModel class StatsActivity : ThematicActivity(R.layout.activity_stats) { - private val statsViewModel: StatsViewModel by inject() + private val statsViewModel by viewModel() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/app/src/main/java/dev/lucasnlm/antimine/text/TextActivity.kt b/app/src/main/java/dev/lucasnlm/antimine/text/TextActivity.kt index cb3d008a..2fc9d8c8 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/text/TextActivity.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/text/TextActivity.kt @@ -15,9 +15,10 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.collect import kotlinx.coroutines.withContext import org.koin.android.ext.android.inject +import org.koin.androidx.viewmodel.ext.android.viewModel class TextActivity : ThematicActivity(R.layout.activity_text) { - private val textViewModel: TextViewModel by inject() + private val textViewModel by viewModel() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/app/src/main/java/dev/lucasnlm/antimine/theme/ThemeActivity.kt b/app/src/main/java/dev/lucasnlm/antimine/theme/ThemeActivity.kt index c026f798..3c5f2cf5 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/theme/ThemeActivity.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/theme/ThemeActivity.kt @@ -14,11 +14,12 @@ import dev.lucasnlm.antimine.theme.viewmodel.ThemeViewModel import kotlinx.android.synthetic.main.activity_theme.* import kotlinx.coroutines.flow.collect import org.koin.android.ext.android.inject +import org.koin.androidx.viewmodel.ext.android.viewModel class ThemeActivity : ThematicActivity(R.layout.activity_theme) { private val dimensionRepository: IDimensionRepository by inject() - private val themeViewModel: ThemeViewModel by inject() + private val themeViewModel by viewModel() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/common/build.gradle b/common/build.gradle index 353e7143..e226f7ca 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -54,6 +54,7 @@ dependencies { // Koin implementation 'org.koin:koin-android:2.1.6' + implementation 'org.koin:koin-androidx-viewmodel:2.1.6' testImplementation 'org.koin:koin-test:2.1.6' // Room diff --git a/common/src/main/java/dev/lucasnlm/antimine/common/level/view/CommonLevelFragment.kt b/common/src/main/java/dev/lucasnlm/antimine/common/level/view/CommonLevelFragment.kt index cfeaf325..df695f8e 100644 --- a/common/src/main/java/dev/lucasnlm/antimine/common/level/view/CommonLevelFragment.kt +++ b/common/src/main/java/dev/lucasnlm/antimine/common/level/view/CommonLevelFragment.kt @@ -8,11 +8,12 @@ import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModel import dev.lucasnlm.antimine.common.level.widget.FixedGridLayoutManager import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository import org.koin.android.ext.android.inject +import org.koin.androidx.viewmodel.ext.android.sharedViewModel abstract class CommonLevelFragment(@LayoutRes val contentLayoutId: Int) : Fragment(contentLayoutId) { private val dimensionRepository: IDimensionRepository by inject() private val preferencesRepository: IPreferencesRepository by inject() - protected val gameViewModel: GameViewModel by inject() + protected val gameViewModel by sharedViewModel() protected val areaAdapter by lazy { AreaAdapter(requireContext(), gameViewModel, preferencesRepository, dimensionRepository) } diff --git a/wear/src/main/java/dev/lucasnlm/antimine/wear/WatchGameActivity.kt b/wear/src/main/java/dev/lucasnlm/antimine/wear/WatchGameActivity.kt index c41a2c9e..c7e9f17f 100644 --- a/wear/src/main/java/dev/lucasnlm/antimine/wear/WatchGameActivity.kt +++ b/wear/src/main/java/dev/lucasnlm/antimine/wear/WatchGameActivity.kt @@ -14,7 +14,6 @@ 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 @@ -26,10 +25,9 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch -@AndroidEntryPoint class WatchGameActivity : AppCompatActivity(), AmbientModeSupport.AmbientCallbackProvider { - private val viewModel: GameViewModel by viewModels() + private val viewModel by viewModels() private val ambientController: AmbientModeSupport.AmbientController by lazy { AmbientModeSupport.attach(this)