Added search view and introduced filter icon

This commit is contained in:
Gilbert Kimutai 2019-02-20 06:51:33 +03:00
parent e975393b5e
commit 9732f236ef
36 changed files with 398 additions and 80 deletions

View File

@ -81,4 +81,8 @@ dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.3.0' implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.miguelcatalan:materialsearchview:1.4.0'
} }

View File

@ -12,6 +12,16 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity
android:name=".ui.product.ProductSearchActivity"
android:label="@string/title_activity_product_search"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
<activity <activity
android:name=".ui.coupon.CouponActivity" android:name=".ui.coupon.CouponActivity"
android:label="@string/title_activity_coupon" android:label="@string/title_activity_coupon"
@ -36,6 +46,9 @@
android:name=".ui.product.ShopActivity" android:name=".ui.product.ShopActivity"
android:label="@string/title_activity_shop" android:label="@string/title_activity_shop"
android:theme="@style/AppTheme.NoActionBar"> android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity> </activity>
<activity android:name=".MainActivity"> <activity android:name=".MainActivity">
<intent-filter> <intent-filter>

View File

@ -22,4 +22,12 @@ public class ProductRepository extends WoocommerceRepository {
} }
public WooLiveData<List<Product>> search(String term) {
final WooLiveData<List<Product>> callBack = new WooLiveData();
woocommerce.ProductRepository().search(term).enqueue(callBack);
return callBack;
}
} }

View File

@ -0,0 +1,132 @@
package me.gilo.wc.ui.product
import android.app.SearchManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.SearchView
import android.view.Menu
import android.widget.Toast
import io.github.inflationx.viewpump.ViewPumpContextWrapper
import me.gilo.wc.R
import kotlinx.android.synthetic.main.activity_product_search.*
import kotlinx.android.synthetic.main.content_shop.*
import me.gilo.wc.adapter.ProductAdapter
import me.gilo.wc.common.BaseActivity
import me.gilo.wc.common.Status
import me.gilo.wc.ui.state.ProgressDialogFragment
import me.gilo.wc.viewmodels.ProductViewModel
import me.gilo.woodroid.models.Product
import org.json.JSONObject
import java.util.ArrayList
class ProductSearchActivity : BaseActivity() {
lateinit var adapter: ProductAdapter
lateinit var products: ArrayList<Product>
lateinit var viewModel: ProductViewModel
val TAG = this::getLocalClassName
override fun attachBaseContext(newBase: Context) {
super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase))
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_product_search)
setSupportActionBar(toolbar)
viewModel = getViewModel(ProductViewModel::class.java)
title = "Search"
val layoutManager = GridLayoutManager(baseContext, 2)
rvShop.layoutManager = layoutManager
rvShop.isNestedScrollingEnabled = false
products = ArrayList()
adapter = ProductAdapter(products)
rvShop.adapter = adapter
handleIntent(intent)
}
override fun onNewIntent(intent: Intent) {
handleIntent(intent)
}
private fun handleIntent(intent: Intent) {
if (Intent.ACTION_SEARCH == intent.action) {
val query = intent.getStringExtra(SearchManager.QUERY)
search(query)
}
}
private fun search(query : String) {
viewModel.search(query).observe(this, android.arch.lifecycle.Observer { response ->
when (response!!.status()) {
Status.LOADING -> {
showLoading("Performing search", "This will only take a short while")
}
Status.SUCCESS -> {
stopShowingLoading()
val productsResponse = response.data()
for (product in productsResponse) {
products.add(product)
}
adapter.notifyDataSetChanged()
}
Status.ERROR -> {
stopShowingLoading()
var message: String
var loginError = JSONObject(response.error().message)
if (loginError.has("status_message")) {
message = loginError.getString("status_message")
} else {
message = response.error().message.toString()
}
Toast.makeText(baseContext, message, Toast.LENGTH_LONG).show()
}
Status.EMPTY -> {
stopShowingLoading()
}
}
})
}
private lateinit var progressDialog: ProgressDialogFragment
fun showLoading(title: String, message: String) {
val manager = supportFragmentManager
progressDialog = ProgressDialogFragment.newInstance(title, message)
progressDialog.isCancelable = false
progressDialog.show(manager, "progress")
}
fun showLoading() {
showLoading("This will only take a sec", "Loading")
}
fun stopShowingLoading() {
progressDialog.dismiss()
}
}

View File

