Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.Dialog;
- import android.app.NotificationManager;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v4.app.DialogFragment;
- import android.support.v4.app.NotificationCompat;
- import android.support.v4.content.ContextCompat;
- import android.support.v7.widget.AppCompatButton;
- import android.support.v7.widget.AppCompatEditText;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.inputmethod.EditorInfo;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import br.com.sapereaude.maskedEditText.MaskedEditText;
- import butterknife.BindView;
- import butterknife.ButterKnife;
- import butterknife.OnClick;
- import butterknife.OnEditorAction;
- import butterknife.OnTextChanged;
- import static android.content.Context.NOTIFICATION_SERVICE;
- public class AuthorizationFragment extends DialogFragment implements AuthUserView {
- private final String TAG = AuthorizationFragment.class.getSimpleName();
- private String unmaskedNumber = "";
- private AuthUserPresenter presenter;
- private PhoneNumber phoneNumber;
- private String deviceId = ""; //TODO
- private boolean mock = false;
- private PreferencesHelper preferences;
- private Gson gson;
- @BindView(R.id.phoneNumberForm)
- LinearLayout phoneNumberForm;
- @BindView(R.id.activationCodeForm)
- LinearLayout activationCodeForm;
- @BindView(R.id.editPhoneNumber)
- MaskedEditText editPhoneNumber;
- @BindView(R.id.btnNextForm)
- AppCompatButton btnNextForm;
- @BindView(R.id.editActivationCode)
- AppCompatEditText editActivationCode;
- @BindView(R.id.btnActivate)
- AppCompatButton btnActivate;
- public static AuthorizationFragment newInstance() {
- Bundle args = new Bundle();
- AuthorizationFragment fragment = new AuthorizationFragment();
- fragment.setArguments(args);
- return fragment;
- }
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- return inflater.inflate(R.layout.fragment_auth, container, false);
- }
- @NonNull
- @Override
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- Dialog dialog = new Dialog(getActivity());
- dialog.setCanceledOnTouchOutside(true);
- dialog.setContentView(R.layout.fragment_auth);
- Utils.initDialogLayout(dialog, this);
- return dialog;
- }
- @Override
- public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- ButterKnife.bind(this, view);
- presenter = new AuthUserPresenterImpl(this);
- gson = new Gson();
- preferences = PreferencesHelper.getInstance(getContext());
- }
- @OnEditorAction(R.id.editPhoneNumber)
- boolean onEditPhoneNumber(TextView v, int actionId, KeyEvent event) {
- if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE)
- KeyboardUtils.hideKeyboard(getActivity());
- return false;
- }
- @OnClick(R.id.btnNextForm)
- void onNext() {
- unmaskedNumber = editPhoneNumber.getRawText().trim();
- if (mock)
- phoneNumber = new PhoneNumber("380", Constants.MOCK_NUMBER);
- else {
- phoneNumber = new PhoneNumber("380", unmaskedNumber);
- deviceId = Utils.getDeviceId(getContext());
- }
- //todo
- LoginRequest loginRequest = new LoginRequest(Constants.APP_VERSION,
- deviceId, phoneNumber, Constants.DEVICE);
- presenter.requestLogin(loginRequest);
- //TODO validate number input + server answer?
- }
- @OnClick(R.id.btnActivate)
- void onActivate() {
- ActivationRequest activationRequest = new ActivationRequest(Constants.APP_VERSION, editActivationCode.getText().toString(),
- deviceId, phoneNumber, Constants.DEVICE);
- presenter.requestActivation(activationRequest);
- }
- @OnTextChanged(R.id.editPhoneNumber)
- void onPhoneNumberChanged() {
- if (editPhoneNumber.getRawText().length() == 9) {
- btnNextForm.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.lightGreen));
- }
- }
- @OnTextChanged(R.id.editActivationCode)
- void onEditActivationCode() {
- if (editActivationCode.getText().toString().length() >= 4)
- btnActivate.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.lightGreen));
- else
- btnActivate.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.lightGray));
- }
- @Override
- public void onAuth(LoginResponse loginResponse) {
- if (loginResponse.getToken() != null) {
- PreferencesHelper.getInstance(getActivity()).saveToken(loginResponse.getToken());
- dismiss();
- Constants.userType = Constants.UserType.FULL;
- preferences.saveUserNumber(gson.toJson(loginResponse.getNumber()));
- startActivity(new Intent(getContext(), DeliveriesActivity.class));
- } else {
- showActivationPush(loginResponse);
- phoneNumberForm.setVisibility(View.GONE);
- activationCodeForm.setVisibility(View.VISIBLE);
- KeyboardUtils.hideKeyboard(getActivity());
- }
- }
- private void showActivationPush(LoginResponse loginResponse) {
- String tempActivationCode = loginResponse.getActivationCode();
- //mock
- NotificationCompat.Builder mBuilder =
- new NotificationCompat.Builder(getContext())
- .setSmallIcon(R.drawable.cart_icon)
- .setContentTitle("Code")
- .setContentText(tempActivationCode);
- int mNotificationId = 333;
- NotificationManager mNotifyMgr =
- (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);
- mNotifyMgr.notify(mNotificationId, mBuilder.build());
- }
- @Override
- public void onActivation(ActivationResponse activationResponse) {
- preferences.saveUserNumber(gson.toJson(activationResponse.getPhoneNumber()));
- PreferencesHelper.getInstance(getContext()).saveToken(activationResponse.getToken());
- dismiss();
- Constants.userType = Constants.UserType.FULL;
- startActivity(new Intent(getContext(), DeliveriesActivity.class));
- }
- @Override
- public void onError() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement