Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Having trouble with ViewModel to hold the state of the order and the wallet/getting purchase total working throughout fragments of the app. Any help is

Having trouble with ViewModel to hold the state of the order and the wallet/getting purchase total working throughout fragments of the app. Any help is much appreciated

image text in transcribedimage text in transcribed

--------------------------------------------- import android.os.Bundle import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Button import android.widget.TextView import androidx.navigation.findNavController class ConfirmationFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val view = inflater.inflate(R.layout.fragment_confirmation, container, false) val orderFinal = view.findViewById(R.id.purchaseFinal) // Set an OnClickListener on the TextView orderFinal.setOnClickListener { val action = ConfirmationFragmentDirections.actionConfirmationFragmentToReceiptFragment() view?.findNavController()?.navigate(action) } return view } }

---------------------------------------

import androidx.navigation.ActionOnlyNavDirections import androidx.navigation.NavDirections class ConfirmationFragmentDirections { companion object { fun actionConfirmationFragmentToReceiptFragment(): NavDirections { return ActionOnlyNavDirections(R.id.action_confirmationFragment_to_receiptFragment) } } }

--------------------------------------

import android.os.Bundle import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } }

------------------------------------

import androidx.lifecycle.ViewModel class MainViewModel : ViewModel() { }

--------------------------------------

import android.os.Bundle import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Button import android.widget.TextView import androidx.navigation.findNavController class OrderFragment : Fragment() { private lateinit var applesQuantity: TextView private lateinit var orangesQuantity: TextView private lateinit var apples_Plus: Button private lateinit var apples_Minus: Button private lateinit var oranges_Plus: Button private lateinit var oranges_Minus: Button private lateinit var viewModel: OrderViewModel override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val view = inflater.inflate(R.layout.fragment_order, container, false) applesQuantity = view.findViewById(R.id.appleCount) orangesQuantity = view.findViewById(R.id.orangeCount) apples_Plus = view.findViewById(R.id.applePlus) apples_Minus = view.findViewById(R.id.appleMinus) oranges_Plus = view.findViewById(R.id.orangePlus) oranges_Minus = view.findViewById(R.id.orangeMinus) apples_Plus.setOnClickListener { incrementQuantity(applesQuantity) } apples_Minus.setOnClickListener { decrementQuantity(applesQuantity) } oranges_Plus.setOnClickListener { incrementQuantity(orangesQuantity) } oranges_Minus.setOnClickListener { decrementQuantity(orangesQuantity) } // Find the TextView for the word "Order" val order = view.findViewById(R.id.order) // Set an OnClickListener on the TextView order.setOnClickListener { val action = OrderFragmentDirections.actionOrderFragmentToConfirmationFragment() view.findNavController().navigate(action) } return view } private fun incrementQuantity(quantity: TextView) { val currentQuantity = quantity.text.toString().toInt() quantity.text = (currentQuantity + 1).toString() } private fun decrementQuantity(quantity: TextView) { val currentQuantity = quantity.text.toString().toInt() if (currentQuantity > 0) { quantity.text = (currentQuantity - 1).toString() } } }

--------------------------

import androidx.navigation.ActionOnlyNavDirections import androidx.navigation.NavDirections class OrderFragmentDirections { companion object { fun actionOrderFragmentToConfirmationFragment(): NavDirections { return ActionOnlyNavDirections(R.id.action_orderFragment_to_confirmationFragment) } } }

--------------------------

import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel class OrderViewModel: ViewModel() { private val _totalCost = MutableLiveData() val totalCost: LiveData get() = _totalCost private val _walletBalance = MutableLiveData() val walletBalance: LiveData get() = _walletBalance fun updateTotalCost(cost: Double) { _totalCost.value = cost } fun updateWalletBalance(balance: Double) { _walletBalance.value = balance } }

-----------------------

import android.os.Bundle import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView class ReceiptFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val view = inflater.inflate(R.layout.fragment_receipt, container, false) val orderTotalTextView = view.findViewById(R.id.orderTotal ) val remainingBalanceTextView = view.findViewById(R.id.remainingBalance) val orderTotal = arguments?.getDouble("order_total") ?: 0.0 val remainingBalance = arguments?.getDouble("remaining_balance") ?: 0.0 // Set the text of the TextViews orderTotalTextView.text = "Order Total: $orderTotal" remainingBalanceTextView.text = "Remaining Balance: $remainingBalance" return view } }

-------------

activity_main.xml

image text in transcribed

----------

fragment_confirmation.xml

image text in transcribed

---------

fragment_order.xml

             

------------

fragment_receipt.xml

image text in transcribed

------

strings.xml

image text in transcribed

In this challenge, you will build a "wallet" app where you can buy apples and oranges. The app will have a MainActivity, which will have three fragments: - an order fragment - a receipt fragment - a confirmation fragment. Also, you will have a ViewModel . Initially, your wallet will have $25.99 in it. Oranges are $4.99 per pound and Apples are $2.49 per pound. You will need to create + and buttons for each (see layout example below), to specify how many pounds of each you want. The user should be able to specify how many pounds of apples and oranges they want, and order them. When the "Order" button is clicked, the app will take you to a confirmation page with the total cost of the apples and oranges you selected displayed in a textView, along with a Purchase button. Pressing the Purchase button completes the order, reducing the available funds by the cost of the order, and returning the user to the Order Screen. You can see the navigation map below. Use this to set up the navigation component in your app. When the user sees the receipt fragment, and then presses the back button, it takes them back to the original fragment, skipping the middle fragment. Review the documentation for the navigation component to understand how to do the skipping part. If the user attempts to purchase an order that costs more than their available balance, you should display a to the user indicating they have insufficient funds. The ViewModel is where are you will maintain the balance available in the wallet. These fragments talks to the ViewModel, then modify the ViewModel. But they all receive information from this ViewModel. LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android: layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> LinearLayout xmlns:android="http://schemas,android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> /LinearLayout>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions