Skip to content

Commit

Permalink
feat: Add optional coordinate frames [#22]
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb3rko committed Apr 17, 2023
1 parent 1bec323 commit fbce823
Show file tree
Hide file tree
Showing 20 changed files with 420 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ internal class SettingsActivity : BackpackSettingsActivity(), BackpackSettings {

companion object {
const val KEY_BUTTON_RANDOMIZER = "button_randomizer"
const val KEY_COORDINATE_FRAME = "coordinate_frame"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import com.cyb3rko.pincredible.modals.InputDialog
import com.cyb3rko.pincredible.utils.BackupHandler
import com.cyb3rko.pincredible.utils.ObjectSerializer
import com.cyb3rko.pincredible.utils.Safe
import com.cyb3rko.pincredible.views.CoordinateViewManager
import java.io.File
import java.security.SecureRandom

Expand Down Expand Up @@ -71,6 +72,13 @@ class PinCreatorFragment : Fragment() {
setTableClickListeners()
setButtonClickListeners()
setFabClickListener()

CoordinateViewManager.initializeViews(
myContext,
binding.coordinatesRow1,
binding.coordinatesCol1,
binding.coordinatesCol2
)
}

private fun setTableClickListeners() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@ import com.cyb3rko.backpack.crypto.CryptoManager
import com.cyb3rko.backpack.crypto.CryptoManager.EnDecryptionException
import com.cyb3rko.backpack.modals.ErrorDialog
import com.cyb3rko.backpack.utils.Vibration
import com.cyb3rko.backpack.utils.show
import com.cyb3rko.backpack.utils.withoutLast
import com.cyb3rko.pincredible.R
import com.cyb3rko.pincredible.SettingsActivity
import com.cyb3rko.pincredible.data.PinTable
import com.cyb3rko.pincredible.databinding.FragmentPinViewerBinding
import com.cyb3rko.pincredible.modals.AcceptDialog
import com.cyb3rko.pincredible.utils.BackupHandler
import com.cyb3rko.pincredible.utils.BackupHandler.SingleBackupStructure
import com.cyb3rko.pincredible.utils.ObjectSerializer
import com.cyb3rko.pincredible.utils.Safe
import com.cyb3rko.pincredible.utils.TableScreenshotHandler
import com.cyb3rko.pincredible.views.CoordinateViewManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -115,6 +119,13 @@ class PinViewerFragment : Fragment() {
binding.hashView.text = getString(R.string.viewer_hash, hash.take(10))
binding.pinNameView.text = args.pin

CoordinateViewManager.initializeViews(
myContext,
binding.coordinatesRow1,
binding.coordinatesCol1,
binding.coordinatesCol2
)

lifecycleScope.launch {
loadDataIntoTable()
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/kotlin/com/cyb3rko/pincredible/utils/Safe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ internal object Safe {
key: String,
default: Boolean
) = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(key, default)

internal fun getString(
context: Context,
key: String,
default: String
) = PreferenceManager.getDefaultSharedPreferences(context).getString(key, default)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.LinearLayout
import androidx.core.view.children
import com.cyb3rko.backpack.utils.show
import com.cyb3rko.pincredible.SettingsActivity
import com.cyb3rko.pincredible.databinding.TableCoordinatesHoriBinding
import com.cyb3rko.pincredible.databinding.TableCoordinatesVertBinding
import com.cyb3rko.pincredible.utils.Safe
import com.google.android.material.textview.MaterialTextView

internal object CoordinateViewManager {
private enum class Orientation {
HORIZONTAL, VERTICAL;
}
private enum class Frame(val pattern: String) {
INDEX("12345671234567"),
CHESS("ABCDEFG7654321"),
CHESS_REVERSE_INDEX("ABCDEFG1234567")
}

fun initializeViews(
context: Context,
row: TableCoordinatesHoriBinding,
col1: TableCoordinatesVertBinding,
col2: TableCoordinatesVertBinding
) {
if (Safe.getString(context, SettingsActivity.KEY_COORDINATE_FRAME, "-1") != "-1") {
fillView(context, row)
fillView(context, col1)
fillView(context, col2)
row.root.show()
col1.root.show()
col2.root.show()
}
}

private fun fillView(context: Context, view: TableCoordinatesHoriBinding) {
lowerFillView(context, view.root, Orientation.HORIZONTAL)
}

private fun fillView(context: Context, view: TableCoordinatesVertBinding) {
lowerFillView(context, view.root, Orientation.VERTICAL)
}

private fun lowerFillView(
context: Context,
view: LinearLayout,
orientation: Orientation
) {
val setting = Safe.getString(context, SettingsActivity.KEY_COORDINATE_FRAME, "-1")!!
val frame = when (setting.toInt()) {
0 -> Frame.INDEX
1 -> Frame.CHESS
2 -> Frame.CHESS_REVERSE_INDEX
else -> Frame.INDEX // shouldn't happen
}.pattern

view.children.forEachIndexed { index, textView ->
(textView as MaterialTextView).text = if (orientation == Orientation.HORIZONTAL) {
"${frame[index]}"
} else {
"${frame[index + 7]}"
}
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/colored_ic_coordinate.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/ic_coordinate"
android:tint="@color/app_bar_icon" />
Binary file added app/src/main/res/drawable/ic_coordinate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 44 additions & 3 deletions app/src/main/res/layout/fragment_pin_creator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,55 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="12dp"
android:layout_marginTop="16dp"
android:text="@string/generate_new_pattern"
android:textSize="16sp" />

<com.cyb3rko.pincredible.views.PinTableView
android:id="@+id/table_view"
<include
layout="@layout/table_coordinates_hori"
android:id="@+id/coordinates_row1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:layout_marginEnd="21dp"
android:layout_marginStart="21dp"
android:visibility="gone"
tools:visibility="visible" />

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<include
layout="@layout/table_coordinates_vert"
android:id="@+id/coordinates_col1"
android:layout_width="@dimen/table_coordinate_size"
android:layout_height="0dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="@id/table_view"
app:layout_constraintTop_toTopOf="@id/table_view"
tools:visibility="visible" />

<com.cyb3rko.pincredible.views.PinTableView
android:id="@+id/table_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toStartOf="@id/coordinates_col2"
app:layout_constraintStart_toEndOf="@id/coordinates_col1"
app:layout_constraintTop_toTopOf="parent" />

<include
layout="@layout/table_coordinates_vert"
android:id="@+id/coordinates_col2"
android:layout_width="@dimen/table_coordinate_size"
android:layout_height="0dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="@id/table_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/table_view"
tools:visibility="visible" />

</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/button_container"
Expand Down
48 changes: 45 additions & 3 deletions app/src/main/res/layout/fragment_pin_viewer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down Expand Up @@ -56,12 +57,53 @@
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="12dp"
android:layout_marginStart="16dp" />

<com.cyb3rko.pincredible.views.PinTableView
android:id="@+id/table_view"
<include
layout="@layout/table_coordinates_hori"
android:id="@+id/coordinates_row1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:layout_marginEnd="21dp"
android:layout_marginStart="21dp"
android:visibility="gone"
tools:visibility="visible" />

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<include
layout="@layout/table_coordinates_vert"
android:id="@+id/coordinates_col1"
android:layout_width="@dimen/table_coordinate_size"
android:layout_height="0dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="@id/table_view"
app:layout_constraintTop_toTopOf="@id/table_view"
tools:visibility="visible" />

<com.cyb3rko.pincredible.views.PinTableView
android:id="@+id/table_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toStartOf="@id/coordinates_col2"
app:layout_constraintStart_toEndOf="@id/coordinates_col1"
app:layout_constraintTop_toTopOf="parent" />

<include
layout="@layout/table_coordinates_vert"
android:id="@+id/coordinates_col2"
android:layout_width="@dimen/table_coordinate_size"
android:layout_height="0dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="@id/table_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/table_view"
tools:visibility="visible" />

</androidx.constraintlayout.widget.ConstraintLayout>

<TextView
android:id="@+id/hash_view"
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/res/layout/table_coordinate.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
~ 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.
-->

<com.google.android.material.textview.MaterialTextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:textColor="@color/table_coordinate_text"
android:textSize="@dimen/table_coordinate_text_size"
tools:layout_height="@dimen/table_cell_height"
tools:layout_width="@dimen/table_cell_width"
tools:text="@string/table_empty_cell" />
76 changes: 76 additions & 0 deletions app/src/main/res/layout/table_coordinates_hori.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
~ 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.
-->

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_height="0dp"
android:baselineAligned="false"
tools:layout_height="@dimen/table_coordinate_size"
tools:layout_width="match_parent">

<include
layout="@layout/table_coordinate"
android:layout_width="0dp"
android:layout_height="@dimen/table_coordinate_size"
android:layout_weight="1" />

<include
layout="@layout/table_coordinate"
android:layout_width="0dp"
android:layout_height="@dimen/table_coordinate_size"
android:layout_marginStart="@dimen/table_horizontal_space"
android:layout_weight="1" />

<include
layout="@layout/table_coordinate"
android:layout_width="0dp"
android:layout_height="@dimen/table_coordinate_size"
android:layout_marginStart="@dimen/table_horizontal_space"
android:layout_weight="1" />

<include
layout="@layout/table_coordinate"
android:layout_width="0dp"
android:layout_height="@dimen/table_coordinate_size"
android:layout_marginStart="@dimen/table_horizontal_space"
android:layout_weight="1" />

<include
layout="@layout/table_coordinate"
android:layout_width="0dp"
android:layout_height="@dimen/table_coordinate_size"
android:layout_marginStart="@dimen/table_horizontal_space"
android:layout_weight="1" />

<include
layout="@layout/table_coordinate"
android:layout_width="0dp"
android:layout_height="@dimen/table_coordinate_size"
android:layout_marginStart="@dimen/table_horizontal_space"
android:layout_weight="1" />

<include
layout="@layout/table_coordinate"
android:layout_width="0dp"
android:layout_height="@dimen/table_coordinate_size"
android:layout_marginStart="@dimen/table_horizontal_space"
android:layout_weight="1" />

</LinearLayout>
Loading

0 comments on commit fbce823

Please sign in to comment.