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

add fitur register with service

parent 8eb8cf67
...@@ -52,6 +52,11 @@ dependencies { ...@@ -52,6 +52,11 @@ dependencies {
implementation "com.github.bumptech.glide:glide:$glide_version" implementation "com.github.bumptech.glide:glide:$glide_version"
annotationProcessor "com.github.bumptech.glide:compiler:$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' testImplementation 'junit:junit:4.13'
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'
......
package com.example.yourcashiertest.activities; package com.example.yourcashiertest.activities;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.webkit.WebView; import android.webkit.WebView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.Toast;
import com.example.yourcashiertest.R; 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 { public class RegisterActivity extends AppCompatActivity {
WebView wbCompany; WebView wbCompany;
ProgressBar pbLoading; ProgressBar pbLoading;
private static final String urLCompany = "http://multidaya.id/"; private static final String urLCompany = "http://multidaya.id/";
ActivityRegisterBinding binding;
User user;
UserViewModel viewModel;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(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) { public void tvSignInHere(View view) {
......
package com.example.yourcashiertest.adapters; package com.example.yourcashiertest.adapters;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
...@@ -11,8 +10,6 @@ import androidx.recyclerview.widget.RecyclerView; ...@@ -11,8 +10,6 @@ import androidx.recyclerview.widget.RecyclerView;
import com.example.yourcashiertest.R; import com.example.yourcashiertest.R;
import com.example.yourcashiertest.databinding.CartItemBinding; import com.example.yourcashiertest.databinding.CartItemBinding;
import com.example.yourcashiertest.entities.Cart; 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.DecimalFormat;
import java.text.DecimalFormatSymbols; 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) {
}
});
}
}
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