Skip to content

Commit

Permalink
rfct: Abstract pin table layout into PinTableView class
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb3rko committed Mar 30, 2023
1 parent 9303a34 commit c4b6eae
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 648 deletions.
Expand Up @@ -17,16 +17,12 @@
package com.cyb3rko.pincredible.fragments

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TableRow
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.get
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.cyb3rko.pincredible.KEY_BUTTON_RANDOMIZER
Expand All @@ -42,12 +38,9 @@ import com.cyb3rko.pincredible.utils.ObjectSerializer
import com.cyb3rko.pincredible.utils.Safe
import com.cyb3rko.pincredible.utils.Vibration
import com.cyb3rko.pincredible.utils.hide
import com.cyb3rko.pincredible.utils.iterate
import com.cyb3rko.pincredible.utils.show
import java.io.File
import java.security.SecureRandom
import kotlin.jvm.Throws
import kotlin.random.Random

class PinCreatorFragment : Fragment() {
private var _binding: FragmentPinCreatorBinding? = null
Expand All @@ -74,88 +67,36 @@ class PinCreatorFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
colorTableView()
binding.tableView.colorizeRandom(pinTable)
setTableClickListeners()
setButtonClickListeners()
setFabClickListener()
}

private fun colorTableView() {
var randomIndex: Int
var randomBackgroundInt: Int
var randomBackground: Drawable
binding.tableLayout.table.iterate { view, row, column ->
randomIndex = Random.nextInt(5)
randomBackgroundInt = when (randomIndex) {
0 -> R.drawable.cell_shape_cyan
1 -> R.drawable.cell_shape_green
2 -> R.drawable.cell_shape_orange
3 -> R.drawable.cell_shape_red
4 -> R.drawable.cell_shape_yellow
else -> -1 // Not possible to appear
}
randomBackground = ResourcesCompat.getDrawable(
resources,
randomBackgroundInt,
myContext.theme
)!!
(view[row] as TableRow)[column].background = randomBackground
pinTable.putBackground(row, column, randomIndex)
}
}

private fun setTableClickListeners() {
var currentBackgroundInt: Int
var selectedBackgroundInt: Int
binding.tableLayout.table.iterate { view, row, column ->
(view[row] as TableRow)[column].setOnClickListener {
binding.tableView.iterate { tableView, row, column ->
val cell = tableView.getCell(row, column)
cell.setOnClickListener {
Vibration.vibrateTick(vibrator)
clickedCell?.let { cell ->
revertSelectedBackground(cell)
if (cell.view == it) {
clickedCell?.let { safeClickedCell ->
tableView.unselect(safeClickedCell)
if (safeClickedCell.view == it) {
clickedCell = null
binding.buttonContainer.hide()
return@setOnClickListener
}
}

currentBackgroundInt = pinTable.getBackground(row, column)
selectedBackgroundInt = when (currentBackgroundInt) {
0 -> R.drawable.cell_shape_cyan_selected
1 -> R.drawable.cell_shape_green_selected
2 -> R.drawable.cell_shape_orange_selected
3 -> R.drawable.cell_shape_red_selected
4 -> R.drawable.cell_shape_yellow_selected
else -> -1 // Not possible to appear
}
(it as TextView).background = ResourcesCompat.getDrawable(
resources,
selectedBackgroundInt,
myContext.theme
)!!
clickedCell = Cell(it, row, column, currentBackgroundInt)
tableView.select(cell, currentBackgroundInt)
clickedCell = Cell(cell, row, column, currentBackgroundInt)
shuffleButtonDigits()
binding.buttonContainer.show()
}
}
}

private fun revertSelectedBackground(cell: Cell) {
val backgroundInt = when (cell.background) {
0 -> R.drawable.cell_shape_cyan
1 -> R.drawable.cell_shape_green
2 -> R.drawable.cell_shape_orange
3 -> R.drawable.cell_shape_red
4 -> R.drawable.cell_shape_yellow
else -> -1 // Not possible to appear
}
cell.view.background = ResourcesCompat.getDrawable(
resources,
backgroundInt,
myContext.theme
)!!
}

private fun setButtonClickListeners() {
var clickedCellView: TextView
binding.run {
Expand All @@ -169,7 +110,7 @@ class PinCreatorFragment : Fragment() {

clickedCellView = it.view
clickedCellView.text = button.text
revertSelectedBackground(it)
tableView.unselect(it)
val number = clickedCellView.text.toString().toInt()
pinTable.put(it.row, it.column, number)
addedIndices.add(it.row * 7 + it.column)
Expand All @@ -179,7 +120,7 @@ class PinCreatorFragment : Fragment() {
}
}
buttonGenerate.setOnClickListener {
colorTableView()
binding.tableView.colorizeRandom(pinTable)
}
buttonFill.setOnClickListener {
fillTable()
Expand Down Expand Up @@ -225,18 +166,14 @@ class PinCreatorFragment : Fragment() {

private fun fillTable() {
pinTable.fill(addedIndices)
binding.tableLayout.table.iterate { view, row, column ->
((view[row] as TableRow)[column] as TextView).text = pinTable.get(row, column)
}
binding.tableView.fill(pinTable)
binding.fab.show()
}

private fun clearTable() {
binding.fab.hide()
pinTable.resetDigits()
binding.tableLayout.table.iterate { view, row, column ->
((view[row] as TableRow)[column] as TextView).text = null
}
binding.tableView.clear()
addedIndices.clear()
}

Expand Down
Expand Up @@ -21,7 +21,6 @@ import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.os.Bundle
Expand All @@ -32,11 +31,7 @@ import android.view.LayoutInflater
import android.view.PixelCopy
import android.view.View
import android.view.ViewGroup
import android.widget.TableRow
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.get
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
Expand All @@ -53,7 +48,6 @@ import com.cyb3rko.pincredible.utils.BackupHandler.SingleBackupStructure
import com.cyb3rko.pincredible.utils.ObjectSerializer
import com.cyb3rko.pincredible.utils.TableScreenshotHandler
import com.cyb3rko.pincredible.utils.Vibration
import com.cyb3rko.pincredible.utils.iterate
import com.cyb3rko.pincredible.utils.withoutLast
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -174,8 +168,8 @@ class PinViewerFragment : Fragment() {
pinTable = decryptData(hash)
withContext(Dispatchers.Main) {
binding.progressBar.hide()
colorTableView(pinTable)
fillTable(pinTable)
binding.tableView.colorize(pinTable)
binding.tableView.fill(pinTable)
}
binding.exportFab.show()
} catch (e: EnDecryptionException) {
Expand All @@ -186,34 +180,6 @@ class PinViewerFragment : Fragment() {
}
}

private fun colorTableView(pinTable: PinTable) {
var backgroundInt: Int
var background: Drawable
binding.tableLayout.table.iterate { view, row, column ->
backgroundInt = when (pinTable.getBackground(row, column)) {
0 -> R.drawable.cell_shape_cyan
1 -> R.drawable.cell_shape_green
2 -> R.drawable.cell_shape_orange
3 -> R.drawable.cell_shape_red
4 -> R.drawable.cell_shape_yellow
else -> -1 // Not possible to appear
}
background = ResourcesCompat.getDrawable(
resources,
backgroundInt,
myContext.theme
)!!
(view[row] as TableRow)[column].background = background
}
}

private fun fillTable(pinTable: PinTable) {
binding.tableLayout.table.iterate { view, row, column ->
((view[row] as TableRow)[column] as TextView).text =
pinTable.get(row, column)
}
}

@Throws(EnDecryptionException::class)
private fun decryptData(hash: String): PinTable {
val file = File(myContext.filesDir, "p$hash")
Expand All @@ -225,19 +191,23 @@ class PinViewerFragment : Fragment() {
}

private fun generateAndExportImage(uri: Uri) {
val view = binding.tableLayout.table
val tableView = binding.tableView
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
val bitmap = Bitmap.createBitmap(
tableView.width,
tableView.height,
Bitmap.Config.ARGB_8888
)
val locationOfViewInWindow = IntArray(2)
view.getLocationInWindow(locationOfViewInWindow)
tableView.getLocationInWindow(locationOfViewInWindow)
try {
PixelCopy.request(
requireActivity().window,
Rect(
locationOfViewInWindow[0],
locationOfViewInWindow[1],
locationOfViewInWindow[0] + view.width,
locationOfViewInWindow[1] + view.height
locationOfViewInWindow[0] + tableView.width,
locationOfViewInWindow[1] + tableView.height
),
bitmap,
{ copyResult ->
Expand All @@ -251,7 +221,7 @@ class PinViewerFragment : Fragment() {
e.printStackTrace()
}
} else {
TableScreenshotHandler.generateTableCacheCopy(view) {
TableScreenshotHandler.generateTableCacheCopy(tableView) {
if (it != null) {
saveImage(it, uri)
}
Expand Down
14 changes: 1 addition & 13 deletions app/src/main/kotlin/com/cyb3rko/pincredible/utils/Utils.kt
Expand Up @@ -22,24 +22,12 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import android.view.View
import android.widget.TableLayout
import android.widget.Toast
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.Fragment
import com.cyb3rko.pincredible.R
import com.cyb3rko.pincredible.data.PinTable
import com.google.android.material.R as MaterialR
import com.google.android.material.dialog.MaterialAlertDialogBuilder

// TableLayout extension functions

internal fun TableLayout.iterate(action: (view: TableLayout, row: Int, column: Int) -> Unit) {
repeat(PinTable.ROW_COUNT) { row ->
repeat(PinTable.COLUMN_COUNT) { column ->
action(this, row, column)
}
}
}
import com.google.android.material.R as MaterialR

// Context extension functions

Expand Down

0 comments on commit c4b6eae

Please sign in to comment.