Guest User

Untitled

a guest
Jan 15th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. public class LoginActivity extends AppCompatActivity {
  2.  
  3. private LoginViewModel loginViewModel;
  4.  
  5. @Override
  6. protected void onCreate(@Nullable Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8.  
  9. ActivityLoginBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
  10. loginViewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
  11. binding.setLoginViewModel(loginViewModel);
  12. binding.setLifecycleOwner(this);
  13.  
  14. loginViewModel.user.observe(this, new Observer<User>() {
  15. @Override
  16. public void onChanged(@Nullable User user) {
  17. Toast.makeText(getApplicationContext(), user.toString(), Toast.LENGTH_LONG).show();
  18. }
  19. });
  20.  
  21. }
  22. }
  23.  
  24. public class LoginViewModel extends ViewModel {
  25.  
  26. private LoginRespository loginRespository = new LoginRespository();
  27. public MutableLiveData<User> user = new MutableLiveData<>();
  28.  
  29. public MutableLiveData<String> email = new MutableLiveData<>();
  30. public MutableLiveData<String> password = new MutableLiveData<>();
  31.  
  32. public LoginViewModel() {}
  33.  
  34. public void onLoginClicked() {
  35. user = loginRespository.doLogin(email.getValue(), password.getValue());
  36. }
  37.  
  38. }
  39.  
  40. @Singleton
  41. public class LoginRespository {
  42. private APIInterface apiInterface;
  43.  
  44. public LoginRespository(){
  45. this.apiInterface = APIClient.getClient().create(APIInterface.class);
  46. }
  47.  
  48. //private final APIInterface apiInterface;
  49. /*@Inject
  50. public LoginRespository(APIInterface apiInterface) {
  51. this.apiInterface = apiInterface;
  52. }*/
  53.  
  54. public MutableLiveData<User> doLogin(String email, String password) {
  55. final MutableLiveData<User> data = new MutableLiveData<>();
  56.  
  57. Call<User> call = apiInterface.doGetLogin(email, password, APIClient.X_API_KEY, APIClient.X_CLIENT_ID);
  58. call.enqueue(new Callback<User>() {
  59. @Override
  60. public void onResponse(Call<User> call, Response<User> response) {
  61. User user = response.body();
  62. Log.d("#####0", user.toString());
  63. if (user.code.equals(APIClient.Codes.OK)){
  64. Log.d("#####1", user.toString());
  65. data.setValue(user);
  66. Log.d("#####2", data.toString());
  67. }
  68. }
  69.  
  70. @Override
  71. public void onFailure(Call<User> call, Throwable t) {
  72. call.cancel();
  73. }
  74. });
  75.  
  76. return data;
  77. }
  78. }
  79.  
  80. public class User{
  81.  
  82. @SerializedName("code")
  83. @Expose
  84. public String code;
  85. @SerializedName("data")
  86. @Expose
  87. public List<Datum> data = null;
  88. @SerializedName("description")
  89. @Expose
  90. public String description;
  91. @SerializedName("datetime")
  92. @Expose
  93. public String datetime;
  94.  
  95. /**
  96. * No args constructor for use in serialization
  97. *
  98. */
  99. public User() {
  100. }
  101.  
  102. /**
  103. *
  104. * @param description
  105. * @param data
  106. * @param code
  107. * @param datetime
  108. */
  109. public User(String code, List<Datum> data, String description, String datetime) {
  110. super();
  111. this.code = code;
  112. this.data = data;
  113. this.description = description;
  114. this.datetime = datetime;
  115. }
  116.  
  117. @Override
  118. public String toString() {
  119. return "User{" +
  120. "code='" + code + ''' +
  121. ", data=" + data +
  122. ", description='" + description + ''' +
  123. ", datetime='" + datetime + ''' +
  124. '}';
  125. }
  126. }
  127.  
  128. public void onLoginClicked() {
  129. user = loginRespository.doLogin(email.getValue(), password.getValue());
  130. }
Add Comment
Please, Sign In to add comment