Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginActivity extends AppCompatActivity {
- private LoginViewModel loginViewModel;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- ActivityLoginBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
- loginViewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
- binding.setLoginViewModel(loginViewModel);
- binding.setLifecycleOwner(this);
- loginViewModel.user.observe(this, new Observer<User>() {
- @Override
- public void onChanged(@Nullable User user) {
- Toast.makeText(getApplicationContext(), user.toString(), Toast.LENGTH_LONG).show();
- }
- });
- }
- }
- public class LoginViewModel extends ViewModel {
- private LoginRespository loginRespository = new LoginRespository();
- public MutableLiveData<User> user = new MutableLiveData<>();
- public MutableLiveData<String> email = new MutableLiveData<>();
- public MutableLiveData<String> password = new MutableLiveData<>();
- public LoginViewModel() {}
- public void onLoginClicked() {
- user = loginRespository.doLogin(email.getValue(), password.getValue());
- }
- }
- @Singleton
- public class LoginRespository {
- private APIInterface apiInterface;
- public LoginRespository(){
- this.apiInterface = APIClient.getClient().create(APIInterface.class);
- }
- //private final APIInterface apiInterface;
- /*@Inject
- public LoginRespository(APIInterface apiInterface) {
- this.apiInterface = apiInterface;
- }*/
- public MutableLiveData<User> doLogin(String email, String password) {
- final MutableLiveData<User> data = new MutableLiveData<>();
- Call<User> call = apiInterface.doGetLogin(email, password, APIClient.X_API_KEY, APIClient.X_CLIENT_ID);
- call.enqueue(new Callback<User>() {
- @Override
- public void onResponse(Call<User> call, Response<User> response) {
- User user = response.body();
- Log.d("#####0", user.toString());
- if (user.code.equals(APIClient.Codes.OK)){
- Log.d("#####1", user.toString());
- data.setValue(user);
- Log.d("#####2", data.toString());
- }
- }
- @Override
- public void onFailure(Call<User> call, Throwable t) {
- call.cancel();
- }
- });
- return data;
- }
- }
- public class User{
- @SerializedName("code")
- @Expose
- public String code;
- @SerializedName("data")
- @Expose
- public List<Datum> data = null;
- @SerializedName("description")
- @Expose
- public String description;
- @SerializedName("datetime")
- @Expose
- public String datetime;
- /**
- * No args constructor for use in serialization
- *
- */
- public User() {
- }
- /**
- *
- * @param description
- * @param data
- * @param code
- * @param datetime
- */
- public User(String code, List<Datum> data, String description, String datetime) {
- super();
- this.code = code;
- this.data = data;
- this.description = description;
- this.datetime = datetime;
- }
- @Override
- public String toString() {
- return "User{" +
- "code='" + code + ''' +
- ", data=" + data +
- ", description='" + description + ''' +
- ", datetime='" + datetime + ''' +
- '}';
- }
- }
- public void onLoginClicked() {
- user = loginRespository.doLogin(email.getValue(), password.getValue());
- }
Add Comment
Please, Sign In to add comment