Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
5.0 kB
3
Indexable
Never
package com.twainarc.minibankpro.view.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.twainarc.minibankpro.databinding.FragmentRegisterBinding;
import com.twainarc.minibankpro.dialog.ErrorDialog;
import com.twainarc.minibankpro.dialog.ProgressDialog;
import com.twainarc.minibankpro.dialog.SuccessDialog;
import com.twainarc.minibankpro.listener.FragmentListener;
import com.twainarc.minibankpro.model.ApiResponse;
import com.twainarc.minibankpro.model.Content;
import com.twainarc.minibankpro.util.Constant;
import com.twainarc.minibankpro.util.Validation;
import com.twainarc.minibankpro.viewmodel.LoginViewModel;

public class RegisterFragment extends Fragment implements SuccessDialog.SuccessDialogListener {

    private FragmentRegisterBinding binding;
    private DialogFragment dialog;
    private FragmentListener listener;

    public RegisterFragment(FragmentListener listener) {

        this.listener = (FragmentListener) listener;
    }

    public static RegisterFragment newInstance(FragmentListener listener) {

        RegisterFragment fragment = new RegisterFragment(listener);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        dialog = new DialogFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        binding = FragmentRegisterBinding.inflate(inflater);
        init();

        return binding.getRoot();
    }

    private void init() {

        binding.nextIV.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                String userName = binding.userNameET.getText().toString().trim();
                String number = binding.phoneNumberET.getText().toString().trim();
                String playerId = binding.playerIdET.getText().toString().trim();

                if (!userName.isEmpty() && Validation.getInstance().isValidNumber(number) && Validation.getInstance().isValidPlayerId(playerId)) {

                    showDialog(Constant.PROGRESS, "");

                    LoginViewModel viewModel = new ViewModelProvider(RegisterFragment.this).get(LoginViewModel.class);

                    viewModel.registerUser(number, userName, playerId).observe(getViewLifecycleOwner(), new Observer<ApiResponse>() {

                        @Override
                        public void onChanged(ApiResponse apiResponse) {

                            if (apiResponse.getStatus().equals(Constant.STATUS_SUCCESS)) {

                                RegisterFragment.this.dialog.dismiss();
                                showDialog(Constant.SUCCESS, "");

                            } else {

                                RegisterFragment.this.dialog.dismiss();
                                showDialog(Constant.ERROR, apiResponse.getFailReason());
                            }
                        }
                    });

                } else {

                    if (userName.isEmpty()) {

                        binding.userNameET.setError("Enter Name");
                    }

                    if (!Validation.getInstance().isValidNumber(number)) {

                        binding.phoneNumberET.setError("Enter Valid Number");
                    }

                    if (!Validation.getInstance().isValidPlayerId(playerId)) {

                        binding.playerIdET.setError("Enter Valid Player Id");
                    }

                }
            }
        });

    }

    private void showDialog(int type, String failReason) {

        if (dialog.isVisible()) {

            return;
        }

        FragmentTransaction ft = requireActivity().getSupportFragmentManager().beginTransaction();
        Fragment prevFragment = requireActivity().getSupportFragmentManager().findFragmentByTag("dialog");

        if (prevFragment != null) {

            ft.remove(prevFragment);
        }

        ft.addToBackStack(null);

        if (type == Constant.PROGRESS) {

            dialog = ProgressDialog.newInstance();

        } else if (type == Constant.SUCCESS) {

            dialog = SuccessDialog.newInstance(new Content("Registration Complete",
                    "New account created with your given phone number",
                    "Login", 2), this);

        } else {

            dialog = ErrorDialog.newInstance(new Content("Registration Failed",
                    failReason,
                    "Continue", 1));
        }

        dialog.show(ft, "dialog");
    }

    @Override
    public void onButtonClick() {

        listener.communicate("");
    }


}