Skip to content

Commit

Permalink
[APP] Add Basic SearchBox
Browse files Browse the repository at this point in the history
  • Loading branch information
yousinix committed Aug 27, 2019
1 parent 82a30da commit cd1be95
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 10 deletions.
54 changes: 54 additions & 0 deletions app/app/src/main/java/uah/vaccination/adapters/ParentsAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package uah.vaccination.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Filter
import android.widget.Filterable
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import uah.vaccination.R
import uah.vaccination.models.Parent

class ParentsAdapter(val parents: ArrayList<Parent>) :
RecyclerView.Adapter<ParentsAdapter.ParentHolder>(), Filterable {

private var filteredParents = parents

override fun getItemCount() = filteredParents.size

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
ParentHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_parent, parent, false))

override fun onBindViewHolder(holder: ParentHolder, position: Int) {
val parent = filteredParents[position]
holder.emailTextView.text = parent.email
}

override fun getFilter() = filteringStrategy

private val filteringStrategy: Filter = object : Filter() {

override fun performFiltering(p0: CharSequence?): FilterResults {
val query = p0.toString()
val filteredList = if (query.isNotEmpty()) {
parents.filter { it.email!!.contains(query, true) } as ArrayList<Parent>
} else {
parents
}
return FilterResults().also { it.values = filteredList }
}

@Suppress("UNCHECKED_CAST")
override fun publishResults(p0: CharSequence?, p1: FilterResults?) {
filteredParents = p1!!.values as ArrayList<Parent>
notifyDataSetChanged()
}

}

inner class ParentHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
internal var emailTextView: TextView = itemView.findViewById(R.id.email_text_view)
}

}
47 changes: 43 additions & 4 deletions app/app/src/main/java/uah/vaccination/fragments/HomeFragment.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,57 @@
package uah.vaccination.fragments

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.firebase.firestore.FirebaseFirestore
import uah.vaccination.R
import uah.vaccination.adapters.ParentsAdapter
import uah.vaccination.models.Parent

class HomeFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
private lateinit var adapter: ParentsAdapter
private var parents = arrayListOf<Parent>()
private val firestore = FirebaseFirestore.getInstance()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

firestore.collection("parents")
.get()
.addOnSuccessListener { result ->
for (document in result) {
parents.add(document.toObject(Parent::class.java))
}
}

val rootView = inflater.inflate(R.layout.fragment_home, container, false)
val parentsRecyclerView = rootView.findViewById(R.id.parents_recycler_view) as RecyclerView // Add this
val searchEditText = rootView.findViewById(R.id.search_edit_text) as EditText // Add this

adapter = ParentsAdapter(parents)
parentsRecyclerView.adapter = adapter
parentsRecyclerView.layoutManager = LinearLayoutManager(activity)

searchEditText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) = Unit
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) = Unit
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
adapter.filter.filter(s.toString())
}
})

return rootView
}

}
26 changes: 20 additions & 6 deletions app/app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".fragments.HomeFragment">
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.HomeFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Home"/>
android:orientation="vertical">

<EditText
android:id="@+id/search_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/search" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/parents_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

</FrameLayout>
7 changes: 7 additions & 0 deletions app/app/src/main/res/layout/item_parent.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/email_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:padding="8dp"/>
1 change: 1 addition & 0 deletions app/app/src/main/res/values/strings-ar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
<string name="sign_up">انشاء حساب</string>
<string name="dateOfBirth">تاريخ الميلاد</string>
<string name="parentId">رقم ولي الأمر</string>
<string name="search">بحث</string>
</resources>

0 comments on commit cd1be95

Please sign in to comment.