lewisjwilson / kmj

The all-in-one Japanese dictionary/flashcard app.
2 stars 0 forks source link

Fix API Search Query #38

Closed lewisjwilson closed 3 years ago

lewisjwilson commented 3 years ago

Search updates at the incorrect time. Fix this in "searchpage.kt"

lewisjwilson commented 3 years ago

Attempted a fix by changing debounce time from 300ms to 500ms

lewisjwilson commented 3 years ago

debounce is not the issue.

Not understanding flow correctly.

Get API data via flow emit (in collect statement), then add data to recyclerview. Right now, data is not emitting. recyclerview updates within the flow (launch).

lewisjwilson commented 3 years ago
import kotlinx.coroutines.*
import kotlin.coroutines.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlin.coroutines.CoroutineContext

val namesFlow = flow {
  val names = listOf("Jody", "Steve", "Lance", "Joe")
  for (name in names) {
    delay(100)
    emit(name)
  }
}

fun main() = runBlocking {
  namesFlow
      .mapLatest { 
          if(it.length>3){
              println(it)
          }
      }
      .catch { println("Error!") }
      .collect { it }

  println()
}

Check to see how flow functions: Online Compiler

lewisjwilson commented 3 years ago

Abort using flow. Update SearchView code as follows?

@Override
public boolean onQueryTextChange(String searchTerm) {
    mQueryString = searchTerm;
    mHandler.removeCallbacksAndMessages(null);

    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
           //Put your call to the server here (with mQueryString)
        }
    }, 300);
    return true;
}

OR

var queryTextChangedJob: Job? = null

...

fun onQueryTextChange(query: String) {

    queryTextChangedJob?.cancel()

    queryTextChangedJob = launch(Dispatchers.Main) {
        delay(500)
        performSearch(query)
    }
}

From here...

lewisjwilson commented 3 years ago

Removed flow and replaces with a coroutine with a delay of 1000ms (open to change)

lewisjwilson commented 3 years ago

Not solved. Delays search, however consider the following:

This means the search terms are combined in mSearchList, displaying results from both "ha" and "hat".

Somehow in the coroutine, must clear mSearchList, however 2 instances of dataFromNetwork are running. Possible solution:

Create a check in dataFromNetwork to see if queryjob is null (set null on new text change). Inside for loop (getting new data for recycler:

if (queryjob = null){
    break
}

Also see job.isActive()

lewisjwilson commented 3 years ago

Pretty much working. Functional, just a little slow.