RedNexX

Backup Configs.java

Aug 20th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.09 KB | None | 0 0
  1. 0 package com.thinkingahead.stockges.config;
  2. 1
  3. 2 import android.app.Activity;
  4. 3 import android.app.AlertDialog;
  5. 4 import android.content.Context;
  6. 5 import android.content.DialogInterface;
  7. 6 import android.content.Intent;
  8. 7 import android.content.res.Configuration;
  9. 8 import android.util.Log;
  10. 9 import android.view.Menu;
  11. 10 import android.view.MenuInflater;
  12. 11 import android.view.MenuItem;
  13. 12 import android.view.View;
  14. 13 import android.widget.*;
  15. 14 import androidx.annotation.NonNull;
  16. 15 import androidx.fragment.app.Fragment;
  17. 16 import androidx.recyclerview.widget.DividerItemDecoration;
  18. 17 import androidx.recyclerview.widget.LinearLayoutManager;
  19. 18 import androidx.recyclerview.widget.RecyclerView;
  20. 19 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
  21. 20 import com.google.android.gms.tasks.OnCompleteListener;
  22. 21 import com.google.android.gms.tasks.Task;
  23. 22 import com.google.android.material.floatingactionbutton.FloatingActionButton;
  24. 23 import com.google.firebase.auth.*;
  25. 24 import com.google.firebase.database.DataSnapshot;
  26. 25 import com.google.firebase.database.DatabaseError;
  27. 26 import com.google.firebase.database.DatabaseReference;
  28. 27 import com.google.firebase.database.ValueEventListener;
  29. 28 import com.google.zxing.integration.android.IntentIntegrator;
  30. 29 import com.google.zxing.integration.android.IntentResult;
  31. 30 import com.thinkingahead.stockges.R;
  32. 31 import com.thinkingahead.stockges.activity.*;
  33. 32 import com.thinkingahead.stockges.adapter.AdapterRecyclerProdutos;
  34. 33 import com.thinkingahead.stockges.helper.RecyclerItemClickListener;
  35. 34 import com.thinkingahead.stockges.model.Frases;
  36. 35 import com.thinkingahead.stockges.model.Produtos;
  37. 36 import mehdi.sakout.aboutpage.AboutPage;
  38. 37
  39. 38 import java.util.Iterator;
  40. 39 import java.util.List;
  41. 40 import java.util.Random;
  42. 41 import java.util.UUID;
  43. 42
  44. 43 import static com.thinkingahead.stockges.fragments.ListarFragment.listaProdutos;
  45. 44
  46. 45
  47. 46 public class Configs {
  48. 47
  49. 48
  50. 49 public static Boolean isResultadoFull = false;
  51. 50 public static String resultado = null;
  52. 51 public static int childrenCount = 0;
  53. 52 public static String userID;
  54. 53
  55. 54 // Geral - Begin
  56. 55
  57. 56 public static void configuracaoRecyclerView(RecyclerView recyclerView, AdapterRecyclerProdutos adapterRecyclerProdutos, Context context) {
  58. 57
  59. 58 RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
  60. 59 recyclerView.setLayoutManager(layoutManager);
  61. 60 recyclerView.setHasFixedSize(true);
  62. 61 recyclerView.setAdapter(adapterRecyclerProdutos);
  63. 62 recyclerView.addItemDecoration(new DividerItemDecoration(context, LinearLayout.VERTICAL));
  64. 63
  65. 64 }
  66. 65
  67. 66 public static void refreshRecyclerProdutos(final Context context, final SwipeRefreshLayout swipeRefreshLayout, final AdapterRecyclerProdutos adapterRecyclerProdutos, final DatabaseReference produtosRef, final View rootView, final TextView textStatus) {
  68. 67
  69. 68 swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
  70. 69 @Override
  71. 70 public void onRefresh() {
  72. 71 try {
  73. 72 ValueEventListener valueEventListenerProdutos = null;
  74. 73 valueEventListenerProdutos = recuperarProdutos(valueEventListenerProdutos, produtosRef, listaProdutos, rootView, adapterRecyclerProdutos, context, textStatus);
  75. 74
  76. 75 swipeRefreshLayout.setRefreshing(false);
  77. 76 produtosRef.removeEventListener(valueEventListenerProdutos);
  78. 77 Toast.makeText(context, "Lista Atualizada com Sucesso!", Toast.LENGTH_SHORT).show();
  79. 78
  80. 79 } catch (Exception e) {
  81. 80 e.printStackTrace();
  82. 81 Toast.makeText(context, "Erro: " + e.getMessage(), Toast.LENGTH_SHORT).show();
  83. 82 }
  84. 83
  85. 84 }
  86. 85 });
  87. 86
  88. 87 }
  89. 88
  90. 89 public static ValueEventListener recuperarProdutos(ValueEventListener valueEventListenerProdutos, DatabaseReference produtosRef, final List<Produtos> listaProdutos, final View rootView, final AdapterRecyclerProdutos adapterRecyclerProdutos, final Context context, final TextView textStatus) {
  91. 90 try {
  92. 91 valueEventListenerProdutos = produtosRef.addValueEventListener(new ValueEventListener() {
  93. 92 @Override
  94. 93 public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  95. 94 listaProdutos.clear();
  96. 95 for (DataSnapshot dados : dataSnapshot.getChildren()) {
  97. 96
  98. 97 Produtos produto = dados.getValue(Produtos.class);
  99. 98
  100. 99 listaProdutos.add(produto);
  101. 100 }
  102. 101
  103. 102 setChildrenCount((int) dataSnapshot.getChildrenCount());
  104. 103 recyclerStatus(textStatus);
  105. 104 rootView.findViewById(R.id.carregamentoInicial).setVisibility(View.GONE);
  106. 105 adapterRecyclerProdutos.notifyDataSetChanged();
  107. 106
  108. 107 }
  109. 108
  110. 109 @Override
  111. 110 public void onCancelled(@NonNull DatabaseError databaseError) {
  112. 111
  113. 112 }
  114. 113 });
  115. 114 return valueEventListenerProdutos;
  116. 115 } catch (Exception e) {
  117. 116 e.printStackTrace();
  118. 117 Toast.makeText(context, "", Toast.LENGTH_SHORT).show();
  119. 118 }
  120. 119
  121. 120 return valueEventListenerProdutos;
  122. 121
  123. 122 }
  124. 123
  125. 124 public static void removerValueEventListener(DatabaseReference databaseReference, ValueEventListener valueEventListener, Context context) {
  126. 125 try {
  127. 126 databaseReference.removeEventListener(valueEventListener);
  128. 127 } catch (Exception e) {
  129. 128 Toast.makeText(context, "Erro: " + e.getMessage(), Toast.LENGTH_SHORT).show();
  130. 129 e.printStackTrace();
  131. 130 }
  132. 131 }
  133. 132
  134. 133 public static void recyclerStatus(TextView textStatus) {
  135. 134
  136. 135 int recyclerSize = getChildrenCount();
  137. 136
  138. 137 if (recyclerSize > 0) {
  139. 138 textStatus.setVisibility(View.GONE);
  140. 139 } else {
  141. 140 textStatus.setVisibility(View.VISIBLE);
  142. 141 }
  143. 142
  144. 143 }
  145. 144
  146. 145 public static String getResultado() {
  147. 146 return resultado;
  148. 147 }
  149. 148
  150. 149 public static void setResultado(String resultado) {
  151. 150 Configs.resultado = resultado;
  152. 151 }
  153. 152
  154. 153 public static String getUserID() {
  155. 154 return userID;
  156. 155 }
  157. 156
  158. 157 public static void setUserID(String userID) {
  159. 158 Configs.userID = userID;
  160. 159 }
  161. 160
  162. 161 // Geral - End
  163. 162
  164. 163 // InicioFragment - Begin
  165. 164
  166. 165 public static ValueEventListener fraseInspiradora(DatabaseReference databaseReference, final TextView textFrase, final Context context) {
  167. 166
  168. 167 final Random random = new Random();
  169. 168 ValueEventListener valueEventListener;
  170. 169
  171. 170 valueEventListener = databaseReference.child("frases").addValueEventListener(new ValueEventListener() {
  172. 171 @Override
  173. 172 public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  174. 173
  175. 174 try {
  176. 175
  177. 176 Frases frase;
  178. 177
  179. 178 setChildrenCount((int) dataSnapshot.getChildrenCount());
  180. 179 int rand = random.nextInt(childrenCount);
  181. 180
  182. 181 Iterator itr = dataSnapshot.getChildren().iterator();
  183. 182
  184. 183 for (int i = 0; i < rand; i++) {
  185. 184 itr.next();
  186. 185 }
  187. 186
  188. 187 DataSnapshot childSnapshot = (DataSnapshot) itr.next();
  189. 188 frase = childSnapshot.getValue(Frases.class);
  190. 189 if (frase != null) {
  191. 190 textFrase.setText('"' + frase.getFrase() + '"' + "\nAutor: " + frase.getAutor());
  192. 191 } else {
  193. 192 textFrase.setText("Nenhuma Frase Encontrada :(");
  194. 193 }
  195. 194
  196. 195
  197. 196 } catch (Exception e) {
  198. 197 e.printStackTrace();
  199. 198 Toast.makeText(context, "Erro: " + e.getMessage(), Toast.LENGTH_SHORT).show();
  200. 199 }
  201. 200
  202. 201
  203. 202 }
  204. 203
  205. 204 @Override
  206. 205 public void onCancelled(@NonNull DatabaseError databaseError) {
  207. 206
  208. 207 }
  209. 208 });
  210. 209
  211. 210 return valueEventListener;
  212. 211
  213. 212 }
  214. 213
  215. 214 public static void definirNomeUtilizador(TextView textIntro, Context context) {
  216. 215 try {
  217. 216
  218. 217 // Partir o Email em 2 partes antes do @ e depois do @, e ficar com a parte antes do @ e utilizar a mesma como nome de Utilizador
  219. 218 String user = ConfiguracaoFireBase.getFirebaseAuth().getCurrentUser().getEmail();
  220. 219 String[] parts = user.split("(?=@)");
  221. 220 String utilizador = parts[0];
  222. 221
  223. 222 // Aqui definimos o valor do Utilizador de "Administrador", se ele o for
  224. 223 if (utilizador.equals("thinkingaheadapps")) {
  225. 224 utilizador = "Administrador";
  226. 225
  227. 226 }
  228. 227
  229. 228 textIntro.setText(utilizador);
  230. 229
  231. 230 } catch (Exception e) {
  232. 231 e.printStackTrace();
  233. 232 Toast.makeText(context, "Erro: " + e.getMessage(), Toast.LENGTH_SHORT).show();
  234. 233 }
  235. 234
  236. 235 }
  237. 236
  238. 237 public static int getChildrenCount() {
  239. 238 return childrenCount;
  240. 239 }
  241. 240
  242. 241 public static void setChildrenCount(int childrenCount) {
  243. 242 Configs.childrenCount = childrenCount;
  244. 243 }
  245. 244
  246. 245 // InicioFragment - End
  247. 246
  248. 247 // InserirFragment - Begin
  249. 248
  250. 249 public static void enviarProduto(FloatingActionButton fab, final TextView textCodigo, final TextView textDescricao, final TextView textQuantidade, final Context context) {
  251. 250 fab.setOnClickListener(new View.OnClickListener() {
  252. 251 @Override
  253. 252 public void onClick(View view) {
  254. 253 try {
  255. 254
  256. 255 String codigo = textCodigo.getText().toString();
  257. 256 String descricao = textDescricao.getText().toString();
  258. 257 String quantidade = textQuantidade.getText().toString();
  259. 258 String uniqueID = UUID.randomUUID().toString();
  260. 259
  261. 260 if (!codigo.isEmpty() || !descricao.isEmpty() || !quantidade.isEmpty()) {
  262. 261 Produtos produto = new Produtos(codigo, descricao, quantidade, uniqueID);
  263. 262 produto.guardarDados();
  264. 263
  265. 264 Toast.makeText(context, "Produto Enviado com Sucesso!", Toast.LENGTH_SHORT).show();
  266. 265 limparCamposInserir(textCodigo, textQuantidade, textDescricao);
  267. 266
  268. 267 setIsResultadoFull(false);
  269. 268 } else {
  270. 269 Toast.makeText(context, "Preencha pelo menos um Campo!", Toast.LENGTH_SHORT).show();
  271. 270 }
  272. 271
  273. 272 } catch (Exception e) {
  274. 273 Toast.makeText(context, "Falha ao Enviar Produto!" + e.getMessage(), Toast.LENGTH_SHORT).show();
  275. 274 }
  276. 275 }
  277. 276 });
  278. 277 }
  279. 278
  280. 279 public static void abrirLeitorDeBarras(ImageButton imagemCamara, final Fragment fragment) {
  281. 280 imagemCamara.setOnClickListener(new View.OnClickListener() {
  282. 281 @Override
  283. 282 public void onClick(View view) {
  284. 283 fragment.startActivity(new Intent(fragment.getContext(), QrCodeActivity.class));
  285. 284 setIsResultadoFull(true);
  286. 285 }
  287. 286 });
  288. 287 }
  289. 288
  290. 289 public static void inserirCodigoProduto(TextView textCodigo) {
  291. 290
  292. 291 resultado = Configs.getResultado();
  293. 292
  294. 293 if (isResultadoFull) {
  295. 294 textCodigo.setText(resultado);
  296. 295 }
  297. 296
  298. 297 if (!isResultadoFull) {
  299. 298 textCodigo.setText("");
  300. 299 }
  301. 300
  302. 301 }
  303. 302
  304. 303 private static void limparCamposInserir(TextView textCodigo, TextView textQuantidade, TextView textDescricao) {
  305. 304 textCodigo.setText("");
  306. 305 textQuantidade.setText("");
  307. 306 textDescricao.setText("");
  308. 307 }
  309. 308
  310. 309 // InserirFragment - End
  311. 310
  312. 311 // RemoverFragment - Begin
  313. 312
  314. 313 public static void recyclerRemover(final Context c, RecyclerView recyclerView, final DatabaseReference firebaseRef, final AdapterRecyclerProdutos adapterRecyclerProdutos) {
  315. 314 recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(c, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
  316. 315 @Override
  317. 316 public void onItemClick(View view, int position) {
  318. 317
  319. 318 }
  320. 319
  321. 320 @Override
  322. 321 public void onLongItemClick(View view, final int position) {
  323. 322 final Produtos produto = listaProdutos.get(position);
  324. 323 final DatabaseReference produtosRef = firebaseRef.child(userID).child("produtos");
  325. 324
  326. 325 AlertDialog.Builder dialog = new AlertDialog.Builder(c);
  327. 326
  328. 327 // Configurar Título e Mensagem
  329. 328 dialog.setTitle("Confirmar Exclusão");
  330. 329 dialog.setCancelable(false);
  331. 330 dialog.setMessage("Deseja remover o produto: " + produto.getCodigo());
  332. 331 dialog.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
  333. 332 @Override
  334. 333 public void onClick(DialogInterface dialog, int which) {
  335. 334 try {
  336. 335 produtosRef.child(produto.getUniqueID()).removeValue();
  337. 336 adapterRecyclerProdutos.notifyItemRemoved(position);
  338. 337 Toast.makeText(c, "Eliminado com Sucesso!", Toast.LENGTH_SHORT).show();
  339. 338 adapterRecyclerProdutos.notifyDataSetChanged();
  340. 339
  341. 340 } catch (Exception e) {
  342. 341 Toast.makeText(c, "Erro: " + e.getMessage(), Toast.LENGTH_SHORT).show();
  343. 342
  344. 343 }
  345. 344
  346. 345 }
  347. 346 });
  348. 347 dialog.setNegativeButton("Não", null);
  349. 348 dialog.show();
  350. 349 }
  351. 350
  352. 351 @Override
  353. 352 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  354. 353
  355. 354 }
  356. 355 }));
  357. 356 }
  358. 357
  359. 358 // RemoverFragment - End
  360. 359
  361. 360 // AtualizarFragment - Begin
  362. 361
  363. 362 public static void recyclerAtualizar(final Context c, RecyclerView recyclerView, final AdapterRecyclerProdutos adapterRecyclerProdutos) {
  364. 363 recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(c, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
  365. 364 @Override
  366. 365 public void onItemClick(final View view, int position) {
  367. 366
  368. 367
  369. 368 }
  370. 369
  371. 370 @Override
  372. 371 public void onLongItemClick(View view, final int position) {
  373. 372
  374. 373 final Produtos produto = listaProdutos.get(position);
  375. 374
  376. 375 AlertDialog.Builder dialog = new AlertDialog.Builder(c);
  377. 376
  378. 377 // Configurar Título e Produto
  379. 378 dialog.setTitle("Confirmar Atualização");
  380. 379 dialog.setCancelable(false);
  381. 380 dialog.setMessage("Deseja atualizar o produto: " + produto.getCodigo());
  382. 381 dialog.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
  383. 382 @Override
  384. 383 public void onClick(DialogInterface dialog, int which) {
  385. 384 try {
  386. 385 Intent i = new Intent(c, AtualizarActivity.class);
  387. 386 i.putExtra("posicao", position);
  388. 387 c.startActivity(i);
  389. 388 adapterRecyclerProdutos.notifyDataSetChanged();
  390. 389 } catch (Exception e) {
  391. 390 Toast.makeText(c, "Erro: " + e.getMessage(), Toast.LENGTH_SHORT).show();
  392. 391
  393. 392 }
  394. 393
  395. 394 }
  396. 395 });
  397. 396 dialog.setNegativeButton("Não", null);
  398. 397 dialog.show();
  399. 398
  400. 399 }
  401. 400
  402. 401 @Override
  403. 402 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  404. 403
  405. 404 }
  406. 405 }));
  407. 406 }
  408. 407
  409. 408 // AtualizarFragment - End
  410. 409
  411. 410 // SobreFragment - Begin
  412. 411
  413. 412 public static View verificaoDoTema(Context context) {
  414. 413 View aboutPage = null;
  415. 414
  416. 415 try {
  417. 416 int currentNightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
  418. 417
  419. 418 switch (currentNightMode) {
  420. 419 case Configuration.UI_MODE_NIGHT_NO:
  421. 420 aboutPage = new AboutPage(context)
  422. 421 .isRTL(false)
  423. 422 .setImage(R.drawable.tautau_mascote_oculos)
  424. 423 .setDescription("Somos especializados, focados, divertidos e diretos. Vamos vestir a camisola da tua marca, empresa ou projeto com o objetivo de te proporcionar os melhores resultados. Esta é a nossa garantia.")
  425. 424 .addGroup("Ótimos Serviços, Profissionais Experientes")
  426. 425 .addEmail("geral@tautau.pt")
  427. 426 .addWebsite("https://tautau.pt/")
  428. 427 .addFacebook("tautauagency")
  429. 428 .addInstagram("tautauagency/")
  430. 429 .enableDarkMode(false)
  431. 430 .create();
  432. 431 break;
  433. 432 case Configuration.UI_MODE_NIGHT_YES:
  434. 433 aboutPage = new AboutPage(context)
  435. 434 .isRTL(false)
  436. 435 .setImage(R.drawable.tautau_mascote_oculos)
  437. 436 .setDescription("Somos especializados, focados, divertidos e diretos. Vamos vestir a camisola da tua marca, empresa ou projeto com o objetivo de te proporcionar os melhores resultados. Esta é a nossa garantia.")
  438. 437 .addGroup("Ótimos Serviços, Profissionais Experientes")
  439. 438 .addEmail("geral@tautau.pt")
  440. 439 .addWebsite("https://tautau.pt/")
  441. 440 .addFacebook("tautauagency")
  442. 441 .addInstagram("tautauagency/")
  443. 442 .enableDarkMode(true)
  444. 443 .create();
  445. 444 break;
  446. 445 default:
  447. 446 Toast.makeText(context, "Erro: " + currentNightMode, Toast.LENGTH_SHORT).show();
  448. 447 throw new IllegalStateException("Unexpected value: " + currentNightMode);
  449. 448 }
  450. 449 } catch (Exception e) {
  451. 450 Toast.makeText(context, "Erro: " + e.getMessage(), Toast.LENGTH_SHORT).show();
  452. 451 e.printStackTrace();
  453. 452 }
  454. 453
  455. 454 return aboutPage;
  456. 455
  457. 456 }
  458. 457
  459. 458 public static View definicaoDoLayoutSobre(View aboutPage, View view) {
  460. 459 if (aboutPage != null) {
  461. 460 return aboutPage;
  462. 461 } else {
  463. 462 aboutPage = view;
  464. 463 }
  465. 464 return aboutPage;
  466. 465 }
  467. 466
  468. 467 // SobreFragment - End
  469. 468
  470. 469 // FraseActivity - Begin
  471. 470
  472. 471 public static void adicionarFrase(Button buttonConfirmar, final EditText editAutor, final EditText editFrase, final Context c, final Activity activity) {
  473. 472 buttonConfirmar.setOnClickListener(new View.OnClickListener() {
  474. 473 @Override
  475. 474 public void onClick(View view) {
  476. 475
  477. 476 String autor = editAutor.getText().toString();
  478. 477 String fraseInspiradora = editFrase.getText().toString();
  479. 478 String childLength = String.valueOf(getChildrenCount() + 1);
  480. 479
  481. 480 try {
  482. 481 Frases frase = new Frases();
  483. 482 frase.setFrase(fraseInspiradora);
  484. 483 frase.setId(childLength);
  485. 484 frase.setAutor(autor);
  486. 485 frase.guardar();
  487. 486
  488. 487 limparCamposFrase(editAutor, editFrase);
  489. 488 Toast.makeText(c, "Frase adicionada com Sucesso!", Toast.LENGTH_SHORT).show();
  490. 489 activity.finish();
  491. 490 } catch (Exception e) {
  492. 491 e.printStackTrace();
  493. 492 Toast.makeText(c, "Erro: " + e.getMessage(), Toast.LENGTH_SHORT).show();
  494. 493 }
  495. 494
  496. 495
  497. 496 }
  498. 497 });
  499. 498 }
  500. 499
  501. 500 private static void limparCamposFrase(EditText editAutor, EditText editFrase) {
  502. 501
  503. 502 editAutor.setText("");
  504. 503 editFrase.setText("");
  505. 504
  506. 505 }
  507. 506
  508. 507 // FraseActivity - End
  509. 508
  510. 509 // AtualizarActivity - Begin
  511. 510
  512. 511 public static void atualizarProduto(FloatingActionButton fab, final Produtos produto, final TextView textCodigo, final TextView textDescricao, final TextView textQuantidade, final Activity activity, ImageButton imageReader) {
  513. 512 fab.setOnClickListener(new View.OnClickListener() {
  514. 513 @Override
  515. 514 public void onClick(View view) {
  516. 515
  517. 516 produto.setCodigo(textCodigo.getText().toString());
  518. 517 produto.setDescricao(textDescricao.getText().toString());
  519. 518 produto.setQuantidade(textQuantidade.getText().toString());
  520. 519
  521. 520 produto.guardarDados();
  522. 521
  523. 522 Toast.makeText(activity.getApplicationContext(), "Produto Atualizado com Sucesso!", Toast.LENGTH_SHORT).show();
  524. 523 limparCamposAtualizar(textCodigo, textQuantidade, textDescricao);
  525. 524 setIsResultadoFull(false);
  526. 525 activity.finish();
  527. 526 }
  528. 527 });
  529. 528
  530. 529 imageReader.setOnClickListener(new View.OnClickListener() {
  531. 530 @Override
  532. 531 public void onClick(View view) {
  533. 532 activity.startActivity(new Intent(activity.getApplicationContext(), QrCodeActivity.class));
  534. 533 setIsResultadoFull(true);
  535. 534 }
  536. 535 });
  537. 536
  538. 537 Log.i("msg", "resultado:" + getIsResultadoFull());
  539. 538
  540. 539 }
  541. 540
  542. 541 private static void limparCamposAtualizar(TextView textCodigo, TextView textQuantidade, TextView textDescricao) {
  543. 542 textCodigo.setText("");
  544. 543 textQuantidade.setText("");
  545. 544 textDescricao.setText("");
  546. 545 }
  547. 546
  548. 547 public static void atualizarCodigoProduto(Context context, TextView textCodigo, Produtos produto) {
  549. 548 String resultado = Configs.getResultado();
  550. 549
  551. 550 try {
  552. 551 if (Configs.getIsResultadoFull()) {
  553. 552 textCodigo.setText(resultado);
  554. 553 } else {
  555. 554 textCodigo.setText(produto.getCodigo());
  556. 555 }
  557. 556 } catch (Exception e) {
  558. 557 e.printStackTrace();
  559. 558 Toast.makeText(context, "Erro: " + e.getMessage(), Toast.LENGTH_SHORT).show();
  560. 559 }
  561. 560
  562. 561 }
  563. 562
  564. 563 public static Boolean getIsResultadoFull() {
  565. 564 return isResultadoFull;
  566. 565 }
  567. 566
  568. 567 public static void setIsResultadoFull(Boolean isResultadoFull) {
  569. 568 Configs.isResultadoFull = isResultadoFull;
  570. 569 }
  571. 570
  572. 571 // AtualizarActivity - End
  573. 572
  574. 573 // HomeActivity - Begin
  575. 574
  576. 575 public static void configuracaoMenuHome(MenuItem item, Activity activity) {
  577. 576 switch (item.getItemId()) {
  578. 577 case R.id.menuSair:
  579. 578 ConfiguracaoFireBase.getFirebaseAuth().signOut();
  580. 579 activity.startActivity(new Intent(activity.getApplicationContext(), MainActivity.class));
  581. 580 activity.finish();
  582. 581 break;
  583. 582 case R.id.menuAdicionarFrase:
  584. 583 activity.startActivity(new Intent(activity.getApplicationContext(), AdicionarFraseActivity.class));
  585. 584 break;
  586. 585 case R.id.menuRemoverFrase:
  587. 586 activity.startActivity(new Intent(activity.getApplicationContext(), RemoverFraseActivity.class));
  588. 587 break;
  589. 588 }
  590. 589 }
  591. 590
  592. 591 public static void definicaoMenuHome(Menu menu, Activity activity) {
  593. 592 MenuInflater inflater = activity.getMenuInflater();
  594. 593 inflater.inflate(R.menu.menu_home, menu);
  595. 594 }
  596. 595
  597. 596 public static void definirUserNameFireBase() {
  598. 597
  599. 598 // Partir o Email em 2 partes antes do @ e depois do @, e ficar com a parte antes do @ e utilizar a mesma como nome de Utilizador
  600. 599 String user = ConfiguracaoFireBase.getFirebaseAuth().getCurrentUser().getEmail();
  601. 600 String[] parts = user.split("(?=@)");
  602. 601 String utilizador = parts[0];
  603. 602
  604. 603 UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
  605. 604 .setDisplayName(utilizador).build();
  606. 605
  607. 606 ConfiguracaoFireBase.getFirebaseAuth().getCurrentUser().updateProfile(profileUpdates);
  608. 607
  609. 608 }
  610. 609
  611. 610 public static void definirPerfil(TextView textUserName, TextView textEmail, Context context) {
  612. 611 try {
  613. 612 textUserName.setText(ConfiguracaoFireBase.getFirebaseAuth().getCurrentUser().getDisplayName());
  614. 613 textEmail.setText(ConfiguracaoFireBase.getFirebaseAuth().getCurrentUser().getEmail());
  615. 614
  616. 615 } catch (Exception e) {
  617. 616 e.printStackTrace();
  618. 617 Toast.makeText(context, "Erro: " + e.getMessage(), Toast.LENGTH_SHORT).show();
  619. 618 }
  620. 619 }
  621. 620
  622. 621 // HomeActivity - End
  623. 622
  624. 623 // LoadingActivity - Begin
  625. 624
  626. 625 public static void configuracaoDoLoadingInicial(final Activity activity, final Intent mainActivity) {
  627. 626 new java.util.Timer().schedule(
  628. 627 new java.util.TimerTask() {
  629. 628 @Override
  630. 629 public void run() {
  631. 630 activity.startActivity(mainActivity);
  632. 631 activity.finish();
  633. 632 }
  634. 633 },
  635. 634 3000
  636. 635 );
  637. 636 }
  638. 637
  639. 638 // LoadingActivity - End
  640. 639
  641. 640 // MainActivity - Begin
  642. 641
  643. 642 public static void loginUtilizador(FirebaseAuth mAuth, final EditText textEmail, final EditText textPassword, final Activity activity) {
  644. 643
  645. 644 try {
  646. 645 mAuth.signInWithEmailAndPassword(textEmail.getText().toString(), textPassword.getText().toString()).addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
  647. 646 @Override
  648. 647 public void onComplete(@NonNull Task<AuthResult> task) {
  649. 648 if (task.isSuccessful()) {
  650. 649 Toast.makeText(activity.getApplicationContext(), "Login Efetuado com Sucesso!", Toast.LENGTH_SHORT).show();
  651. 650 limparCamposLogin(textEmail, textPassword);
  652. 651 Intent intent = new Intent(activity.getApplicationContext(), HomeActivity.class);
  653. 652 activity.startActivity(intent);
  654. 653 } else {
  655. 654 String excecao;
  656. 655
  657. 656 try {
  658. 657 throw task.getException();
  659. 658 } catch (FirebaseAuthInvalidUserException e) {
  660. 659 excecao = "Utilizador não Registado";
  661. 660 } catch (FirebaseAuthInvalidCredentialsException e) {
  662. 661 excecao = "Credenciais Incorretas";
  663. 662 } catch (Exception e) {
  664. 663 excecao = "Erro ao fazer Login: " + e.getMessage();
  665. 664 e.printStackTrace();
  666. 665 }
  667. 666
  668. 667 Toast.makeText(activity.getApplicationContext(), excecao, Toast.LENGTH_SHORT).show();
  669. 668
  670. 669 }
  671. 670 }
  672. 671 });
  673. 672
  674. 673 } catch (Exception e) {
  675. 674 e.printStackTrace();
  676. 675 Toast.makeText(activity, "Erro: " + e.getMessage(), Toast.LENGTH_SHORT).show();
  677. 676 }
  678. 677
  679. 678 }
  680. 679
  681. 680 public static void verificarUtilizador(FirebaseAuth mAuth, Activity activity, Intent mainIntent) {
  682. 681 try {
  683. 682
  684. 683 if (mAuth.getCurrentUser() != null) {
  685. 684 setUserID(FirebaseAuth.getInstance().getCurrentUser().getUid());
  686. 685 activity.startActivity(mainIntent);
  687. 686 activity.finish();
  688. 687 }
  689. 688
  690. 689 } catch (Exception e) {
  691. 690 e.printStackTrace();
  692. 691 Toast.makeText(activity.getApplicationContext(), "Erro: " + e.getMessage(), Toast.LENGTH_SHORT).show();
  693. 692 }
  694. 693 }
  695. 694
  696. 695 // SobreActivity - Begin
  697. 696
  698. 697 public static void configuracaoMenuMain(MenuItem item, Activity activity) {
  699. 698 switch (item.getItemId()) {
  700. 699 case R.id.menuSobre:
  701. 700 ConfiguracaoFireBase.getFirebaseAuth().signOut();
  702. 701 activity.startActivity(new Intent(activity.getApplicationContext(), SobreActivity.class));
  703. 702 break;
  704. 703 case R.id.menuEsquecerPassword:
  705. 704 activity.startActivity(new Intent(activity.getApplicationContext(), EsquecerPasswordActivity.class));
  706. 705 break;
  707. 706 }
  708. 707 }
  709. 708
  710. 709 public static void definicaoMenuMain(Menu menu, Activity activity) {
  711. 710 MenuInflater inflater = activity.getMenuInflater();
  712. 711 inflater.inflate(R.menu.menu_main, menu);
  713. 712 }
  714. 713
  715. 714 // SobreActivity - End
  716. 715
  717. 716 public static void botaoLogin(Button buttonLogin, final EditText textEmail, final EditText textPassword, final Activity activity, final FirebaseAuth mAuth) {
  718. 717 buttonLogin.setOnClickListener(new View.OnClickListener() {
  719. 718 @Override
  720. 719 public void onClick(View view) {
  721. 720 try {
  722. 721
  723. 722 if (!textEmail.getText().toString().equals("")) {
  724. 723 if (!textPassword.getText().toString().equals("")) {
  725. 724 loginUtilizador(mAuth, textEmail, textPassword, activity);
  726. 725 } else {
  727. 726 Toast.makeText(activity.getApplicationContext(), "Introduza a Password!", Toast.LENGTH_SHORT).show();
  728. 727 }
  729. 728 } else {
  730. 729 Toast.makeText(activity.getApplicationContext(), "Introduza o Email!", Toast.LENGTH_SHORT).show();
  731. 730 }
  732. 731
  733. 732 } catch (Exception e) {
  734. 733 e.printStackTrace();
  735. 734 Toast.makeText(activity.getApplicationContext(), "Tente Novamente!", Toast.LENGTH_SHORT).show();
  736. 735 }
  737. 736
  738. 737
  739. 738 }
  740. 739 });
  741. 740 }
  742. 741
  743. 742 private static void limparCamposLogin(EditText textEmail, EditText textPassword) {
  744. 743 textEmail.setText("");
  745. 744 textPassword.setText("");
  746. 745 }
  747. 746
  748. 747 // MainActivity - End
  749. 748
  750. 749 // QrCodeActivity - Begin
  751. 750
  752. 751 public static void scanCode(IntentIntegrator integrator, Activity activity) {
  753. 752
  754. 753 try {
  755. 754 integrator.setCaptureActivity(CapturaActivity.class);
  756. 755 integrator.setOrientationLocked(false);
  757. 756 integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
  758. 757 integrator.setPrompt("Procurando Código de Barras...");
  759. 758 integrator.initiateScan();
  760. 759
  761. 760 } catch (Exception e) {
  762. 761 e.printStackTrace();
  763. 762 Toast.makeText(activity, "Erro: " + e.getMessage(), Toast.LENGTH_SHORT).show();
  764. 763 }
  765. 764
  766. 765
  767. 766 }
  768. 767
  769. 768 public static void lerCodigoDeBarras(int requestCode, int resultCode, Intent data, final Activity activity, final IntentIntegrator integrator) {
  770. 769
  771. 770 try {
  772. 771
  773. 772 IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
  774. 773 setResultado(result.getContents());
  775. 774
  776. 775 if (result != null) {
  777. 776 if (result.getContents() != null) {
  778. 777 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
  779. 778 builder.setCancelable(false);
  780. 779 builder.setMessage(result.getContents());
  781. 780 builder.setTitle("Resultado:");
  782. 781 builder.setPositiveButton("Novamente", new DialogInterface.OnClickListener() {
  783. 782 @Override
  784. 783 public void onClick(DialogInterface dialogInterface, int i) {
  785. 784 Configs.scanCode(integrator, activity);
  786. 785 }
  787. 786 }).setNegativeButton("Sair", new DialogInterface.OnClickListener() {
  788. 787 @Override
  789. 788 public void onClick(DialogInterface dialogInterface, int i) {
  790. 789 activity.finish();
  791. 790 }
  792. 791 });
  793. 792 AlertDialog dialog = builder.create();
  794. 793 dialog.show();
  795. 794 } else {
  796. 795 Toast.makeText(activity.getApplicationContext(), "Sem Resultados", Toast.LENGTH_LONG).show();
  797. 796 activity.finish();
  798. 797 }
  799. 798 }
  800. 799
  801. 800 } catch (Exception e) {
  802. 801 e.printStackTrace();
  803. 802 Toast.makeText(activity, "Erro: " + e.getMessage(), Toast.LENGTH_SHORT).show();
  804. 803 }
  805. 804
  806. 805
  807. 806 }
  808. 807
  809. 808 // QrCodeActivity - End
  810. 809
  811. 810 // Adapter - Begin
  812. 811
  813. 812 // NENHUM CODIGO AQUI
  814. 813 // PARA FACILITAR DECIDI DEIXAR O CÓDIGO NO ADAPTER
  815. 814
  816. 815 // Adapter - End
  817. 816
  818. 817
  819. 818}
  820. 819
Add Comment
Please, Sign In to add comment