PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/app/src/main/java/com/kse/slp/auth/LoginDialogFragment.java

https://gitlab.com/khoa-io/slp
Java | 168 lines | 109 code | 27 blank | 32 comment | 6 complexity | 0211fec1f975c1b2ac138d455b951830 MD5 | raw file
  1. package com.kse.slp.auth;
  2. import android.annotation.SuppressLint;
  3. import android.app.Dialog;
  4. import android.content.Context;
  5. import android.os.Bundle;
  6. import android.support.annotation.NonNull;
  7. import android.support.v4.app.DialogFragment;
  8. import android.support.v7.app.AlertDialog;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.Button;
  13. import android.widget.CheckBox;
  14. import android.widget.EditText;
  15. import com.kse.slp.BuildConfig;
  16. import com.kse.slp.R;
  17. import com.kse.slp.common.AppCommon;
  18. import com.kse.slp.common.AppPreferences;
  19. /**
  20. * A simple {@link DialogFragment} subclass provides signing in function.
  21. * Activities that contain this fragment must implement the
  22. * {@link SignInDialogListener} interface
  23. * to handle interaction events.
  24. * Use the {@link LoginDialogFragment#newInstance} factory method to
  25. * create an instance of this fragment.
  26. */
  27. public class LoginDialogFragment extends DialogFragment {
  28. private SignInDialogListener listener;
  29. private EditText usernameEdit;
  30. private EditText passwordEdit;
  31. private CheckBox checkRememberLogin;
  32. public LoginDialogFragment() {
  33. // Required empty public constructor
  34. }
  35. public static LoginDialogFragment newInstance() {
  36. LoginDialogFragment fragment = new LoginDialogFragment();
  37. Bundle args = new Bundle();
  38. fragment.setArguments(args);
  39. return fragment;
  40. }
  41. @Override
  42. public void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. // if (getArguments() != null) {
  45. // // ignore for now
  46. // }
  47. }
  48. @SuppressLint("SetTextI18n")
  49. @NonNull
  50. @Override
  51. public Dialog onCreateDialog(Bundle savedInstanceState) {
  52. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  53. // Get the layout inflater
  54. LayoutInflater inflater = getActivity().getLayoutInflater();
  55. // Inflate and set the layout for the dialog
  56. // Pass null as the parent view because its going in the dialog layout
  57. @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.dialog_sign_in, null);
  58. builder.setView(view);
  59. Button signInButton = (Button) view.findViewById(R.id.btn_sign_in);
  60. Button cancelButton = (Button) view.findViewById(R.id.btn_cancel);
  61. usernameEdit = (EditText) view.findViewById(R.id.edit_username);
  62. passwordEdit = (EditText) view.findViewById(R.id.edit_password);
  63. checkRememberLogin = (CheckBox) view.findViewById(R.id.check_remember_login);
  64. AppPreferences prefs = AppPreferences.getInstance(getActivity());
  65. usernameEdit.setText(prefs.getString(getActivity().getString(R.string.username), ""));
  66. passwordEdit.setText(prefs.getString(getActivity().getString(R.string.password), ""));
  67. if (BuildConfig.DEBUG) {
  68. usernameEdit.setText("dungpqshipper");
  69. passwordEdit.setText("12345678");
  70. }
  71. signInButton.setOnClickListener(new OnClickSignInListener());
  72. cancelButton.setOnClickListener(new OnClickCancelListener());
  73. AlertDialog dialog = builder.create();
  74. dialog.setCanceledOnTouchOutside(false);
  75. return dialog;
  76. }
  77. @Override
  78. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  79. Bundle savedInstanceState) {
  80. // Inflate the layout for this fragment
  81. return inflater.inflate(R.layout.dialog_sign_in, container, false);
  82. }
  83. @Override
  84. public void onAttach(Context context) {
  85. super.onAttach(context);
  86. if (context instanceof SignInDialogListener) {
  87. listener = (SignInDialogListener) context;
  88. return;
  89. }
  90. throw new RuntimeException(context.toString() + " must implement SignInDialogListener");
  91. }
  92. @Override
  93. public void onDetach() {
  94. super.onDetach();
  95. listener = null;
  96. }
  97. /**
  98. * This interface must be implemented by activities that contain this
  99. * fragment to allow an interaction in this fragment to be communicated
  100. * to the activity and potentially other fragments contained in that
  101. * activity.
  102. * <p/>
  103. * See the Android Training lesson <a href=
  104. * "http://developer.android.com/training/basics/fragments/communicating.html"
  105. * >Communicating with Other Fragments</a> for more information.
  106. */
  107. public interface SignInDialogListener {
  108. /**
  109. * Call when user click "Sign in" button.
  110. */
  111. void onClickSignIn(String username, String password, boolean rememberLogin);
  112. /**
  113. * Call when user click "Cancel" button.
  114. */
  115. void onClickCancel();
  116. }
  117. private class OnClickSignInListener implements View.OnClickListener {
  118. @Override
  119. public void onClick(View view) {
  120. if (listener == null) {
  121. throw new RuntimeException(getActivity().toString()
  122. + " must implement SignInDialogListener");
  123. }
  124. String username = usernameEdit.getText().toString();
  125. String password = passwordEdit.getText().toString();
  126. boolean rememberLogin = checkRememberLogin.isChecked();
  127. listener.onClickSignIn(username, password, rememberLogin);
  128. }
  129. }
  130. private class OnClickCancelListener implements View.OnClickListener {
  131. @Override
  132. public void onClick(View view) {
  133. if (listener == null) {
  134. throw new RuntimeException(getActivity().toString()
  135. + " must implement SignInDialogListener");
  136. }
  137. dismiss();
  138. listener.onClickCancel();
  139. }
  140. }
  141. }