Use new classes

This commit is contained in:
2020-08-19 19:10:34 +02:00
parent e8044a7c70
commit 2c07e433e0
8 changed files with 202 additions and 100 deletions

View File

@@ -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<Int> = emptyList()
private var listB: List<Int> = 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
}
}

View File

@@ -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