Created
March 31, 2019 13:11
-
-
Save saegeullee/0ddb495026a6ef8ef8ce21961874c545 to your computer and use it in GitHub Desktop.
Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//------LoginActivty.java-------- | |
package com.example.android.foodrecipes; | |
import android.arch.lifecycle.Observer; | |
import android.arch.lifecycle.ViewModelProviders; | |
import android.content.Intent; | |
import android.support.annotation.Nullable; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import com.example.android.foodrecipes.viewmodels.UserViewModel; | |
public class LoginActivity extends BaseActivity { | |
private static final String TAG = "LoginActivity"; | |
private EditText editTextId, editTextPassword; | |
private Button buttonLogin, buttonSignUp; | |
private UserViewModel mUserViewModel; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_login); | |
mUserViewModel = ViewModelProviders.of(this).get(UserViewModel.class); | |
initUI(); | |
subscribeObservers(); | |
} | |
private void initUI() { | |
editTextId = findViewById(R.id.editTextId); | |
editTextPassword = findViewById(R.id.editTextPassword); | |
buttonLogin = findViewById(R.id.buttonLogin); | |
buttonSignUp = findViewById(R.id.buttonSignup1); | |
buttonSignUp.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(LoginActivity.this, SignupActivity.class); | |
startActivity(intent); | |
} | |
}); | |
buttonLogin.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Log.d(TAG, "onClick: "); | |
if(!TextUtils.isEmpty(editTextId.getText()) && | |
!TextUtils.isEmpty(editTextPassword.getText())) { | |
String id = editTextId.getText().toString().trim(); | |
String password = editTextPassword.getText().toString().trim(); | |
mUserViewModel.loginUser(id, password); | |
} | |
} | |
}); | |
} | |
private void subscribeObservers() { | |
mUserViewModel.didLoginSuccess().observe(this, new Observer<Boolean>() { | |
@Override | |
public void onChanged(@Nullable Boolean aBoolean) { | |
if(aBoolean) { | |
Toast.makeText(LoginActivity.this, "login success", Toast.LENGTH_SHORT).show(); | |
goToRecipeListActivity(); | |
} else { | |
Toast.makeText(LoginActivity.this, "login fail", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); | |
} | |
private void goToRecipeListActivity() { | |
Intent intent = new Intent(LoginActivity.this, RecipeListActivity.class); | |
startActivity(intent); | |
finish(); | |
} | |
} | |
//------LoginActivty.java-------- | |
// -------UserApiClient class---- | |
package com.example.android.foodrecipes.requests; | |
import android.arch.lifecycle.MutableLiveData; | |
import android.util.Log; | |
import com.example.android.foodrecipes.model.User; | |
import com.example.android.foodrecipes.requests.responses.ServerPostRequestResponse; | |
import retrofit2.Call; | |
import retrofit2.Callback; | |
import retrofit2.Response; | |
public class UserApiClient { | |
private static final String TAG = "UserApiClient"; | |
private static UserApiClient instance; | |
private MutableLiveData<Boolean> didSignUpSuccess = new MutableLiveData<>(); | |
private MutableLiveData<Boolean> didLoginSuccess = new MutableLiveData<>(); | |
public static UserApiClient getInstance() { | |
if(instance == null) { | |
instance = new UserApiClient(); | |
} | |
return instance; | |
} | |
public UserApiClient() { | |
} | |
public void signUpUser(String id, String password) { | |
ServiceGenerator.getUserApi().createUser(id, password). | |
enqueue(new Callback<ServerPostRequestResponse>() { | |
@Override | |
public void onResponse(Call<ServerPostRequestResponse> call, Response<ServerPostRequestResponse> response) { | |
if(response.isSuccessful()) { | |
Log.d(TAG, "onResponse: success " + response.body()); | |
if(!response.body().isError()) { | |
String message = response.body().getMessage(); | |
Log.d(TAG, "onResponse: message : " + message); | |
didSignUpSuccess.postValue(true); | |
} else { | |
Log.d(TAG, "onResponse: " + response.body().getMessage()); | |
didSignUpSuccess.postValue(false); | |
} | |
} else { | |
Log.d(TAG, "onResponse: " + response.errorBody()); | |
} | |
} | |
@Override | |
public void onFailure(Call<ServerPostRequestResponse> call, Throwable t) { | |
Log.d(TAG, "onFailure: " + t.getMessage()); | |
} | |
}); | |
} | |
public void loginUser(String id, String password) { | |
ServiceGenerator.getUserApi().loginUser(id, password) | |
.enqueue(new Callback<ServerPostRequestResponse>() { | |
@Override | |
public void onResponse(Call<ServerPostRequestResponse> call, Response<ServerPostRequestResponse> response) { | |
if(response.isSuccessful()) { | |
if(!response.body().isError()) { | |
Log.d(TAG, "onResponse: " + response.body().getMessage()); | |
didLoginSuccess.postValue(true); | |
} else { | |
Log.d(TAG, "onResponse: " + response.body().getMessage()); | |
didLoginSuccess.postValue(false); | |
} | |
} | |
} | |
@Override | |
public void onFailure(Call<ServerPostRequestResponse> call, Throwable t) { | |
Log.d(TAG, "onFailure: " + t.getMessage()); | |
} | |
}); | |
} | |
public MutableLiveData<Boolean> didSignUpSuccess() { | |
return didSignUpSuccess; | |
} | |
public MutableLiveData<Boolean> didLoginSuccess() { | |
return didLoginSuccess; | |
} | |
} | |
// -------UserApiClient class---- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment