Skip to content

Instantly share code, notes, and snippets.

@sajjadyousefnia
Created December 27, 2025 23:52
Show Gist options
  • Select an option

  • Save sajjadyousefnia/6b28c57935bcc1d4aab1606d76b12073 to your computer and use it in GitHub Desktop.

Select an option

Save sajjadyousefnia/6b28c57935bcc1d4aab1606d76b12073 to your computer and use it in GitHub Desktop.
package com.asantech.asanpay.product;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AtomicFile;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.asantech.asanpay.setting.Globals;
import com.asantech.asanpay.R;
import com.google.android.material.materialswitch.MaterialSwitch;
import com.google.android.material.switchmaterial.SwitchMaterial;
import com.squareup.picasso.Picasso;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import de.hdodenhof.circleimageview.CircleImageView;
public class ProductsActivity extends AppCompatActivity {
private RecyclerView mrecyclerView;
private RequestQueue requestQueue;
private StringRequest stringRequest;
private int cat_id;
private String user_mobile = "";
RcyclerAdapter adapter;
boolean isSendOther = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products);
Objects.requireNonNull(getSupportActionBar()).hide();
Log.d("TAG", "onCreate: ");
Bundle bundle = getIntent().getExtras();
cat_id = bundle.getInt("cat_id");
String cat_name = bundle.getString("cat_name");
int operator_id = bundle.getInt("operator_id");
String operator_name = bundle.getString("operator_name");
user_mobile = bundle.getString("user_mobile", "");
isSendOther = bundle.getBoolean("other", false);
TextView titile_tv = findViewById(R.id.titile_tv);
titile_tv.setText(cat_name + " " + operator_name);
findViewById(R.id.back_ibtn).setOnClickListener(v -> finish());
mrecyclerView = findViewById(R.id.recycler_view);
mrecyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
SwitchMaterial materialSwitch = findViewById(R.id.swtichBuyPrice);
materialSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
adapter.showShowingPriceBuy(b);
}
});
GetProducts(String.valueOf(cat_id), operator_id, cat_name);
}
private void GetProducts(String cat, int opr, String scname) {
requestQueue = Volley.newRequestQueue(ProductsActivity.this);
String req_url = Globals.global_link + "user_mproducts_new.php";
stringRequest = new StringRequest(Request.Method.POST, req_url, response -> {
try {
JSONObject jresponse = new JSONObject(response);
boolean status = jresponse.getBoolean("status");
String message = jresponse.getString("message");
if (status) {
Globals.setResponseMProducts(ProductsActivity.this, response);
ApplyResponse(scname);
} else {
Toast.makeText(ProductsActivity.this, message.split(":")[1], Toast.LENGTH_LONG).show();
}
} catch (JSONException ignored) {
}
}, error -> {
Toast.makeText(ProductsActivity.this, getString(R.string.severError), Toast.LENGTH_LONG).show();
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("private_key", Globals.getUser(ProductsActivity.this));
if (opr != 0) {
params.put("operator", String.valueOf(opr));
}
params.put("cat", cat);
params.put("lang", Globals.getLanguage(ProductsActivity.this));
return params;
}
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(100000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
stringRequest.setTag("ALL");
requestQueue.add(stringRequest);
}
@SuppressLint("NotifyDataSetChanged")
private void ApplyResponse(String mcname) {
if (Globals.getResponseMProducts(ProductsActivity.this) != null) {
try {
JSONObject jresponse = new JSONObject(Globals.getResponseMProducts(ProductsActivity.this));
boolean status = jresponse.getBoolean("status");
String currency = jresponse.getString("currency");
String balance = jresponse.getString("balance");
JSONArray prdcts_id = jresponse.getJSONArray("prdcts_id");
JSONArray prdcts_name = jresponse.getJSONArray("prdcts_name");
JSONArray prdcts_dsc = jresponse.getJSONArray("prdcts_dsc");
JSONArray prdcts_img = jresponse.getJSONArray("prdcts_img");
String afg = jresponse.getString("asan_afg");
String irt = jresponse.getString("asan_irt");
String usd = jresponse.getString("asan_usd");
String lir = jresponse.getString("asan_lir");
String powerOfCurrency = switch (currency) {
case "afg" -> afg;
case "irt" -> irt;
case "usd" -> usd;
case "lir" -> lir;
default -> "1";
};
JSONArray prdcts_price = jresponse.getJSONArray("prdcts_price");
JSONArray prdcts_price_show = jresponse.getJSONArray("prdcts_price_show");
JSONArray prdcts_needmob = jresponse.getJSONArray("prdcts_needmob");
JSONArray prdcts_needserial = jresponse.getJSONArray("prdcts_needserial");
JSONArray prdcts_needlink = jresponse.getJSONArray("prdcts_needlink");
if (status) {
View empty = findViewById(R.id.empty);
if (prdcts_id.length() == 0) {
empty.setVisibility(View.VISIBLE);
} else {
empty.setVisibility(View.GONE);
}
adapter = new RcyclerAdapter(ProductsActivity.this, powerOfCurrency, currency,
balance, prdcts_id, prdcts_name, prdcts_dsc, prdcts_img, mcname, prdcts_price, prdcts_price_show, prdcts_needmob, prdcts_needserial, prdcts_needlink);
adapter.notifyDataSetChanged();
mrecyclerView.setAdapter(adapter);
}
} catch (JSONException ignored) {
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (requestQueue != null) {
requestQueue.cancelAll("ALL");
}
if (stringRequest != null) {
stringRequest.cancel();
}
}
@Override
protected void onResume() {
super.onResume();
if (Globals.getShouldFinish(this)) {
if (cat_id != 0) {
finish();
}
Globals.setShouldFinish(this, false);
}
}
private class RcyclerAdapter extends RecyclerView.Adapter<RcyclerAdapter.mViewHolder> {
Context context;
JSONArray prdcts_id, prdcts_name, prdcts_dsc, prdcts_img, prdcts_price, prdcts_price_show, prdcts_needmob, prdcts_needserial, prdcts_needlink;
String pstitle, crnc, blnc, pricePower;
boolean isShowPriceBuy = false;
private RcyclerAdapter(Context context, String pricePower, String crnc, String blnc, JSONArray prdcts_id, JSONArray prdcts_name, JSONArray prdcts_dsc,
JSONArray prdcts_img, String pstitle, JSONArray prdcts_price, JSONArray prdcts_price_show, JSONArray prdcts_needmob, JSONArray prdcts_needserial, JSONArray prdcts_needlink) {
this.context = context;
this.crnc = crnc;
this.blnc = blnc;
this.pricePower = pricePower;
this.prdcts_id = prdcts_id;
this.prdcts_name = prdcts_name;
this.prdcts_dsc = prdcts_dsc;
this.prdcts_img = prdcts_img;
this.pstitle = pstitle;
this.prdcts_price = prdcts_price;
this.prdcts_price_show = prdcts_price_show;
this.prdcts_needmob = prdcts_needmob;
this.prdcts_needserial = prdcts_needserial;
this.prdcts_needlink = prdcts_needlink;
}
@NonNull
@Override
public RcyclerAdapter.mViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.produts_list_item, parent, false);
return new RcyclerAdapter.mViewHolder(itemView);
}
public void showShowingPriceBuy(boolean isShow) {
isShowPriceBuy = isShow;
notifyDataSetChanged();
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull RcyclerAdapter.mViewHolder holder, int position) {
try {
Picasso.get().load(Globals.global_link + "images/" + prdcts_img.getString(position)).into(holder.image_view);
holder.titile_tv.setText(pstitle);
holder.desc_tv.setText(prdcts_name.getString(position));
String priceHolder = context.getString(R.string.usd_crncy);
switch (crnc) {
case "usd": {
priceHolder = context.getString(R.string.usd_crncy);
// holder.price_tv.setText(String.format("%s %s", new DecimalFormat("#,###.##").format(prdcts_price.getDouble(position)), context.getString(R.string.usd_crncy)));
break;
}
case "afg": {
priceHolder = context.getString(R.string.afg_crncy);
//holder.price_tv.setText(String.format("%s %s", new DecimalFormat("#,###.##").format(prdcts_price.getDouble(position)), context.getString(R.string.afg_crncy)));
break;
}
case "irt": {
priceHolder = context.getString(R.string.irt_crncy);
//holder.price_tv.setText(String.format("%s %s", new DecimalFormat("#,###.##").format(prdcts_price.getDouble(position)), context.getString(R.string.irt_crncy)));
break;
}
case "lir": {
priceHolder = context.getString(R.string.lir_crncy);
//holder.price_tv.setText(String.format("%s %s", new DecimalFormat("#,###.##").format(prdcts_price.getDouble(position)), context.getString(R.string.irt_crncy)));
break;
}
}
double productPrice = prdcts_price.getDouble(position) * Double.parseDouble(pricePower);
double productPriceShow = prdcts_price_show.getDouble(position) * Double.parseDouble(pricePower);
holder.price_tv.setText(String.format("%s %s", new DecimalFormat("#,###.##").format(productPriceShow), priceHolder));
holder.price_tv_show.setText(getString(R.string.price_show_title) + String.format("%s %s", new DecimalFormat("#,###.##").format(productPrice), priceHolder));
int pid = prdcts_id.getInt(position);
if (isShowPriceBuy) {
holder.price_tv_show.setVisibility(View.VISIBLE);
} else {
holder.price_tv_show.setVisibility(View.GONE);
}
String ptitle = pstitle;
String pname = prdcts_name.getString(position);
String pdsc = prdcts_dsc.getString(position);
String pimg = prdcts_img.getString(position);
String pprice = prdcts_price.getString(position);
String ppriceShow = prdcts_price_show.getString(position);
int pneedmob = prdcts_needmob.getInt(position);
int pneedserial = prdcts_needserial.getInt(position);
int pneedlink = prdcts_needlink.getInt(position);
holder.itemView.setOnClickListener(view -> {
Intent intent = new Intent(context, ProductActivity.class);
intent.putExtra("currency", crnc);
double priceWithPower = Double.parseDouble(pprice) * Double.parseDouble(pricePower);
double priceWithPowerShow = Double.parseDouble(ppriceShow) * Double.parseDouble(pricePower);
intent.putExtra("pprice", priceWithPower + "");
intent.putExtra("pprice_show", priceWithPowerShow + "");
//double balancePriceWithPower = Double.parseDouble(blnc);//* Double.parseDouble(pricePower);
intent.putExtra("balance", blnc + "");
intent.putExtra("pid", pid);
intent.putExtra("ptitle", ptitle);
intent.putExtra("power_price", pricePower);
intent.putExtra("pname", pname);
intent.putExtra("pdsc", pdsc);
intent.putExtra("pimg", pimg);
intent.putExtra("pneedmob", pneedmob);
intent.putExtra("pneedserial", pneedserial);
intent.putExtra("pneedlink", pneedlink);
intent.putExtra("user_mobile", user_mobile);
intent.putExtra("other", isSendOther);
context.startActivity(intent);
});
} catch (JSONException ignored) {
}
}
@Override
public int getItemCount() {
return prdcts_id.length();
}
class mViewHolder extends RecyclerView.ViewHolder {
CircleImageView image_view;
TextView titile_tv, desc_tv, price_tv, price_tv_show;
mViewHolder(@NonNull View itemView) {
super(itemView);
image_view = itemView.findViewById(R.id.image_view);
titile_tv = itemView.findViewById(R.id.titile_tv);
desc_tv = itemView.findViewById(R.id.desc_tv);
price_tv = itemView.findViewById(R.id.price_tv);
price_tv_show = itemView.findViewById(R.id.price_tv_show);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment