diff --git a/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt b/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt index c636d1b..ed95624 100644 --- a/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt +++ b/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt @@ -7,135 +7,203 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.widget.doOnTextChanged import kotlinx.android.synthetic.main.content_main.* import android.text.InputType -import kotlinx.coroutines.sync.Mutex +import android.widget.ScrollView class MainActivity : AppCompatActivity() { - var mut = Mutex() - private var listA: List = emptyList() private var listB: List = emptyList() - private var tempCounterTeamA: Int = 0 - private var tempCounterTeamB: Int = 0 private var counterTeamA: Int = 0 private var counterTeamB: Int = 0 + private var invertA: Boolean = false + private var invertB: Boolean = false + + private var updateOnChange: Boolean = true + + private var history = History() + private var currentRound = Round() + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(findViewById(R.id.toolbar)) inputTeamA.setRawInputType(InputType.TYPE_NULL) inputTeamB.setRawInputType(InputType.TYPE_NULL) + inputTeamA.requestFocus() + disableSubmitButton() inputTeamA.doOnTextChanged { text, start, count, after -> if (inputTeamA.isFocused) { if (inputTeamA.text.isNotEmpty()){ - inputTeamB.setText(updateNumber(text, tempCounterTeamA + tempCounterTeamB)) + if(updateOnChange){ + currentRound = try { + Round(text.toString().toInt(), true) + + } catch (e: java.lang.Exception){ + Round(0, 0) + } + inputTeamB.setText(currentRound.scoreB.toString()) + }else{ + updateOnChange = true + } }else{ inputTeamA.text.clear() inputTeamB.text.clear() } } + + if(currentRound.isValidRound()){ + enableSubmitButton() + }else{ + disableSubmitButton() + } } + + inputTeamB.doOnTextChanged { text, start, count, after -> if (inputTeamB.isFocused) { if (inputTeamB.text.isNotEmpty()){ - inputTeamA.setText(updateNumber(text, tempCounterTeamB + tempCounterTeamA)) + if(updateOnChange){ + currentRound = try { + Round(text.toString().toInt(), false) + + } catch (e: java.lang.Exception){ + Round(0, 0) + } + inputTeamA.setText(currentRound.scoreA.toString()) + + }else{ + updateOnChange = true + } + }else{ inputTeamA.text.clear() inputTeamB.text.clear() } } + + if(currentRound.isValidRound()){ + enableSubmitButton() + }else{ + disableSubmitButton() + } } buttonAdd100.setOnClickListener { if (inputTeamA.isFocused) { - tempCounterTeamA += 100 + val temp = try { inputTeamA.text.toString().toInt() + 100 } catch (e: Exception) { 100 } + updateOnChange = false inputTeamA.setText(temp.toString()) } if (inputTeamB.isFocused) { - tempCounterTeamB += 100 val temp = try { inputTeamB.text.toString().toInt() + 100 } catch (e: Exception) { 100 } + updateOnChange = false inputTeamB.setText(temp.toString()) } } buttonSub100.setOnClickListener { if (inputTeamA.isFocused) { - tempCounterTeamA -= 100 val temp = try { inputTeamA.text.toString().toInt() - 100 } catch (e: Exception) { -100 } + updateOnChange = false inputTeamA.setText(temp.toString()) } if (inputTeamB.isFocused) { - tempCounterTeamB -= 100 val temp = try { inputTeamB.text.toString().toInt() - 100 } catch (e: Exception) { -100 } + updateOnChange = false inputTeamB.setText(temp.toString()) } } button0.setOnClickListener { - appedtoFocusedInput('0') + appendToFocusedInput('0') } button1.setOnClickListener { - appedtoFocusedInput('1') + appendToFocusedInput('1') } button2.setOnClickListener { - appedtoFocusedInput('2') + appendToFocusedInput('2') } button3.setOnClickListener { - appedtoFocusedInput('3') + appendToFocusedInput('3') } button4.setOnClickListener { - appedtoFocusedInput('4') + appendToFocusedInput('4') } button5.setOnClickListener { - appedtoFocusedInput('5') + appendToFocusedInput('5') } button6.setOnClickListener { - appedtoFocusedInput('6') + appendToFocusedInput('6') } button7.setOnClickListener { - appedtoFocusedInput('7') + appendToFocusedInput('7') } button8.setOnClickListener { - appedtoFocusedInput('8') + appendToFocusedInput('8') } button9.setOnClickListener { - appedtoFocusedInput('9') + appendToFocusedInput('9') + } + + buttonInv.setOnClickListener { + var tempInt: Int + + + if(inputTeamA.isFocused ){ + if (inputTeamA.text.isNotEmpty()){ + tempInt = inputTeamA.text.toString().toInt() * -1 + inputTeamA.setText(tempInt.toString()) + }else{ + invertB = false + invertA = true + } + + + }else if(inputTeamB.isFocused) { + if(inputTeamB.text.isNotEmpty()){ + tempInt = inputTeamB.text.toString().toInt() * -1 + inputTeamB.setText(tempInt.toString()) + } else{ + invertA = false + invertB = true + } + } } buttonBack.setOnClickListener{ @@ -153,23 +221,21 @@ class MainActivity : AppCompatActivity() { } } - add.setOnClickListener { + submit.setOnClickListener { if (inputTeamA.text.isNotEmpty() && inputTeamB.text.isNotEmpty()) { - tempCounterTeamA = 0 - tempCounterTeamB = 0 - counterTeamA += inputTeamA.text.toString().toInt() - counterTeamB += inputTeamB.text.toString().toInt() + history.logRound(Round(inputTeamA.text.toString().toInt(), inputTeamB.text.toString().toInt())) - historyA.text = historyA.text.toString().plus(scoreA.text.toString().plus("\n")) - historyB.text = historyB.text.toString().plus(scoreB.text.toString().plus("\n")) - scoreA.text = counterTeamA.toString() - scoreB.text = counterTeamB.toString() + updateView() + inputTeamA.text.clear() inputTeamB.text.clear() + + scrolViewHistory.fullScroll(ScrollView.FOCUS_DOWN) + } } } @@ -186,41 +252,69 @@ class MainActivity : AppCompatActivity() { clearAll() true } + R.id.action_undo -> { + undoLastRound() + true + } else -> super.onOptionsItemSelected(item) } } + private fun undoLastRound(){ + + history.revertLastRound() + updateView() + + } + + private fun updateView() + { + scoreA.text = history.getScoreA().toString() + scoreB.text = history.getScoreB().toString() + + historyA.text = history.getHistoryA() + historyB.text = history.getHistoryB() + } + private fun clearAll() { historyA.text = "" historyB.text = "" inputTeamA.text.clear() inputTeamB.text.clear() - scoreA.text = "" - scoreB.text = "" + scoreA.text = "0" + scoreB.text = "0" + + history.clearAll() } - private fun appedtoFocusedInput(toAppend: Char){ + private fun appendToFocusedInput(toAppend: Char){ if(inputTeamA.isFocused){ + if(invertA){ + invertA = false + inputTeamA.text.append('-') + } + inputTeamA.text.append(toAppend) }else if(inputTeamB.isFocused) { + if(invertB){ + invertB = false + inputTeamB.text.append('-') + } inputTeamB.text.append(toAppend) } + + } - - private fun updateNumber(inputText: CharSequence?, offset: Int): String { - var toSet: Int = 0 - - toSet = try { - 100 - inputText.toString().toInt() - - } catch (e: Exception) { - 0 - } - - toSet += offset - - return "$toSet" + private fun enableSubmitButton() { + submit.imageAlpha = 255 // 0 being transparent and 255 being opaque + submit.isEnabled = true } + + private fun disableSubmitButton(){ + submit.imageAlpha = 60 // 0 being transparent and 255 being opaque + submit.isEnabled = false + } + } \ No newline at end of file diff --git a/app/src/main/java/me/zobrist/tichucounter/Round.kt b/app/src/main/java/me/zobrist/tichucounter/Round.kt index fad6b61..88fc5d4 100644 --- a/app/src/main/java/me/zobrist/tichucounter/Round.kt +++ b/app/src/main/java/me/zobrist/tichucounter/Round.kt @@ -14,6 +14,8 @@ class Round() { } } + constructor(score: String, isScoreA: Boolean): this(score.toInt(), isScoreA) + constructor(scoreA: Int, scoreB: Int) : this() { this.scoreA = scoreA this.scoreB = scoreB diff --git a/app/src/main/res/drawable/checkmark.png b/app/src/main/res/drawable/checkmark.png new file mode 100644 index 0000000..75d2263 Binary files /dev/null and b/app/src/main/res/drawable/checkmark.png differ diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index dff5ef9..b4629c7 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -33,19 +33,49 @@ + + + + + + + + + app:layout_constraintBottom_toBottomOf="@+id/scrolViewHistory" /> + android:layout_marginTop="16dp" + app:layout_constraintBottom_toTopOf="@+id/Input" + app:layout_constraintTop_toBottomOf="@+id/Score"> - - - - - - - - + android:inputType="numberSigned" /> + android:inputType="numberSigned" /> - + android:layout_weight="1.0" + android:text="+/-" />