30 private double totalCompra = 0;
32 private static final double UMBRAL_AUTENTICACION = 50000;
41 Bundle args =
new Bundle();
42 args.putDouble(
"totalCompra", total);
43 fragment.setArguments(args);
48 public void onCreate(Bundle savedInstanceState) {
49 super.onCreate(savedInstanceState);
50 if (getArguments() !=
null) {
51 totalCompra = getArguments().getDouble(
"totalCompra", 0);
56 public View
onCreateView(LayoutInflater inflater, ViewGroup container,
57 Bundle savedInstanceState) {
59 View view = inflater.inflate(R.layout.fragment_datos_entrega, container,
false);
62 EditText editTextDireccion = view.findViewById(R.id.editTextDireccionEntrega);
64 String direccion = profileManager.
getAddress();
65 if (direccion !=
null && !direccion.isEmpty()) {
66 editTextDireccion.setText(direccion);
70 TextView tvTiempo = view.findViewById(R.id.tvSubTitle2);
71 TextView tvCosto = view.findViewById(R.id.tvSubTitle3);
72 int minTiempo = 20 + (int)(Math.random() * 21);
73 int maxTiempo = minTiempo + 5 + (int)(Math.random() * 11);
74 int costoEnvio = 2000 + (int)(Math.random() * 2001);
75 tvTiempo.setText(minTiempo +
" - " + maxTiempo +
" min");
76 tvCosto.setText(
"$" + costoEnvio);
79 Button button = view.findViewById(R.id.btnHacerPedido);
80 button.setOnClickListener(
new View.OnClickListener() {
82 public void onClick(View v) {
94 private void hacerPedido() {
95 EditText editTextDireccion = getView().findViewById(R.id.editTextDireccionEntrega);
96 String nuevaDireccion = editTextDireccion.getText().toString().trim();
97 ProfileManager profileManager =
new ProfileManager(requireContext());
98 String direccionGuardada = profileManager.getAddress();
100 if (nuevaDireccion.isEmpty()) {
101 Toast.makeText(requireContext(),
"Por favor ingresa una dirección de entrega", Toast.LENGTH_SHORT).show();
105 if (!nuevaDireccion.equals(direccionGuardada)) {
107 actualizarDireccionPerfil(nuevaDireccion);
110 replaceFragment(
new PaymentFragment());
114 private void actualizarDireccionPerfil(String nuevaDireccion) {
115 String url =
"https://backmobile1.onrender.com/appUSERS/update/";
117 String token = sessionManager.getToken();
119 Toast.makeText(requireContext(),
"No hay sesión activa", Toast.LENGTH_SHORT).show();
123 android.app.ProgressDialog progressDialog =
new android.app.ProgressDialog(requireContext());
124 progressDialog.setMessage(
"Actualizando dirección...");
125 progressDialog.setCancelable(
false);
126 progressDialog.show();
127 org.json.JSONObject body =
new org.json.JSONObject();
129 body.put(
"direccion", nuevaDireccion);
130 }
catch (org.json.JSONException e) {
131 progressDialog.dismiss();
132 Toast.makeText(requireContext(),
"Error al preparar los datos", Toast.LENGTH_SHORT).show();
135 com.android.volley.toolbox.JsonObjectRequest request =
new com.android.volley.toolbox.JsonObjectRequest(
136 com.android.volley.Request.Method.PUT,
140 progressDialog.dismiss();
142 ProfileManager profileManager = new ProfileManager(requireContext());
143 String nombre = profileManager.getName();
144 String apellido = profileManager.getSurname();
145 String email = profileManager.getEmail();
146 String telefono = profileManager.getPhone();
147 String imagen = profileManager.getProfileImageUrl();
148 profileManager.saveInfo(nombre, apellido, email, telefono, imagen, nuevaDireccion);
149 Toast.makeText(requireContext(),
"Dirección actualizada", Toast.LENGTH_SHORT).show();
151 replaceFragment(new PaymentFragment());
154 progressDialog.dismiss();
155 String msg =
"Error al actualizar dirección";
156 if (error.networkResponse != null) {
157 msg +=
". Código: " + error.networkResponse.statusCode;
159 String bodyStr = new String(error.networkResponse.data,
"UTF-8");
160 msg +=
"\n" + bodyStr;
161 } catch (Exception ignored) {}
163 Toast.makeText(requireContext(), msg, Toast.LENGTH_LONG).show();
167 public Map<String, String> getHeaders() throws com.android.volley.AuthFailureError {
168 Map<String, String> headers =
new HashMap<>();
169 headers.put(
"Authorization",
"Bearer " + token);
170 headers.put(
"Content-Type",
"application/json");
174 com.android.volley.RequestQueue queue = com.android.volley.toolbox.Volley.newRequestQueue(requireContext());
178 private void replaceFragment(Fragment newFragment) {
180 FragmentManager fragmentManager = requireActivity().getSupportFragmentManager();
181 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
185 if (newFragment instanceof PaymentFragment && totalCompra >= UMBRAL_AUTENTICACION) {
186 FingerprintAuthFragment authFragment = FingerprintAuthFragment.newInstance(totalCompra);
187 fragmentTransaction.replace(R.id.fragment_container_view, authFragment);
189 fragmentTransaction.replace(R.id.fragment_container_view, newFragment);
192 fragmentTransaction.addToBackStack(
null);
193 fragmentTransaction.commit();