Skip to content

Commit

Permalink
Merge pull request #13 from CraveFood/remove-bottom-buttons
Browse files Browse the repository at this point in the history
Removed bottom buttons and implemented a delegate to be called with any calc update
  • Loading branch information
rafael1mc authored Mar 14, 2019
2 parents 58919f5 + ec58275 commit 562393a
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ class MainActivity : AppCompatActivity() {

supportFragmentManager.beginTransaction().add(
R.id.frameLayout,
CalculatorFragment.newInstance({
Toast.makeText(this, "Cancel", Toast.LENGTH_SHORT).show()
}, {
Toast.makeText(this, "Confirm $it", Toast.LENGTH_SHORT).show()
})
CalculatorFragment.newInstance {
Toast.makeText(this, "Total $it", Toast.LENGTH_SHORT).show()
}
).commit()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.HorizontalScrollView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
Expand All @@ -15,8 +14,7 @@ import kotlinx.android.synthetic.main.c_calc_fragment.*
class CalculatorFragment : Fragment() {

private lateinit var viewModel: CalculatorViewModel
private var onCancelListener: (() -> Unit)? = null
private var onConfirmListener: ((Double) -> Unit)? = null
private var onTotalListener: ((Double) -> Unit)? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.c_calc_fragment, container, false)
Expand All @@ -37,14 +35,12 @@ class CalculatorFragment : Fragment() {
focusOperationText()
})

viewModel.resultObservable.observe(this, Observer {
if (it != null) {
textViewResult.text = getFormatResult(it)

buttonConfirm.isEnabled = it > 0.0
viewModel.resultObservable.observe(this, Observer { total ->
if (total != null) {
textViewResult.text = getFormatResult(total)
onTotalListener?.invoke(total)
} else {
textViewResult.text = ""
buttonConfirm.isEnabled = false
}
})
}
Expand Down Expand Up @@ -79,21 +75,6 @@ class CalculatorFragment : Fragment() {
imageButtonPlus.setOnClickListener { viewModel.addOperator(OPERATOR_PLUS) }

imageButtonEqual.setOnClickListener { viewModel.getResult() }

buttonCancel.setOnClickListener { cancelTapped() }
buttonConfirm.setOnClickListener { confirmTapped() }
}

private fun confirmTapped() {
val total = viewModel.resultObservable.value ?: 0.0
if (onConfirmListener != null) {
onConfirmListener?.invoke(total)
} else Toast.makeText(context, "Total: $total", Toast.LENGTH_SHORT).show()
}

private fun cancelTapped() {
if (onCancelListener != null) onCancelListener?.invoke()
else activity?.onBackPressed()
}

private fun focusOperationText() {
Expand All @@ -104,13 +85,9 @@ class CalculatorFragment : Fragment() {
}

companion object {
fun newInstance(
cancelListener: (() -> Unit)? = null,
confirmListener: ((Double) -> Unit)? = null
): CalculatorFragment {
fun newInstance(onTotalListener: ((Double) -> Unit)? = null): CalculatorFragment {
val fragment = CalculatorFragment()
fragment.onCancelListener = cancelListener
fragment.onConfirmListener = confirmListener
fragment.onTotalListener = onTotalListener
return fragment
}
}
Expand Down
24 changes: 1 addition & 23 deletions cravecalculator/src/main/res/layout/c_calc_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layoutParentButtons"
android:layout_width="0dp"
app:layout_constraintBottom_toTopOf="@+id/layoutParentBottomButtons"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
Expand Down Expand Up @@ -239,26 +239,4 @@

</androidx.constraintlayout.widget.ConstraintLayout>

<LinearLayout
android:id="@+id/layoutParentBottomButtons"
android:layout_width="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:elevation="8dp"
android:padding="16dp"
android:orientation="horizontal"
android:weightSum="10">

<Button
android:id="@+id/buttonCancel"
style="@style/CCalcButtonCancel"/>

<Button
android:id="@+id/buttonConfirm"
style="@style/CCalcButtonConfirm"/>
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 0 additions & 2 deletions cravecalculator/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@
<string name="c_calc_8">8</string>
<string name="c_calc_9">9</string>

<string name="c_calc_charge_customer">Charge Customer</string>
<string name="c_calc_clear">C</string>
<string name="c_calc_cancel">Cancel</string>
</resources>
27 changes: 0 additions & 27 deletions cravecalculator/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,6 @@
<item name="android:fontFamily">@font/c_calc_lato_regular</item>
</style>

<style name="CCalcButtonConfirm">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">56dp</item>
<item name="android:layout_weight">4</item>
<item name="android:background">@drawable/c_calc_selector_confirm_button</item>
<item name="android:textColor">@color/cCalColorConfirmButtonText</item>
<item name="android:textSize">14sp</item>
<item name="android:text">@string/c_calc_charge_customer</item>
<item name="android:maxLines">1</item>
<item name="android:layout_marginStart">4dp</item>
<item name="android:fontFamily">@font/c_calc_lato_semibold</item>
</style>

<style name="CCalcButtonCancel">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">56dp</item>
<item name="android:layout_weight">6</item>
<item name="android:elevation">0dp</item>
<item name="android:background">@drawable/c_calc_ripple_cancel_button</item>
<item name="android:textColor">@color/cCalColorCancelButtonText</item>
<item name="android:maxLines">1</item>
<item name="android:textSize">14sp</item>
<item name="android:text">@string/c_calc_cancel</item>
<item name="android:layout_marginEnd">4dp</item>
<item name="android:fontFamily">@font/c_calc_lato_semibold</item>
</style>

<style name="CCalcTextViewOperation">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
Expand Down

0 comments on commit 562393a

Please sign in to comment.