Commit 974f98d2 authored by Wahyu Wibowo's avatar Wahyu Wibowo

crud product

parent 77bd59af
package com.example.yourcashiertest.adapters; package com.example.yourcashiertest.adapters;
public class ProductAdapter { import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import com.example.yourcashiertest.R;
import com.example.yourcashiertest.databinding.ItemListBinding;
import com.example.yourcashiertest.entities.Product;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.List;
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {
private List<Product> products = new ArrayList<>();
public interface ProductListener {
void onUpdate(Product product);
void onDelete(Product product);
}
private ProductListener listener;
public void setListener(ProductListener listener) {
this.listener = listener;
}
public void setProducts(List<Product> products) {
this.products = products;
notifyDataSetChanged();
}
@NonNull
@Override
public ProductAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(
DataBindingUtil.inflate(
LayoutInflater.from(parent.getContext()),
R.layout.item_product,
parent,
false
)
);
}
@Override
public void onBindViewHolder(@NonNull ProductAdapter.ViewHolder holder, int position) {
holder.bindData(products.get(position), listener);
}
@Override
public int getItemCount() {
return products.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private ItemListBinding binding;
public ViewHolder(@NonNull ItemListBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public void bindData(Product product, ProductListener listener) {
binding.setProduct(product);
DecimalFormat rupiah = (DecimalFormat) DecimalFormat.getCurrencyInstance();
DecimalFormatSymbols format = new DecimalFormatSymbols();
format.setCurrencySymbol("Rp. ");
format.setMonetaryDecimalSeparator(',');
format.setGroupingSeparator('.');
rupiah.setDecimalFormatSymbols(format);
binding.setPrice(rupiah.format(product.getPrice()));
binding.ivUpdate.setOnClickListener(view -> listener.onUpdate(product));
binding.ivDelete.setOnClickListener(view -> listener.onDelete(product));
}
}
} }
\ No newline at end of file
package com.example.yourcashiertest.databases; package com.example.yourcashiertest.databases;
public class LocalDatabase { import android.content.Context;
import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.DatabaseConfiguration;
import androidx.room.InvalidationTracker;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.sqlite.db.SupportSQLiteOpenHelper;
import com.example.yourcashiertest.daos.ProductDao;
import com.example.yourcashiertest.entities.Product;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Database(entities = {Product.class}, version = 1, exportSchema = false)
public abstract class LocalDatabase extends RoomDatabase {
public abstract ProductDao productDao();
private static volatile LocalDatabase INSTANCE;
private static final int NUMBER_OF_THREADS = 4;
public static final ExecutorService databaseWriteExecutor =
Executors.newFixedThreadPool(NUMBER_OF_THREADS);
public static LocalDatabase getDatabase(Context context) {
if (INSTANCE == null) {
synchronized (LocalDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
LocalDatabase.class, "pasarindo_database")
.build();
}
}
}
return INSTANCE;
}
@NonNull
@Override
protected SupportSQLiteOpenHelper createOpenHelper(DatabaseConfiguration config) {
return null;
}
@NonNull
@Override
protected InvalidationTracker createInvalidationTracker() {
return null;
}
@Override
public void clearAllTables() {
}
} }
\ No newline at end of file
package com.example.yourcashiertest.repositories; package com.example.yourcashiertest.repositories;
import android.app.Application;
import androidx.lifecycle.LiveData;
import com.example.yourcashiertest.daos.ProductDao;
import com.example.yourcashiertest.databases.LocalDatabase;
import com.example.yourcashiertest.entities.Product;
import java.util.List;
public class ProductRepository { public class ProductRepository {
private ProductDao productDao;
private LiveData<List<Product>> products;
public ProductRepository(Application application) {
LocalDatabase db = LocalDatabase.getDatabase(application);
productDao = db.productDao();
products = productDao.getProducts();
}
public void insert(Product product) {
LocalDatabase.databaseWriteExecutor.execute(() -> productDao.insertProduct(product));
}
public void update(Product product) {
LocalDatabase.databaseWriteExecutor.execute(() -> productDao.updateProduct(product));
}
public void delete(Product product) {
LocalDatabase.databaseWriteExecutor.execute(() -> productDao.deleteProduct(product));
}
public LiveData<List<Product>> filteredProducts(String s) {
return productDao.getFilteredProducts(s);
}
} }
package com.example.yourcashiertest.viewmodels; package com.example.yourcashiertest.viewmodels;
public class ProductViewModel { import android.app.Application;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import com.example.yourcashiertest.entities.Product;
import com.example.yourcashiertest.repositories.ProductRepository;
import java.io.File;
import java.util.List;
public class ProductViewModel extends AndroidViewModel {
private MutableLiveData<String> photo = new MutableLiveData<>("");
private MutableLiveData<String> query = new MutableLiveData<>("%");
private MutableLiveData<Product> product = new MutableLiveData<>();
private LiveData<List<Product>> products;
private ProductRepository repository;
public void setPhoto(String photo) {
this.photo.setValue(photo);
}
public void setProduct(Product product) {
this.product.setValue(product);
}
public LiveData<String> getPhoto() {
return photo;
}
public LiveData<List<Product>> getProducts() {
return products;
}
public ProductViewModel(@NonNull Application application) {
super(application);
repository = new ProductRepository(application);
products = Transformations.switchMap(query, s -> repository.filteredProducts(s));
}
public void filter(String s) {
String query = TextUtils.isEmpty(s) ? "%" : "%" + s + "%";
this.query.postValue(query);
}
public void insertProduct(Product product) {
repository.insert(product);
}
public void updateProduct(Product product) {
repository.update(product);
}
public void deleteProduct(Product product) {
File photo = new File(product.getPhoto());
if (photo.exists() && photo.delete())
System.out.println("DELETED: " + product.getPhoto());
repository.delete(product);
}
public MutableLiveData<Product> getProduct() {
return product;
}
} }
\ No newline at end of file
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M11,9h2L13,6h3L16,4h-3L13,1h-2v3L8,4v2h3v3zM7,18c-1.1,0 -1.99,0.9 -1.99,2S5.9,22 7,22s2,-0.9 2,-2 -0.9,-2 -2,-2zM17,18c-1.1,0 -1.99,0.9 -1.99,2s0.89,2 1.99,2 2,-0.9 2,-2 -0.9,-2 -2,-2zM7.17,14.75l0.03,-0.12 0.9,-1.63h7.45c0.75,0 1.41,-0.41 1.75,-1.03l3.86,-7.01L19.42,4h-0.01l-1.1,2 -2.76,5L8.53,11l-0.13,-0.27L6.16,6l-0.95,-2 -0.94,-2L1,2v2h2l3.6,7.59 -1.35,2.45c-0.16,0.28 -0.25,0.61 -0.25,0.96 0,1.1 0.9,2 2,2h12v-2L7.42,15c-0.13,0 -0.25,-0.11 -0.25,-0.25z"/>
</vector>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="380"
android:viewportHeight="384">
<path
android:fillColor="@android:color/white"
android:pathData="M64,341.333C64,364.907 83.093,384 106.667,384h170.667C300.907,384 320,364.907 320,341.333v-256H64V341.333z" />
<path
android:fillColor="@android:color/white"
android:pathData="M266.667,21.333l-21.334,-21.333l-106.666,0l-21.334,21.333l-74.666,0l0,42.667l298.666,0l0,-42.667z" />
</vector>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="348.882"
android:viewportHeight="348.882">
<path
android:fillColor="@android:color/white"
android:pathData="M333.988,11.758l-0.42,-0.383C325.538,4.04 315.129,0 304.258,0c-12.187,0 -23.888,5.159 -32.104,14.153L116.803,184.231c-1.416,1.55 -2.49,3.379 -3.154,5.37l-18.267,54.762c-2.112,6.331 -1.052,13.333 2.835,18.729c3.918,5.438 10.23,8.685 16.886,8.685c0,0 0.001,0 0.001,0c2.879,0 5.693,-0.592 8.362,-1.76l52.89,-23.138c1.923,-0.841 3.648,-2.076 5.063,-3.626L336.771,73.176C352.937,55.479 351.69,27.929 333.988,11.758zM130.381,234.247l10.719,-32.134l0.904,-0.99l20.316,18.556l-0.904,0.99L130.381,234.247zM314.621,52.943L182.553,197.53l-20.316,-18.556L294.305,34.386c2.583,-2.828 6.118,-4.386 9.954,-4.386c3.365,0 6.588,1.252 9.082,3.53l0.419,0.383C319.244,38.922 319.63,47.459 314.621,52.943z" />
<path
android:fillColor="@android:color/white"
android:pathData="M303.85,138.388c-8.284,0 -15,6.716 -15,15v127.347c0,21.034 -17.113,38.147 -38.147,38.147H68.904c-21.035,0 -38.147,-17.113 -38.147,-38.147V100.413c0,-21.034 17.113,-38.147 38.147,-38.147h131.587c8.284,0 15,-6.716 15,-15s-6.716,-15 -15,-15H68.904c-37.577,0 -68.147,30.571 -68.147,68.147v180.321c0,37.576 30.571,68.147 68.147,68.147h181.798c37.576,0 68.147,-30.571 68.147,-68.147V153.388C318.85,145.104 312.134,138.388 303.85,138.388z" />
</vector>
...@@ -25,23 +25,46 @@ ...@@ -25,23 +25,46 @@
<ImageView <ImageView
android:id="@+id/iv_product" android:id="@+id/iv_product"
android:layout_width="wrap_content" android:adjustViewBounds="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/elips"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452" app:layout_constraintHorizontal_bias="0.526"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.026" app:layout_constraintVertical_bias="0.075"
tools:srcCompat="@tools:sample/avatars" /> android:src="@drawable/burger"/>
<ImageView
android:id="@+id/ivUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/space_small"
android:src="@drawable/ic_update"
app:layout_constraintBottom_toBottomOf="@+id/iv_product"
app:layout_constraintStart_toStartOf="@+id/iv_product"
app:layout_constraintTop_toTopOf="@+id/iv_product"
app:layout_constraintVertical_bias="0.0" />
<ImageView
android:id="@+id/ivDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:padding="@dimen/space_small"
android:src="@drawable/ic_delete"
app:layout_constraintBottom_toBottomOf="@+id/iv_product"
app:layout_constraintEnd_toEndOf="@+id/iv_product"
app:layout_constraintTop_toTopOf="@+id/iv_product"
app:layout_constraintVertical_bias="0.0" />
<TextView <TextView
android:id="@+id/tv_name" android:id="@+id/tv_name"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/name" android:text="@string/name"
android:layout_margin="20dp" android:layout_margin="10dp"
android:textStyle="bold" android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
...@@ -67,15 +90,15 @@ ...@@ -67,15 +90,15 @@
android:id="@+id/tv_price" android:id="@+id/tv_price"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/price"
android:layout_marginTop="3dp" android:layout_marginTop="3dp"
android:text="@string/price"
android:textStyle="bold" android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.055" app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="@+id/tv_desc"
app:layout_constraintTop_toBottomOf="@+id/tv_desc" app:layout_constraintTop_toBottomOf="@+id/tv_desc"
app:layout_constraintVertical_bias="0.038" /> app:layout_constraintVertical_bias="0.37" />
<View <View
android:id="@+id/v_add" android:id="@+id/v_add"
...@@ -97,7 +120,7 @@ ...@@ -97,7 +120,7 @@
app:layout_constraintEnd_toEndOf="@+id/v_add" app:layout_constraintEnd_toEndOf="@+id/v_add"
app:layout_constraintStart_toStartOf="@+id/v_add" app:layout_constraintStart_toStartOf="@+id/v_add"
app:layout_constraintTop_toTopOf="@+id/v_add" app:layout_constraintTop_toTopOf="@+id/v_add"
app:srcCompat="@drawable/ic_baseline_add_24" /> app:srcCompat="@drawable/ic_baseline_add_shopping_cart_24" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
......
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