Commit 89dc87e2 authored by Nihar Thakkar's avatar Nihar Thakkar
Browse files

Improve search request

parent d9781017
......@@ -6,39 +6,31 @@ import io.eelo.appinstaller.api.SearchRequest
import io.eelo.appinstaller.application.model.Application
import io.eelo.appinstaller.applicationmanager.ApplicationManager
import io.eelo.appinstaller.utils.Error
import io.eelo.appinstaller.utils.Common
import io.eelo.appinstaller.utils.Constants
class SearchElement(private val query: String, private val applicationManager: ApplicationManager, private val callback: SearchModelInterface) : AsyncTask<Context, Void, Void>() {
val apps = ArrayList<Application>()
private var nextPage = 1
class SearchElement(private val query: String, private val pageNumber: Int,
private val applicationManager: ApplicationManager,
private val callback: SearchModelInterface) :
AsyncTask<Context, Void, ArrayList<Application>>() {
private var error: Error? = null
fun loadMoreInBackground(context: Context) {
executeOnExecutor(Common.EXECUTOR, context)
}
override fun doInBackground(vararg params: Context): Void? {
loadMore(params[0])
nextPage++
return null
}
override fun onPostExecute(result: Void?) {
callback.onSearchComplete(error, apps)
override fun doInBackground(vararg params: Context): ArrayList<Application> {
val apps = ArrayList<Application>()
SearchRequest(query, pageNumber, Constants.RESULTS_PER_PAGE)
.request { applicationError, searchResult ->
when (applicationError) {
null -> {
apps.addAll(searchResult!!.getApplications(applicationManager, params[0]))
}
else -> {
error = applicationError
}
}
}
return apps
}
private fun loadMore(context: Context) {
SearchRequest(query, nextPage, Constants.RESULTS_PER_PAGE).request { applicationError, searchResult ->
when (applicationError) {
null -> {
apps.addAll(searchResult!!.getApplications(applicationManager, context))
}
else -> {
error = applicationError
}
}
}
override fun onPostExecute(result: ArrayList<Application>) {
callback.onSearchComplete(error, result)
}
}
......@@ -2,6 +2,7 @@ package io.eelo.appinstaller.search.model
import android.arch.lifecycle.MutableLiveData
import android.content.Context
import android.os.AsyncTask
import io.eelo.appinstaller.application.model.Application
import io.eelo.appinstaller.applicationmanager.ApplicationManager
import io.eelo.appinstaller.utils.Common
......@@ -12,8 +13,9 @@ class SearchModel : SearchModelInterface {
val suggestionList = MutableLiveData<ArrayList<String>>()
val applicationList = MutableLiveData<ArrayList<Application>>()
var screenError = MutableLiveData<Error>()
private var element: SearchElement? = null
private var applicationManager: ApplicationManager? = null
private var pageNumber = 0
private lateinit var searchQuery: String
override fun initialise(applicationManager: ApplicationManager) {
this.applicationManager = applicationManager
......@@ -31,20 +33,22 @@ class SearchModel : SearchModelInterface {
}
override fun search(context: Context, searchQuery: String) {
if (Common.isNetworkAvailable(context)) {
element?.apps?.forEach { app ->
app.decrementUses()
}
element = SearchElement(searchQuery, applicationManager!!, this)
loadMore(context)
} else {
applicationList.value = ArrayList()
screenError.value = Error.NO_INTERNET
pageNumber = 0
this.searchQuery = searchQuery
applicationList.value?.forEach { app ->
app.decrementUses()
}
loadMore(context)
}
override fun loadMore(context: Context) {
element!!.loadMoreInBackground(context)
if (Common.isNetworkAvailable(context)) {
pageNumber++
SearchElement(searchQuery, pageNumber, applicationManager!!, this)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, context)
} else {
screenError.value = Error.NO_INTERNET
}
}
override fun onSearchComplete(error: Error?, applicationList: ArrayList<Application>) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment