Commit 5e3f552d authored by Wahyu Wibowo's avatar Wahyu Wibowo

add fitur register with service

parent 8eb8cf67
......@@ -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.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) {
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 com.google.gson.annotations.SerializedName;
public class User{
@SerializedName("password")
private String password;
@SerializedName("full_name")
private String fullName;
@SerializedName("phone_number")
private String phoneNumber;
@SerializedName("email")
private String email;
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;
}
}
\ No newline at end of file
package com.example.yourcashiertest.services;
import com.example.yourcashiertest.models.User;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;
public interface UserService {
@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 androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.example.yourcashiertest.clients.ApiClient;
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 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) {
}
});
}
}
<?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>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.RegisterActivity">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/tvRegister"
android:layout_width="wrap_content"
......@@ -15,7 +22,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textStyle="bold"
android:text="REGISTER ACCOUNT"/>
android:text="REGISTER ACCOUNT" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilUsername"
......@@ -37,6 +44,7 @@
android:imeOptions="actionNext"
android:textSize="@dimen/text_default" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilEmail"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
......@@ -57,6 +65,7 @@
android:imeOptions="actionNext"
android:textSize="@dimen/text_default" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilPhoneNumber"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
......@@ -77,6 +86,7 @@
android:imeOptions="actionNext"
android:textSize="@dimen/text_default" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilPassword"
app:passwordToggleEnabled="true"
......@@ -121,6 +131,7 @@
android:textColor="@color/blue"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvDescSk" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/tvYourCashier"
android:onClick="tvSignInHere"
......@@ -130,7 +141,7 @@
android:layout_marginTop="10dp"
android:text="Your Cashier"
app:layout_constraintStart_toEndOf="@id/tvSK"
app:layout_constraintTop_toBottomOf="@id/tvDescSk"/>
app:layout_constraintTop_toBottomOf="@id/tvDescSk" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnRegister"
......@@ -168,4 +179,5 @@
android:textColor="@color/blue"
app:layout_constraintStart_toEndOf="@id/tvDontHaveAnAccount"
app:layout_constraintTop_toBottomOf="@id/btnRegister" />
</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