Commit 92075dc6 authored by Ahmad Abi Mulya's avatar Ahmad Abi Mulya

Update Login & Register

parents 1d179431 2a6a2dab
......@@ -52,6 +52,11 @@ dependencies {
implementation "com.github.bumptech.glide:glide:$glide_version"
annotationProcessor "com.github.bumptech.glide:compiler:$glide_version"
// service
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.google.code.gson:gson:2.8.6'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
......
package com.example.yourcashiertest.activities;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import android.content.Intent;
import android.os.Bundle;
......@@ -9,15 +11,21 @@ import android.view.View;
import android.widget.Toast;
import com.example.yourcashiertest.R;
import com.example.yourcashiertest.models.User;
import com.example.yourcashiertest.viewmodels.UserViewModel;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;
import java.util.List;
public class LoginActivity extends AppCompatActivity {
TextInputEditText etUsername, etPassword;
MaterialButton btnLogin;
public static final String DATA_LOGIN = "DATA_LOGIN";
private PrefManager prefManager;
UserViewModel viewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -28,7 +36,7 @@ public class LoginActivity extends AppCompatActivity {
Log.d("login1", ""+ prefManager.isFirstLogin());
if (!prefManager.isFirstLogin()) {
Log.d("login2", ""+ prefManager.isFirstLogin());
launchMain();
checkSession();
finish();
}
Log.d("login3", ""+ prefManager.isFirstLogin());
......@@ -36,15 +44,13 @@ public class LoginActivity extends AppCompatActivity {
etUsername = findViewById(R.id.etUsername);
btnLogin = findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(view -> {
viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(UserViewModel.class);
btnLogin.setOnClickListener(view -> {
if(etUsername.getText().toString().length() == 0 || etPassword.getText().toString().length() == 0){
Toast.makeText(getApplicationContext(), "Please input Username and Password", Toast.LENGTH_SHORT).show();
}else if(!etPassword.getText().toString().matches("[A-Za-z0-9]+")){
etPassword.setError("Passwords can only contain Alphanumeric");
}else if(etPassword.getText().toString().length() < 8) {
etPassword.setError("Password length cannot be less than 8 characters");
}else{
viewModel.setListUser();
launchMain();
}
});
......@@ -58,6 +64,27 @@ public class LoginActivity extends AppCompatActivity {
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
}
public void launchMain() {
viewModel.getListUser().observe(this, users -> {
for (int i = 0; i < users.size(); i++){
if (etUsername.getText().toString().equals(users.get(i).getEmail())
&& etPassword.getText().toString().equals(users.get(i).getPassword())){
prefManager.setFirstLogin(false);
startActivity(new Intent(LoginActivity.this, MainActivity.class).putExtra(DATA_LOGIN, users.get(i).getFullName()).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
finish();
return;
}else if(!etUsername.getText().toString().equals(users.get(i).getEmail())){
etUsername.setError("Email Not Found!");
}else if (!etPassword.getText().toString().equals(users.get(i).getPassword())){
etPassword.setError("Password Wrong!");
}
}
});
// prefManager.setFirstLogin(false);
// startActivity(new Intent(LoginActivity.this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
// finish();
}
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.activities;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.example.yourcashiertest.R;
import com.example.yourcashiertest.databinding.ActivityRegisterBinding;
import com.example.yourcashiertest.models.User;
import com.example.yourcashiertest.viewmodels.UserViewModel;
public class RegisterActivity extends AppCompatActivity {
WebView wbCompany;
ProgressBar pbLoading;
private static final String urLCompany = "http://multidaya.id/";
ActivityRegisterBinding binding;
User user;
UserViewModel viewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
binding = DataBindingUtil.setContentView(this, R.layout.activity_register);
user = new User();
viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(UserViewModel.class);
binding.btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(binding.etUsername.getText().toString().length() == 0 || binding.etPassword.getText().toString().length() == 0
|| binding.etEmail.getText().toString().length() == 0 ||binding.etPhoneNumber.getText().toString().length() == 0){
Toast.makeText(getApplicationContext(), "Please input All Required Data", Toast.LENGTH_SHORT).show();
}else if(!binding.etPassword.getText().toString().matches("[A-Za-z0-9]+")){
binding.etPassword.setError("Passwords can only contain Alphanumeric");
}else if(binding.etPassword.getText().toString().length() < 8) {
binding.etPassword.setError("Password length cannot be less than 8 characters");
}else {
user.setEmail(binding.etEmail.getText().toString());
user.setFullName(binding.etUsername.getText().toString());
user.setPassword(binding.etPassword.getText().toString());
user.setPhoneNumber(binding.etPhoneNumber.getText().toString());
viewModel.registrasi(user);
Toast.makeText(getApplicationContext(), "Register berhasil", Toast.LENGTH_LONG).show();
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
}
}
});
}
public void tvSignInHere(View view) {
......
package com.example.yourcashiertest.adapters;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
......@@ -11,8 +10,6 @@ import androidx.recyclerview.widget.RecyclerView;
import com.example.yourcashiertest.R;
import com.example.yourcashiertest.databinding.CartItemBinding;
import com.example.yourcashiertest.entities.Cart;
import com.example.yourcashiertest.entities.Product;
import com.example.yourcashiertest.models.CartModel;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
......
package com.example.yourcashiertest.clients;
import com.google.gson.GsonBuilder;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ApiClient {
public static <T> T client(Class<T> service, String url) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(
GsonConverterFactory.create(
new GsonBuilder().setLenient().create()
)
)
.build();
return retrofit.create(service);
}
}
package com.example.yourcashiertest.models;
public class CartModel {
long price, qty, idProduct;
String nameProduct, image;
public long getPrice() {
return price;
}
public void setPrice(long price) {
this.price = price;
}
public long getQty() {
return qty;
}
public void setQty(long qty) {
this.qty = qty;
}
public long getIdProduct() {
return idProduct;
}
public void setIdProduct(long idProduct) {
this.idProduct = idProduct;
}
public String getNameProduct() {
return nameProduct;
}
public void setNameProduct(String nameProduct) {
this.nameProduct = nameProduct;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
package com.example.yourcashiertest.models;
import java.util.List;
import com.google.gson.annotations.SerializedName;
public class ResponseUser{
@SerializedName("data")
private List<User> user;
@SerializedName("message")
private String message;
@SerializedName("status")
private boolean status;
public void setData(List<User> data){
this.user = data;
}
public List<User> getData(){
return user;
}
public void setMessage(String message){
this.message = message;
}
public String getMessage(){
return message;
}
public void setStatus(boolean status){
this.status = status;
}
public boolean isStatus(){
return status;
}
}
\ No newline at end of file
package com.example.yourcashiertest.models;
import android.os.Parcel;
import android.os.Parcelable;
import com.google.gson.annotations.SerializedName;
public class User implements Parcelable {
@SerializedName("password")
private String password;
@SerializedName("full_name")
private String fullName;
@SerializedName("phone_number")
private String phoneNumber;
@SerializedName("email")
private String email;
public User(Parcel in) {
password = in.readString();
fullName = in.readString();
phoneNumber = in.readString();
email = in.readString();
}
public static final Creator<User> CREATOR = new Creator<User>() {
@Override
public User createFromParcel(Parcel in) {
return new User(in);
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
public User() {
}
public void setPassword(String password) {
this.password = password;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword(){
return password;
}
public String getFullName(){
return fullName;
}
public String getPhoneNumber(){
return phoneNumber;
}
public String getEmail(){
return email;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(password);
dest.writeString(fullName);
dest.writeString(phoneNumber);
dest.writeString(email);
}
}
\ No newline at end of file
package com.example.yourcashiertest.services;
import com.example.yourcashiertest.models.ResponseUser;
import com.example.yourcashiertest.models.User;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.POST;
public interface UserService {
@GET("v1/customer")
Call<ResponseUser> getAllUser();
@Headers("Content-Type: application/json")
@POST("v1/customer")
Call<User> registerUser(@Body User user);
}
package com.example.yourcashiertest.viewmodels;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.example.yourcashiertest.models.CartModel;
import java.util.List;
public class CartVM extends ViewModel {
public MutableLiveData<List<CartModel>> allCart;
public MutableLiveData<List<CartModel>> getAllCart() {
return allCart;
}
public void setAllCart(List<CartModel> cart) {
this.allCart.postValue(cart);
}
}
package com.example.yourcashiertest.viewmodels;
import android.util.Log;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.example.yourcashiertest.clients.ApiClient;
import com.example.yourcashiertest.models.ResponseUser;
import com.example.yourcashiertest.models.User;
import com.example.yourcashiertest.services.UserService;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class UserViewModel extends ViewModel {
MutableLiveData<List<User>> listUser = new MutableLiveData<>();
public final String BASE_URL = "https://your-cashier.herokuapp.com/api/";
public MutableLiveData<List<User>> getListUser(){
return this.listUser;
}
public void registrasi(User user){
ApiClient.client(UserService.class, BASE_URL).registerUser(user)
.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
}
@Override
public void onFailure(Call<User> call, Throwable t) {
}
});
}
public void setListUser(){
ApiClient.client(UserService.class, BASE_URL).getAllUser().enqueue(new Callback<ResponseUser>() {
@Override
public void onResponse(Call<ResponseUser> call, Response<ResponseUser> response) {
listUser.setValue(response.body().getData());
Log.d("user", String.valueOf(response.body().getData()));
}
@Override
public void onFailure(Call<ResponseUser> call, Throwable t) {
}
});
}
}
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