-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from lucasnlm/fix-center-board
Add CommonLevelFragment and fix vertical center
- Loading branch information
Showing
3 changed files
with
86 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
common/src/main/java/dev/lucasnlm/antimine/common/level/view/CommonLevelFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters