Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1f8f08ce authored by Nihar Thakkar's avatar Nihar Thakkar
Browse files

Implement "load more" feature for search screen

parent 374935f4
Loading
Loading
Loading
Loading
+17 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.RelativeLayout
import android.widget.TextView
import io.eelo.appinstaller.R
import io.eelo.appinstaller.application.model.Application
@@ -36,6 +37,7 @@ class SearchFragment : Fragment(), SearchView.OnQueryTextListener, SearchView.On
    private lateinit var splashContainer: LinearLayout
    private var applicationList = ArrayList<Application>()
    private var applicationManager: ApplicationManager? = null
    private var isLoadingMoreApplications = false

    fun initialise(applicationManager: ApplicationManager) {
        this.applicationManager = applicationManager
@@ -56,6 +58,7 @@ class SearchFragment : Fragment(), SearchView.OnQueryTextListener, SearchView.On
        splashContainer = view.findViewById(R.id.splash_container)
        val errorContainer = view.findViewById<LinearLayout>(R.id.error_container)
        val errorDescription = view.findViewById<TextView>(R.id.error_description)
        val loadMoreContainer = view.findViewById<RelativeLayout>(R.id.load_more_container)

        searchViewModel.initialise(applicationManager!!)
        recyclerView.visibility = View.GONE
@@ -68,6 +71,7 @@ class SearchFragment : Fragment(), SearchView.OnQueryTextListener, SearchView.On
        }
        errorContainer.visibility = View.GONE
        view.findViewById<TextView>(R.id.error_resolve).setOnClickListener {
            loadMoreContainer.visibility = View.GONE
            progressBar.visibility = View.VISIBLE
            onQueryTextSubmit(searchView.query.toString())
        }
@@ -83,11 +87,19 @@ class SearchFragment : Fragment(), SearchView.OnQueryTextListener, SearchView.On
        recyclerView.setHasFixedSize(true)
        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.adapter = ApplicationListAdapter(activity!!, applicationList)
        loadMoreContainer.visibility = View.GONE
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                if (!recyclerView.canScrollVertically(1)) {
                    loadMoreContainer.visibility = View.VISIBLE
                    recyclerView.scrollToPosition(applicationList.size - 1)
                    if (!isLoadingMoreApplications) {
                        isLoadingMoreApplications = true
                        searchViewModel.loadMore(context!!)
                    }
                } else {
                    loadMoreContainer.visibility = View.GONE
                }
            }
        })

@@ -105,6 +117,8 @@ class SearchFragment : Fragment(), SearchView.OnQueryTextListener, SearchView.On
                applicationList.addAll(it)
                progressBar.visibility = View.GONE
                recyclerView.adapter.notifyDataSetChanged()
                loadMoreContainer.visibility = View.GONE
                isLoadingMoreApplications = false
                if (applicationList.isEmpty()) {
                    recyclerView.visibility = View.GONE
                } else {
@@ -119,9 +133,11 @@ class SearchFragment : Fragment(), SearchView.OnQueryTextListener, SearchView.On
                errorDescription.text = activity!!.getString(Common.getScreenErrorDescriptionId(it))
                errorContainer.visibility = View.VISIBLE
                progressBar.visibility = View.GONE
                loadMoreContainer.visibility = View.GONE
            } else {
                errorContainer.visibility = View.GONE
            }
            isLoadingMoreApplications = false
        })

        // Handle suggestion clicks
+6 −0
Original line number Diff line number Diff line
@@ -49,6 +49,8 @@ class SearchModel : SearchModelInterface {
        } else {
            if (pageNumber == 1) {
                screenError.value = Error.NO_INTERNET
            } else {
                screenError.value = null
            }
        }
    }
@@ -64,10 +66,14 @@ class SearchModel : SearchModelInterface {
            }
            if (applicationList.isEmpty() && pageNumber == 1) {
                screenError.value = Error.NO_RESULTS
            } else {
                screenError.value = null
            }
        } else {
            if (pageNumber == 1) {
                screenError.value = error
            } else {
                screenError.value = null
            }
        }
    }
+15 −2
Original line number Diff line number Diff line
@@ -61,8 +61,21 @@
        android:id="@+id/app_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:scrollbars="none"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintBottom_toTopOf="@id/load_more_container"
        app:layout_constraintTop_toBottomOf="@id/toolbar" />

    <RelativeLayout
        android:id="@+id/load_more_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/layout_padding_medium"
        app:layout_constraintBottom_toBottomOf="parent">

        <ProgressBar
            android:layout_width="@dimen/load_more_progress_bar_size"
            android:layout_height="@dimen/load_more_progress_bar_size"
            android:layout_centerInParent="true" />

    </RelativeLayout>

</android.support.constraint.ConstraintLayout>
 No newline at end of file