Add CommonLevelFragment and fix vertical center

This commit is contained in:
Lucas Lima 2020-06-20 11:41:32 -03:00
parent c6aac5f078
commit d6075fbad6
No known key found for this signature in database
GPG key ID: 0259A3F43EC1027A
3 changed files with 86 additions and 113 deletions

View file

@ -2,59 +2,24 @@ package dev.lucasnlm.antimine.level.view
import android.os.Bundle
import android.text.format.DateUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import dagger.android.support.DaggerFragment
import dev.lucasnlm.antimine.DeepLink
import dev.lucasnlm.antimine.common.R
import dev.lucasnlm.antimine.common.level.models.Difficulty
import dev.lucasnlm.antimine.common.level.models.Event
import dev.lucasnlm.antimine.common.level.repository.IDimensionRepository
import dev.lucasnlm.antimine.common.level.view.AreaAdapter
import dev.lucasnlm.antimine.common.level.view.CommonLevelFragment
import dev.lucasnlm.antimine.common.level.view.SpaceItemDecoration
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 kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject
open class LevelFragment : DaggerFragment() {
@Inject
lateinit var viewModelFactory: GameViewModelFactory
@Inject
lateinit var dimensionRepository: IDimensionRepository
private lateinit var viewModel: GameViewModel
private lateinit var recyclerGrid: RecyclerView
private lateinit var areaAdapter: AreaAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? =
inflater.inflate(R.layout.fragment_level, container, false)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activity?.let {
viewModel = ViewModelProviders.of(it, viewModelFactory).get(GameViewModel::class.java)
areaAdapter = AreaAdapter(it.applicationContext, viewModel)
}
}
open class LevelFragment : CommonLevelFragment() {
override val levelFragmentResId: Int = R.layout.fragment_level
override fun onPause() {
super.onPause()
GlobalScope.launch {
viewModel.saveGame()
}
@ -171,23 +136,4 @@ open class LevelFragment : DaggerFragment() {
null
}
}
private fun makeNewLayoutManager(boardWidth: Int) =
FixedGridLayoutManager().apply {
setTotalColumnCount(boardWidth)
}
private fun calcHorizontalPadding(boardWidth: Int): Int {
val width = recyclerGrid.measuredWidth
val recyclerViewWidth = (dimensionRepository.areaSize() * boardWidth)
val separatorsWidth = (dimensionRepository.areaSeparator() * (boardWidth - 1))
return ((width - recyclerViewWidth - separatorsWidth) / 2).coerceAtLeast(0.0f).toInt()
}
private fun calcVerticalPadding(boardHeight: Int): Int {
val height = recyclerGrid.measuredHeight
val recyclerViewHeight = (dimensionRepository.areaSize() * boardHeight)
val separatorsHeight = (dimensionRepository.areaSeparator() * (boardHeight - 1))
return ((height - recyclerViewHeight - separatorsHeight) / 2).coerceAtLeast(0.0f).toInt()
}
}

View file

@ -0,0 +1,80 @@
package dev.lucasnlm.antimine.common.level.view
import android.os.Bundle
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import dagger.android.support.DaggerFragment
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 : DaggerFragment() {
@Inject
lateinit var viewModelFactory: GameViewModelFactory
@Inject
lateinit var dimensionRepository: IDimensionRepository
protected lateinit var viewModel: GameViewModel
protected lateinit var recyclerGrid: RecyclerView
protected lateinit var areaAdapter: AreaAdapter
abstract val levelFragmentResId: Int
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(levelFragmentResId, container, false)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activity?.let {
viewModel = ViewModelProviders.of(it, viewModelFactory).get(GameViewModel::class.java)
areaAdapter = AreaAdapter(it.applicationContext, viewModel)
}
}
protected fun makeNewLayoutManager(boardWidth: Int) =
FixedGridLayoutManager().apply {
setTotalColumnCount(boardWidth)
}
protected fun calcHorizontalPadding(boardWidth: Int): Int {
val activity = requireActivity()
val displayMetrics = DisplayMetrics()
activity.windowManager.defaultDisplay.getMetrics(displayMetrics)
val width = displayMetrics.widthPixels
val recyclerViewWidth = (dimensionRepository.areaSize() * boardWidth)
val separatorsWidth = (dimensionRepository.areaSeparator() * (boardWidth - 1))
return ((width - recyclerViewWidth - separatorsWidth) / 2).coerceAtLeast(0.0f).toInt()
}
protected fun calcVerticalPadding(boardHeight: Int): Int {
val activity = requireActivity()
val displayMetrics = DisplayMetrics()
activity.windowManager.defaultDisplay.getMetrics(displayMetrics)
val typedValue = TypedValue()
val actionBarHeight = if (activity.theme.resolveAttribute(android.R.attr.actionBarSize, typedValue, true)) {
TypedValue.complexToDimensionPixelSize(typedValue.data, resources.displayMetrics)
} else {
0
}
val height = displayMetrics.heightPixels - actionBarHeight
val recyclerViewHeight = (dimensionRepository.areaSize() * boardHeight)
val separatorsHeight = (dimensionRepository.areaSeparator() * (boardHeight - 1))
return ((height - recyclerViewHeight - separatorsHeight) / 2).coerceAtLeast(0.0f).toInt()
}
}

View file

@ -2,54 +2,20 @@ package dev.lucasnlm.antimine.wear
import android.os.Bundle
import android.text.format.DateUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import dev.lucasnlm.antimine.common.R
import dagger.android.support.DaggerFragment
import dev.lucasnlm.antimine.common.level.models.AmbientSettings
import dev.lucasnlm.antimine.common.level.models.Event
import dev.lucasnlm.antimine.common.level.repository.IDimensionRepository
import dev.lucasnlm.antimine.common.level.view.AreaAdapter
import dev.lucasnlm.antimine.common.level.view.CommonLevelFragment
import dev.lucasnlm.antimine.common.level.view.SpaceItemDecoration
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 kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject
class WatchLevelFragment : DaggerFragment() {
@Inject
lateinit var viewModelFactory: GameViewModelFactory
@Inject
lateinit var dimensionRepository: IDimensionRepository
private lateinit var viewModel: GameViewModel
private lateinit var recyclerGrid: RecyclerView
private lateinit var areaAdapter: AreaAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? =
inflater.inflate(R.layout.fragment_level, container, false)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activity?.let {
viewModel = ViewModelProviders.of(it, viewModelFactory).get(GameViewModel::class.java)
areaAdapter = AreaAdapter(it.applicationContext, viewModel)
}
}
class WatchLevelFragment : CommonLevelFragment() {
override val levelFragmentResId: Int = R.layout.fragment_level
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
@ -119,25 +85,6 @@ class WatchLevelFragment : DaggerFragment() {
}
}
private fun makeNewLayoutManager(boardWidth: Int) =
FixedGridLayoutManager().apply {
setTotalColumnCount(boardWidth)
}
private fun calcHorizontalPadding(boardWidth: Int): Int {
val width = recyclerGrid.measuredWidth
val recyclerViewWidth = (dimensionRepository.areaSize() * boardWidth)
val separatorsWidth = (dimensionRepository.areaSeparator() * (boardWidth - 1))
return ((width - recyclerViewWidth - separatorsWidth) / 2).coerceAtLeast(0.0f).toInt()
}
private fun calcVerticalPadding(boardHeight: Int): Int {
val height = recyclerGrid.measuredHeight
val recyclerViewHeight = (dimensionRepository.areaSize() * boardHeight)
val separatorsHeight = (dimensionRepository.areaSeparator() * (boardHeight - 1))
return ((height - recyclerViewHeight - separatorsHeight) / 2).coerceAtLeast(0.0f).toInt()
}
fun setAmbientMode(ambientSettings: AmbientSettings) {
areaAdapter.apply {
setAmbientMode(ambientSettings.isAmbientMode, ambientSettings.isLowBitAmbient)