-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
125 additions
and
10 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
app/app/src/main/java/uah/vaccination/adapters/ParentsAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
app/app/src/main/java/uah/vaccination/fragments/HomeFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters