Move hilt to koin view models

This commit is contained in:
Lucas Lima 2020-08-19 20:10:34 -03:00
parent d4d7d09110
commit 5da1aa7cf5
No known key found for this signature in database
GPG key ID: 049CCC5A365B00D2
20 changed files with 50 additions and 40 deletions

View file

@ -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

View file

@ -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<GameViewModel>()
override val noActionBar: Boolean = true

View file

@ -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<GameViewModel>()
private var status: Status = Status.PreGame
private var totalMines: Int = 0

View file

@ -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 -> {

View file

@ -1,7 +1,6 @@
package dev.lucasnlm.antimine.about.viewmodel
enum class AboutEvent {
Info,
ThirdPartyLicenses,
SourceCode,
Translators

View file

@ -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()

View file

@ -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)

View file

@ -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)

View file

@ -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<ControlViewModel>()
private val adapter by lazy { ControlListAdapter(controlViewModel) }
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

View file

@ -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<GameViewModel>()
private val createGameViewModel by viewModel<CreateGameViewModel>()
private lateinit var mapWidth: TextView
private lateinit var mapHeight: TextView

View file

@ -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())
}
}

View file

@ -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<HistoryViewModel>()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

View file

@ -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<EndGameDialogViewModel>()
private val gameViewModel by sharedViewModel<GameViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

View file

@ -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<PlayGamesViewModel>()
private val adapter by lazy { PlayGamesAdapter(playGamesViewModel) }
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

View file

@ -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<StatsViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

View file

@ -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<TextViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

View file

@ -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<ThemeViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

View file

@ -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

View file

@ -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<GameViewModel>()
protected val areaAdapter by lazy {
AreaAdapter(requireContext(), gameViewModel, preferencesRepository, dimensionRepository)
}

View file

@ -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<GameViewModel>()
private val ambientController: AmbientModeSupport.AmbientController by lazy {
AmbientModeSupport.attach(this)