From 38ed14b8b5921f38ac91223f4db73592d145be43 Mon Sep 17 00:00:00 2001 From: ktprograms Date: Tue, 16 Feb 2021 16:31:14 +0800 Subject: [PATCH] Added ability to swipe left/right to go to the previous/next E12/E24 value. --- CHANGELOG.md | 8 + README.md | 2 + app/build.gradle | 4 +- app/src/main/AndroidManifest.xml | 3 +- .../com/ktprograms/ohmsnow/MainActivity.kt | 196 +++++++++++++----- app/src/main/res/layout/activity_main.xml | 12 ++ .../metadata/android/en-US/changelogs/3.txt | 1 + .../android/en-US/full_description.txt | 3 +- 8 files changed, 169 insertions(+), 60 deletions(-) create mode 100644 fastlane/metadata/android/en-US/changelogs/3.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d37864..16c47d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index 1f62b22..d89c7ce 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ Once you have set all the bands to match your resistor's colours, you can then r 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.
## Download diff --git a/app/build.gradle b/app/build.gradle index 737bea0..213a42a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b3ca471..24a424e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -31,7 +31,8 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.OhmsNow"> - + diff --git a/app/src/main/java/com/ktprograms/ohmsnow/MainActivity.kt b/app/src/main/java/com/ktprograms/ohmsnow/MainActivity.kt index 93e6538..fc9210a 100644 --- a/app/src/main/java/com/ktprograms/ohmsnow/MainActivity.kt +++ b/app/src/main/java/com/ktprograms/ohmsnow/MainActivity.kt @@ -33,6 +33,7 @@ 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 @@ -40,6 +41,7 @@ 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 @@ -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) @@ -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) @@ -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() } } @@ -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 } } @@ -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 diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index b57f5c6..3dfcfa2 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -126,4 +126,16 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/resistor_body" /> + + + + \ No newline at end of file diff --git a/fastlane/metadata/android/en-US/changelogs/3.txt b/fastlane/metadata/android/en-US/changelogs/3.txt new file mode 100644 index 0000000..dc44c4a --- /dev/null +++ b/fastlane/metadata/android/en-US/changelogs/3.txt @@ -0,0 +1 @@ +Added ability to swipe left/right to go to the previous/next E12/E24 value. \ No newline at end of file diff --git a/fastlane/metadata/android/en-US/full_description.txt b/fastlane/metadata/android/en-US/full_description.txt index 945de22..f4406c8 100644 --- a/fastlane/metadata/android/en-US/full_description.txt +++ b/fastlane/metadata/android/en-US/full_description.txt @@ -1,4 +1,5 @@ Ohms Now! 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. \ No newline at end of file +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. \ No newline at end of file