No Description

WithdrawFragment.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package uprrp.tania.fragments;
  2. import android.app.ProgressDialog;
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.content.SharedPreferences;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.Button;
  13. import android.widget.Toast;
  14. import androidx.annotation.NonNull;
  15. import androidx.annotation.Nullable;
  16. import androidx.appcompat.app.AlertDialog;
  17. import androidx.fragment.app.Fragment;
  18. import com.google.android.gms.tasks.OnSuccessListener;
  19. import com.google.firebase.iid.FirebaseInstanceId;
  20. import com.google.firebase.iid.InstanceIdResult;
  21. import java.util.Objects;
  22. import uprrp.tania.R;
  23. import uprrp.tania.URLEventListener;
  24. import uprrp.tania.activities.GettingStartedActivity;
  25. import uprrp.tania.networking.SendWithdrawal;
  26. public class WithdrawFragment extends Fragment {
  27. private static final String TAG = "WithdrawFragment";
  28. private String DEVICE_TOKEN;
  29. @Nullable
  30. @Override
  31. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  32. // Get reference for this fragment and other components
  33. View view = inflater.inflate(R.layout.fragment_withdrawal, container, false);
  34. final Button withdrawButton = view.findViewById(R.id.withdrawButton);
  35. // Change all caps text to normal capitalization
  36. // TODO: this is a workaround I found, any other acceptable solution is welcome
  37. withdrawButton.setTransformationMethod(null);
  38. withdrawButton.setText(R.string.loadingText);
  39. // Get device's token and attach click listener once we have it
  40. FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
  41. @Override
  42. public void onSuccess(InstanceIdResult instanceIdResult) {
  43. DEVICE_TOKEN = instanceIdResult.getToken();
  44. withdrawButton.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View v) {
  47. promptConfirmation();
  48. }
  49. });
  50. withdrawButton.setText(R.string.withdrawButtonText);
  51. }
  52. });
  53. return view;
  54. }
  55. private void promptConfirmation() {
  56. AlertDialog.Builder builder = new AlertDialog.Builder(Objects.requireNonNull(getContext()));
  57. builder.setCancelable(true);
  58. builder.setTitle("WITHDRAWING");
  59. builder.setMessage("Are you absolutely sure you want to withdraw from the project?");
  60. builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
  61. @Override
  62. public void onClick(DialogInterface dialog, int which) {
  63. sendWithdrawalRequest();
  64. }
  65. });
  66. builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  67. @Override
  68. public void onClick(DialogInterface dialog, int which) {
  69. Log.d(TAG, "Cancelled withdrawal!");
  70. }
  71. });
  72. AlertDialog dialog = builder.create();
  73. dialog.show();
  74. }
  75. private void sendWithdrawalRequest() {
  76. // Initiate progress dialog
  77. // TODO: find substitute for this deprecated dialog box
  78. final ProgressDialog progressDialog = ProgressDialog.show(getContext(),
  79. "Withdrawing",
  80. "This shouldn't take long");
  81. // Send withdrawal request to server
  82. SendWithdrawal sendWithdrawalTask = new SendWithdrawal(new URLEventListener() {
  83. @Override
  84. public void onSuccess() {
  85. progressDialog.dismiss();
  86. Context context = getContext();
  87. // Change system preferences to begin in GettingStartedActivity by default
  88. SharedPreferences prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE);
  89. SharedPreferences.Editor editor = prefs.edit();
  90. editor.putBoolean("needsToRegister", true);
  91. editor.apply();
  92. // Start GettingStartedActivity with a success message
  93. Intent intent = new Intent(context, GettingStartedActivity.class);
  94. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  95. context.startActivity(intent);
  96. Toast.makeText(context.getApplicationContext(), "Successfully withdrawn!", Toast.LENGTH_LONG).show();
  97. }
  98. @Override
  99. public void onFailure(Exception e) {
  100. progressDialog.dismiss();
  101. Toast.makeText(getContext(), "Oops! Something went wrong...", Toast.LENGTH_LONG).show();
  102. Log.e(TAG, "Error occurred while sending withdrawal request to server...");
  103. e.printStackTrace();
  104. }
  105. });
  106. // Start task
  107. sendWithdrawalTask.execute(DEVICE_TOKEN);
  108. // UNCOMMENT THIS FOR TESTING (REMEMBER TO COMMENT .execute() LINE)
  109. /*
  110. Handler handler = new Handler();
  111. handler.postDelayed(new Runnable() {
  112. public void run() {
  113. progressDialog.dismiss();
  114. if(true) { // Imitate success
  115. Context context = getContext();
  116. Intent intent = new Intent(context, GettingStartedActivity.class);
  117. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  118. context.startActivity(intent);
  119. Toast.makeText(context.getApplicationContext(), "Successfully withdrawn!", Toast.LENGTH_LONG).show();
  120. } else { // Imitate failure
  121. Toast.makeText(getContext(), "Oops! Something went wrong...", Toast.LENGTH_LONG).show();
  122. }
  123. }
  124. }, 5000);
  125. */
  126. // UNCOMMENT THIS FOR TESTING (REMEMBER TO COMMENT .execute() LINE)
  127. }
  128. }