Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. import android.app.Dialog;
  2. import android.app.NotificationManager;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.annotation.NonNull;
  6. import android.support.annotation.Nullable;
  7. import android.support.v4.app.DialogFragment;
  8. import android.support.v4.app.NotificationCompat;
  9. import android.support.v4.content.ContextCompat;
  10. import android.support.v7.widget.AppCompatButton;
  11. import android.support.v7.widget.AppCompatEditText;
  12. import android.util.Log;
  13. import android.view.KeyEvent;
  14. import android.view.LayoutInflater;
  15. import android.view.View;
  16. import android.view.ViewGroup;
  17. import android.view.inputmethod.EditorInfo;
  18. import android.widget.LinearLayout;
  19. import android.widget.TextView;
  20.  
  21. import br.com.sapereaude.maskedEditText.MaskedEditText;
  22. import butterknife.BindView;
  23. import butterknife.ButterKnife;
  24. import butterknife.OnClick;
  25. import butterknife.OnEditorAction;
  26. import butterknife.OnTextChanged;
  27.  
  28. import static android.content.Context.NOTIFICATION_SERVICE;
  29.  
  30. public class AuthorizationFragment extends DialogFragment implements AuthUserView {
  31.  
  32. private final String TAG = AuthorizationFragment.class.getSimpleName();
  33. private String unmaskedNumber = "";
  34. private AuthUserPresenter presenter;
  35. private PhoneNumber phoneNumber;
  36. private String deviceId = ""; //TODO
  37. private boolean mock = false;
  38. private PreferencesHelper preferences;
  39. private Gson gson;
  40.  
  41. @BindView(R.id.phoneNumberForm)
  42. LinearLayout phoneNumberForm;
  43. @BindView(R.id.activationCodeForm)
  44. LinearLayout activationCodeForm;
  45. @BindView(R.id.editPhoneNumber)
  46. MaskedEditText editPhoneNumber;
  47. @BindView(R.id.btnNextForm)
  48. AppCompatButton btnNextForm;
  49. @BindView(R.id.editActivationCode)
  50. AppCompatEditText editActivationCode;
  51. @BindView(R.id.btnActivate)
  52. AppCompatButton btnActivate;
  53.  
  54. public static AuthorizationFragment newInstance() {
  55. Bundle args = new Bundle();
  56. AuthorizationFragment fragment = new AuthorizationFragment();
  57. fragment.setArguments(args);
  58. return fragment;
  59. }
  60.  
  61. @Nullable
  62. @Override
  63. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  64. return inflater.inflate(R.layout.fragment_auth, container, false);
  65. }
  66.  
  67. @NonNull
  68. @Override
  69. public Dialog onCreateDialog(Bundle savedInstanceState) {
  70. Dialog dialog = new Dialog(getActivity());
  71. dialog.setCanceledOnTouchOutside(true);
  72. dialog.setContentView(R.layout.fragment_auth);
  73. Utils.initDialogLayout(dialog, this);
  74. return dialog;
  75. }
  76.  
  77.  
  78. @Override
  79. public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  80. super.onViewCreated(view, savedInstanceState);
  81. ButterKnife.bind(this, view);
  82. presenter = new AuthUserPresenterImpl(this);
  83. gson = new Gson();
  84. preferences = PreferencesHelper.getInstance(getContext());
  85. }
  86.  
  87. @OnEditorAction(R.id.editPhoneNumber)
  88. boolean onEditPhoneNumber(TextView v, int actionId, KeyEvent event) {
  89. if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE)
  90. KeyboardUtils.hideKeyboard(getActivity());
  91. return false;
  92. }
  93.  
  94. @OnClick(R.id.btnNextForm)
  95. void onNext() {
  96. unmaskedNumber = editPhoneNumber.getRawText().trim();
  97. if (mock)
  98. phoneNumber = new PhoneNumber("380", Constants.MOCK_NUMBER);
  99. else {
  100. phoneNumber = new PhoneNumber("380", unmaskedNumber);
  101. deviceId = Utils.getDeviceId(getContext());
  102. }
  103. //todo
  104. LoginRequest loginRequest = new LoginRequest(Constants.APP_VERSION,
  105. deviceId, phoneNumber, Constants.DEVICE);
  106. presenter.requestLogin(loginRequest);
  107. //TODO validate number input + server answer?
  108. }
  109.  
  110. @OnClick(R.id.btnActivate)
  111. void onActivate() {
  112. ActivationRequest activationRequest = new ActivationRequest(Constants.APP_VERSION, editActivationCode.getText().toString(),
  113. deviceId, phoneNumber, Constants.DEVICE);
  114. presenter.requestActivation(activationRequest);
  115.  
  116. }
  117.  
  118. @OnTextChanged(R.id.editPhoneNumber)
  119. void onPhoneNumberChanged() {
  120. if (editPhoneNumber.getRawText().length() == 9) {
  121. btnNextForm.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.lightGreen));
  122. }
  123. }
  124.  
  125. @OnTextChanged(R.id.editActivationCode)
  126. void onEditActivationCode() {
  127. if (editActivationCode.getText().toString().length() >= 4)
  128. btnActivate.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.lightGreen));
  129. else
  130. btnActivate.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.lightGray));
  131. }
  132.  
  133. @Override
  134. public void onAuth(LoginResponse loginResponse) {
  135. if (loginResponse.getToken() != null) {
  136. PreferencesHelper.getInstance(getActivity()).saveToken(loginResponse.getToken());
  137. dismiss();
  138. Constants.userType = Constants.UserType.FULL;
  139. preferences.saveUserNumber(gson.toJson(loginResponse.getNumber()));
  140. startActivity(new Intent(getContext(), DeliveriesActivity.class));
  141. } else {
  142. showActivationPush(loginResponse);
  143. phoneNumberForm.setVisibility(View.GONE);
  144. activationCodeForm.setVisibility(View.VISIBLE);
  145. KeyboardUtils.hideKeyboard(getActivity());
  146. }
  147.  
  148. }
  149.  
  150. private void showActivationPush(LoginResponse loginResponse) {
  151. String tempActivationCode = loginResponse.getActivationCode();
  152. //mock
  153. NotificationCompat.Builder mBuilder =
  154. new NotificationCompat.Builder(getContext())
  155. .setSmallIcon(R.drawable.cart_icon)
  156. .setContentTitle("Code")
  157. .setContentText(tempActivationCode);
  158. int mNotificationId = 333;
  159. NotificationManager mNotifyMgr =
  160. (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);
  161. mNotifyMgr.notify(mNotificationId, mBuilder.build());
  162. }
  163.  
  164. @Override
  165. public void onActivation(ActivationResponse activationResponse) {
  166. preferences.saveUserNumber(gson.toJson(activationResponse.getPhoneNumber()));
  167. PreferencesHelper.getInstance(getContext()).saveToken(activationResponse.getToken());
  168. dismiss();
  169. Constants.userType = Constants.UserType.FULL;
  170. startActivity(new Intent(getContext(), DeliveriesActivity.class));
  171. }
  172.  
  173. @Override
  174. public void onError() {
  175.  
  176. }
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement