Commit 2a6a2dab authored by Wahyu Wibowo's avatar Wahyu Wibowo

add fitur login

parent 5e3f552d
package com.example.yourcashiertest.activities;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import android.content.Context;
import android.content.Intent;
......@@ -10,9 +12,13 @@ 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;
......@@ -23,6 +29,8 @@ public class LoginActivity extends AppCompatActivity {
// public static final String my_shared = "session_status";
private PrefManager prefManager;
UserViewModel viewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -38,6 +46,8 @@ public class LoginActivity extends AppCompatActivity {
etUsername = findViewById(R.id.etUsername);
btnLogin = findViewById(R.id.btnLogin);
viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(UserViewModel.class);
btnLogin.setOnClickListener(view -> {
if(etUsername.getText().toString().length() == 0 || etPassword.getText().toString().length() == 0){
......@@ -47,10 +57,23 @@ public class LoginActivity extends AppCompatActivity {
}else if(etPassword.getText().toString().length() < 8) {
etPassword.setError("Password length cannot be less than 8 characters");
}else{
startActivity(new Intent(LoginActivity.this, MainActivity.class)
.putExtra(DATA_LOGIN, etUsername.getText().toString()));
finish();
viewModel.setListUser();
checkLogin();
}
});
}
private void checkLogin() {
viewModel.getListUser().observe(this, users -> {
for (int i = 0; i < users.size(); i++){
if (users.get(i).getEmail().equals(etUsername.getText().toString())
&& users.get(i).getPassword().equals(etPassword.getText().toString())){
startActivity(new Intent(LoginActivity.this, MainActivity.class)
.putExtra(DATA_LOGIN, users.get(i).getFullName()));
}
}
// Toast.makeText(getApplication(), "Username or password wrong", Toast.LENGTH_LONG).show();
});
}
......
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{
public class User implements Parcelable {
@SerializedName("password")
private String password;
......@@ -16,6 +19,29 @@ public class User{
@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;
}
......@@ -47,4 +73,17 @@ public class User{
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 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;
......@@ -18,6 +21,11 @@ public class UserViewModel extends ViewModel {
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>() {
......@@ -32,4 +40,20 @@ public class UserViewModel extends ViewModel {
}
});
}
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