Created
December 28, 2025 21:50
-
-
Save sajjadyousefnia/bc36127c16b1ca1ab8937b0a083a5889 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.asantech.asanpay.sim; | |
| import android.app.ProgressDialog; | |
| import android.content.Intent; | |
| import android.net.Uri; | |
| import android.os.Bundle; | |
| import android.text.Editable; | |
| import android.text.Spannable; | |
| import android.text.SpannableString; | |
| import android.text.TextWatcher; | |
| import android.util.Log; | |
| import android.view.LayoutInflater; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.view.WindowManager; | |
| import android.widget.Button; | |
| import android.widget.CompoundButton; | |
| import android.widget.EditText; | |
| import android.widget.FrameLayout; | |
| import android.widget.ImageButton; | |
| import android.widget.ImageView; | |
| import android.widget.TextView; | |
| import android.widget.Toast; | |
| import androidx.annotation.NonNull; | |
| import androidx.annotation.Nullable; | |
| import androidx.core.widget.NestedScrollView; | |
| import androidx.fragment.app.Fragment; | |
| import androidx.fragment.app.FragmentActivity; | |
| import androidx.lifecycle.ViewModelProvider; | |
| import androidx.viewpager2.adapter.FragmentStateAdapter; | |
| import androidx.viewpager2.widget.ViewPager2; | |
| 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.App; | |
| import com.asantech.asanpay.product.ProductActivity; | |
| import com.asantech.asanpay.setting.Globals; | |
| import com.asantech.asanpay.auth.LoginActivity; | |
| import com.asantech.asanpay.R; | |
| import com.asantech.asanpay.databinding.FragmentSimcardBinding; | |
| import com.asantech.asanpay.user.UserObject; | |
| import com.google.android.material.bottomsheet.BottomSheetBehavior; | |
| import com.google.android.material.bottomsheet.BottomSheetDialog; | |
| import com.google.android.material.switchmaterial.SwitchMaterial; | |
| import com.google.android.material.tabs.TabLayout; | |
| import com.google.android.material.tabs.TabLayoutMediator; | |
| import com.squareup.picasso.Picasso; | |
| import org.json.JSONArray; | |
| import org.json.JSONException; | |
| import org.json.JSONObject; | |
| import java.text.DecimalFormat; | |
| import java.text.ParsePosition; | |
| import java.util.ArrayList; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| import java.util.Objects; | |
| import de.hdodenhof.circleimageview.CircleImageView; | |
| public class SimCardFragment extends Fragment { | |
| private FragmentSimcardBinding binding; | |
| private SharedViewModel viewModel; | |
| private RequestQueue requestQueue; | |
| private ProgressDialog progressDialog; | |
| // Currency and balance variables | |
| private String balance = ""; | |
| private String currency = ""; | |
| private String priceHolder = ""; | |
| private double asan_afg, asan_irt, asan_usd, asan_lir; | |
| // Operator and country data | |
| private int operator = 0; | |
| private String soperator = ""; | |
| private String currentPrefix = ""; | |
| private String currentTax = ""; | |
| private String currentProfit = ""; | |
| private String currentCountry = ""; | |
| private JSONArray companies_id, companies_name, companies_prefix, companies_image; | |
| private JSONArray country_prefixes, country_ids, country_taxes, country_profit, country_showing; | |
| private StringRequest stringRequest; | |
| private boolean isPriceShow = true; | |
| private double finalPriceInUserCurrency = 0.0; | |
| @Override | |
| public void onCreate(@Nullable Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| initializeViewModel(); | |
| initializeProgressDialog(); | |
| } | |
| private void initializeViewModel() { | |
| if (getActivity() != null) { | |
| viewModel = new ViewModelProvider(getActivity()).get(SharedViewModel.class); | |
| } | |
| } | |
| private void initializeProgressDialog() { | |
| if (getActivity() != null) { | |
| progressDialog = new ProgressDialog(getActivity()); | |
| progressDialog.setCancelable(false); | |
| progressDialog.setMessage(getString(R.string.wait)); | |
| } | |
| } | |
| @Override | |
| public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
| binding = FragmentSimcardBinding.inflate(inflater, container, false); | |
| View root = binding.getRoot(); | |
| setupUI(); | |
| setupListeners(); | |
| fetchInitialData(); | |
| return root; | |
| } | |
| private void setupUI() { | |
| ApplyResponse(); | |
| } | |
| private void setupListeners() { | |
| binding.balanceBtn.setOnClickListener(v -> ShowAdbalanceBottomSheet()); | |
| binding.btnAddDebt.setOnClickListener(v -> showSendCreditDialog()); | |
| viewModel.getData().observe(getViewLifecycleOwner(), newValue -> { | |
| if (newValue) { | |
| binding.btnAddDebt.callOnClick(); | |
| viewModel.setData(false); | |
| } | |
| }); | |
| } | |
| private void fetchInitialData() { | |
| // if (Globals.getIsNeedRefreshHomeResponse(requireActivity()) || App.Companion.isNeedRefreshLang()) { | |
| GetHome(); | |
| //} | |
| getCurrencyData(); | |
| } | |
| private void showSendCreditDialog() { | |
| if (getActivity() == null) return; | |
| BottomSheetDialog dialog = new BottomSheetDialog(getActivity()); | |
| View viewSheet = getLayoutInflater().inflate(R.layout.bottom_sheet_send_credit, null); | |
| // Configure BottomSheetBehavior | |
| FrameLayout bottomSheet = dialog.findViewById(com.google.android.material.R.id.design_bottom_sheet); | |
| if (bottomSheet != null) { | |
| BottomSheetBehavior<FrameLayout> behavior = BottomSheetBehavior.from(bottomSheet); | |
| behavior.setState(BottomSheetBehavior.STATE_EXPANDED); | |
| behavior.setFitToContents(true); // Adjust height to content | |
| behavior.setHideable(false); // Prevent collapsing | |
| // Ensure it resizes properly | |
| bottomSheet.getLayoutParams().height = FrameLayout.LayoutParams.WRAP_CONTENT; | |
| bottomSheet.requestLayout(); | |
| } | |
| // Initialize dialog components | |
| initializeSendCreditDialog(viewSheet, dialog); | |
| dialog.show(); | |
| // Adjust for keyboard | |
| dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); | |
| // Optional: Scroll to focused EditText | |
| EditText edtCredit = viewSheet.findViewById(R.id.edtCredit); | |
| EditText edtPhone = viewSheet.findViewById(R.id.edtPhone); | |
| EditText orderDesc = viewSheet.findViewById(R.id.order_usrdsc_edt); | |
| View.OnFocusChangeListener focusListener = (v, hasFocus) -> { | |
| if (hasFocus) { | |
| NestedScrollView scrollView = viewSheet.findViewById(android.R.id.content); | |
| if (scrollView != null) { | |
| scrollView.smoothScrollTo(0, v.getBottom()); | |
| } | |
| } | |
| }; | |
| edtCredit.setOnFocusChangeListener(focusListener); | |
| edtPhone.setOnFocusChangeListener(focusListener); | |
| orderDesc.setOnFocusChangeListener(focusListener); | |
| initializeSendCreditDialog(viewSheet, dialog); | |
| dialog.setContentView(viewSheet); | |
| dialog.show(); | |
| dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); | |
| } | |
| private void initializeSendCreditDialog(View viewSheet, BottomSheetDialog dialog) { | |
| Button btnSend = viewSheet.findViewById(R.id.btnSend); | |
| Button btnBalance = viewSheet.findViewById(R.id.balance_btnMy); | |
| EditText edtCredit = viewSheet.findViewById(R.id.edtCredit); | |
| EditText edtDesc = viewSheet.findViewById(R.id.order_usrdsc_edt); | |
| EditText edtPhone = viewSheet.findViewById(R.id.edtPhone); | |
| TextView cprefTv = viewSheet.findViewById(R.id.cpref_tv); | |
| TextView edtEqual = viewSheet.findViewById(R.id.edtEqual); | |
| TextView companyTv = viewSheet.findViewById(R.id.company_tv); | |
| ImageView companyIv = viewSheet.findViewById(R.id.company_iv); | |
| SwitchMaterial materialSwitch = viewSheet.findViewById(R.id.swtichBuyPrice); | |
| materialSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | |
| @Override | |
| public void onCheckedChanged(CompoundButton compoundButton, boolean b) { | |
| isPriceShow = !b; | |
| updateCreditConversion(edtCredit, edtEqual); | |
| } | |
| }); | |
| setupSendCreditDialogUI(btnBalance, cprefTv); | |
| setupSendCreditListeners(btnSend, edtCredit, edtDesc, edtPhone, edtEqual, companyTv, companyIv, dialog); | |
| } | |
| private void setupSendCreditListeners(Button btnSend, EditText edtCredit, EditText edtDesc, EditText edtPhone, | |
| TextView edtEqual, TextView companyTv, ImageView companyIv, | |
| BottomSheetDialog dialog) { | |
| btnSend.setOnClickListener(v -> handleSendCreditClick(edtCredit, edtDesc, edtPhone, dialog)); | |
| edtCredit.addTextChangedListener(new TextWatcher() { | |
| @Override | |
| public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
| } | |
| @Override | |
| public void onTextChanged(CharSequence s, int start, int before, int count) { | |
| } | |
| @Override | |
| public void afterTextChanged(Editable s) { | |
| updateCreditConversion(edtCredit, edtEqual); | |
| } | |
| }); | |
| edtPhone.addTextChangedListener(new TextWatcher() { | |
| @Override | |
| public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
| } | |
| @Override | |
| public void onTextChanged(CharSequence s, int start, int before, int count) { | |
| } | |
| @Override | |
| public void afterTextChanged(Editable s) { | |
| updateOperatorInfo(edtPhone, companyIv, companyTv); | |
| } | |
| }); | |
| } | |
| private void updateCreditConversion(EditText edtCredit, TextView edtEqual) { | |
| String creditText = edtCredit.getText().toString(); | |
| if (creditText.isEmpty()) { | |
| edtEqual.setText("0"); | |
| return; | |
| } | |
| double price = Double.parseDouble(creditText); | |
| finalPriceInUserCurrency = calculateConvertedPrice(price); | |
| String moneyCurrency = getCurrencySymbol(); | |
| finalPriceInUserCurrency = Math.floor(finalPriceInUserCurrency * 100) / 100.0; | |
| String baseText = getString(R.string.equalTo) + finalPriceInUserCurrency + moneyCurrency; | |
| String postFix = ""; | |
| if (Integer.parseInt(currentTax) > 0) { | |
| //double taxFinal = Math.round((finalPrice * Integer.parseInt(currentTax)) / 100); | |
| double tax = (finalPriceInUserCurrency * Integer.parseInt(currentTax)) / 100.0; | |
| double taxFinal = Math.floor(tax * 100) / 100.0; // فقط دو رقم اعشار بدون رُند کردن بالا | |
| postFix = "\n (" + currentTax + " % " + getString(R.string.tax) + ") = " + taxFinal + moneyCurrency; | |
| postFix += "\n " + getString(R.string.finalPrice) + (taxFinal + finalPriceInUserCurrency) + moneyCurrency; | |
| } | |
| edtEqual.setText(baseText + postFix); | |
| } | |
| // Helper method for operator detection | |
| private void updateOperatorInfo(EditText edtPhone, ImageView companyIv, TextView companyTv) { | |
| String phoneText = edtPhone.getText().toString(); | |
| operator = 0; | |
| soperator = ""; | |
| if (phoneText.isEmpty()) { | |
| companyIv.setImageResource(0); | |
| companyTv.setText(""); | |
| return; | |
| } | |
| if (phoneText.startsWith("0")) { | |
| edtPhone.setText(phoneText.substring(1)); | |
| return; | |
| } | |
| String name = null; | |
| String link = null; | |
| for (int i = 0; i < companies_id.length(); i++) { | |
| try { | |
| String mprefix = currentPrefix + phoneText.substring(0, 1).replace("0", "") + phoneText.substring(1); | |
| String[] mprefixes = companies_prefix.getString(i).split(","); | |
| for (String prefix : mprefixes) { | |
| if (mprefix.startsWith(prefix)) { | |
| name = companies_name.getString(i); | |
| link = Globals.global_link + "images/" + companies_image.getString(i); | |
| operator = companies_id.getInt(i); | |
| soperator = companies_name.getString(i); | |
| break; | |
| } | |
| } | |
| } catch (JSONException ignored) { | |
| } | |
| if (operator != 0) break; | |
| } | |
| if (link != null) { | |
| Picasso.get().load(link).into(companyIv); | |
| } | |
| companyTv.setText(name != null ? String.format("%s%s", getString(R.string.operator), name) : ""); | |
| } | |
| // Helper methods for currency calculation | |
| private double calculateConvertedPrice(double price) { | |
| double newPrice = price; | |
| if (isPriceShow) { | |
| double profit = Double.parseDouble(currentProfit); | |
| double newPriceProfit = (profit * price) / 100; | |
| newPrice += newPriceProfit; | |
| } | |
| switch (currency) { | |
| case "afg": | |
| return newPrice * asan_afg; | |
| case "usd": | |
| return newPrice * asan_usd; | |
| case "irt": | |
| return newPrice * asan_irt; | |
| case "lir": | |
| return newPrice * asan_lir; | |
| default: | |
| return newPrice; | |
| } | |
| } | |
| private String getCurrencySymbol() { | |
| switch (currency) { | |
| case "afg": | |
| return getString(R.string.AFG); | |
| case "usd": | |
| return getString(R.string.USD); | |
| case "irt": | |
| return getString(R.string.IRT); | |
| case "lir": | |
| return getString(R.string.lir_crncy); | |
| default: | |
| return ""; | |
| } | |
| } | |
| // Helper method for PIN dialog | |
| private void showPinDialog(String credit, String desc, String phone, BottomSheetDialog parentDialog) { | |
| if (getActivity() == null) return; | |
| BottomSheetDialog pinDialog = new BottomSheetDialog(getActivity()); | |
| View pinView = getLayoutInflater().inflate(R.layout.bottomsheet_buyproduct, null); | |
| EditText pinEdt = pinView.findViewById(R.id.pin_edt); | |
| pinView.findViewById(R.id.cancel_btn).setOnClickListener(v -> parentDialog.dismiss()); | |
| pinView.findViewById(R.id.confirm_btn).setOnClickListener(v -> { | |
| if (pinEdt.getText().length() == 4) { | |
| progressDialog.show(); | |
| pinDialog.dismiss(); | |
| sendCredit(credit, desc, phone, pinEdt.getText().toString(), parentDialog); | |
| } else { | |
| if (isAdded()) { | |
| Toast.makeText(getActivity(), R.string.incorrectPin, Toast.LENGTH_SHORT).show(); | |
| } | |
| } | |
| }); | |
| pinDialog.setContentView(pinView); | |
| pinDialog.show(); | |
| } | |
| // Helper method for send button click | |
| private void handleSendCreditClick(EditText edtCredit, EditText edtDesc, EditText edtPhone, BottomSheetDialog dialog) { | |
| String credit = edtCredit.getText().toString(); | |
| String desc = edtDesc.getText().toString(); | |
| String phone = edtPhone.getText().toString(); | |
| if (credit.isEmpty() || phone.isEmpty()) { | |
| if (isAdded()) { | |
| Toast.makeText(getActivity(), R.string.check_inputrs, Toast.LENGTH_LONG).show(); | |
| } | |
| return; | |
| } | |
| //double price = Double.parseDouble(credit); | |
| double balanceD = Double.parseDouble(balance); | |
| if (finalPriceInUserCurrency <= balanceD) { | |
| showPinDialog(credit, desc, phone, dialog); | |
| } else { | |
| if (isAdded()) { | |
| Toast.makeText(getActivity(), R.string.max_balance_err, Toast.LENGTH_LONG).show(); | |
| } | |
| } | |
| } | |
| private void setupSendCreditDialogUI(Button btnBalance, TextView cprefTv) { | |
| btnBalance.setText(String.format("%s %s", | |
| new DecimalFormat("#,###.##").format(Double.valueOf(balance)), | |
| priceHolder)); | |
| cprefTv.setText(currentPrefix.replace("+", "") + "+"); | |
| } | |
| private void GetHome() { | |
| requestQueue = Volley.newRequestQueue(getContext()); | |
| String req_url = Globals.global_link + "user_home_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) { | |
| if (isAdded()) { | |
| Globals.setIsNeedRefreshHomeResponse(getActivity(), false); | |
| Globals.setResponseHome(getActivity(), response); | |
| ApplyResponse(); | |
| } | |
| } else { | |
| if (isAdded()) { | |
| Toast.makeText(getActivity(), message.split(":")[1], Toast.LENGTH_LONG).show(); | |
| } | |
| } | |
| } catch (JSONException ignored) { | |
| } | |
| }, error -> { | |
| if (isAdded()) { | |
| Toast.makeText(getActivity(), getString(R.string.severError), Toast.LENGTH_LONG).show(); | |
| } | |
| }) { | |
| @Override | |
| protected Map<String, String> getParams() { | |
| Map<String, String> params = new HashMap<>(); | |
| if (isAdded()) { | |
| params.put("private_key", Globals.getUser(getActivity())); | |
| params.put("lang", Globals.getLanguage(getActivity())); | |
| } | |
| return params; | |
| } | |
| }; | |
| stringRequest.setRetryPolicy(new DefaultRetryPolicy(100000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); | |
| stringRequest.setTag("ALL"); | |
| requestQueue.add(stringRequest); | |
| } | |
| private void sendCredit(String amount, String desc, String mobile, String pin, BottomSheetDialog dialog) { | |
| RequestQueue requestQueue = Volley.newRequestQueue(getContext()); | |
| String req_url = Globals.global_link + "user_move_credit.php"; | |
| StringRequest stringRequest = new StringRequest(Request.Method.POST, req_url, response -> { | |
| progressDialog.dismiss(); | |
| try { | |
| JSONObject jresponse = new JSONObject(response); | |
| boolean status = jresponse.getBoolean("status"); | |
| String message = jresponse.getString("message"); | |
| //{"status":true,"message":"0:Fetched currency values successfully","afg_irt":"3.25","afg_usd":"2.00","afg_lir":"3.00"} | |
| if (status) { | |
| if (isAdded()) { | |
| Toast.makeText(getActivity(), R.string.send_treansfer_donw, Toast.LENGTH_LONG).show(); | |
| dialog.dismiss(); | |
| } | |
| } else { | |
| if (isAdded()) { | |
| Toast.makeText(getActivity(), getString(R.string.err_move) + message, Toast.LENGTH_LONG).show(); | |
| } | |
| } | |
| } catch (JSONException ignored) { | |
| } | |
| }, error -> { | |
| progressDialog.dismiss(); | |
| if (isAdded()) { | |
| Toast.makeText(getActivity(), getString(R.string.severError), Toast.LENGTH_LONG).show(); | |
| } | |
| }) { | |
| @Override | |
| protected Map<String, String> getParams() { | |
| Map<String, String> params = new HashMap<>(); | |
| if (isAdded()) { | |
| params.put("private_key", Globals.getUser(getActivity())); | |
| } | |
| params.put("amount", amount); | |
| params.put("mobile", mobile); | |
| params.put("desc", desc); | |
| params.put("pin", pin); | |
| params.put("country_id", currentCountry); | |
| return params; | |
| } | |
| }; | |
| stringRequest.setRetryPolicy(new DefaultRetryPolicy(100000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); | |
| stringRequest.setTag("ALL"); | |
| requestQueue.add(stringRequest); | |
| } | |
| private void getCurrencyData() { | |
| RequestQueue requestQueue = Volley.newRequestQueue(getContext()); | |
| String req_url = Globals.global_link + "user_currency.php"; | |
| StringRequest stringRequest = new StringRequest(Request.Method.POST, req_url, response -> { | |
| try { | |
| JSONObject jresponse = new JSONObject(response); | |
| boolean status = jresponse.getBoolean("status"); | |
| //{"status":true,"message":"0:Fetched currency values successfully","afg_irt":"3.25","afg_usd":"2.00","afg_lir":"3.00"} | |
| if (status) { | |
| asan_afg = Double.parseDouble(jresponse.getString("asan_afg")); | |
| asan_irt = Double.parseDouble(jresponse.getString("asan_irt")); | |
| asan_usd = Double.parseDouble(jresponse.getString("asan_usd")); | |
| asan_lir = Double.parseDouble(jresponse.getString("asan_lir")); | |
| } else { | |
| if (isAdded()) { | |
| Toast.makeText(getActivity(), getString(R.string.NothingFound), Toast.LENGTH_LONG).show(); | |
| } | |
| } | |
| } catch (JSONException ignored) { | |
| } | |
| }, error -> { | |
| if (isAdded()) { | |
| Toast.makeText(getActivity(), getString(R.string.severError), Toast.LENGTH_LONG).show(); | |
| } | |
| }) { | |
| @Override | |
| protected Map<String, String> getParams() { | |
| Map<String, String> params = new HashMap<>(); | |
| if (isAdded()) { | |
| params.put("private_key", Globals.getUser(getActivity())); | |
| params.put("lang", Globals.getLanguage(getActivity())); | |
| } | |
| return params; | |
| } | |
| }; | |
| stringRequest.setRetryPolicy(new DefaultRetryPolicy(100000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); | |
| stringRequest.setTag("ALL"); | |
| requestQueue.add(stringRequest); | |
| } | |
| private void ShowAdbalanceBottomSheet() { | |
| if (getActivity() == null) return; | |
| View view_sheet = getLayoutInflater().inflate(R.layout.bottom_sheet_addbalance, null); | |
| BottomSheetDialog dialog = new BottomSheetDialog(getActivity()); | |
| view_sheet.findViewById(R.id.cancel_btn).setOnClickListener(view1 -> dialog.dismiss()); | |
| view_sheet.findViewById(R.id.contact_telegram).setOnClickListener(view2 -> { | |
| Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("tg://resolve?domain=asan_payment")); | |
| myIntent.setPackage("org.telegram.messenger"); | |
| startActivity(myIntent); | |
| dialog.dismiss(); | |
| }); | |
| view_sheet.findViewById(R.id.contact_whatsapp1).setOnClickListener(view3 -> { | |
| Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://api.whatsapp.com/send?phone=+93790553020")); | |
| myIntent.setPackage("com.whatsapp"); | |
| startActivity(myIntent); | |
| dialog.dismiss(); | |
| }); | |
| view_sheet.findViewById(R.id.contact_whatsapp2).setOnClickListener(view4 -> { | |
| Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://api.whatsapp.com/send?phone=+93799508452")); | |
| myIntent.setPackage("com.whatsapp"); | |
| startActivity(myIntent); | |
| dialog.dismiss(); | |
| }); | |
| view_sheet.findViewById(R.id.contact_call).setOnClickListener(view5 -> { | |
| Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "+93790553020")); | |
| startActivity(intent); | |
| dialog.dismiss(); | |
| }); | |
| dialog.setContentView(view_sheet); | |
| dialog.show(); | |
| } | |
| private void ApplyResponse() { | |
| if (Globals.getResponseHome(requireActivity()) != null) { | |
| try { | |
| JSONObject jresponse = new JSONObject(Globals.getResponseHome(requireActivity())); | |
| boolean status = jresponse.getBoolean("status"); | |
| currency = jresponse.getString("currency"); | |
| balance = jresponse.getString("balance"); | |
| country_ids = jresponse.getJSONArray("country_ids"); | |
| JSONArray country_names = jresponse.getJSONArray("country_names"); | |
| JSONArray country_images = jresponse.getJSONArray("country_images"); | |
| country_prefixes = jresponse.getJSONArray("country_prefixes"); | |
| country_taxes = jresponse.getJSONArray("country_taxes"); | |
| country_profit = jresponse.getJSONArray("country_profit"); | |
| country_showing = jresponse.getJSONArray("country_showing"); | |
| JSONArray cats2_id = jresponse.getJSONArray("cats2_id"); | |
| JSONArray cats2_name = jresponse.getJSONArray("cats2_name"); | |
| JSONArray cats2_prnt = jresponse.getJSONArray("cats2_prnt"); | |
| companies_id = jresponse.getJSONArray("companies_id"); | |
| companies_name = jresponse.getJSONArray("companies_name"); | |
| companies_prefix = jresponse.getJSONArray("companies_prefix"); | |
| companies_image = jresponse.getJSONArray("companies_image"); | |
| JSONArray simcats_id = new JSONArray(); | |
| JSONArray simcats_name = new JSONArray(); | |
| for (int i = 0; i < cats2_id.length(); i++) { | |
| if (cats2_prnt.getInt(i) == 0) { | |
| simcats_id.put(cats2_id.get(i)); | |
| simcats_name.put(cats2_name.get(i)); | |
| } | |
| } | |
| if (status) { | |
| if (jresponse.getInt("active") == 0) { | |
| if (isAdded()) { | |
| Globals.setUser(getActivity(), null); | |
| getActivity().startActivity(new Intent(getActivity(), LoginActivity.class)); | |
| getActivity().finish(); | |
| } | |
| } | |
| ArrayList<Fragment> fragments = new ArrayList<>(); | |
| for (int i = 0; i < country_ids.length(); i++) { | |
| SimServiceFragment fragment = new SimServiceFragment(); | |
| // Create a Bundle to store the fragment arguments | |
| Bundle args = new Bundle(); | |
| try { | |
| args.putInt("country_id", country_ids.getInt(i)); | |
| args.putString("country_prefix", country_prefixes.getString(i)); | |
| args.putString("country_showing", country_showing.getString(i)); | |
| args.putString("simcats_id", simcats_id.toString()); // Convert to String | |
| args.putString("simcats_name", simcats_name.toString()); // Convert to String | |
| args.putString("companies_id", companies_id.toString()); // Convert to String | |
| args.putString("companies_name", companies_name.toString()); // Convert to String | |
| args.putString("companies_prefix", companies_prefix.toString()); // Convert to String | |
| args.putString("companies_image", companies_image.toString()); // Convert to String | |
| } catch (JSONException e) { | |
| e.printStackTrace(); | |
| } | |
| // Set the arguments to the fragment | |
| fragment.setArguments(args); | |
| // Add the fragment to the list | |
| fragments.add(fragment); | |
| } | |
| if (getActivity() != null) { | |
| FragmentStateAdapter adapter = new SectionsPagerAdapter(getActivity(), fragments); | |
| binding.viewPager.setAdapter(adapter); | |
| TabLayoutMediator tabLayoutMediator = getTabLayoutMediator(country_names, country_images); | |
| tabLayoutMediator.attach(); | |
| } | |
| binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { | |
| @Override | |
| public void onPageSelected(int position) { | |
| super.onPageSelected(position); | |
| Log.d("ViewPager", "New Page Selected: " + position); | |
| try { | |
| String showing = country_showing.getString(position); | |
| currentPrefix = country_prefixes.getString(position); | |
| currentCountry = country_ids.getString(position); | |
| currentTax = country_taxes.getString(position); | |
| currentProfit = country_profit.getString(position); | |
| if (showing.equalsIgnoreCase("1")) { | |
| binding.btnAddDebt.setVisibility(View.VISIBLE); | |
| } else { | |
| binding.btnAddDebt.setVisibility(View.GONE); | |
| } | |
| } catch (JSONException e) { | |
| throw new RuntimeException(e); | |
| } | |
| // اینجا میتوانید عملیات مورد نظر خود را انجام دهید | |
| } | |
| @Override | |
| public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { | |
| super.onPageScrolled(position, positionOffset, positionOffsetPixels); | |
| // این بخش اختیاری است و زمانی فراخوانی میشود که کاربر در حال اسکرول بین صفحات است | |
| } | |
| @Override | |
| public void onPageScrollStateChanged(int state) { | |
| super.onPageScrollStateChanged(state); | |
| // این بخش اختیاری است و زمانی فراخوانی میشود که حالت اسکرول تغییر کند | |
| } | |
| }); | |
| /* switch (currency) { | |
| case "usd": { | |
| binding.balanceBtn.setText(String.format("%s %s", new DecimalFormat("#,###.##").format(Double.valueOf(balance)), getString(R.string.usd_crncy))); | |
| } | |
| break; | |
| case "afg": { | |
| binding.balanceBtn.setText(String.format("%s %s", new DecimalFormat("#,###.##").format(Double.valueOf(balance)), getString(R.string.afg_crncy))); | |
| } | |
| break; | |
| case "irt": { | |
| binding.balanceBtn.setText(String.format("%s %s", new DecimalFormat("#,###.##").format(Double.valueOf(balance)), getString(R.string.irt_crncy))); | |
| } | |
| break; | |
| }*/ | |
| priceHolder = getString(R.string.usd_crncy); | |
| switch (currency) { | |
| case "usd": { | |
| priceHolder = 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 = 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 = 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 = 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; | |
| } | |
| } | |
| binding.balanceBtn.setText(String.format("%s %s", new DecimalFormat("#,###.##").format(Double.valueOf(balance)), priceHolder)); | |
| } | |
| } catch (JSONException ignored) { | |
| } | |
| } | |
| } | |
| private static class SectionsPagerAdapter extends FragmentStateAdapter { | |
| private final ArrayList<Fragment> frags; | |
| public SectionsPagerAdapter(FragmentActivity fragmentActivity, ArrayList<Fragment> mfrags) { | |
| super(fragmentActivity); | |
| frags = mfrags; | |
| } | |
| @NonNull | |
| @Override | |
| public Fragment createFragment(int position) { | |
| return frags.get(position); | |
| } | |
| @Override | |
| public int getItemCount() { | |
| return frags.size(); | |
| } | |
| } | |
| private TabLayoutMediator getTabLayoutMediator(JSONArray country_names, JSONArray country_images) { | |
| TabLayout tabs = binding.tabs; | |
| return new TabLayoutMediator(tabs, binding.viewPager, (tab, i) -> { | |
| android.util.Log.e("asan", "tab" + i); | |
| try { | |
| tab.setCustomView(R.layout.tab_item); | |
| TextView tv = Objects.requireNonNull(tab.getCustomView()).findViewById(R.id.text); | |
| CircleImageView iv = Objects.requireNonNull(tab.getCustomView()).findViewById(R.id.image); | |
| tv.setText(country_names.getString(i)); | |
| Picasso.get().load(Globals.global_link + "images/" + country_images.getString(i)).into(iv); | |
| } catch (JSONException ignored) { | |
| } | |
| // ... other methods like GetHome(), sendCredit(), etc. | |
| }); | |
| } | |
| @Override | |
| public void onDestroyView() { | |
| super.onDestroyView(); | |
| if (requestQueue != null) { | |
| requestQueue.cancelAll("ALL"); | |
| } | |
| binding = null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment