Commit 0020fa87 authored by Ahmad Abi Mulya's avatar Ahmad Abi Mulya

change payment layout with recycler view

parent 3e4baf10
...@@ -2,9 +2,10 @@ package com.example.yourcashiertest.activities; ...@@ -2,9 +2,10 @@ package com.example.yourcashiertest.activities;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.databinding.DataBindingUtil; import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Intent; import android.content.Intent;
...@@ -14,20 +15,25 @@ import android.os.Bundle; ...@@ -14,20 +15,25 @@ import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.View; import android.view.View;
import com.example.yourcashiertest.R; import com.example.yourcashiertest.R;
import com.example.yourcashiertest.adapters.PaymentAdapter;
import com.example.yourcashiertest.databinding.ActivityPaymentBinding; import com.example.yourcashiertest.databinding.ActivityPaymentBinding;
import com.example.yourcashiertest.models.ResponsePayment;
import com.example.yourcashiertest.utils.Convert; import com.example.yourcashiertest.utils.Convert;
import com.example.yourcashiertest.utils.ViewUtil; import com.example.yourcashiertest.utils.ViewUtil;
import com.example.yourcashiertest.viewmodels.CartViewModel; import com.example.yourcashiertest.viewmodels.CartViewModel;
import java.util.ArrayList;
public class PaymentActivity extends AppCompatActivity { public class PaymentActivity extends AppCompatActivity {
ActivityPaymentBinding binding; ActivityPaymentBinding binding;
public static String paymentMethod = "";
public static String REFUND = "refund"; public static String REFUND = "refund";
public int refund; public int refund;
public int amount; public int amount;
CartViewModel cartViewModel; CartViewModel cartViewModel;
public String payment = "";
public static final String PAY_WITH = "pay_with"; public static final String PAY_WITH = "pay_with";
private ArrayList<ResponsePayment> responsePayments = new ArrayList<>();
@SuppressLint("ResourceAsColor") @SuppressLint("ResourceAsColor")
@RequiresApi(api = Build.VERSION_CODES.N) @RequiresApi(api = Build.VERSION_CODES.N)
...@@ -40,36 +46,23 @@ public class PaymentActivity extends AppCompatActivity { ...@@ -40,36 +46,23 @@ public class PaymentActivity extends AppCompatActivity {
amount = getIntent().getIntExtra(CartActivity.PRICE, 0); amount = getIntent().getIntExtra(CartActivity.PRICE, 0);
binding.setAmount(Convert.changeToCurrency(amount)); binding.setAmount(Convert.changeToCurrency(amount));
// binding.rvPayment.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
responsePayments.add(new ResponsePayment("https://i.ibb.co/yfcFMHc/undraw-wallet-aym5.png", "Cash"));
responsePayments.add(new ResponsePayment("https://i.ibb.co/qNnKh5n/undraw-Credit-card-re-blml.png", "Card"));
binding.cbCash.setOnClickListener(v -> { PaymentAdapter adapter = new PaymentAdapter(responsePayments, this);
if (binding.getCheckCash()){ binding.rvPayment.setAdapter(adapter);
binding.setCheckCash(false);
binding.cvCash.setCardBackgroundColor(0);
payment = "";
}else {
binding.setCheckCash(true);
binding.cvCash.setCardBackgroundColor(R.color.blue);
payment = "Cash";
}
});
binding.cbCard.setOnClickListener(v -> {
if (binding.getCheckCard()){
binding.setCheckCard(false);
binding.cvCard.setCardBackgroundColor(0);
payment = "";
}else {
binding.setCheckCard(true);
binding.cvCard.setCardBackgroundColor(R.color.blue);
payment = "Card";
}
});
adapter.setListener(responsePayments -> {
paymentMethod = responsePayments.getBank();
binding.btnPay.setEnabled(true);
});
} }
public void btnPay(View view) { public void btnPay(View view) {
if (TextUtils.isEmpty(binding.etAmountPaid.getText().toString())){ if (TextUtils.isEmpty(binding.etAmountPaid.getText().toString())){
ViewUtil.showMessage(view, "Please input Amount Paid by Customer!"); ViewUtil.showMessage(view, "Please input Amount Paid by Customer!");
}else if (payment.equals("")){ }else if (paymentMethod.equals("")){
ViewUtil.showMessage(view, "Please choose type payment"); ViewUtil.showMessage(view, "Please choose type payment");
} else { } else {
int amountPaid = Integer.parseInt(binding.etAmountPaid.getText().toString()); int amountPaid = Integer.parseInt(binding.etAmountPaid.getText().toString());
...@@ -77,15 +70,13 @@ public class PaymentActivity extends AppCompatActivity { ...@@ -77,15 +70,13 @@ public class PaymentActivity extends AppCompatActivity {
ViewUtil.showMessage(view, "AmountPaid doesn't less than total price"); ViewUtil.showMessage(view, "AmountPaid doesn't less than total price");
} else { } else {
refund = amountPaid - amount; refund = amountPaid - amount;
startActivity(new Intent(PaymentActivity.this, StatusPayment.class).putExtra(REFUND, refund).putExtra(PAY_WITH, payment)); startActivity(new Intent(PaymentActivity.this, StatusPayment.class).putExtra(REFUND, refund).putExtra(PAY_WITH, paymentMethod));
cartViewModel.clear(); cartViewModel.clear();
finish(); finish();
} }
} }
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
......
...@@ -15,7 +15,6 @@ import android.os.Environment; ...@@ -15,7 +15,6 @@ import android.os.Environment;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.example.yourcashiertest.R; import com.example.yourcashiertest.R;
import com.example.yourcashiertest.databinding.ActivityProductBinding; import com.example.yourcashiertest.databinding.ActivityProductBinding;
import com.example.yourcashiertest.entities.Product; import com.example.yourcashiertest.entities.Product;
......
...@@ -19,7 +19,6 @@ import java.util.List; ...@@ -19,7 +19,6 @@ import java.util.List;
public class CartAdapter extends RecyclerView.Adapter<CartAdapter.ViewHolder> { public class CartAdapter extends RecyclerView.Adapter<CartAdapter.ViewHolder> {
private List<Cart> carts = new ArrayList<>(); private List<Cart> carts = new ArrayList<>();
public interface CartListener{ public interface CartListener{
void onIncrease(Cart cart); void onIncrease(Cart cart);
void onDecrease(Cart cart); void onDecrease(Cart cart);
......
package com.example.yourcashiertest.adapters;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import com.example.yourcashiertest.R;
import com.example.yourcashiertest.activities.PaymentActivity;
import com.example.yourcashiertest.databinding.PaymentItemBinding;
import com.example.yourcashiertest.models.ResponsePayment;
import java.util.ArrayList;
public class PaymentAdapter extends RecyclerView.Adapter<PaymentAdapter.ViewHolder> {
private ArrayList<ResponsePayment> responsePayments;
Context context;
int getPosition = -1;
public interface PaymentListener {
void onSelectPayment(ResponsePayment responsePayment);
}
private PaymentListener listener;
public void setListener(PaymentListener listener) {
this.listener = listener;
}
public PaymentAdapter(ArrayList<ResponsePayment> responsePayments, PaymentActivity paymentActivity) {
this.responsePayments = responsePayments;
this.context = paymentActivity;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
PaymentItemBinding paymentItemBinding = DataBindingUtil.inflate(
LayoutInflater.from(parent.getContext()),
R.layout.payment_item,
parent,
false
);
return new ViewHolder(paymentItemBinding);
}
@Override
public void onBindViewHolder (@NonNull PaymentAdapter.ViewHolder holder,int position){
holder.binData(responsePayments.get(position));
holder.radioButton.setChecked(position == getPosition);
}
@Override
public int getItemCount () {
return responsePayments.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
PaymentItemBinding binding;
RadioButton radioButton;
public ViewHolder(@NonNull PaymentItemBinding itemView) {
super(itemView.getRoot());
this.binding = itemView;
this.radioButton = itemView.rbPayment;
}
@SuppressLint("ResourceAsColor")
public void binData(ResponsePayment responsePayment){
binding.setPayment(responsePayment);
binding.setLogo(responsePayment.getUrl());
binding.cvPayment.setOnClickListener(view ->{
getPosition = getAdapterPosition();
notifyDataSetChanged();
listener.onSelectPayment(responsePayment);});
}
}
}
package com.example.yourcashiertest.models;
import android.widget.ImageView;
import androidx.databinding.BindingAdapter;
import com.bumptech.glide.Glide;
public class ResponsePayment {
private String url;
private String bank;
private boolean isSelected;
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
public ResponsePayment(String url, String bank) {
this.url = url;
this.bank = bank;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
public String getBank() {
return bank;
}
public void setBank(String bank) {
this.bank = bank;
}
@BindingAdapter("img_bank")
public static void loadImage(ImageView view, String url) {
Glide.with(view.getContext())
.load(url)
.into(view);
}
}
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
android:gravity="top" android:gravity="top"
android:text="@string/payment" android:text="@string/payment"
android:textAppearance="@style/payment" android:textAppearance="@style/payment"
app:layout_constraintBottom_toTopOf="@id/cvCash" app:layout_constraintBottom_toTopOf="@id/rvPayment"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0" app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
...@@ -47,105 +47,37 @@ ...@@ -47,105 +47,37 @@
android:gravity="center" android:gravity="center"
android:text="@{amount}" android:text="@{amount}"
android:textSize="24dp" android:textSize="24dp"
app:layout_constraintBottom_toTopOf="@id/rvPayment"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.902" app:layout_constraintHorizontal_bias="0.902"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvPayment" app:layout_constraintTop_toBottomOf="@id/tvPayment"
app:layout_constraintVertical_bias="0.0" /> app:layout_constraintVertical_bias="0.0" />
<com.google.android.material.card.MaterialCardView
android:id="@+id/cvCash"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:clickable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvTotal">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:paddingVertical="5dp">
<ImageView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/iv_cash" android:id="@+id/rvPayment"
android:layout_width="150dp"
android:layout_height="0dp"
android:layout_marginStart="4dp"
android:src="@drawable/ic_undraw_wallet"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<CheckBox
android:id="@+id/cbCash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:buttonTint="@color/black"
android:text="Cash"
android:checked="@{checkCash}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/cvCard"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="100dp" android:layout_height="wrap_content"
android:clickable="true" android:padding="@dimen/space_small"
android:layout_marginTop="20dp" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/tvAmountPaid"
app:layout_constraintHorizontal_bias="0.666"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cvCash" tools:listitem="@layout/payment_item" />
app:layout_constraintVertical_bias="0.04000002">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingVertical="5dp">
<ImageView
android:id="@+id/iv_card"
android:layout_width="150dp"
android:layout_height="0dp"
android:src="@drawable/ic_undraw_credit_card"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/cbCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:buttonTint="@color/black"
android:text="Card"
android:checked="@{checkCard}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<TextView <TextView
android:id="@+id/tvAmountPaid" android:id="@+id/tvAmountPaid"
android:layout_width="100dp" android:layout_width="100dp"
android:layout_height="30dp" android:layout_height="30dp"
android:layout_marginTop="208dp" android:layout_marginTop="208dp"
android:layout_marginBottom="70dp" android:layout_marginBottom="20dp"
android:gravity="start" android:gravity="start"
android:text="Amount paid" android:text="Amount paid"
app:layout_constraintBottom_toTopOf="@id/etAmountPaid" app:layout_constraintBottom_toTopOf="@id/etAmountPaid"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0" app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent" />
app:layout_constraintTop_toBottomOf="@id/cvCard" />
<EditText <EditText
android:id="@+id/etAmountPaid" android:id="@+id/etAmountPaid"
......
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="logo"
type="String" />
<variable
name="payment"
type="com.example.yourcashiertest.models.ResponsePayment" />
</data>
<com.google.android.material.card.MaterialCardView
android:id="@+id/cvPayment"
android:clickable="true"
android:focusable="true"
android:checkable="true"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginVertical="10dp"
android:layout_marginHorizontal="2dp"
app:layout_constraintTop_toTopOf="parent"
app:strokeWidth="1dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp">
<ImageView
android:id="@+id/ivLogo"
android:layout_width="120dp"
android:layout_height="100dp"
android:src="@drawable/ic_undraw_wallet"
app:img_bank="@{logo}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<RadioButton
android:id="@+id/rbPayment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/colorPrimary"
android:enabled="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</layout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment