Skip to content

Commit

Permalink
fix: Revamp table cell size calculation [#29]
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb3rko committed Sep 26, 2023
1 parent 2b2f8ca commit d9544f4
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 278 deletions.
40 changes: 40 additions & 0 deletions app/src/main/kotlin/com/cyb3rko/pincredible/views/PinTableCell.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2023 Cyb3rKo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.cyb3rko.pincredible.views

import android.content.Context
import android.graphics.Typeface
import android.util.TypedValue
import android.view.Gravity
import android.view.ViewGroup.MarginLayoutParams
import com.cyb3rko.pincredible.R
import com.google.android.material.textview.MaterialTextView

class PinTableCell(context: Context) : MaterialTextView(context) {
init {
gravity = Gravity.CENTER
setTextSize(TypedValue.COMPLEX_UNIT_PX, resources.getDimension(R.dimen.table_text_size))
setTextColor(resources.getColor(R.color.table_cell_text, null))
typeface = Typeface.DEFAULT_BOLD
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
(layoutParams as MarginLayoutParams).setMargins(6, 0, 6, 0)
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
setMeasuredDimension(measuredWidth, measuredWidth)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2023 Cyb3rKo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.cyb3rko.pincredible.views

import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import com.cyb3rko.pincredible.R
import com.google.android.material.textview.MaterialTextView

class PinTableCoordinate(
context: Context,
attrs: AttributeSet
) : MaterialTextView(context, attrs) {
init {
gravity = Gravity.CENTER
setTextSize(
TypedValue.COMPLEX_UNIT_PX,
resources.getDimension(R.dimen.table_coordinate_text_size)
)
setTextColor(resources.getColor(R.color.table_coordinate_text, null))
}
}
27 changes: 27 additions & 0 deletions app/src/main/kotlin/com/cyb3rko/pincredible/views/PinTableRow.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2023 Cyb3rKo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.cyb3rko.pincredible.views

import android.content.Context
import android.widget.TableRow

internal class PinTableRow(context: Context) : TableRow(context) {
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
(layoutParams as MarginLayoutParams).setMargins(0, 6, 0, 0)
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}
}
52 changes: 25 additions & 27 deletions app/src/main/kotlin/com/cyb3rko/pincredible/views/PinTableView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.widget.TableLayout
import android.widget.TableRow
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.get
import com.cyb3rko.backpack.crypto.CryptoManager
import com.cyb3rko.backpack.utils.getDrawableCompat
import com.cyb3rko.pincredible.R
import com.cyb3rko.pincredible.data.Cell
import com.cyb3rko.pincredible.data.PinTable
Expand All @@ -35,7 +34,20 @@ internal class PinTableView(
attrs: AttributeSet
) : TableLayout(context, attrs) {
init {
inflate(context, R.layout.table_view, this)
isStretchAllColumns = true
var row = PinTableRow(context)
var currentRowIndex = 0
var cell: PinTableCell
iterate { _, rowIndex, _ ->
if (currentRowIndex != rowIndex) {
this.addView(row)
row = PinTableRow(context)
currentRowIndex += 1
}
cell = PinTableCell(context)
row.addView(cell)
}
this.addView(row)
}

fun iterate(action: (view: PinTableView, row: Int, column: Int) -> Unit) {
Expand All @@ -46,13 +58,15 @@ internal class PinTableView(
}
}

fun getCell(row: Int, column: Int): TextView {
return ((this[0] as TableLayout)[row] as TableRow)[column] as TextView
fun getCell(row: Int, column: Int): PinTableCell {
return (this[row] as TableRow)[column] as PinTableCell
}

fun fill(pinTable: PinTable) {
var cell: PinTableCell
iterate { _, row, column ->
getCell(row, column).text = pinTable.get(row, column)
cell = getCell(row, column)
cell.text = pinTable.get(row, column)
}
}

Expand All @@ -62,11 +76,7 @@ internal class PinTableView(
iterate { _, row, column ->
colorIndex = pinTable.getBackground(row, column)
backgroundInt = colorIndexToDrawableID(colorIndex, colorBlindAlternative)
getCell(row, column).background = ResourcesCompat.getDrawable(
resources,
backgroundInt,
context.theme
)!!
getCell(row, column).background = context.getDrawableCompat(backgroundInt)
}
}

Expand All @@ -77,39 +87,27 @@ internal class PinTableView(
iterate { _, row, column ->
randomIndex = CryptoManager.getRandom(0, 5)
randomBackgroundInt = colorIndexToDrawableID(randomIndex, colorBlindAlternative)
randomBackground = ResourcesCompat.getDrawable(
resources,
randomBackgroundInt,
context.theme
)!!
randomBackground = context.getDrawableCompat(randomBackgroundInt)
getCell(row, column).background = randomBackground
pinTable.putBackground(row, column, randomIndex)
}
}

fun select(
cell: TextView,
cell: PinTableCell,
@ColorRes backgroundInt: Int,
colorBlindAlternative: Boolean
) {
val selectedBackgroundInt = colorIndexToSelectedDrawableID(
backgroundInt,
colorBlindAlternative
)
cell.background = ResourcesCompat.getDrawable(
resources,
selectedBackgroundInt,
context.theme
)!!
cell.background = context.getDrawableCompat(selectedBackgroundInt)
}

fun unselect(cell: Cell, colorBlindAlternative: Boolean) {
val backgroundInt = colorIndexToDrawableID(cell.background, colorBlindAlternative)
cell.view.background = ResourcesCompat.getDrawable(
resources,
backgroundInt,
context.theme
)!!
cell.view.background = context.getDrawableCompat(backgroundInt)
}

fun clear() {
Expand Down
32 changes: 0 additions & 32 deletions app/src/main/res/layout/table_cell.xml

This file was deleted.

29 changes: 0 additions & 29 deletions app/src/main/res/layout/table_coordinate.xml

This file was deleted.

42 changes: 21 additions & 21 deletions app/src/main/res/layout/table_coordinates_hori.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,52 +25,52 @@
tools:layout_height="@dimen/table_coordinate_size"
tools:layout_width="match_parent">

<include
layout="@layout/table_coordinate"
<com.cyb3rko.pincredible.views.PinTableCoordinate
android:layout_width="0dp"
android:layout_height="@dimen/table_coordinate_size"
android:layout_weight="1" />
android:layout_weight="1"
tools:text="1" />

<include
layout="@layout/table_coordinate"
<com.cyb3rko.pincredible.views.PinTableCoordinate
android:layout_width="0dp"
android:layout_height="@dimen/table_coordinate_size"
android:layout_marginStart="@dimen/table_horizontal_space"
android:layout_weight="1" />
android:layout_weight="1"
tools:text="2" />

<include
layout="@layout/table_coordinate"
<com.cyb3rko.pincredible.views.PinTableCoordinate
android:layout_width="0dp"
android:layout_height="@dimen/table_coordinate_size"
android:layout_marginStart="@dimen/table_horizontal_space"
android:layout_weight="1" />
android:layout_weight="1"
tools:text="3" />

<include
layout="@layout/table_coordinate"
<com.cyb3rko.pincredible.views.PinTableCoordinate
android:layout_width="0dp"
android:layout_height="@dimen/table_coordinate_size"
android:layout_marginStart="@dimen/table_horizontal_space"
android:layout_weight="1" />
android:layout_weight="1"
tools:text="4" />

<include
layout="@layout/table_coordinate"
<com.cyb3rko.pincredible.views.PinTableCoordinate
android:layout_width="0dp"
android:layout_height="@dimen/table_coordinate_size"
android:layout_marginStart="@dimen/table_horizontal_space"
android:layout_weight="1" />
android:layout_weight="1"
tools:text="5" />

<include
layout="@layout/table_coordinate"
<com.cyb3rko.pincredible.views.PinTableCoordinate
android:layout_width="0dp"
android:layout_height="@dimen/table_coordinate_size"
android:layout_marginStart="@dimen/table_horizontal_space"
android:layout_weight="1" />
android:layout_weight="1"
tools:text="6" />

<include
layout="@layout/table_coordinate"
<com.cyb3rko.pincredible.views.PinTableCoordinate
android:layout_width="0dp"
android:layout_height="@dimen/table_coordinate_size"
android:layout_marginStart="@dimen/table_horizontal_space"
android:layout_weight="1" />
android:layout_weight="1"
tools:text="7" />

</LinearLayout>
Loading

0 comments on commit d9544f4

Please sign in to comment.