Fix clock when dismiss dialog

This commit is contained in:
Lucas Lima 2020-06-27 20:36:53 -03:00
parent feb16dd3a3
commit af6e0119f3
No known key found for this signature in database
GPG key ID: 0259A3F43EC1027A
10 changed files with 53 additions and 67 deletions

View file

@ -85,6 +85,7 @@ dependencies {
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'androidx.activity:activity-ktx:1.1.0'
implementation "androidx.fragment:fragment-ktx:1.2.5"
// Constraint
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

View file

@ -18,6 +18,7 @@ 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.DialogFragment
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.Observer
import androidx.preference.PreferenceManager
@ -35,7 +36,7 @@ import dev.lucasnlm.antimine.core.analytics.models.Analytics
import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository
import dev.lucasnlm.antimine.history.HistoryActivity
import dev.lucasnlm.antimine.instant.InstantAppManager
import dev.lucasnlm.antimine.level.view.CustomLevelDialogFragment
import dev.lucasnlm.antimine.custom.CustomLevelDialogFragment
import dev.lucasnlm.antimine.level.view.EndGameDialogFragment
import dev.lucasnlm.antimine.level.view.LevelFragment
import dev.lucasnlm.antimine.preferences.PreferencesActivity
@ -248,7 +249,10 @@ class GameActivity : AppCompatActivity(), DialogInterface.OnDismissListener {
}
override fun onDrawerClosed(drawerView: View) {
viewModel.resumeGame()
if (hasNoOtherFocusedDialog()) {
viewModel.resumeGame()
}
analyticsManager.sentEvent(Analytics.CloseDrawer())
}
@ -384,9 +388,7 @@ class GameActivity : AppCompatActivity(), DialogInterface.OnDismissListener {
}
private fun showControlDialog() {
if (status == Status.Running) {
viewModel.pauseGame()
}
viewModel.pauseGame()
if (supportFragmentManager.findFragmentByTag(CustomLevelDialogFragment.TAG) == null) {
ControlDialogFragment().apply {
@ -582,10 +584,16 @@ class GameActivity : AppCompatActivity(), DialogInterface.OnDismissListener {
}
}
private fun hasNoOtherFocusedDialog(): Boolean {
return supportFragmentManager.fragments.count {
it !is LevelFragment && it is DialogFragment
} == 0
}
override fun onDismiss(dialog: DialogInterface?) {
if (status == Status.Running) {
viewModel.updateGameControl()
viewModel.resumeGame()
viewModel.run {
updateGameControl()
resumeGame()
}
}

View file

@ -21,7 +21,7 @@ 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.level.view.CustomLevelDialogFragment
import dev.lucasnlm.antimine.custom.CustomLevelDialogFragment
import dev.lucasnlm.antimine.level.view.LevelFragment
import dev.lucasnlm.antimine.preferences.PreferencesActivity
import kotlinx.android.synthetic.main.activity_tv_game.*

View file

@ -8,13 +8,13 @@ import android.view.ViewGroup
import android.widget.BaseAdapter
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.fragment.app.activityViewModels
import dagger.hilt.android.AndroidEntryPoint
import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.control.view.ControlItemView
import dev.lucasnlm.antimine.control.viewmodel.ControlViewModel
import dev.lucasnlm.antimine.core.control.ControlStyle
import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository
import dev.lucasnlm.antimine.core.viewModels
import javax.inject.Inject
@AndroidEntryPoint
@ -22,7 +22,7 @@ class ControlDialogFragment : AppCompatDialogFragment() {
@Inject
lateinit var preferencesRepository: IPreferencesRepository
private val controlViewModel by viewModels<ControlViewModel>()
private val controlViewModel by activityViewModels<ControlViewModel>()
private val adapter by lazy { ControlListAdapter(controlViewModel) }
override fun onCreate(savedInstanceState: Bundle?) {

View file

@ -1,11 +1,12 @@
package dev.lucasnlm.antimine.level.view
package dev.lucasnlm.antimine.custom
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.widget.TextView
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.fragment.app.activityViewModels
import dagger.hilt.android.AndroidEntryPoint
import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.common.level.models.Difficulty
@ -22,15 +23,7 @@ class CustomLevelDialogFragment : AppCompatDialogFragment() {
@Inject
lateinit var preferencesRepository: IPreferencesRepository
private lateinit var viewModel: GameViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activity?.let {
viewModel = it.viewModels<GameViewModel>().value
}
}
private val viewModel by activityViewModels<GameViewModel>()
private fun filterInput(target: String, min: Int): Int {
var result = min
@ -56,9 +49,15 @@ class CustomLevelDialogFragment : AppCompatDialogFragment() {
val mapHeight: TextView? = dialog?.findViewById(R.id.map_height)
val mapMines: TextView? = dialog?.findViewById(R.id.map_mines)
var width = filterInput(mapWidth?.text.toString(), MIN_WIDTH)
var height = filterInput(mapHeight?.text.toString(), MIN_HEIGHT)
var mines = filterInput(mapMines?.text.toString(), MIN_MINES)
var width = filterInput(mapWidth?.text.toString(),
MIN_WIDTH
)
var height = filterInput(mapHeight?.text.toString(),
MIN_HEIGHT
)
var mines = filterInput(mapMines?.text.toString(),
MIN_MINES
)
if (width * height - 1 < mines) {
mines = width * height - 1
@ -83,6 +82,13 @@ class CustomLevelDialogFragment : AppCompatDialogFragment() {
}.create()
}
override fun onDismiss(dialog: DialogInterface) {
if (activity is DialogInterface.OnDismissListener) {
(activity as DialogInterface.OnDismissListener).onDismiss(dialog)
}
super.onDismiss(dialog)
}
companion object {
const val MIN_WIDTH = 5
const val MIN_HEIGHT = 5

View file

@ -5,10 +5,10 @@ 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.fragment.app.activityViewModels
import dagger.hilt.android.AndroidEntryPoint
import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModel
@ -24,9 +24,9 @@ class EndGameDialogFragment : AppCompatDialogFragment() {
@Inject
lateinit var instantAppManager: InstantAppManager
private lateinit var endGameViewModel: EngGameDialogViewModel
private lateinit var viewModel: GameViewModel
private lateinit var shareViewModel: ShareViewModel
private val endGameViewModel by activityViewModels<EngGameDialogViewModel>()
private val viewModel by activityViewModels<GameViewModel>()
private val shareViewModel by activityViewModels<ShareViewModel>()
private var hasValidData = false
private var isVictory: Boolean = false
@ -38,12 +38,6 @@ class EndGameDialogFragment : AppCompatDialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activity?.let {
viewModel = it.viewModels<GameViewModel>().value
endGameViewModel = it.viewModels<EngGameDialogViewModel>().value
shareViewModel = it.viewModels<ShareViewModel>().value
}
arguments?.run {
isVictory = getBoolean(DIALOG_IS_VICTORY) == true
time = getLong(DIALOG_TIME, 0L)

View file

@ -45,6 +45,7 @@ dependencies {
implementation 'androidx.preference:preference:1.1.1'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.activity:activity-ktx:1.1.0'
implementation "androidx.fragment:fragment-ktx:1.2.5"
// Constraint
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

View file

@ -10,6 +10,7 @@ import android.view.ViewGroup
import android.view.WindowManager
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.RecyclerView
import dev.lucasnlm.antimine.common.level.repository.IDimensionRepository
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModel
@ -20,9 +21,9 @@ abstract class CommonLevelFragment : Fragment() {
@Inject
lateinit var dimensionRepository: IDimensionRepository
protected lateinit var viewModel: GameViewModel
protected val viewModel: GameViewModel by activityViewModels()
protected val areaAdapter by lazy { AreaAdapter(requireContext(), viewModel) }
protected lateinit var recyclerGrid: RecyclerView
protected lateinit var areaAdapter: AreaAdapter
abstract val levelFragmentResId: Int
@ -32,15 +33,6 @@ abstract class CommonLevelFragment : Fragment() {
savedInstanceState: Bundle?
): View? = inflater.inflate(levelFragmentResId, container, false)
override fun onAttach(context: Context) {
super.onAttach(context)
activity?.let {
viewModel = it.viewModels<GameViewModel>().value
areaAdapter = AreaAdapter(it.applicationContext, viewModel)
}
}
protected fun makeNewLayoutManager(boardWidth: Int) =
FixedGridLayoutManager().apply {
setTotalColumnCount(boardWidth)

View file

@ -296,9 +296,11 @@ class GameViewModel @ViewModelInject constructor(
}
fun updateGameControl() {
val controlType = preferencesRepository.controlType()
val gameControl = GameControl.fromControlType(controlType)
gameController.updateGameControl(gameControl)
if (initialized) {
val controlType = preferencesRepository.controlType()
val gameControl = GameControl.fromControlType(controlType)
gameController.updateGameControl(gameControl)
}
}
fun runClock() {

View file

@ -1,18 +0,0 @@
package dev.lucasnlm.antimine.core
import androidx.annotation.MainThread
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelLazy
import androidx.lifecycle.ViewModelProvider
@MainThread
inline fun <reified VM : ViewModel> Fragment.viewModels(
noinline factoryProducer: (() -> ViewModelProvider.Factory)? = null
): Lazy<VM> {
val factoryPromise = factoryProducer ?: {
defaultViewModelProviderFactory
}
return ViewModelLazy(VM::class, { viewModelStore }, factoryPromise)
}