Commit 9e2c6217 authored by Ahmad Abi Mulya's avatar Ahmad Abi Mulya

fix login bug get response from API)

parent d09b3daf
......@@ -57,10 +57,9 @@ dependencies {
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.facebook.stetho:stetho:1.5.1'
implementation 'com.squareup.okhttp3:logging-interceptor:4.1.1'
testImplementation 'junit:junit:4.13'
implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
\ No newline at end of file
......@@ -69,7 +69,7 @@ public class CartActivity extends AppCompatActivity {
public void onIncrease(Cart cart) {
Log.d("stock1", ""+product.getQuantity());
Log.d("qty1", ""+cart.getQty());
if(product.getQuantity() < cart.getQty()){
if(product.getQuantity() > cart.getQty()){
Log.d("stock2", ""+product.getQuantity());
Log.d("qty2", ""+cart.getQty());
cart.setQty(cart.getQty() + 1);
......
......@@ -13,10 +13,14 @@ import android.widget.Toast;
import com.example.yourcashiertest.R;
import com.example.yourcashiertest.clients.ApiClient;
import com.example.yourcashiertest.databinding.ActivityLoginBinding;
import com.example.yourcashiertest.models.Data;
import com.example.yourcashiertest.models.Login;
import com.example.yourcashiertest.models.ResponseLogin;
import com.example.yourcashiertest.services.UserService;
import com.example.yourcashiertest.viewmodels.UserViewModel;
import com.google.gson.Gson;
import java.io.IOException;
import retrofit2.Call;
import retrofit2.Callback;
......@@ -32,29 +36,31 @@ public class LoginActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
prefManager = new PrefManager(this);
Log.d("login1", ""+ prefManager.isFirstLogin());
Log.d("login1", "" + prefManager.isFirstLogin());
if (!prefManager.isFirstLogin()) {
Log.d("login2", ""+ prefManager.isFirstLogin());
Log.d("login2", "" + prefManager.isFirstLogin());
checkSession();
finish();
}
viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(UserViewModel.class);
binding.btnLogin.setOnClickListener(view -> {
if(binding.etUsername.getText().toString().length() == 0 || binding.etPassword.getText().toString().length() == 0){
if (binding.etUsername.getText().toString().length() == 0 || binding.etPassword.getText().toString().length() == 0) {
Toast.makeText(getApplicationContext(), "Please input Username and Password", Toast.LENGTH_SHORT).show();
}else{
} else {
binding.setLoading(true);
viewModel.setListUser();
launchMain();
}
});
}
public void tvForgetPassword(View view) {
startActivity(new Intent(LoginActivity.this, ForgetPassword.class));
}
public void tvSignUpHere(View view) {
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
}
public void launchMain() {
Login login = new Login();
login.setEmail(binding.etUsername.getText().toString());
......@@ -62,40 +68,40 @@ public class LoginActivity extends AppCompatActivity {
requestLogin(login);
}
public void requestLogin(Login login){
try {
public void requestLogin(Login login) {
ApiClient.client(UserService.class, UserViewModel.BASE_URL).loginUser(login).enqueue(new Callback<ResponseLogin>() {
@Override
public void onResponse(Call<ResponseLogin> call, Response<ResponseLogin> response) {
if (response.body()!= null){
Log.d("body", String.valueOf(response.body().getData().getFullName()));
if (response.code() < 400) {
// Log.d("body", "" + response.errorBody().toString());
prefManager.setFirstLogin(false);
binding.setLoading(false);
prefManager.setDataUser(response.body().getData().getFullName());
Toast.makeText(getApplicationContext(), response.body().getMessages(), Toast.LENGTH_LONG).show();
startActivity(new Intent(LoginActivity.this, MainActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
prefManager.setDataUser(response.body().getData().getFullName());
finish();
} else if (response.code() == 302){
Toast.makeText(getApplicationContext(), "Please verify your account!", Toast.LENGTH_LONG).show();
binding.setLoading(false);
}else{
Toast.makeText(getApplicationContext(), response.body().getMessages(), Toast.LENGTH_LONG).show();
binding.setLoading(false);
} else {
String errorJson = null;
try {
errorJson = response.errorBody().string();
Log.d("response", "" + errorJson);
ResponseLogin responseLogin = new Gson().fromJson(errorJson, ResponseLogin.class);
Toast.makeText(getApplicationContext(), responseLogin.getMessages(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
binding.setLoading(false);
}
@Override
public void onFailure(Call<ResponseLogin> call, Throwable t) {
t.printStackTrace();
}
});
}catch (Exception e){
e.printStackTrace();
}
}
public void checkSession(){
public void checkSession() {
prefManager.setFirstLogin(false);
startActivity(new Intent(LoginActivity.this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
finish();
......
package com.example.yourcashiertest.clients;
import android.util.Log;
import com.example.yourcashiertest.BuildConfig;
import com.google.gson.GsonBuilder;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
public class ApiClient {
public static <T> T client(Class<T> service, String url) {
OkHttpClient.Builder okHttp = new OkHttpClient().newBuilder();
okHttp.connectTimeout(60, TimeUnit.SECONDS);
okHttp.writeTimeout(60, TimeUnit.SECONDS);
okHttp.readTimeout(60, TimeUnit.SECONDS);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(s -> Log.e("API-LOG", s));
interceptor.level(HttpLoggingInterceptor.Level.BODY);
if (BuildConfig.DEBUG) okHttp.addInterceptor(interceptor);
okHttp.addInterceptor(chain -> {
Request request = chain.request();
Request newReq = request.newBuilder()
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
return chain.proceed(newReq);
});
OkHttpClient client = okHttp.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(
GsonConverterFactory.create(
new GsonBuilder().setLenient().create()
)
)
.client(client)
.addConverterFactory(GsonConverterFactory.create(
new GsonBuilder().setLenient().create())
).addConverterFactory(ScalarsConverterFactory.create())
.build();
return retrofit.create(service);
}
}
......@@ -5,13 +5,13 @@ import com.google.gson.annotations.SerializedName;
public class ResponseLogin{
@SerializedName("data")
private Data data;
public Data data;
@SerializedName("messages")
private String messages;
public String messages;
@SerializedName("status")
private boolean status;
public boolean status;
public void setData(Data data){
this.data = data;
......@@ -36,4 +36,5 @@ public class ResponseLogin{
public boolean isStatus(){
return status;
}
}
\ No newline at end of file
package com.example.yourcashiertest.services;
import com.example.yourcashiertest.models.Data;
import com.example.yourcashiertest.models.Login;
import com.example.yourcashiertest.models.Password;
import com.example.yourcashiertest.models.ResponseLogin;
......@@ -7,6 +8,7 @@ import com.example.yourcashiertest.models.ResponseUser;
import com.example.yourcashiertest.models.User;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Headers;
......@@ -23,7 +25,7 @@ public interface UserService {
@POST("v1/customer")
Call<User> registerUser(@Body User user);
@Headers("Content-Type: application/json")
// @Headers("Content-Type: application/json")
@POST("v1/login")
Call<ResponseLogin> loginUser(@Body Login login);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment