Skip to content

Commit

Permalink
Calculator cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
mtotschnig committed Jun 25, 2024
1 parent fc03d85 commit cfb5e5f
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import android.view.ContextMenu.ContextMenuInfo
import android.view.KeyEvent
import android.view.Menu
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.widget.PopupMenu
import androidx.core.content.ContextCompat
import org.totschnig.myexpenses.R
import org.totschnig.myexpenses.databinding.CalculatorBinding
import org.totschnig.myexpenses.databinding.OkCancelButtonsBinding
import org.totschnig.myexpenses.provider.DatabaseConstants
import org.totschnig.myexpenses.util.Utils
import java.math.BigDecimal
Expand All @@ -29,7 +28,7 @@ Originally based on Financisto's Calculator
*/
class CalculatorInput : ProtectedFragmentActivity(), View.OnClickListener {
private lateinit var binding: CalculatorBinding
private var tvOp: TextView? = null
private lateinit var okCancelButtonsBinding: OkCancelButtonsBinding

private var stack = Stack<String>()
private var result: String? = "0"
Expand All @@ -39,6 +38,7 @@ class CalculatorInput : ProtectedFragmentActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = CalculatorBinding.inflate(layoutInflater)
okCancelButtonsBinding = OkCancelButtonsBinding.bind(binding.root)
setContentView(binding.root)

arrayOf(
Expand Down Expand Up @@ -88,25 +88,19 @@ class CalculatorInput : ProtectedFragmentActivity(), View.OnClickListener {
} ?: false
}
setDisplay("0")
tvOp = findViewById<View>(R.id.op) as TextView
var b = findViewById<View>(R.id.bOK) as Button
b.setOnClickListener {

okCancelButtonsBinding.bOK.setOnClickListener {
if (!isInEquals) {
doEqualsChar()
}
close()
}
b = findViewById<View>(R.id.bCancel) as Button
b.setOnClickListener {
okCancelButtonsBinding.bCancel.setOnClickListener {
setResult(RESULT_CANCELED)
finish()
}
val intent = intent
if (intent != null) {
val amount = intent.getSerializableExtra(DatabaseConstants.KEY_AMOUNT) as BigDecimal?
if (amount != null) {
setDisplay(amount.toPlainString())
}
(intent?.getSerializableExtra(DatabaseConstants.KEY_AMOUNT) as? BigDecimal)?.let {
setDisplay(it.toPlainString())
}
}

Expand Down Expand Up @@ -184,7 +178,7 @@ class CalculatorInput : ProtectedFragmentActivity(), View.OnClickListener {

private fun resetAll() {
setDisplay("0")
tvOp!!.text = ""
binding.resultPane.op.text = ""
lastOp = 0
isRestart = true
stack.clear()
Expand Down Expand Up @@ -248,7 +242,7 @@ class CalculatorInput : ProtectedFragmentActivity(), View.OnClickListener {
stack.push(result)
doLastOp()
lastOp = op
tvOp!!.text = lastOpLabel
binding.resultPane.op.text = lastOpLabel
}

private val lastOpLabel: String
Expand Down Expand Up @@ -307,7 +301,7 @@ class CalculatorInput : ProtectedFragmentActivity(), View.OnClickListener {
BigDecimal(result).divide(HUNDRED).multiply(BigDecimal(stack.peek()))
.toPlainString()
)
tvOp!!.text = ""
binding.resultPane.op.text = ""
}

private fun doEqualsChar() {
Expand All @@ -319,7 +313,7 @@ class CalculatorInput : ProtectedFragmentActivity(), View.OnClickListener {
stack.push(result)
}
doLastOp()
tvOp!!.text = ""
binding.resultPane.op.text = ""
}

private fun close() {
Expand All @@ -345,7 +339,7 @@ class CalculatorInput : ProtectedFragmentActivity(), View.OnClickListener {
isInEquals = savedInstanceState.getBoolean("isInEquals")
stack = Stack()
stack.addAll(savedInstanceState.getSerializable("stack") as Array<String>)
if (lastOp != 0 && !isInEquals) tvOp!!.text = lastOpLabel
if (lastOp != 0 && !isInEquals) binding.resultPane.op.text = lastOpLabel
setDisplay(result)
}

Expand Down
43 changes: 23 additions & 20 deletions myExpenses/src/main/res/layout-h400dp/calculator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,29 @@
android:orientation="vertical"
android:padding="3dp">

<include android:id="@+id/result_pane" layout="@layout/calculator_result" />
<include
android:id="@+id/result_pane"
layout="@layout/calculator_result" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">

<Button
tools:text="7"
android:id="@+id/b7"
style="@style/CalculatorButton" />
style="@style/CalculatorButton"
tools:text="7" />

<Button
tools:text="8"
android:id="@+id/b8"
style="@style/CalculatorButton" />
style="@style/CalculatorButton"
tools:text="8" />

<Button
tools:text="9"
android:id="@+id/b9"
style="@style/CalculatorButton" />
style="@style/CalculatorButton"
tools:text="9" />

<Button
android:id="@+id/bAdd"
Expand All @@ -52,19 +54,19 @@
android:layout_gravity="center">

<Button
tools:text="4"
android:id="@+id/b4"
style="@style/CalculatorButton" />
style="@style/CalculatorButton"
tools:text="4" />

<Button
tools:text="5"
android:id="@+id/b5"
style="@style/CalculatorButton" />
style="@style/CalculatorButton"
tools:text="5" />

<Button
tools:text="6"
android:id="@+id/b6"
style="@style/CalculatorButton" />
style="@style/CalculatorButton"
tools:text="6" />

<Button
android:id="@+id/bSubtract"
Expand All @@ -81,19 +83,19 @@
android:layout_gravity="center">

<Button
tools:text="1"
android:id="@+id/b1"
style="@style/CalculatorButton" />
style="@style/CalculatorButton"
tools:text="1" />

<Button
tools:text="2"
android:id="@+id/b2"
style="@style/CalculatorButton" />
style="@style/CalculatorButton"
tools:text="2" />

<Button
tools:text="3"
android:id="@+id/b3"
style="@style/CalculatorButton" />
style="@style/CalculatorButton"
tools:text="3" />

<Button
android:id="@+id/bMultiply"
Expand Down Expand Up @@ -136,6 +138,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">

<include layout="@layout/ok_cancel_buttons" />
<include
layout="@layout/ok_cancel_buttons" />
</LinearLayout>
</LinearLayout>
7 changes: 5 additions & 2 deletions myExpenses/src/main/res/layout/calculator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
android:orientation="vertical"
android:padding="3dp">

<include android:id="@+id/result_pane" layout="@layout/calculator_result" />
<include
android:id="@+id/result_pane"
layout="@layout/calculator_result" />

<LinearLayout
android:layout_width="wrap_content"
Expand Down Expand Up @@ -111,7 +113,8 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">

<include layout="@layout/ok_cancel_buttons" />
<include
layout="@layout/ok_cancel_buttons" />

<Button
android:id="@+id/bDivide"
Expand Down
14 changes: 0 additions & 14 deletions myExpenses/src/main/res/layout/cancel_button.xml

This file was deleted.

14 changes: 0 additions & 14 deletions myExpenses/src/main/res/layout/ok_button.xml

This file was deleted.

22 changes: 19 additions & 3 deletions myExpenses/src/main/res/layout/ok_cancel_buttons.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,25 @@
Contributors:
Denis Solonenko - initial API and implementation
-->
<merge>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<include layout="@layout/cancel_button"/>
<Button
android:id="@+id/bCancel"
style="@style/CalculatorButtonBase"
android:layout_width="0dp"
android:layout_weight="1"
android:maxLines="1"
android:text="@android:string/cancel"
app:autoSizeTextType="uniform" />

<include layout="@layout/ok_button"/>
<Button
android:id="@+id/bOK"
style="@style/CalculatorButtonBase"
android:layout_width="0dp"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:text="@android:string/ok"
app:autoSizeTextType="uniform" />
</merge>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<resources>
<dimen name="calculator_button_min">50dp</dimen>
<dimen name="calculator_button_text_size">24sp</dimen>
<dimen name="calculator_button_layout_margin">3dp</dimen>
<dimen name="calculator_button_layout_margin_vertical">3dp</dimen>
<integer name="filter_chip_max_lines">4</integer>
</resources>
3 changes: 2 additions & 1 deletion myExpenses/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<dimen name="calculator_button_min">47dp</dimen>
<dimen name="calculator_button_text_size">18sp</dimen>
<dimen name="calculator_button_layout_margin">0dp</dimen>
<dimen name="calculator_button_layout_margin_vertical">0dp</dimen>
<dimen name="calculator_button_layout_margin_horizontal">3dp</dimen>
<dimen name="general_padding">12dp</dimen>
<dimen name="padding_main_screen">12dp</dimen>
<dimen name="padding_form">16dp</dimen>
Expand Down
5 changes: 4 additions & 1 deletion myExpenses/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@
<item name="android:layout_weight">1</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">@dimen/calculator_button_layout_margin</item>
<item name="android:layout_marginTop">@dimen/calculator_button_layout_margin_vertical</item>
<item name="android:layout_marginBottom">@dimen/calculator_button_layout_margin_vertical</item>
<item name="android:layout_marginStart">@dimen/calculator_button_layout_margin_horizontal</item>
<item name="android:layout_marginEnd">@dimen/calculator_button_layout_margin_horizontal</item>
</style>

<style name="CalculatorButton" parent="CalculatorButtonBase">
Expand Down

0 comments on commit cfb5e5f

Please sign in to comment.