Use new classes
This commit is contained in:
@@ -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
|
||||
private fun enableSubmitButton() {
|
||||
submit.imageAlpha = 255 // 0 being transparent and 255 being opaque
|
||||
submit.isEnabled = true
|
||||
}
|
||||
|
||||
toSet += offset
|
||||
private fun disableSubmitButton(){
|
||||
submit.imageAlpha = 60 // 0 being transparent and 255 being opaque
|
||||
submit.isEnabled = false
|
||||
}
|
||||
|
||||
return "$toSet"
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
BIN
app/src/main/res/drawable/checkmark.png
Normal file
BIN
app/src/main/res/drawable/checkmark.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 9.1 KiB |
@@ -33,19 +33,49 @@
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/Score"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
app:layout_constraintTop_toBottomOf="@+id/Names">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/scoreA"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:gravity="center"
|
||||
android:text="0"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
|
||||
android:textSize="18sp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/scoreB"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:gravity="center"
|
||||
android:text="0"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
|
||||
android:textSize="18sp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<View
|
||||
android:id="@+id/divider5"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="1dp"
|
||||
android:background="?android:attr/listDivider"
|
||||
app:layout_constraintBottom_toBottomOf="@+id/scrollView2" />
|
||||
app:layout_constraintBottom_toBottomOf="@+id/scrolViewHistory" />
|
||||
|
||||
<ScrollView
|
||||
android:id="@+id/scrollView2"
|
||||
android:id="@+id/scrolViewHistory"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
app:layout_constraintBottom_toTopOf="@+id/Score"
|
||||
app:layout_constraintTop_toBottomOf="@+id/Names">
|
||||
android:layout_marginTop="16dp"
|
||||
app:layout_constraintBottom_toTopOf="@+id/Input"
|
||||
app:layout_constraintTop_toBottomOf="@+id/Score">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
@@ -69,33 +99,6 @@
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/Score"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:orientation="horizontal"
|
||||
app:layout_constraintBottom_toTopOf="@+id/Input">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/scoreA"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:gravity="center"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
|
||||
android:textSize="18sp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/scoreB"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:gravity="center"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
|
||||
android:textSize="18sp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<View
|
||||
android:id="@+id/divider6"
|
||||
android:layout_width="match_parent"
|
||||
@@ -116,10 +119,10 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:ems="10"
|
||||
android:gravity="center"
|
||||
android:hint="0"
|
||||
android:importantForAutofill="no"
|
||||
android:inputType="numberSigned"
|
||||
android:gravity="center"/>
|
||||
android:inputType="numberSigned" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/inputTeamB"
|
||||
@@ -127,10 +130,10 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:ems="10"
|
||||
android:gravity="center"
|
||||
android:hint="0"
|
||||
android:importantForAutofill="no"
|
||||
android:inputType="numberSigned"
|
||||
android:gravity="center"/>
|
||||
android:inputType="numberSigned" />
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
@@ -255,10 +258,12 @@
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
tools:layout_editor_absoluteX="1dp">
|
||||
|
||||
<Space
|
||||
<Button
|
||||
android:id="@+id/buttonInv"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1.0" />
|
||||
android:layout_weight="1.0"
|
||||
android:text="+/-" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button0"
|
||||
@@ -272,11 +277,12 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1.0" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/add"
|
||||
<ImageButton
|
||||
android:id="@+id/submit"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1.0"
|
||||
android:text="@string/add" />
|
||||
android:scaleType="fitCenter"
|
||||
app:srcCompat="@drawable/checkmark" />
|
||||
</LinearLayout>
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
@@ -8,4 +8,8 @@
|
||||
android:orderInCategory="100"
|
||||
android:title="@string/clear"
|
||||
app:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/action_undo"
|
||||
android:icon="@android:drawable/ic_menu_revert"
|
||||
android:title="@string/undo" />
|
||||
</menu>
|
||||
@@ -1,7 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="team_a">Team A</string>
|
||||
<string name="team_b">Team B</string>
|
||||
<string name="clear">Löschen</string>
|
||||
<string name="add">Hinzufügen</string>
|
||||
<string name="clear">Neues Spiel Starten</string>
|
||||
<string name="undo">Letzte Runde Löschen</string>
|
||||
</resources>
|
||||
@@ -1,7 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="clear">Lösche</string>
|
||||
<string name="team_a">Team A</string>
|
||||
<string name="team_b">Team B</string>
|
||||
<string name="add">Drzuetue</string>
|
||||
<string name="clear">Neus Spil Starte</string>
|
||||
<string name="undo">Letschti Rundi Lösche</string>
|
||||
</resources>
|
||||
@@ -2,8 +2,8 @@
|
||||
<string name="app_name" translatable="false">Tichu Counter</string>
|
||||
<!-- Strings used for fragments for navigation -->
|
||||
|
||||
<string name="team_a">Team A</string>
|
||||
<string name="team_b">Team B</string>
|
||||
<string name="add">Add</string>
|
||||
<string name="clear">Clear</string>
|
||||
<string name="team_a" translatable="false">Team A</string>
|
||||
<string name="team_b" translatable="false">Team B</string>
|
||||
<string name="clear">Start New Game</string>
|
||||
<string name="undo">Undo Last Round</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user