Commit 928de6d8 authored by Muhammad Suryono's avatar Muhammad Suryono

Update code cart, dan utils

parent 53a73c15
......@@ -6,7 +6,7 @@ android {
defaultConfig {
applicationId "com.yono.messeripos"
minSdkVersion 23
minSdkVersion 22
targetSdkVersion 30
versionCode 1
versionName "1.0"
......
......@@ -51,7 +51,6 @@ public class CartActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
......@@ -63,6 +62,7 @@ public class CartActivity extends AppCompatActivity {
toolbars.setNavigationOnClickListener(view -> onBackPressed());
recyclerView = findViewById(R.id.rvCart);
cartAdapter = new CartAdapter();
LinearLayoutManager llm = new LinearLayoutManager(this);
recyclerView.setLayoutManager(llm);
......@@ -76,12 +76,36 @@ public class CartActivity extends AppCompatActivity {
@Override
public void onChanged(List<ProductCartModels> productCartModels) {
if (productCartModels != null){
cartAdapter = new CartAdapter(CartActivity.this, productCartModels);
cartAdapter.setCartAdapter(CartActivity.this, productCartModels);
recyclerView.setAdapter(cartAdapter);
}
}
});
cartAdapter.setListener(new CartAdapter.CartListener() {
@Override
public void onDelete(ProductCartModels productCartModels) {
modelsCart.deleteCart(productCartModels);
}
@Override
public void onUpdateQuantityPlus(ProductCartModels productCartModels) {
}
@Override
public void onUpdateQuantityMinus(ProductCartModels productCartModels) {
}
@Override
public void onCheckout(ProductCartModels productCartModels) {
}
});
//
// counter = Integer.parseInt(tvQtyView.getText().toString());
// prices = Integer.parseInt(tvPrice.getText().toString());
......
......@@ -17,6 +17,7 @@ import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
......@@ -122,12 +123,13 @@ public class FormProductActivity extends AppCompatActivity {
}
// if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
// Log.d("Get filepath photo", "" + file.getName());
// Log.d("Get filepath photo", "" + file.getAbsolutePath());
// binding.setPhoto(file.getAbsolutePath());
// productModels.setImageProduct(file.getName());
// }
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
......
......@@ -14,7 +14,9 @@ import com.google.gson.Gson;
import com.yono.messeripos.R;
import com.yono.messeripos.databinding.ItemCartBinding;
import com.yono.messeripos.models.CartModels;
import com.yono.messeripos.models.CategoryModels;
import com.yono.messeripos.models.ProductCartModels;
import com.yono.messeripos.utils.Utils;
import java.text.NumberFormat;
import java.util.ArrayList;
......@@ -26,8 +28,25 @@ public class CartAdapter extends RecyclerView.Adapter<CartAdapter.MyViewHolder>
private List<ProductCartModels> cartModels;
private int price;
Context context;
Utils utils = new Utils();
public CartAdapter(Context context, List<ProductCartModels> cartModels) {
public interface CartListener {
void onDelete(ProductCartModels productCartModels);
void onUpdateQuantityPlus(ProductCartModels productCartModels);
void onUpdateQuantityMinus(ProductCartModels productCartModels);
void onCheckout(ProductCartModels productCartModels);
}
private CartListener listener;
public void setListener(CartListener listener) {
this.listener = listener;
}
public void setCartAdapter(Context context, List<ProductCartModels> cartModels) {
this.cartModels = cartModels;
this.context = context;
// String gs = new Gson().toJson(cartModels);
......@@ -59,6 +78,7 @@ public class CartAdapter extends RecyclerView.Adapter<CartAdapter.MyViewHolder>
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private long subtotal = 0;
private ItemCartBinding binding;
public MyViewHolder(@NonNull ItemCartBinding binding) {
......@@ -68,14 +88,12 @@ public class CartAdapter extends RecyclerView.Adapter<CartAdapter.MyViewHolder>
public void bindData(ProductCartModels cartModels) {
binding.setCarts(cartModels);
// price = binding.getPrice();
binding.setPrice(utils.convertPrice("Rp. ", cartModels.getPrice_orders()));
}
subtotal = cartModels.getPrice_orders() * cartModels.getQuantity_orders();
private NumberFormat setToRp() {
Locale ID = new Locale("in", "ID");
binding.setSubTotal(utils.convertPrice("Rp. ", subtotal));
return NumberFormat.getCurrencyInstance(ID);
}
}
}
......@@ -12,6 +12,7 @@ import androidx.recyclerview.widget.RecyclerView;
import com.yono.messeripos.R;
import com.yono.messeripos.databinding.ItemCategoryBinding;
import com.yono.messeripos.models.CategoryModels;
import com.yono.messeripos.models.ProductModels;
import java.util.ArrayList;
......@@ -20,6 +21,17 @@ public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.MyView
private ArrayList<CategoryModels> categoryModels;
Context context;
public interface CategoryListener {
void onGetProduct(CategoryModels categoryModels);
}
private CategoryListener listener;
public void setListener(CategoryListener listener) {
this.listener = listener;
}
public void setCategoriAdapter(Context context, ArrayList<CategoryModels> categoryModels){
this.categoryModels = categoryModels;
this.context = context;
......@@ -58,12 +70,7 @@ public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.MyView
public void binData(CategoryModels categoryModels){
binding.setCategory(categoryModels);
binding.cvCategory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
binding.cvCategory.toggle();
}
});
binding.cvCategory.setOnClickListener(view -> listener.onGetProduct(categoryModels));
}
}
}
......@@ -28,6 +28,7 @@ import com.yono.messeripos.models.CartModels;
import com.yono.messeripos.models.MainViewModelsCart;
import com.yono.messeripos.models.ProductCartModels;
import com.yono.messeripos.models.ProductModels;
import com.yono.messeripos.utils.Utils;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
......@@ -37,6 +38,7 @@ public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHo
private ArrayList<ProductModels> productModels = new ArrayList<>();
Context context;
MainViewModelsCart mainViewModelsCart;
Utils utils = new Utils();
public interface ProductListener {
void onUpdate(ProductModels product);
......@@ -97,21 +99,15 @@ public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHo
public void bindData(ProductModels products) {
binding.setProducts(products);
DecimalFormat rupiah = (DecimalFormat) DecimalFormat.getCurrencyInstance();
DecimalFormatSymbols format = new DecimalFormatSymbols();
format.setCurrencySymbol("Rp. ");
format.setMonetaryDecimalSeparator(',');
format.setGroupingSeparator('.');
rupiah.setDecimalFormatSymbols(format);
binding.setPrice(rupiah.format(products.getPriceProduct()));
binding.setPrice(utils.convertPrice("Rp. ", products.getPriceProduct()));
binding.setImage(products.getImageProduct());
if (!MainActivity.status_update) {
binding.btnDelete.setVisibility(View.GONE);
binding.btnEdit.setVisibility(View.GONE);
}else{
binding.btnDelete.setVisibility(View.VISIBLE);
binding.btnEdit.setVisibility(View.VISIBLE);
}
if (products.getStockProduct() == 0) {
......
......@@ -2,14 +2,20 @@ package com.yono.messeripos.api.service;
import com.yono.messeripos.api.ApiHelper;
import com.yono.messeripos.models.CategoryModels;
import com.yono.messeripos.models.ProductModels;
import com.yono.messeripos.response.DataResponse;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;
public interface CategoryServise {
@GET(ApiHelper.VERSI_API_1+"categories")
Call<DataResponse<List<CategoryModels>>> getCategory();
@GET(ApiHelper.VERSI_API_1+"categories-join/{id}")
Call<DataResponse<List<CategoryModels<List<ProductModels>>>>> getCategoryById(@Path("id") int id);
}
......@@ -25,9 +25,12 @@ public interface CartDaos {
@Insert(onConflict = OnConflictStrategy.IGNORE)
public void insertProduct(ProductCartModels product);
@Update
@Update(onConflict = OnConflictStrategy.IGNORE)
public void updateProduct(ProductCartModels product);
@Query("UPDATE carts SET quantity_orders = :qty WHERE id = :idCart")
public void updateCart (long qty, long idCart);
@Delete
public void deleteProduct(ProductCartModels product);
......
......@@ -2,13 +2,16 @@ package com.yono.messeripos.models;
import com.google.gson.annotations.SerializedName;
public class CategoryModels {
public class CategoryModels<T> {
@SerializedName("id")
private int idCategory;
@SerializedName("name")
private String nameCategory;
@SerializedName("product")
private T productCategory;
public int getIdCategory() {
return idCategory;
}
......@@ -24,4 +27,12 @@ public class CategoryModels {
public void setNameCategory(String nameCategory) {
this.nameCategory = nameCategory;
}
public T getProductCategory() {
return productCategory;
}
public void setProductCategory(T productCategory) {
this.productCategory = productCategory;
}
}
......@@ -2,6 +2,7 @@ package com.yono.messeripos.models;
import android.util.Log;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
......@@ -21,6 +22,7 @@ import retrofit2.Response;
public class MainViewModels extends ViewModel {
MutableLiveData<DataResponse<List<ProductModels<CategoryModels>>>> productList;
MutableLiveData<List<CategoryModels<List<ProductModels>>>> productByCategory;
MutableLiveData<DataResponse<List<CategoryModels>>> categoryList;
MutableLiveData<DataResponse<UsersModels>> usersLogin;
......@@ -29,18 +31,45 @@ public class MainViewModels extends ViewModel {
public MainViewModels(){
productList = new MutableLiveData<>();
categoryList = new MutableLiveData<>();
productByCategory = new MutableLiveData<>();
}
public MutableLiveData<DataResponse<List<ProductModels<CategoryModels>>>> getProduct(){
public LiveData<DataResponse<List<ProductModels<CategoryModels>>>> getProduct(){
getProductList();
return productList;
}
public MutableLiveData<DataResponse<List<CategoryModels>>> getCategory(){
public LiveData<DataResponse<List<CategoryModels>>> getCategory(){
getCategoryList();
return categoryList;
}
public LiveData<List<CategoryModels<List<ProductModels>>>> getProductByCategory(int id){
getDataProductByCategory(id);
return productByCategory;
}
private void getDataProductByCategory(int id) {
CategoryServise categoryServise = client.Client(CategoryServise.class);
categoryServise.getCategoryById(id).enqueue(new Callback<DataResponse<List<CategoryModels<List<ProductModels>>>>>() {
@Override
public void onResponse(Call<DataResponse<List<CategoryModels<List<ProductModels>>>>> call,
Response<DataResponse<List<CategoryModels<List<ProductModels>>>>> response) {
String js = new Gson().toJson(response.body());
Log.i("Get data by category", "onResponse: "+js);
if (response != null){
productByCategory.setValue(response.body().getData());
}
}
@Override
public void onFailure(Call<DataResponse<List<CategoryModels<List<ProductModels>>>>> call, Throwable t) {
Log.e("Error!", "onFailure: ", t);
}
});
}
public MutableLiveData<DataResponse<UsersModels>> checkLogin(UsersModels usersModels){
usersLogin = new MutableLiveData<>();
PeriksaLogin(usersModels);
......@@ -67,25 +96,24 @@ public class MainViewModels extends ViewModel {
});
}
private void getProductList() {
ProductService productService = client.Client(ProductService.class);
productService.getProducts().enqueue(new Callback<DataResponse<List<ProductModels<CategoryModels>>>>() {
@Override
public void onResponse(Call<DataResponse<List<ProductModels<CategoryModels>>>> call,
Response<DataResponse<List<ProductModels<CategoryModels>>>> response) {
public void onResponse(Call<DataResponse<List<ProductModels<CategoryModels>>>> call, Response<DataResponse<List<ProductModels<CategoryModels>>>> response) {
if (response.body() != null){
productList.setValue(response.body());
}else{
productList.setValue(null);
}
String js = new Gson().toJson(response.body());
Log.d("Get Data", "Response "+js);
}
@Override
public void onFailure(Call<DataResponse<List<ProductModels<CategoryModels>>>> call, Throwable t) {
Log.e("Error get product", "Response "+t.getMessage());
}
});
}
......
......@@ -42,6 +42,8 @@ public class MainViewModelsCart extends AndroidViewModel {
public void deleteCart(ProductCartModels productCartModels){cartRepositories.delete(productCartModels);}
public void updateQtyCart(long qty, long id){cartRepositories.updateQty(qty, id);}
public LiveData<List<ProductCartModels>> getCartById(long id){
return cartRepositories.getDataById(id);
}
......
......@@ -32,6 +32,10 @@ public class ProductCartModels implements Parcelable {
@SerializedName("grand_total")
private int grand_total_orders;
public ProductCartModels(){
}
public ProductCartModels(int id_orders,
long quantity_orders,
int id_product_orders,
......
......@@ -35,6 +35,10 @@ public class CartRepositories {
LocalDatabase.databaseWriterExecutor.execute(() -> cartDaos.deleteProduct(productCartModels));
}
public void updateQty(long qty, long id){
LocalDatabase.databaseWriterExecutor.execute(() ->cartDaos.updateCart(qty, id));
}
public LiveData<List<ProductCartModels>> getDataById(long id){
return cartDaos.getCartById(id);
}
......
package com.yono.messeripos.utils;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
public class Utils {
public String convertPrice(String mataUang, int nilai ){
DecimalFormat rupiah = (DecimalFormat) DecimalFormat.getCurrencyInstance();
DecimalFormatSymbols format = new DecimalFormatSymbols();
format.setCurrencySymbol(mataUang);
format.setMonetaryDecimalSeparator(',');
format.setGroupingSeparator('.');
rupiah.setDecimalFormatSymbols(format);
return rupiah.format(nilai);
}
public String convertPrice(String mataUang, long nilai ){
DecimalFormat rupiah = (DecimalFormat) DecimalFormat.getCurrencyInstance();
DecimalFormatSymbols format = new DecimalFormatSymbols();
format.setCurrencySymbol(mataUang);
format.setMonetaryDecimalSeparator(',');
format.setGroupingSeparator('.');
rupiah.setDecimalFormatSymbols(format);
return rupiah.format(nilai);
}
}
......@@ -14,6 +14,8 @@
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/topBar"
android:layout_width="match_parent"
......@@ -22,9 +24,20 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:paddingTop="20dp"
android:paddingStart="15dp"
tools:ignore="RtlSymmetry">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/appBarUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="0dp"
android:paddingTop="0dp"
android:background="@drawable/gradient"
style="@style/Widget.MaterialComponents.Toolbar.Primary"
xmlns:android="http://schemas.android.com/apk/res/android">
</com.google.android.material.appbar.MaterialToolbar>
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/topAppBar"
android:layout_width="match_parent"
......@@ -59,6 +72,25 @@
android:layout_marginTop="10dp"
tools:listitem="@layout/item_category" />
<ImageView
android:id="@+id/imgEmpty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:src="@drawable/no"
app:layout_constraintTop_toBottomOf="@id/rvCategory"/>
<ProgressBar
android:id="@+id/pbLoading"
android:indeterminateTint="@color/colorAccent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorAccent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<!-- Scrollable content -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvProduct"
......
......@@ -17,7 +17,7 @@
type="String" />
<variable
name="product_name"
name="subTotal"
type="String" />
</data>
......@@ -88,7 +88,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:text="@string/hint_price"
android:text="@{subTotal}"
android:textFontWeight="600"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="@id/tvQuantity"
......
......@@ -19,6 +19,9 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="#2041B8"
android:clickable="true"
android:focusable="true"
android:checkable="true"
app:cardCornerRadius="40dp">
......
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