Commit 52927de2 authored by Wahyu Wibowo's avatar Wahyu Wibowo

filter by category

parent 1d80eafc
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourcashiertest">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
......@@ -19,23 +21,23 @@
android:theme="@style/AppTheme2">
<activity
android:name=".activities.SkActivity"
android:screenOrientation="portrait"/>
android:screenOrientation="portrait" />
<activity
android:name=".activities.WelcomeActivity"
android:screenOrientation="portrait"/>
android:screenOrientation="portrait" />
<activity
android:name=".activities.ForgetPassword"
android:screenOrientation="portrait"/>
android:screenOrientation="portrait" />
<activity
android:name=".activities.RegisterActivity"
android:screenOrientation="portrait"/>
android:screenOrientation="portrait" />
<activity
android:name=".activities.StatusPayment"
android:screenOrientation="portrait"/>
android:screenOrientation="portrait" />
<activity
android:name=".activities.SplashActivity"
android:theme="@style/AppTheme2"
android:screenOrientation="portrait">
android:screenOrientation="portrait"
android:theme="@style/AppTheme2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
......@@ -44,19 +46,20 @@
</activity>
<activity
android:name=".activities.PaymentActivity"
android:screenOrientation="portrait"/>
android:screenOrientation="portrait" />
<activity
android:name=".activities.CartActivity"
android:screenOrientation="portrait"/>
android:screenOrientation="portrait" />
<activity
android:name=".activities.MainActivity"
android:screenOrientation="portrait"/>
android:screenOrientation="portrait" />
<activity
android:name=".activities.ProductActivity"
android:screenOrientation="portrait"/>
android:screenOrientation="portrait" />
<activity
android:name=".activities.LoginActivity"
android:screenOrientation="portrait"/>
android:screenOrientation="portrait" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.yourcashiertest.fileprovider"
......
......@@ -19,6 +19,7 @@ import android.widget.PopupMenu;
import android.widget.Toast;
import com.example.yourcashiertest.R;
import com.example.yourcashiertest.adapters.CategoryAdapter;
import com.example.yourcashiertest.adapters.ProductAdapter;
import com.example.yourcashiertest.databinding.ActivityMainBinding;
import com.example.yourcashiertest.entities.Cart;
......@@ -26,6 +27,7 @@ import com.example.yourcashiertest.entities.Product;
import com.example.yourcashiertest.viewmodels.CartViewModel;
import com.example.yourcashiertest.viewmodels.ProductViewModel;
import java.util.ArrayList;
import java.util.List;
......@@ -55,6 +57,7 @@ public class MainActivity extends AppCompatActivity {
prefManager = new PrefManager(this);
ProductAdapter adapter = new ProductAdapter();
CategoryAdapter categoryAdapter = new CategoryAdapter();
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
cartViewModel = new ViewModelProvider(this).get(CartViewModel.class);
......@@ -109,10 +112,22 @@ public class MainActivity extends AppCompatActivity {
popupMenu.show();
});
// binding.rvCategory.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false));
binding.rvCategory.setLayoutManager(new LinearLayoutManager(MainActivity.this,
LinearLayoutManager.HORIZONTAL, false));
binding.rvCategory.setAdapter(categoryAdapter);
binding.rvProducts.setAdapter(adapter);
viewModel = new ViewModelProvider(this).get(ProductViewModel.class);
viewModel.getProducts().observe(this, adapter::setProducts);
viewModel.getProducts().observe(this, products -> {
adapter.setProducts(products);
List<String> list = new ArrayList<>();
for (int i = 0; i < products.size(); i++){
list.add(products.get(i).getCategory());
}
categoryAdapter.setCategories(list);
});
categoryAdapter.setListener(category -> viewModel.filter(category));
adapter.setListener(new ProductAdapter.ProductListener() {
@Override
......
package com.example.yourcashiertest.adapters;
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.CategoryItemBinding;
import java.util.ArrayList;
import java.util.List;
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {
List<String> categories = new ArrayList<>();
public interface CategoryListener{
void onClickItem(String category);
}
private CategoryListener listener;
public void setListener(CategoryListener listener){
this.listener = listener;
}
public void setCategories(List<String> categories) {
this.categories = categories;
notifyDataSetChanged();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new CategoryAdapter.ViewHolder(
DataBindingUtil.inflate(
LayoutInflater.from(parent.getContext()),
R.layout.category_item,
parent,
false
)
);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.bindData(categories.get(position), listener);
}
@Override
public int getItemCount() {
return categories.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
CategoryItemBinding binding;
public ViewHolder(@NonNull CategoryItemBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public void bindData(String category, CategoryListener listener){
binding.setCategory(category);
binding.cvCategory.setOnClickListener(v -> listener.onClickItem(category));
}
}
}
......@@ -17,7 +17,7 @@ public interface ProductDao {
@Query("SELECT * FROM products ORDER BY id DESC")
public LiveData<List<Product>> getProducts();
@Query("SELECT * FROM products WHERE name LIKE :query ORDER BY id DESC")
@Query("SELECT * FROM products WHERE name LIKE :query OR category LIKE:query ORDER BY id DESC")
public LiveData<List<Product>> getFilteredProducts(String query);
@Insert(onConflict = OnConflictStrategy.IGNORE)
......
......@@ -19,6 +19,7 @@ public class ProductViewModel extends AndroidViewModel {
private MutableLiveData<String> photo = new MutableLiveData<>("");
private MutableLiveData<String> query = new MutableLiveData<>("%");
private MutableLiveData<String> category = new MutableLiveData<>("%");
private MutableLiveData<Product> product = new MutableLiveData<>();
private LiveData<List<Product>> products;
......@@ -44,7 +45,6 @@ public class ProductViewModel extends AndroidViewModel {
super(application);
repository = new ProductRepository(application);
products = Transformations.switchMap(query, s -> repository.filteredProducts(s));
}
......
......@@ -128,22 +128,22 @@
android:imeOptions="actionSearch"
android:inputType="textCapWords"
android:textSize="@dimen/text_default" />
<!-- <androidx.recyclerview.widget.RecyclerView-->
<!-- android:id="@+id/rvCategory"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="0dp"-->
<!-- tools:listitem="@layout/category_item"-->
<!-- app:layout_constraintStart_toStartOf="parent"-->
<!-- app:layout_constraintEnd_toEndOf="parent"-->
<!-- app:layout_constraintTop_toBottomOf="@id/rectangle_4"-->
<!-- app:layout_constraintBottom_toTopOf="@id/rvProducts"-->
<!-- />-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvCategory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/rectangle_4"
app:layout_constraintBottom_toTopOf="@id/rvProducts"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvProducts"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginVertical="15dp"
app:layout_constraintTop_toBottomOf="@id/rectangle_4"
app:layout_constraintTop_toBottomOf="@id/rvCategory"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
......
......@@ -17,7 +17,7 @@
<com.google.android.material.card.MaterialCardView
android:id="@+id/cvCategory"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimaryDark"
android:clickable="true"
......@@ -31,17 +31,16 @@
app:layout_constraintEnd_toEndOf="parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingHorizontal="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example"
android:text="@{category}"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="#fff"
tools:text="elizabeth"
android:layout_centerInParent="true"/>
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
......
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