123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- package uprrp.tania.fragments;
-
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.Toast;
-
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AlertDialog;
- import androidx.fragment.app.Fragment;
-
- import com.google.android.gms.tasks.OnSuccessListener;
- import com.google.firebase.iid.FirebaseInstanceId;
- import com.google.firebase.iid.InstanceIdResult;
-
- import java.util.Objects;
-
- import uprrp.tania.R;
- import uprrp.tania.URLEventListener;
- import uprrp.tania.activities.GettingStartedActivity;
- import uprrp.tania.networking.SendWithdrawal;
-
- public class WithdrawFragment extends Fragment {
-
- private static final String TAG = "WithdrawFragment";
- private String DEVICE_TOKEN;
-
- @Nullable
- @Override
- public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
-
- // Get reference for this fragment and other components
- View view = inflater.inflate(R.layout.fragment_withdrawal, container, false);
- final Button withdrawButton = view.findViewById(R.id.withdrawButton);
-
- // Change all caps text to normal capitalization
- // TODO: this is a workaround I found, any other acceptable solution is welcome
- withdrawButton.setTransformationMethod(null);
- withdrawButton.setText(R.string.loadingText);
-
- // Get device's token and attach click listener once we have it
- FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
- @Override
- public void onSuccess(InstanceIdResult instanceIdResult) {
- DEVICE_TOKEN = instanceIdResult.getToken();
- withdrawButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- promptConfirmation();
- }
- });
- withdrawButton.setText(R.string.withdrawButtonText);
- }
- });
-
- return view;
- }
-
- private void promptConfirmation() {
- AlertDialog.Builder builder = new AlertDialog.Builder(Objects.requireNonNull(getContext()));
- builder.setCancelable(true);
- builder.setTitle("WITHDRAWING");
- builder.setMessage("Are you absolutely sure you want to withdraw from the project?");
- builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- sendWithdrawalRequest();
- }
- });
- builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Log.d(TAG, "Cancelled withdrawal!");
- }
- });
-
- AlertDialog dialog = builder.create();
- dialog.show();
- }
-
- private void sendWithdrawalRequest() {
-
- // Initiate progress dialog
- // TODO: find substitute for this deprecated dialog box
- final ProgressDialog progressDialog = ProgressDialog.show(getContext(),
- "Withdrawing",
- "This shouldn't take long");
-
- // Send withdrawal request to server
- SendWithdrawal sendWithdrawalTask = new SendWithdrawal(new URLEventListener() {
- @Override
- public void onSuccess() {
-
- progressDialog.dismiss();
- Context context = getContext();
-
- // Change system preferences to begin in GettingStartedActivity by default
- SharedPreferences prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = prefs.edit();
- editor.putBoolean("needsToRegister", true);
- editor.apply();
-
- // Start GettingStartedActivity with a success message
- Intent intent = new Intent(context, GettingStartedActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(intent);
- Toast.makeText(context.getApplicationContext(), "Successfully withdrawn!", Toast.LENGTH_LONG).show();
-
- }
-
- @Override
- public void onFailure(Exception e) {
- progressDialog.dismiss();
- Toast.makeText(getContext(), "Oops! Something went wrong...", Toast.LENGTH_LONG).show();
- Log.e(TAG, "Error occurred while sending withdrawal request to server...");
- e.printStackTrace();
- }
- });
-
- // Start task
- sendWithdrawalTask.execute(DEVICE_TOKEN);
-
- // UNCOMMENT THIS FOR TESTING (REMEMBER TO COMMENT .execute() LINE)
- /*
- Handler handler = new Handler();
- handler.postDelayed(new Runnable() {
- public void run() {
- progressDialog.dismiss();
- if(true) { // Imitate success
- Context context = getContext();
- Intent intent = new Intent(context, GettingStartedActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(intent);
- Toast.makeText(context.getApplicationContext(), "Successfully withdrawn!", Toast.LENGTH_LONG).show();
- } else { // Imitate failure
- Toast.makeText(getContext(), "Oops! Something went wrong...", Toast.LENGTH_LONG).show();
- }
- }
- }, 5000);
- */
- // UNCOMMENT THIS FOR TESTING (REMEMBER TO COMMENT .execute() LINE)
-
- }
-
- }
|