Commit 088d0930 authored by iman Fauzi's avatar iman Fauzi

fix conflict

parents 4a2b3a49 e6e8c712
package com.yono.messeripos;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
......@@ -14,10 +16,16 @@ import android.widget.TextView;
import androidx.annotation.Nullable;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.yono.messeripos.api.client.Client;
import com.yono.messeripos.api.service.PaymentService;
import com.yono.messeripos.models.PaymentsModels;
import com.yono.messeripos.response.DataResponse;
import com.yono.messeripos.utils.Utils;
import okhttp3.internal.Util;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class BottomSheets extends BottomSheetDialogFragment {
......@@ -26,6 +34,8 @@ public class BottomSheets extends BottomSheetDialogFragment {
Button btnCash;
Utils utils;
String defaultText = "0";
Client client = new Client();
public static long grossAmount;
PaymentsModels paymentsModels;
public BottomSheets(PaymentsModels paymentsModels) {
......@@ -43,7 +53,6 @@ public class BottomSheets extends BottomSheetDialogFragment {
hargaChange = v.findViewById(R.id.hargaChange);
btnCash = v.findViewById(R.id.btnCash);
totalHarga = v.findViewById(R.id.totalHarga);
utils = new Utils();
long ga = paymentsModels.getGrossAmount();
......@@ -62,7 +71,7 @@ public class BottomSheets extends BottomSheetDialogFragment {
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (TextUtils.isEmpty(charSequence)) inputAmount.setText("0");
long grossAmount = paymentsModels.getGrossAmount();
grossAmount = paymentsModels.getGrossAmount();
long input = Long.parseLong(inputAmount.getText().toString());
long result = input-grossAmount;
......@@ -76,6 +85,34 @@ public class BottomSheets extends BottomSheetDialogFragment {
}
});
btnCash.setOnClickListener(view -> bayar());
return v;
}
private void bayar() {
// String orderId, String paymentType, String bank, long grossAmount
PaymentsModels paymentsModels = new PaymentsModels(
MainActivity.orderId,
"cash",
"none",
grossAmount
);
PaymentService paymentService = client.Client(PaymentService.class);
paymentService.postFromCart(paymentsModels).enqueue(new Callback<DataResponse<PaymentsModels>>() {
@Override
public void onResponse(Call<DataResponse<PaymentsModels>> call, Response<DataResponse<PaymentsModels>> response) {
if (response.body() != null){
}
}
@Override
public void onFailure(Call<DataResponse<PaymentsModels>> call, Throwable t) {
}
});
}
}
package com.yono.messeripos;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
......@@ -12,6 +14,7 @@ import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
......@@ -22,15 +25,27 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textview.MaterialTextView;
import com.yono.messeripos.adapter.CartAdapter;
import com.yono.messeripos.api.client.Client;
import com.yono.messeripos.api.service.PaymentService;
import com.yono.messeripos.models.MainViewModelsCart;
import com.yono.messeripos.models.PaymentsModels;
import com.yono.messeripos.models.ProductCartModels;
import com.yono.messeripos.response.DataResponse;
import com.yono.messeripos.response.OrdersResponse;
import com.yono.messeripos.utils.Utils;
import java.util.List;
import java.util.Objects;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import static com.yono.messeripos.MainActivity.CHECKOUT;
import static com.yono.messeripos.MainActivity.ORDER_ID;
import static com.yono.messeripos.MainActivity.orderId;
public class CartActivity extends AppCompatActivity {
......@@ -41,21 +56,39 @@ public class CartActivity extends AppCompatActivity {
List<ProductCartModels> dataCart = null;
public static Boolean statusPost;
SharedPreferences sharedPreferences;
Button btnCheckout;
ImageView cartEmptyImage;
MaterialTextView cartEmptyText;
public static MaterialTextView grandToral;
Utils utils = new Utils();
MaterialButton btnSaveCart;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
Button btnCheckout;
grandToral = findViewById(R.id.tvHarga);
btnSaveCart = findViewById(R.id.btSave);
modelsCart = new ViewModelProvider(this).get(MainViewModelsCart.class);
// actionSaveCart();
btnSaveCart.setOnClickListener(view -> actionSaveCart());
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
sharedPreferences = getSharedPreferences(SplashScreen.MY_SHARED_PREFERENCES, Context.MODE_PRIVATE);
Log.i("Id Order on acart", "onCreate: "+orderId);
if (orderId.isEmpty()){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(ORDER_ID, "MES-ORDER"+utils.timeStamp());
if (editor.commit()){
Toast.makeText(CartActivity.this, orderId, Toast.LENGTH_LONG).show();
}
}
// Toolbars
MaterialToolbar toolbars = findViewById(R.id.cAppBar);
setSupportActionBar(toolbars);
......@@ -82,7 +115,7 @@ public class CartActivity extends AppCompatActivity {
modelsCart = new ViewModelProvider(this).get(MainViewModelsCart.class);
modelsCart.getCartByOrderId(orderId).observe(this, new Observer<List<ProductCartModels>>() {
modelsCart.getCartById(orderId).observe(this, new Observer<List<ProductCartModels>>() {
@Override
public void onChanged(List<ProductCartModels> productCartModels) {
if (productCartModels != null) {
......@@ -94,6 +127,10 @@ public class CartActivity extends AppCompatActivity {
});
// grandToral.setText(utils.convertPrice("Rp. ", utils.grandTotal));
cartAdapter.setListener(new CartAdapter.CartListener() {
@Override
public void onDelete(ProductCartModels productCartModels) {
......@@ -147,4 +184,54 @@ public class CartActivity extends AppCompatActivity {
});
}
private void actionSaveCart() {
Client client = new Client();
modelsCart.getCartById(orderId).observe(this, new Observer<List<ProductCartModels>>() {
@Override
public void onChanged(List<ProductCartModels> productCartModels) {
long grossAmount = 0;
PaymentsModels paymentsModels = new PaymentsModels();
for (ProductCartModels pd:productCartModels){
grossAmount = grossAmount + (pd.getQuantity_orders()*pd.getPrice_orders());
paymentsModels.setGrossAmount(grossAmount);
}
paymentsModels.setBank("none");
paymentsModels.setPaymentType("pending");
paymentsModels.setOrderId(orderId);
paymentsModels.setTransactionStatus("pending");
Log.i("Convert Input", "onChanged: "+utils.convertGson(paymentsModels));
PaymentService paymentService = client.Client(PaymentService.class);
paymentService.postFromCart(paymentsModels).enqueue(new Callback<DataResponse<PaymentsModels>>() {
@Override
public void onResponse(Call<DataResponse<PaymentsModels>> call, Response<DataResponse<PaymentsModels>> response) {
Log.i("Response simpan_cart", "onResponse: "+utils.convertGson(response.body()));
if (response.body() != null){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(ORDER_ID, "");
if (editor.commit()){
startActivity(new Intent(CartActivity.this, MainActivity.class));
finish();
}
}else{
new AlertDialog.Builder(CartActivity.this)
.setCancelable(false)
.setTitle("Save nwe Order")
.setMessage("Erorr save!").show();
}
}
@Override
public void onFailure(Call<DataResponse<PaymentsModels>> call, Throwable t) {
new AlertDialog.Builder(CartActivity.this)
.setCancelable(false)
.setTitle("Save nwe Order")
.setMessage("Add new order failed, try again!").show();
}
});
}
});
}
}
\ No newline at end of file
......@@ -2,8 +2,10 @@ package com.yono.messeripos;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.CursorLoader;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
......@@ -12,22 +14,17 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.PersistableBundle;
import android.provider.MediaStore;
import android.provider.Settings;
import android.util.Base64;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
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;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
......@@ -46,20 +43,14 @@ import com.yono.messeripos.utils.Utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.http.Multipart;
public class FormProductActivity extends AppCompatActivity {
......@@ -72,24 +63,8 @@ public class FormProductActivity extends AppCompatActivity {
private static final int REQUEST_IMAGE_CAPTURE = 1;
private static final int REQUEST_PERMISSIONS = 448;
private boolean isUpdate = false;
String kondisi_spin;
String locationPath = null;
private static final String[] ANDROID_VERSIONS = {
"Cupcake",
"Donut",
"Eclair",
"Froyo",
"Gingerbread",
"Honeycomb",
"Ice Cream Sandwich",
"Jelly Bean",
"KitKat",
"Lollipop",
"Marshmallow",
"Nougat",
"Oreo"
};
int idCategorySelected = 0;
private String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
......@@ -115,12 +90,30 @@ public class FormProductActivity extends AppCompatActivity {
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
ProductModels productModelsIntent = getIntent().getParcelableExtra(MainActivity.DATA_PRODUCT);
MaterialToolbar toolbars = findViewById(R.id.topAppBarForm);
setSupportActionBar(toolbars);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("Add Product");
toolbars.setNavigationOnClickListener(view -> onBackPressed());
if (getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("Add Product");
toolbars.setNavigationOnClickListener(view -> onBackPressed());
}
if (productModelsIntent != null){
isUpdate = true;
productModels.setProductName(productModelsIntent.getProductName());
productModels.setPriceProduct(productModelsIntent.getPriceProduct());
productModels.setStockProduct(productModelsIntent.getStockProduct());
productModels.setImageProduct(productModelsIntent.getImageProduct());
binding.btnAdd.setText("UPDATE PRODUCT");
}else{
productModelsIntent = new ProductModels("",0,0, utils.convertImageBase64Resource(getResources(), R.drawable.logo_32));
}
binding.cvProduct.setOnClickListener(view -> openFile());
binding.btnAdd.setOnClickListener(view -> saveProduct());
......@@ -129,7 +122,29 @@ public class FormProductActivity extends AppCompatActivity {
categoryModels = new ArrayList<>();
categoryModels.add("SELECT CATEGORY");
setSpinner();
setSelectedSpinner();
}
private void setSelectedSpinner() {
binding.listCategory.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
@Override
public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
utils.toastMessage(FormProductActivity.this, ""+position);
Log.i("Object", "onItemSelected: "+item);
mainViewModels.getCategory().observe(FormProductActivity.this, new Observer<DataResponse<List<CategoryModels>>>() {
@Override
public void onChanged(DataResponse<List<CategoryModels>> listDataResponse) {
List<CategoryModels> cat = listDataResponse.getData();
for (CategoryModels cM : cat){
if (cM.getNameCategory() == item){
idCategorySelected = cM.getIdCategory();
}
}
}
});
}
});
}
private void setSpinner() {
......@@ -159,9 +174,44 @@ public class FormProductActivity extends AppCompatActivity {
File fileImage = new File(locationPath);
Log.i("save", "saveProduct: "+locationPath);
if (fileImage.exists()){
progressDialog.show();
// Toast.makeText(this, productModels.getImageProduct()+" "+productModels.getProductName(), Toast.LENGTH_LONG).show();
utils.toastMessage(FormProductActivity.this, utils.convertGson(productModels));
if (
!productModels.getProductName().isEmpty() ||
productModels.getPriceProduct() != 0 ||
productModels.getStockProduct() != 0
){
progressDialog.show();
ProductService productService = client.Client(ProductService.class);
Log.i("data_post", "saveProduct: "+utils.convertGson(productModels));
productService.postProduct(productModels).enqueue(new Callback<DataResponse<ProductModels>>() {
@Override
public void onResponse(Call<DataResponse<ProductModels>> call, Response<DataResponse<ProductModels>> response) {
Log.i("Success Post", "onResponse: "+utils.convertGson(response.body()));
if (response.body() != null){
new AlertDialog.Builder(FormProductActivity.this)
.setCancelable(false)
.setTitle("Add New Product")
.setMessage("Add new product success")
.setPositiveButton("Oke", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
startActivity(new Intent(FormProductActivity.this, MainActivity.class));
finish();
}
}).show();
}else{
new AlertDialog.Builder(FormProductActivity.this)
.setTitle("Add New Product")
.setMessage("Add new product failed, try again!").show();
}
progressDialog.dismiss();
}
@Override
public void onFailure(Call<DataResponse<ProductModels>> call, Throwable t) {
t.printStackTrace();
}
});
}
}
}
......@@ -211,16 +261,14 @@ public class FormProductActivity extends AppCompatActivity {
if (requestCode == 100 && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
binding.setPhoto(getRealPathFromURI(selectedImage));
binding.setPhoto(utils.convertImageBase64File(getRealPathFromURI(selectedImage)));
locationPath = getRealPathFromURI(selectedImage);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] imageBytes = baos.toByteArray();
imageBytes = Base64.decode(utils.convertImageBase64File(getRealPathFromURI(selectedImage)), Base64.DEFAULT);
Bitmap decodeImages = BitmapFactory.decodeByteArray(imageBytes,0,imageBytes.length);
binding.ivProduct.setImageBitmap(decodeImages);
productModels.setImageProduct(utils.convertImageBase64File(getRealPathFromURI(selectedImage)));
binding.ivProduct.setImageBitmap(utils.decodeImageBase64(utils.convertImageBase64File(getRealPathFromURI(selectedImage))));
productModels.setImageProduct(getRealPathFromURI(selectedImage));
// utils.convertImageBase64File(getRealPathFromURI(selectedImage))
}
}
......
......@@ -65,8 +65,11 @@ public class MainActivity extends AppCompatActivity {
public static Boolean checkout;
public static Boolean selectCategory = false;
public static String orderId;
private static final String ORDER_ID = "orderId";
public static final String ORDER_ID = "orderId";
Client client = new Client();
public static ArrayList<ProductModels> productModelsArrayList = new ArrayList<>();
public static final String DATA_PRODUCT = "DATA_PRODUCT";
ProgressDialog progressDialog;
@SuppressLint("LongLogTag")
@Override
......@@ -86,6 +89,12 @@ public class MainActivity extends AppCompatActivity {
sharedPreferences = getSharedPreferences(SplashScreen.MY_SHARED_PREFERENCES, Context.MODE_PRIVATE);
checkout = sharedPreferences.getBoolean(CHECKOUT,false);
orderId = sharedPreferences.getString(ORDER_ID,"");
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading...");
Log.i("ORDER ID", "onCreate: "+orderId);
toolbar = findViewById(R.id.topAppBar);
mainViewModels = ViewModelProviders.of(this).get(MainViewModels.class);
if (orderId.isEmpty())
{
......@@ -96,33 +105,22 @@ public class MainActivity extends AppCompatActivity {
}
}
Log.i("ORDER ID", "onCreate: "+orderId);
toolbar = findViewById(R.id.topAppBar);
mainViewModels = ViewModelProviders.of(this).get(MainViewModels.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
binding.etSearchProduct.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
productAdapter.getFilter().filter(charSequence);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
functionSearch();
toolbarSetOnItemClicked();
mainViewModelsGettCategory();
imageEmptyGetProducts();
mainViewModelsGetProduct();
productAdapterListener();
categoryAdapterListener();
}
private void toolbarSetOnItemClicked() {
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
......@@ -155,7 +153,10 @@ public class MainActivity extends AppCompatActivity {
return true;
}
});
}
private void mainViewModelsGettCategory() {
mainViewModels.getCategory().observe(this, new Observer<DataResponse<List<CategoryModels>>>() {
@Override
public void onChanged(DataResponse<List<CategoryModels>> listDataResponse) {
......@@ -179,7 +180,9 @@ public class MainActivity extends AppCompatActivity {
Log.d("Get data from category", "Response " + js);
}
});
}
private void imageEmptyGetProducts() {
binding.imgEmpty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
......@@ -213,14 +216,17 @@ public class MainActivity extends AppCompatActivity {
});
}
});
}
private void mainViewModelsGetProduct() {
progressDialog.show();
mainViewModels.getProduct().observe(this, new Observer<DataResponse<List<ProductModels<CategoryModels>>>>() {
@Override
public void onChanged(DataResponse<List<ProductModels<CategoryModels>>> listDataResponse) {
if (listDataResponse != null) {
progressDialog.dismiss();
if (listDataResponse.getData().size() != 0) {
List<ProductModels<CategoryModels>> productModels = listDataResponse.getData();
ArrayList<ProductModels> productModelsArrayList = new ArrayList<>();
productModelsArrayList.clear();
productModelsArrayList.addAll(productModels);
showLoading(false);
if (productModelsArrayList.isEmpty()){
......@@ -234,25 +240,28 @@ public class MainActivity extends AppCompatActivity {
}
}
String js = new Gson().toJson(listDataResponse);
Log.d("Get data from products", "Response " + js);
}
});
}
private void productAdapterListener() {
productAdapter.setListener(new ProductAdapter.ProductListener() {
@Override
public void onUpdate(ProductModels product) {
startActivity(new Intent(MainActivity.this,
FormProductActivity.class).putExtra(DATA_PRODUCT, product));
}
@Override
public void onDelete(ProductModels product) {
public void onDelete(ProductModels product, int position) {
ProductService productService = client.Client(ProductService.class);
productService.deleteProducts(product.getIdProduct()).enqueue(new Callback<DataResponse<ProductModels>>() {
@Override
public void onResponse(Call<DataResponse<ProductModels>> call, Response<DataResponse<ProductModels>> response) {
productModelsArrayList.remove(position);
binding.rvProduct.removeViewAt(position);
productAdapter.notifyItemRemoved(position);
productAdapter.notifyItemRangeChanged(position, productModelsArrayList.size());
}
@Override
......@@ -264,7 +273,7 @@ public class MainActivity extends AppCompatActivity {
@Override
public void onAddCart(ProductModels product) {
mainViewModelsCart.getCartById(product.getIdProduct()).observe(MainActivity.this, new Observer<List<ProductCartModels>>() {
mainViewModelsCart.checkOrderProduct(product.getIdProduct(), orderId).observe(MainActivity.this, new Observer<List<ProductCartModels>>() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onChanged(List<ProductCartModels> productCartModels) {
......@@ -293,9 +302,9 @@ public class MainActivity extends AppCompatActivity {
});
}
});
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading...");
}
private void categoryAdapterListener() {
categoryAdapter.setListener(new CategoryAdapter.CategoryListener() {
@Override
public void onGetProduct(CategoryModels categoryModels) {
......@@ -315,8 +324,7 @@ public class MainActivity extends AppCompatActivity {
mainViewModels.getProduct().observe(MainActivity.this, new Observer<DataResponse<List<ProductModels<CategoryModels>>>>() {
@Override
public void onChanged(DataResponse<List<ProductModels<CategoryModels>>> listDataResponse) {
ArrayList<ProductModels> productModelsArrayList = new ArrayList<>();
productModelsArrayList.clear();
productModelsArrayList.addAll(listDataResponse.getData());
progressDialog.dismiss();
binding.rvProduct.setVisibility(View.VISIBLE);
......@@ -338,8 +346,7 @@ public class MainActivity extends AppCompatActivity {
@Override
public void onChanged(DataResponse<List<ProductModels>> listDataResponse) {
if (listDataResponse != null) {
ArrayList<ProductModels> productModelsArrayList = new ArrayList<>();
productModelsArrayList.clear();
productModelsArrayList.addAll(listDataResponse.getData());
progressDialog.dismiss();
showLoading(false);
......@@ -363,6 +370,25 @@ public class MainActivity extends AppCompatActivity {
});
}
private void functionSearch() {
binding.etSearchProduct.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
productAdapter.getFilter().filter(charSequence);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
private void updateFinish() {
status_update = false;
binding.topAppBar.setVisibility(View.VISIBLE);
......
......@@ -7,7 +7,10 @@ import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
......@@ -16,21 +19,32 @@ import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.gson.Gson;
import com.yono.messeripos.adapter.CheckoutAdapter;
import com.yono.messeripos.adapter.PaymentAdapter;
import com.yono.messeripos.api.client.Client;
import com.yono.messeripos.api.service.PaymentService;
import com.yono.messeripos.models.MainViewModelsCart;
import com.yono.messeripos.models.PaymentsModels;
import com.yono.messeripos.models.ProductCartModels;
import com.yono.messeripos.response.DataResponse;
import com.yono.messeripos.response.PaymentResponse;
import com.yono.messeripos.utils.Utils;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import static com.yono.messeripos.MainActivity.CHECKOUT;
import static com.yono.messeripos.MainActivity.ORDER_ID;
import static com.yono.messeripos.MainActivity.checkout;
import static com.yono.messeripos.MainActivity.orderId;
public class PaymentActivity extends AppCompatActivity {
......@@ -45,6 +59,8 @@ public class PaymentActivity extends AppCompatActivity {
private CheckoutAdapter checkoutAdapter;
private TextView grandTotal;
public static BottomSheets bottomSheet;
public static SharedPreferences sharedPreferences;
Button cashBtn;
......@@ -57,6 +73,9 @@ public class PaymentActivity extends AppCompatActivity {
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
sharedPreferences = getSharedPreferences(SplashScreen.MY_SHARED_PREFERENCES, Context.MODE_PRIVATE);
paymentsModels = new PaymentsModels();
checkoutAdapter = new CheckoutAdapter();
recyclerView = findViewById(R.id.rvItem);
......@@ -119,13 +138,45 @@ public class PaymentActivity extends AppCompatActivity {
bottomSheet.show(getSupportFragmentManager(),
"ModalBottomSheet");
}else{
Client client = new Client();
paymentsModels.setPaymentType("bank_transfer");
paymentsModels.setBank(methodPay);
paymentsModels.setOrderId(orderId);
Log.i("BANK", "onClick: "+utils.convertGson(paymentsModels));
PaymentService paymentService = client.Client(PaymentService.class);
paymentService.postFromCart(paymentsModels).enqueue(new Callback<DataResponse<PaymentsModels>>() {
@Override
public void onResponse(Call<DataResponse<PaymentsModels>> call, Response<DataResponse<PaymentsModels>> response) {
Log.i("Paymen_bank", "onResponse: "+utils.convertGson(response.body()));
if (response.body() != null){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(ORDER_ID, "");
editor.putBoolean(CHECKOUT, false);
if (editor.commit()){
startActivity(new Intent(PaymentActivity.this, MainActivity.class));
finish();
}
}else{
new AlertDialog.Builder(PaymentActivity.this)
.setTitle("Payment Order")
.setMessage("Erorr save!").show();
}
}
@Override
public void onFailure(Call<DataResponse<PaymentsModels>> call, Throwable t) {
new AlertDialog.Builder(PaymentActivity.this)
.setCancelable(false)
.setTitle("Payment Order")
.setMessage("Add new payment failed, try again!").show();
}
});
}
}
});
}
@Override
......@@ -134,5 +185,14 @@ public class PaymentActivity extends AppCompatActivity {
startActivity(new Intent(PaymentActivity.this, MainActivity.class));
finish();
}
public void savePaymentSuccess(){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(ORDER_ID, "");
if (editor.commit()){
startActivity(new Intent(PaymentActivity.this, MainActivity.class));
finish();
}
}
}
......@@ -13,6 +13,7 @@ import androidx.recyclerview.widget.RecyclerView;
import com.google.gson.Gson;
import com.yono.messeripos.CartActivity;
import com.yono.messeripos.MainActivity;
import com.yono.messeripos.R;
import com.yono.messeripos.databinding.ItemCartBinding;
import com.yono.messeripos.databinding.ItemCheckoutBinding;
......@@ -97,6 +98,9 @@ public class CartAdapter extends RecyclerView.Adapter<CartAdapter.MyViewHolder>
* */
subtotal = cartModels.getPrice_orders() * cartModels.getQuantity_orders();
utils.grandTotal = utils.grandTotal + (cartModels.getPrice_orders() * cartModels.getQuantity_orders());
Log.i("Grand Total", "bindData: "+utils.grandTotal);
CartActivity.grandToral.setText(utils.convertPrice("Rp. ", utils.grandTotal));
binding.setCarts(cartModels);
binding.setPrice(utils.convertPrice("Rp. ", cartModels.getPrice_orders()));
......
......@@ -2,6 +2,7 @@ package com.yono.messeripos.adapter;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
......@@ -32,7 +33,7 @@ public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHo
public interface ProductListener {
void onUpdate(ProductModels product);
void onDelete(ProductModels product);
void onDelete(ProductModels product, int position);
void onAddCart(ProductModels product);
}
......@@ -149,7 +150,12 @@ public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHo
}
});
binding.btnDelete.setOnClickListener(view -> listener.onDelete(products));
binding.btnDelete.setOnClickListener(view -> {
Log.i("position", "bindData: "+getAdapterPosition());
listener.onDelete(products, getAdapterPosition());
});
binding.btnEdit.setOnClickListener(view -> listener.onUpdate(products));
}
}
}
package com.yono.messeripos.api.service;
import com.yono.messeripos.api.ApiHelper;
import com.yono.messeripos.models.PaymentsModels;
import com.yono.messeripos.response.DataResponse;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;
public interface PaymentService {
// @POST(ApiHelper.VERSI_API_1+"payment")
// Call
@POST(ApiHelper.VERSI_API_1+"payment")
Call<DataResponse<PaymentsModels>> postFromCart(@Body PaymentsModels paymentsModels);
}
......@@ -11,6 +11,7 @@ import java.util.List;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.DELETE;
import retrofit2.http.GET;
import retrofit2.http.Multipart;
......@@ -19,21 +20,15 @@ import retrofit2.http.Part;
import retrofit2.http.Path;
public interface ProductService {
@GET(ApiHelper.VERSI_API_1+"product-categories")
@GET(ApiHelper.VERSI_API_1+"products")
Call<DataResponse<List<ProductModels<CategoryModels>>>> getProducts();
@DELETE(ApiHelper.VERSI_API_1+"product/{id}")
Call<DataResponse<ProductModels>> deleteProducts(@Path("id") int id);
@Multipart
@POST(ApiHelper.VERSI_API_1+"product")
Call<HashMap<String, Object>> postProduct(
@Part MultipartBody.Part image,
@Part("name") RequestBody name,
@Part("price") RequestBody price,
@Part("stock") RequestBody stock,
@Part("category_id") RequestBody category_id
);
Call<DataResponse<ProductModels>> postProduct(@Body ProductModels productModels);
@Multipart
@POST(ApiHelper.VERSI_API_1+"product")
......
......@@ -18,11 +18,11 @@ public interface CartDaos {
@Query("SELECT * FROM carts ORDER BY id DESC")
public LiveData<List<ProductCartModels>> getCart();
@Query("SELECT * FROM carts WHERE id_product_orders = :idCart")
public LiveData<List<ProductCartModels>> getCartById(long idCart);
@Query("SELECT * FROM carts WHERE id_orders = :idCart")
public LiveData<List<ProductCartModels>> getCartById(String idCart);
@Query("SELECT * FROM carts WHERE id_orders = :orderId")
public LiveData<List<ProductCartModels>> getCartByOrderId(String orderId);
@Query("SELECT * FROM carts WHERE id_orders = :orderId AND id_product_orders = :idOrders")
public LiveData<List<ProductCartModels>> getCartByOrderId(String orderId, long idOrders);
@Insert(onConflict = OnConflictStrategy.IGNORE)
public void insertProduct(ProductCartModels product);
......
......@@ -39,6 +39,7 @@ public abstract class LocalDatabase extends RoomDatabase {
return INSTANCE;
}
@NonNull
@Override
protected SupportSQLiteOpenHelper createOpenHelper(DatabaseConfiguration config) {
......
......@@ -152,10 +152,11 @@ public class MainViewModels extends ViewModel {
private void getProductList() {
ProductService productService = client.Client(ProductService.class);
Log.i("TAG", "getProductList: dari get product");
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) {
Log.i("Get products", "onResponse: "+utils.convertGson(response.body()));
if (response.body() != null){
productList.setValue(response.body());
}else{
......
......@@ -8,12 +8,16 @@ import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.Transformations;
import androidx.room.Dao;
import com.google.gson.Gson;
import com.yono.messeripos.CartActivity;
import com.yono.messeripos.MainActivity;
import com.yono.messeripos.api.client.Client;
import com.yono.messeripos.api.service.OrdersService;
import com.yono.messeripos.api.service.PaymentService;
import com.yono.messeripos.repositories.CartRepositories;
import com.yono.messeripos.response.DataResponse;
import com.yono.messeripos.response.OrdersResponse;
......@@ -29,6 +33,7 @@ import static com.yono.messeripos.CartActivity.statusPost;
public class MainViewModelsCart extends AndroidViewModel {
private MutableLiveData<ProductCartModels> cart = new MutableLiveData<>();
private LiveData<List<ProductCartModels>> productsLive;
private Client client;
private CartRepositories cartRepositories;
......@@ -44,6 +49,7 @@ public class MainViewModelsCart extends AndroidViewModel {
cartRepositories = new CartRepositories(application);
productsLive = cartRepositories.getDataCart();
client = new Client();
}
......@@ -55,6 +61,25 @@ public class MainViewModelsCart extends AndroidViewModel {
public void updateQtyCart(long qty, long id){cartRepositories.updateQty(qty, id);}
public void insertPayments(){
}
public void insertPayments(PaymentsModels paymentsModels){
PaymentService paymentService = client.Client(PaymentService.class);
paymentService.postFromCart(paymentsModels).enqueue(new Callback<DataResponse<PaymentsModels>>() {
@Override
public void onResponse(Call<DataResponse<PaymentsModels>> call, Response<DataResponse<PaymentsModels>> response) {
}
@Override
public void onFailure(Call<DataResponse<PaymentsModels>> call, Throwable t) {
}
});
}
public void insertOrders(OrdersResponse ordersResponse){
Client client = new Client();
OrdersService ordersService = client.Client(OrdersService.class);
......@@ -82,14 +107,18 @@ public class MainViewModelsCart extends AndroidViewModel {
});
}
public LiveData<List<ProductCartModels>> getCartById(long id){
public LiveData<List<ProductCartModels>> getCartById(String id){
return cartRepositories.getDataById(id);
}
public LiveData<List<ProductCartModels>> getCartByOrderId(String id){
return cartRepositories.getCartByOrderId(id);
public LiveData<List<ProductCartModels>> checkOrderProduct(long idProduct, String idOrders){
return cartRepositories.getCartByOrderId(idOrders, idProduct);
}
// public LiveData<List<ProductCartModels>> getCartByOrderId(long id){
// return cartRepositories.getDataById(id);
// }
public MutableLiveData<ProductCartModels> getCart(){
// String js = new Gson().toJson(cartRepositories.getDataCart());
// Log.d("Get data cart", "Response "+js);
......
package com.yono.messeripos.models;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.widget.ImageView;
......@@ -10,10 +12,12 @@ import com.bumptech.glide.request.RequestOptions;
import com.google.gson.annotations.SerializedName;
import com.yono.messeripos.R;
import com.yono.messeripos.api.ApiHelper;
import com.yono.messeripos.utils.Utils;
import java.io.File;
public class ProductModels<T> {
public class ProductModels<T> implements Parcelable {
@SerializedName("id")
private int idProduct;
......@@ -29,8 +33,28 @@ public class ProductModels<T> {
@SerializedName("image")
private String imageProduct;
@SerializedName("category")
private T categoryProduct;
@SerializedName("category_id")
private int categoryProduct;
protected ProductModels(Parcel in) {
idProduct = in.readInt();
productName = in.readString();
priceProduct = in.readInt();
stockProduct = in.readInt();
imageProduct = in.readString();
}
public static final Creator<ProductModels> CREATOR = new Creator<ProductModels>() {
@Override
public ProductModels createFromParcel(Parcel in) {
return new ProductModels(in);
}
@Override
public ProductModels[] newArray(int size) {
return new ProductModels[size];
}
};
public int getIdProduct() {
return idProduct;
......@@ -64,11 +88,11 @@ public class ProductModels<T> {
this.stockProduct = stockProduct;
}
public T getCategoryProduct() {
public int getCategoryProduct() {
return categoryProduct;
}
public void setCategoryProduct(T categoryProduct) {
public void setCategoryProduct(int categoryProduct) {
this.categoryProduct = categoryProduct;
}
......@@ -80,26 +104,50 @@ public class ProductModels<T> {
this.imageProduct = imageProduct;
}
public ProductModels() {
}
public ProductModels(String productName, int priceProduct, int stockProduct, String imageProduct) {
this.productName = productName;
this.priceProduct = priceProduct;
this.stockProduct = stockProduct;
this.imageProduct = imageProduct;
}
@BindingAdapter("url")
public static void setImage(ImageView view, String url){
if (ApiHelper.BASE_URL_IMAGE+url == null){
view.setImageResource(R.drawable.ic_meser);
Utils utils = new Utils();
if (TextUtils.isEmpty(url) || url.length() < 1000){
view.setImageResource(R.drawable.ic_meser_create);
}else{
Glide.with(view)
.applyDefaultRequestOptions(new RequestOptions().placeholder(R.drawable.skeleton).error(R.drawable.skeleton))
.load(ApiHelper.BASE_URL_IMAGE+url)
.into(view);
// Glide.with(view)
// .applyDefaultRequestOptions(new RequestOptions().placeholder(R.drawable.skeleton).error(R.drawable.skeleton))
// .load(ApiHelper.BASE_URL_IMAGE+url)
// .into(view);
view.setImageBitmap(utils.decodeImageBase64(url));
}
}
@BindingAdapter("path")
public static void setPathImage(ImageView view, String path) {
Utils utils = new Utils();
if (TextUtils.isEmpty(path)) view.setImageResource(R.drawable.ic_meser_create);
else {
File file = new File(path);
if (file.exists()) Glide.with(view).load(file).into(view);
else view.setImageResource(R.drawable.ic_meser_create);
view.setImageBitmap(utils.decodeImageBase64(path));
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(idProduct);
parcel.writeString(productName);
parcel.writeInt(priceProduct);
parcel.writeInt(stockProduct);
parcel.writeString(imageProduct);
}
}
......@@ -4,6 +4,7 @@ import android.app.Application;
import android.util.Log;
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import com.google.gson.Gson;
import com.yono.messeripos.daos.CartDaos;
......@@ -19,7 +20,9 @@ public class CartRepositories {
public CartRepositories(Application application){
LocalDatabase db = LocalDatabase.geDatabase(application);
cartDaos = db.cartDaos();
products = cartDaos.getCart();
Log.d("Data cart", "Response "+products);
}
......@@ -39,12 +42,12 @@ public class CartRepositories {
LocalDatabase.databaseWriterExecutor.execute(() ->cartDaos.updateCart(qty, id));
}
public LiveData<List<ProductCartModels>> getDataById(long id){
public LiveData<List<ProductCartModels>> getDataById(String id){
return cartDaos.getCartById(id);
}
public LiveData<List<ProductCartModels>> getCartByOrderId(String id){
return cartDaos.getCartByOrderId(id);
public LiveData<List<ProductCartModels>> getCartByOrderId(String idOrder, long idProduct){
return cartDaos.getCartByOrderId(idOrder, idProduct);
}
public LiveData<List<ProductCartModels>> getDataCart(){
......
package com.yono.messeripos.utils;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
......@@ -10,6 +11,7 @@ import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.DividerItemDecoration;
import com.bumptech.glide.load.engine.Resource;
import com.google.gson.Gson;
import java.io.ByteArrayOutputStream;
......@@ -23,6 +25,9 @@ import java.util.Date;
import java.util.Locale;
public class Utils {
public long grandTotal = 0;
public String convertPrice(String mataUang, int nilai ){
DecimalFormat rupiah = (DecimalFormat) DecimalFormat.getCurrencyInstance();
DecimalFormatSymbols format = new DecimalFormatSymbols();
......@@ -92,4 +97,28 @@ public class Utils {
public void toastMessage(Context context, String message){
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
public String convertImageBase64Resource(Resources resource, int id){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = baos.toByteArray();
Bitmap bitmap = BitmapFactory.decodeResource(resource, id);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
String encodeImage = Base64.encodeToString(b, Base64.DEFAULT);
return encodeImage;
}
public Bitmap decodeImageBase64(String source){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] imageBytes = baos.toByteArray();
imageBytes = Base64.decode(source, Base64.DEFAULT);
Bitmap decodeImages = BitmapFactory.decodeByteArray(imageBytes,0,imageBytes.length);
return decodeImages;
}
public long grandTotalCart(){
return grandTotal;
}
}
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