Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Added ability to swipe left/right to go to the previous/next E12/E24 …
Browse files Browse the repository at this point in the history
…value.
  • Loading branch information
ktprograms committed Feb 16, 2021
1 parent 3c01dce commit 38ed14b
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 60 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelogs

### V 0.1.2 Beta
- Added ability to swipe left/right to go to the previous/next E12/E24 value.

>MD5: d43a651aad222840d83375d3d906759c
>
>SHA1: d2bc62053fdd721dcc01425cff0ad5467fffbc04

### V 0.1.1 Beta
- Added support for dark mode.
- Added popup menu to choose band color on long click.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Once you have set all the bands to match your resistor's colours, you can then r
<img src="fastlane/metadata/android/en-US/images/phoneScreenshots/3.png" width="214" height="419" />

By long pressing on a band, there will be a popup menu for you to choose the band's colour.

You can swipe left/right to go to the previous/next E12/E24 value.
<hr />

## Download
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ android {
applicationId "com.ktprograms.ohmsnow"
minSdkVersion 16
targetSdkVersion 30
versionCode 2
versionName "v0.1.1-beta"
versionCode 3
versionName "v0.1.2-beta"

vectorDrawables.useSupportLibrary true
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.OhmsNow">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity"
android:screenOrientation="sensorPortrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
196 changes: 140 additions & 56 deletions app/src/main/java/com/ktprograms/ohmsnow/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ import android.widget.ImageView
import android.widget.PopupMenu
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import java.text.DecimalFormat
import kotlin.math.floor
import kotlin.math.log10
import kotlin.math.pow

class MainActivity : AppCompatActivity() {
// View references
private lateinit var screen: ConstraintLayout
private lateinit var resistorBody: ImageView
private lateinit var band1: ImageButton
private lateinit var band2: ImageButton
Expand All @@ -59,6 +61,54 @@ class MainActivity : AppCompatActivity() {
// Had no long press
private var hadNoLongPress = true

// X coordinate on ACTION_DOWN
private var previousX = 0F

// Minimum swipe amount
private val MIN_DISTANCE = 100

// E12 / E24 values
private val e12 = listOf(
Pair(1, 0),
Pair(1, 2),
Pair(1, 5),
Pair(1, 8),
Pair(2, 2),
Pair(2, 7),
Pair(3, 3),
Pair(3, 9),
Pair(4, 7),
Pair(5, 6),
Pair(6, 8),
Pair(8, 2)
)
private val e24 = listOf(
Pair(1, 0),
Pair(1, 1),
Pair(1, 2),
Pair(1, 3),
Pair(1, 5),
Pair(1, 6),
Pair(1, 8),
Pair(2, 0),
Pair(2, 2),
Pair(2, 4),
Pair(2, 7),
Pair(3, 0),
Pair(3, 3),
Pair(3, 6),
Pair(3, 9),
Pair(4, 3),
Pair(4, 7),
Pair(5, 1),
Pair(5, 6),
Pair(6, 2),
Pair(6, 8),
Pair(7, 5),
Pair(8, 2),
Pair(9, 1)
)

@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -74,6 +124,7 @@ class MainActivity : AppCompatActivity() {
supportActionBar?.setDisplayUseLogoEnabled(true)

// View references
screen = findViewById(R.id.screen)
resistorBody = findViewById(R.id.resistor_body)
ohmsTextView = findViewById(R.id.ohms_text_view)
band1 = findViewById(R.id.band_1)
Expand All @@ -88,34 +139,75 @@ class MainActivity : AppCompatActivity() {
bandLast.isDrawingCacheEnabled = true

// On touch listener
bandLast.setOnTouchListener { _,m ->
screen.setOnTouchListener { _, m ->
when (m.action) {
MotionEvent.ACTION_DOWN -> {
touchedBand = -1
hadNoLongPress = true
if (!checkBandLast(m)) {
if (!checkBandMultiplier(m)) {
if (!checkBand2(m)) {
if (!checkBand1(m)) {
touchedBand = -1
}
if (!bandClicked(m, bandMultiplier)) {
if (!bandClicked(m, band2)) {
bandClicked(m, band1)
}
}
}
previousX = m.x
}
MotionEvent.ACTION_UP -> {
if (hadNoLongPress) {
when (touchedBand) {
0 -> {
nextColor(band1State)
}
1 -> {
nextColor(band2State)
}
2 -> {
nextMultiplierColor(bandMultiplierState)
0 -> nextColor(band1State)
1 -> nextColor(band2State)
2 -> nextMultiplierColor(bandMultiplierState)
-1 -> {
if (previousX - m.x > MIN_DISTANCE) {
var goBackOne = false
val prevPair = when (bandLastState) {
ToleranceBandColors.SILVER -> {
try {
e12.dropLastWhile { (it.first > band1State.value.ordinal) or ((it.first == band1State.value.ordinal) and (it.second >= band2State.value.ordinal)) }.last()
} catch (e: NoSuchElementException) {
goBackOne = true
e12.last()
}
}
ToleranceBandColors.GOLD -> {
try {
e24.dropLastWhile { (it.first > band1State.value.ordinal) or ((it.first == band1State.value.ordinal) and (it.second >= band2State.value.ordinal)) }.last()
} catch (e: NoSuchElementException) {
goBackOne = true
e24.last()
}
}
else -> Pair(1, 0)
}
band1State.value = BandColors.values()[prevPair.first]
band2State.value = BandColors.values()[prevPair.second]
if (goBackOne) {
prevMultiplierColor(bandMultiplierState)
}
} else if (m.x - previousX > MIN_DISTANCE) {
val nextPair = try {
when (bandLastState) {
ToleranceBandColors.SILVER -> {
e12.dropWhile { (it.first < band1State.value.ordinal) or ((it.first == band1State.value.ordinal) and (it.second <= band2State.value.ordinal)) }[0]
}
ToleranceBandColors.GOLD -> {
e24.dropWhile { (it.first < band1State.value.ordinal) or ((it.first == band1State.value.ordinal) and (it.second <= band2State.value.ordinal)) }[0]
}
else -> Pair(1, 0)
}
} catch (e: IndexOutOfBoundsException) {
Pair(1, 0)
}
band1State.value = BandColors.values()[nextPair.first]
band2State.value = BandColors.values()[nextPair.second]
if (nextPair == Pair(1, 0)) {
nextMultiplierColor(bandMultiplierState)
}
}
}
}

updateAll()
}
}
Expand All @@ -125,54 +217,32 @@ class MainActivity : AppCompatActivity() {
}

// On long click listener
bandLast.setOnLongClickListener {
screen.setOnLongClickListener {
hadNoLongPress = false
when (touchedBand) {
0 -> {
showBandPopup(band1, band1State)
}
1 -> {
showBandPopup(band2, band2State)
}
2 -> {
showMultiplierBandPopup(bandMultiplier, bandMultiplierState)
}
0 -> showBandPopup(band1, band1State)
1 -> showBandPopup(band2, band2State)
2 -> showMultiplierBandPopup(bandMultiplier, bandMultiplierState)
}
false
}
}

private fun bandClicked(m: MotionEvent, band: ImageButton): Boolean {
return Bitmap.createBitmap(band.drawingCache)
.getPixel(m.x.toInt(), m.y.toInt()) != Color.TRANSPARENT
}

// Check if band 1 was clicked
private fun checkBand1(m: MotionEvent): Boolean {
return if (bandClicked(m, band1)) {
touchedBand = 0
true
} else {
false
}
}

// Check if band 2 was clicked
private fun checkBand2(m: MotionEvent): Boolean {
return if (bandClicked(m, band2)) {
touchedBand = 1
true
} else {
false
}
}

// Check if band 3 was clicked
private fun checkBandMultiplier(m: MotionEvent): Boolean {
return if (bandClicked(m, bandMultiplier)) {
touchedBand = 2
true
} else {
private fun bandClicked(m: MotionEvent, band: ImageButton): Boolean {
return try {
if (Bitmap.createBitmap(band.drawingCache)
.getPixel(m.x.toInt(), m.y.toInt()) != Color.TRANSPARENT) {
touchedBand = when (band) {
band1 -> 0
band2 -> 1
bandMultiplier -> 2
else -> -1
}
true
} else {
false
}
} catch (e: IllegalArgumentException) {
false
}
}
Expand Down Expand Up @@ -245,6 +315,20 @@ class MainActivity : AppCompatActivity() {
}
}

private fun prevMultiplierColor(multiplierBandState: MultiplierBand) {
multiplierBandState.value = when (multiplierBandState.value) {
MultiplierBandColors.VIOLET -> MultiplierBandColors.BLUE
MultiplierBandColors.BLUE -> MultiplierBandColors.GREEN
MultiplierBandColors.GREEN -> MultiplierBandColors.YELLOW
MultiplierBandColors.YELLOW -> MultiplierBandColors.ORANGE
MultiplierBandColors.ORANGE -> MultiplierBandColors.RED
MultiplierBandColors.RED -> MultiplierBandColors.BROWN
MultiplierBandColors.BROWN -> MultiplierBandColors.BLACK
MultiplierBandColors.BLACK -> MultiplierBandColors.VIOLET
else -> MultiplierBandColors.BLACK
}
}

private fun nextToleranceColor(toleranceBandState: ToleranceBandColors): ToleranceBandColors =
when (toleranceBandState) {
ToleranceBandColors.SILVER -> ToleranceBandColors.GOLD
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,16 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/resistor_body" />

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/screen"
android:layout_width="0dp"
android:layout_height="0dp"
android:longClickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions fastlane/metadata/android/en-US/changelogs/3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ability to swipe left/right to go to the previous/next E12/E24 value.
3 changes: 2 additions & 1 deletion fastlane/metadata/android/en-US/full_description.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<i>Ohms Now!</i> is a simple app that helps you decode resistor colour bands.

Simply tap on each band to change its colour. Once you have set all the bands to match your resistor's colours, you can then read its value in the text field below.
By long pressing on a band, a popup menu shows up to let you choose the band's color.
By long pressing on a band, a popup menu shows up to let you choose the band's color.
You can swipe left/right to go to the previous/next E12/E24 value.

0 comments on commit 38ed14b

Please sign in to comment.