Commit a45fcaf4 authored by Wahyu Wibowo's avatar Wahyu Wibowo

update forgot password

parent 2a6a2dab
......@@ -2,6 +2,7 @@ package com.example.yourcashiertest.activities;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
......@@ -13,8 +14,12 @@ import com.example.yourcashiertest.R;
import com.example.yourcashiertest.adapters.CartAdapter;
import com.example.yourcashiertest.databinding.ActivityCartBinding;
import com.example.yourcashiertest.entities.Cart;
import com.example.yourcashiertest.entities.Product;
import com.example.yourcashiertest.utils.Convert;
import com.example.yourcashiertest.viewmodels.CartViewModel;
import com.example.yourcashiertest.viewmodels.ProductViewModel;
import java.util.List;
public class CartActivity extends AppCompatActivity {
......@@ -33,6 +38,8 @@ public class CartActivity extends AppCompatActivity {
binding.rvCartList.setLayoutManager(new LinearLayoutManager(this));
binding.rvCartList.setAdapter(adapter);
ProductViewModel viewModel = new ViewModelProvider(this).get(ProductViewModel.class);
CartViewModel cartViewModel = new ViewModelProvider(this).get(CartViewModel.class);
cartViewModel.getCarts().observe(this, carts -> adapter.setCartList(carts));
......
package com.example.yourcashiertest.activities;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.example.yourcashiertest.R;
import com.example.yourcashiertest.databinding.ActivityForgetPasswordBinding;
import com.example.yourcashiertest.models.Password;
import com.example.yourcashiertest.models.User;
import com.example.yourcashiertest.utils.ViewUtil;
import com.example.yourcashiertest.viewmodels.UserViewModel;
import java.util.List;
public class ForgetPassword extends AppCompatActivity {
ActivityForgetPasswordBinding binding;
UserViewModel userViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forget_password);
binding = DataBindingUtil.setContentView(this, R.layout.activity_forget_password);
userViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(UserViewModel.class);
userViewModel.setListUser();
binding.btnResetPw.setOnClickListener(v -> {
resetPassword();
binding.setLoading(true);
});
}
public void resetPassword() {
userViewModel.getListUser().observe(this, users -> {
for (int i = 0; i < users.size(); i++){
if (binding.etEmail.getText().toString().equals(users.get(i).getEmail())){
Password password = new Password();
password.setNewPassword(binding.etNewPw.getText().toString());
userViewModel.resetPassword(users.get(i).getId() , password);
binding.setLoading(false);
startActivity(new Intent(ForgetPassword.this, LoginActivity.class));
return;
}
}
binding.setLoading(false);
Toast.makeText(ForgetPassword.this, "User tidak ditemukan", Toast.LENGTH_LONG).show();
});
}
}
\ No newline at end of file
package com.example.yourcashiertest.activities;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
......@@ -12,6 +13,7 @@ import android.view.View;
import android.widget.Toast;
import com.example.yourcashiertest.R;
import com.example.yourcashiertest.databinding.ActivityLoginBinding;
import com.example.yourcashiertest.models.User;
import com.example.yourcashiertest.viewmodels.UserViewModel;
import com.google.android.material.button.MaterialButton;
......@@ -20,8 +22,7 @@ import com.google.android.material.textfield.TextInputEditText;
import java.util.List;
public class LoginActivity extends AppCompatActivity {
TextInputEditText etUsername, etPassword;
MaterialButton btnLogin;
ActivityLoginBinding binding;
public static final String DATA_LOGIN = "DATA_LOGIN";
public static Boolean syarat;
// SharedPreferences sharedPreferences;
......@@ -34,7 +35,7 @@ public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
// sharedPreferences = getSharedPreferences(my_shared, Context.MODE_PRIVATE);
// syarat = sharedPreferences.getBoolean(SESS_SYARAT, false);
prefManager = new PrefManager(this);
......@@ -42,21 +43,18 @@ public class LoginActivity extends AppCompatActivity {
launchMain();
finish();
}
etPassword = findViewById(R.id.etPassword);
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){
binding.btnLogin.setOnClickListener(view -> {
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 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 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{
binding.setLoading(true);
viewModel.setListUser();
checkLogin();
}
......@@ -67,8 +65,9 @@ public class LoginActivity extends AppCompatActivity {
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())){
if (users.get(i).getEmail().equals(binding.etUsername.getText().toString())
&& users.get(i).getPassword().equals(binding.etPassword.getText().toString())){
binding.setLoading(false);
startActivity(new Intent(LoginActivity.this, MainActivity.class)
.putExtra(DATA_LOGIN, users.get(i).getFullName()));
}
......
package com.example.yourcashiertest.models;
import com.google.gson.annotations.SerializedName;
public class Password{
@SerializedName("new_password")
private String newPassword;
public void setNewPassword(String newPassword) {
this.newPassword = newPassword;
}
public String getNewPassword(){
return newPassword;
}
@Override
public String toString(){
return
"{" +
" \"new_password\" = '\"" + newPassword + '\"' +
"}";
}
}
......@@ -7,6 +7,9 @@ import com.google.gson.annotations.SerializedName;
public class User implements Parcelable {
@SerializedName("id")
private int id;
@SerializedName("password")
private String password;
......@@ -20,6 +23,7 @@ public class User implements Parcelable {
private String email;
public User(Parcel in) {
id = in.readInt();
password = in.readString();
fullName = in.readString();
phoneNumber = in.readString();
......@@ -42,6 +46,14 @@ public class User implements Parcelable {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setPassword(String password) {
this.password = password;
}
......@@ -81,9 +93,11 @@ public class User implements Parcelable {
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
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.Password;
import com.example.yourcashiertest.models.ResponseUser;
import com.example.yourcashiertest.models.User;
......@@ -10,6 +11,8 @@ import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Path;
public interface UserService {
......@@ -20,4 +23,8 @@ public interface UserService {
@POST("v1/customer")
Call<User> registerUser(@Body User user);
@Headers("Content-Type: application/json")
@PUT("v1/change_password/{id}")
Call<Password> editPassword(@Path("id") int id, @Body Password password);
}
......@@ -6,9 +6,11 @@ import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.example.yourcashiertest.clients.ApiClient;
import com.example.yourcashiertest.models.Password;
import com.example.yourcashiertest.models.ResponseUser;
import com.example.yourcashiertest.models.User;
import com.example.yourcashiertest.services.UserService;
import com.google.gson.Gson;
import java.util.List;
......@@ -56,4 +58,24 @@ public class UserViewModel extends ViewModel {
});
}
public void resetPassword(int id, Password password){
ApiClient.client(UserService.class, BASE_URL)
.editPassword(id, password).enqueue(new Callback<Password>() {
@Override
public void onResponse(Call<Password> call, Response<Password> response) {
Log.d("response", response.message());
}
@Override
public void onFailure(Call<Password> call, Throwable t) {
}
});
}
public String toStringPassword(String password){
return "{ new_password:" + password + "}";
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="android.view.View"/>
<variable
name="loading"
type="boolean" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingHorizontal="10dp"
tools:context=".activities.ForgetPassword">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/tvResetPassword"
android:layout_width="wrap_content"
......@@ -16,7 +28,8 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textStyle="bold"
android:text="RESET PASSWORD"/>
android:text="RESET PASSWORD" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilEmail"
app:boxBackgroundColor="@color/white"
......@@ -39,6 +52,7 @@
android:imeOptions="actionNext"
android:textSize="@dimen/text_default" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilCrPw"
app:passwordToggleEnabled="true"
......@@ -62,6 +76,7 @@
android:inputType="text"
android:textSize="@dimen/text_default" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilNewPw"
app:passwordToggleEnabled="true"
......@@ -94,10 +109,24 @@
android:layout_marginEnd="4dp"
android:backgroundTint="@color/colorPrimary"
android:padding="@dimen/space_default"
android:visibility="@{loading ? View.GONE : View.VISIBLE}"
android:text="RESET PASSWORD"
app:cornerRadius="@dimen/space_default"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tilNewPw"
app:layout_constraintVertical_bias="0.008" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
<ProgressBar
android:id="@+id/pgLoading"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:visibility="@{loading ? View.VISIBLE : View.GONE}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/btnResetPw"
app:layout_constraintHorizontal_bias="0.519"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tilNewPw"
app:layout_constraintVertical_bias="0.069" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="android.view.View"/>
<variable
name="loading"
type="boolean" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent"
......@@ -17,7 +27,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textStyle="bold"
android:text="LOGIN"/>
android:text="LOGIN" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilUsername"
......@@ -63,11 +73,12 @@
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textview.MaterialTextView
android:onClick="tvForgetPassword"
android:id="@+id/materialTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="24dp"
android:onClick="tvForgetPassword"
android:text="@string/lupa_password"
android:textColor="@color/colorPrimary"
android:textStyle="bold"
......@@ -83,6 +94,7 @@
android:layout_marginEnd="@dimen/space_default"
android:backgroundTint="@color/colorPrimary"
android:padding="@dimen/space_default"
android:visibility="@{loading ? View.GONE : View.VISIBLE}"
android:text="Login"
app:cornerRadius="@dimen/space_default"
app:layout_constraintEnd_toEndOf="parent"
......@@ -92,23 +104,39 @@
android:id="@+id/tvDontHaveAnAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginTop="15dp"
android:layout_marginStart="40dp"
android:text="@string/dont_have_an_account"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/tvSignUpHere"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/btnLogin" />
app:layout_constraintTop_toBottomOf="@+id/materialTextView"
app:layout_constraintVertical_bias="0.293" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/tvSignUpHere"
android:onClick="tvSignUpHere"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="15dp"
android:onClick="tvSignUpHere"
android:text="@string/sign_up_here"
android:textColor="@color/blue"
app:layout_constraintStart_toEndOf="@id/tvDontHaveAnAccount"
app:layout_constraintTop_toBottomOf="@id/btnLogin" />
android:layout_marginEnd="15dp"
app:layout_constraintBottom_toBottomOf="@+id/tvDontHaveAnAccount"
app:layout_constraintStart_toEndOf="@+id/tvDontHaveAnAccount"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/tvDontHaveAnAccount"
app:layout_constraintVertical_bias="0.0" />
<ProgressBar
android:id="@+id/pgLoading"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:visibility="@{loading ? View.VISIBLE : View.GONE}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialTextView"
app:layout_constraintVertical_bias="0.057" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
\ No newline at end of file
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