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