add search
This commit is contained in:
parent
1d3c8ca14b
commit
b27a218b51
@ -5,24 +5,18 @@ import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.commit
|
||||
import kotlinx.android.synthetic.main.fragment_library.*
|
||||
import kotlinx.coroutines.*
|
||||
import org.mosad.teapod.MainActivity
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
import org.mosad.teapod.ui.MediaFragment
|
||||
import org.mosad.teapod.util.CustomAdapter
|
||||
import org.mosad.teapod.util.GUIMedia
|
||||
|
||||
class LibraryFragment : Fragment() {
|
||||
|
||||
private val parser = AoDParser()
|
||||
|
||||
private var mediaList = arrayListOf<GUIMedia>()
|
||||
private lateinit var adapter : CustomAdapter
|
||||
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
return inflater.inflate(R.layout.fragment_library, container, false)
|
||||
}
|
||||
@ -35,13 +29,10 @@ class LibraryFragment : Fragment() {
|
||||
AoDParser().listAnimes()
|
||||
}
|
||||
|
||||
mediaList = AoDParser.mediaList
|
||||
|
||||
// create and set the adapter, needs context
|
||||
withContext(Dispatchers.Main) {
|
||||
adapter = CustomAdapter(requireContext(), mediaList)
|
||||
adapter = CustomAdapter(requireContext(), AoDParser.mediaList)
|
||||
list_library.adapter = adapter
|
||||
//adapter.notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
|
||||
@ -49,35 +40,13 @@ class LibraryFragment : Fragment() {
|
||||
}
|
||||
|
||||
private fun initActions() {
|
||||
list_library.setOnItemClickListener { parent, view, position, id ->
|
||||
println("selected item is: ${mediaList[position]}")
|
||||
//showDetailFragment(mediaList[position])
|
||||
list_library.setOnItemClickListener { _, _, position, _ ->
|
||||
val media = adapter.getItem(position) as GUIMedia
|
||||
println("selected item is: ${media.title}")
|
||||
|
||||
val mainActivity = activity as MainActivity
|
||||
mainActivity.showDetailFragment(mediaList[position])
|
||||
mainActivity.showDetailFragment(media)
|
||||
}
|
||||
}
|
||||
|
||||
private fun showDetailFragment(media: GUIMedia) {
|
||||
val streams = parser.loadStreams(media.link) // load the streams for the selected media
|
||||
println("done: $streams")
|
||||
|
||||
// TODO create detail fragment
|
||||
val mediaFragment = MediaFragment(media, streams)
|
||||
|
||||
activity?.supportFragmentManager?.commit {
|
||||
add(mediaFragment, "MediaFragment")
|
||||
addToBackStack(null)
|
||||
}
|
||||
|
||||
// activity?.supportFragmentManager?.beginTransaction()?.let {
|
||||
// it.replace(mContainer.id, mediaFragment, "MediaFragment")
|
||||
// it.addToBackStack("MediaFragment")
|
||||
// it.commit()
|
||||
// }
|
||||
|
||||
|
||||
println("done!!!")
|
||||
}
|
||||
|
||||
}
|
@ -4,18 +4,65 @@ import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.SearchView
|
||||
import androidx.fragment.app.Fragment
|
||||
import kotlinx.android.synthetic.main.fragment_search.*
|
||||
import kotlinx.coroutines.*
|
||||
import org.mosad.teapod.MainActivity
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
import org.mosad.teapod.util.CustomAdapter
|
||||
import org.mosad.teapod.util.GUIMedia
|
||||
|
||||
class SearchFragment : Fragment() {
|
||||
|
||||
private lateinit var adapter : CustomAdapter
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
return inflater.inflate(R.layout.fragment_search, container, false)
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
text_search.text = "This is the search Fragment"
|
||||
|
||||
GlobalScope.launch {
|
||||
if (AoDParser.mediaList.isEmpty()) {
|
||||
AoDParser().listAnimes()
|
||||
}
|
||||
|
||||
// create and set the adapter, needs context
|
||||
withContext(Dispatchers.Main) {
|
||||
adapter = CustomAdapter(requireContext(), AoDParser.mediaList)
|
||||
list_search.adapter = adapter
|
||||
//adapter.notifyDataSetChanged()
|
||||
}
|
||||
|
||||
initActions()
|
||||
}
|
||||
}
|
||||
|
||||
private fun initActions() {
|
||||
search_text.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
|
||||
override fun onQueryTextSubmit(query: String?): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
override fun onQueryTextChange(newText: String?): Boolean {
|
||||
println("new text is: $newText")
|
||||
adapter.filter.filter(newText)
|
||||
adapter.notifyDataSetChanged()
|
||||
|
||||
return false
|
||||
}
|
||||
})
|
||||
|
||||
list_search.setOnItemClickListener { _, _, position, _ ->
|
||||
val media = adapter.getItem(position) as GUIMedia
|
||||
|
||||
println("selected item is: ${media.title}")
|
||||
|
||||
val mainActivity = activity as MainActivity
|
||||
mainActivity.showDetailFragment(media)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,16 +1,28 @@
|
||||
package org.mosad.teapod.util
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ArrayAdapter
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import android.widget.*
|
||||
import androidx.core.content.res.ResourcesCompat
|
||||
import com.bumptech.glide.Glide
|
||||
import com.bumptech.glide.request.target.CustomTarget
|
||||
import com.bumptech.glide.request.transition.Transition
|
||||
import org.mosad.teapod.R
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
class CustomAdapter(context: Context, private val media: ArrayList<GUIMedia>) : ArrayAdapter<GUIMedia>(context, R.layout.linear_media, media) {
|
||||
class CustomAdapter(val context: Context, private val originalMedia: ArrayList<GUIMedia>) : BaseAdapter(), Filterable {
|
||||
|
||||
private var filteredMedia = originalMedia.map { it.copy() }
|
||||
private val customFilter = CustomFilter()
|
||||
|
||||
init {
|
||||
println("initial filtered size is: ${filteredMedia.size}")
|
||||
}
|
||||
|
||||
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
||||
val view = convertView ?: LayoutInflater.from(context).inflate(R.layout.linear_media, parent, false)
|
||||
@ -18,10 +30,54 @@ class CustomAdapter(context: Context, private val media: ArrayList<GUIMedia>) :
|
||||
val textTitle = view.findViewById<TextView>(R.id.text_title)
|
||||
val imagePoster = view.findViewById<ImageView>(R.id.image_poster)
|
||||
|
||||
textTitle.text = media[position].title
|
||||
Glide.with(context).load(media[position].posterLink).into(imagePoster)
|
||||
textTitle.text = filteredMedia[position].title
|
||||
Glide.with(context).load(filteredMedia[position].posterLink).into(imagePoster)
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
override fun getFilter(): Filter {
|
||||
return customFilter
|
||||
}
|
||||
|
||||
override fun getCount(): Int {
|
||||
return filteredMedia.size
|
||||
}
|
||||
|
||||
override fun getItem(position: Int): Any {
|
||||
return filteredMedia[position]
|
||||
}
|
||||
|
||||
override fun getItemId(position: Int): Long {
|
||||
return position.toLong()
|
||||
}
|
||||
|
||||
inner class CustomFilter : Filter() {
|
||||
override fun performFiltering(constraint: CharSequence?): FilterResults {
|
||||
val filterTerm = constraint.toString().toLowerCase(Locale.ROOT)
|
||||
val results = FilterResults()
|
||||
|
||||
val filteredList = if (filterTerm.isEmpty()) {
|
||||
originalMedia
|
||||
} else {
|
||||
originalMedia.filter {
|
||||
it.title.toLowerCase(Locale.ROOT).contains(filterTerm)
|
||||
}
|
||||
}
|
||||
|
||||
results.values = filteredList
|
||||
results.count = filteredList.size
|
||||
|
||||
println("filtered size is: ${results.count}")
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
|
||||
filteredMedia = results?.values as ArrayList<GUIMedia>
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,3 +1,7 @@
|
||||
package org.mosad.teapod.util
|
||||
|
||||
data class GUIMedia(val title: String, val posterLink: String, val shortDesc : String, val link: String)
|
||||
data class GUIMedia(val title: String, val posterLink: String, val shortDesc : String, val link: String) {
|
||||
override fun toString(): String {
|
||||
return title
|
||||
}
|
||||
}
|
@ -7,17 +7,29 @@
|
||||
android:background="#fafafa"
|
||||
tools:context=".ui.search.SearchFragment">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text_search"
|
||||
<SearchView
|
||||
android:id="@+id/search_text"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:textAlignment="center"
|
||||
android:textSize="20sp"
|
||||
android:layout_height="48dp"
|
||||
android:iconifiedByDefault="false"
|
||||
android:paddingStart="5dp"
|
||||
android:paddingTop="5dp"
|
||||
android:paddingEnd="5dp"
|
||||
android:paddingBottom="5dp"
|
||||
android:queryHint="@string/search_hint"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent">
|
||||
|
||||
</SearchView>
|
||||
|
||||
<ListView
|
||||
android:id="@+id/list_search"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
app:layout_constraintTop_toBottomOf="@+id/search_text" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
@ -24,5 +24,6 @@
|
||||
android:id="@+id/image_poster"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:minHeight="223dp"
|
||||
tools:srcCompat="@drawable/ic_launcher_background" />
|
||||
</LinearLayout>
|
@ -5,6 +5,9 @@
|
||||
<string name="title_search">Search</string>
|
||||
<string name="title_account">Account</string>
|
||||
|
||||
<!-- search fragment -->
|
||||
<string name="search_hint">Search for movies and series</string>
|
||||
|
||||
<!-- dialogs -->
|
||||
<string name="save">save</string>
|
||||
<string name="cancel">@android:string/cancel</string>
|
||||
|
Loading…
Reference in New Issue
Block a user