@ -3,7 +3,9 @@ package me.gilo.wc.ui.product
import android.content.Context import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.support.v7.widget.GridLayoutManager import android.support.v7.widget.GridLayoutManager
import android.view.Menu
import android.widget.Toast import android.widget.Toast
import com.miguelcatalan.materialsearchview.MaterialSearchView
import io.github.inflationx.viewpump.ViewPumpContextWrapper import io.github.inflationx.viewpump.ViewPumpContextWrapper
import kotlinx.android.synthetic.main.activity_shop.* import kotlinx.android.synthetic.main.activity_shop.*
import kotlinx.android.synthetic.main.content_shop.* import kotlinx.android.synthetic.main.content_shop.*
@ -16,6 +18,9 @@ import me.gilo.wc.viewmodels.ProductViewModel
import me.gilo.woodroid.models.Product import me.gilo.woodroid.models.Product
import org.json.JSONObject import org.json.JSONObject
import java.util.* import java.util.*
import android.text.Editable
import android.text.TextWatcher
class ShopActivity : BaseActivity() { class ShopActivity : BaseActivity() {
@ -49,17 +54,29 @@ class ShopActivity : BaseActivity() {
products() products()
etSearch.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable) {}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (s.isNotEmpty()) {
search(s.toString())
}else{
products()
}
}
})
} }
private fun products() { private fun search(query : String) {
viewModel.products().observe(this, android.arch.lifecycle.Observer { response -> viewModel.search(query).observe(this, android.arch.lifecycle.Observer { response ->
when (response!!.status()) { when (response!!.status()) {
Status.LOADING -> { Status.LOADING -> {
showLoading("Performing log in", "This will only take a short while")
} }
Status.SUCCESS -> { Status.SUCCESS -> {
stopShowingLoading() products.clear()
val productsResponse = response.data() val productsResponse = response.data()
for (product in productsResponse) { for (product in productsResponse) {
@ -71,22 +88,12 @@ class ShopActivity : BaseActivity() {
} }
Status.ERROR -> { Status.ERROR -> {
stopShowingLoading()
var message: String
var loginError = JSONObject(response.error().message)
if (loginError.has("status_message")) {
message = loginError.getString("status_message")
} else {
message = response.error().message.toString()
}
Toast.makeText(baseContext, message, Toast.LENGTH_LONG).show()
} }
Status.EMPTY -> { Status.EMPTY -> {
stopShowingLoading()
} }
} }
@ -94,6 +101,43 @@ class ShopActivity : BaseActivity() {
} }
private fun products() {
viewModel.products().observe(this, android.arch.lifecycle.Observer { response ->
when (response!!.status()) {
Status.LOADING -> {
}
Status.SUCCESS -> {
products.clear()
val productsResponse = response.data()
for (product in productsResponse) {
products.add(product)
}
adapter.notifyDataSetChanged()
}
Status.ERROR -> {
}
Status.EMPTY -> {
}
}
})
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.products, menu)
return true
}
private lateinit var progressDialog: ProgressDialogFragment private lateinit var progressDialog: ProgressDialogFragment
fun showLoading(title: String, message: String) { fun showLoading(title: String, message: String) {

View File

@ -20,4 +20,8 @@ public final class ProductViewModel extends ViewModel {
public WooLiveData<List<Product>> products() { public WooLiveData<List<Product>> products() {
return productRepository.products(); return productRepository.products();
} }
public WooLiveData<List<Product>> search(String term) {
return productRepository.search(term);
}
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 139 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 134 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 107 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 97 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 107 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 134 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 107 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 110 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 117 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 107 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 117 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 B

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="#ffffff"
android:pathData="M10,18h4v-2h-4v2zM3,6v2h18L21,6L3,6zM6,13h12v-2L6,11v2z"/>
</vector>

Binary file not shown.

After

Width:  |  Height:  |  Size: 373 B

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.product.ProductSearchActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_product_search"/>
</android.support.design.widget.CoordinatorLayout>

View File

@ -17,7 +17,47 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/> app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:elevation="2dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="8dp">
<android.support.v7.widget.AppCompatEditText
android:id="@+id/etSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:cursorVisible="true"
android:textColorHint="@color/text_black_9"
android:textColor="@color/text_black_2"
android:hint="Seatch"
android:background="@drawable/rect_white"
android:paddingLeft="12dp"
android:paddingTop="12dp"
android:paddingRight="12dp"
android:paddingBottom="12dp"
android:textSize="16sp"
app:backgroundTint="#ffffff"/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="16dp"
android:src="@drawable/baseline_search_white_18dp"
android:tint="@color/text_black_9"
android:layout_gravity="right|center"
/>
</FrameLayout>
</android.support.design.widget.AppBarLayout> </android.support.design.widget.AppBarLayout>

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_shop"
tools:context=".ui.product.ShopActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:id="@+id/rvProducts"
></android.support.v7.widget.RecyclerView>
</LinearLayout>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_filter"
android:icon="@drawable/baseline_filter_list_24"
android:orderInCategory="100"
android:title="Filter"
android:iconTint="#ffffff"
app:showAsAction="always"/>
</menu>

View File

@ -10,5 +10,8 @@
<string name="font_regular">fonts/GT-America-Regular.otf</string> <string name="font_regular">fonts/GT-America-Regular.otf</string>
<string name="title_activity_add_coupon">AddCouponActivity</string> <string name="title_activity_add_coupon">AddCouponActivity</string>
<string name="title_activity_coupons">CouponsActivity</string> <string name="title_activity_coupons">CouponsActivity</string>
<string name="search_title">search</string>
<string name="search_hint">Search product</string>
<string name="title_activity_product_search">ProductSearchActivity</string>
</resources> </resources>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint" />