0

I am learning xamarin forms. I using Azure search API to query news but sometimes the result of search is not good because of my search code. I can tap for instance "cat" but because of the delay "ca" is sent to the API.

What I woud like please :

I would like to launch automatically my search function each time the user is not tapping on search bar.

For instance lets say a user is willing to tap "tennis is a sport":

So he taps quikly on search bar and make a pause on "tennis is" => launch function on "tennis is"

Then he continues and finishs the sentence : "tennis is a sport" => launch function on "tennis is a sport"

If there is a better way of doing it, I will take.

Here is my code :

Task tasksearch = null;
int timeDelaySearch = 1000;

void OnSearcMyNews(System.Object sender, Xamarin.Forms.TextChangedEventArgs e)
{


    try {

        if (tasksearch == null || tasksearch.IsCompleted)
        {
            tasksearch = Task.Run(async () =>
                {
                    await Task.Delay(timeDelaySearch);



                    Device.BeginInvokeOnMainThread( () =>
                    {
                   

                        searchTerm = searchTerm.ToLower();
                        _ = SearchNewsDataAsync(searchTerm, UserConnected.NewsLanguage);
                        tasksearch = null;

                    });



            });
        }



    }
    catch (Exception ex)
    {
        
    }




}

Update 26 Feb: I have tried this but still do not work correctly. if I would like to type "working" sometimes I get search for "workin".

New code :

   int _countSeconds = 0;
    void OnSearcMyNews(System.Object sender, Xamarin.Forms.TextChangedEventArgs e)
    {
    
    
    try {
    
          
        _countSeconds = 0; // if user type again the function is Initialize
        Device.StartTimer(TimeSpan.FromMilliseconds(1000), () =>
        {
        // count == 2 to wait a little bit before searching
        if (_countSeconds == 2)
        {
            _ = Task.Run(async () => {
    
                await Device.InvokeOnMainThreadAsync(async () =>
                {
    
                     GetNewsAPIDataAsync();
    
                });
    
            });
    
    
            return false;
        }
    
       
        _countSeconds = _countSeconds + 1;
        return true;
        });
             
    
    
    
    }
    catch (Exception ex)
    {
        
    }
    
    }
hugo
  • 441
  • 5
  • 25
  • if you only want to search after they have finished typing, don't use the OnTextChanged event. Use the search button instead – Jason Feb 18 '23 at 12:35
  • @Jason For the User experience it is not good. Today many app do the search without tapping on a button (social network app for instance) – hugo Feb 18 '23 at 12:47
  • That is exactly what you asked for - "I would like to launch my search function once the user has finished". If that is not what you want, then please [edit] your question to more clearly explain what your goal is. – Jason Feb 18 '23 at 12:50
  • @Jason I have updated the code to make it clear – hugo Feb 18 '23 at 13:01
  • You probably need to try some combination of 1) timestamp each keystroke and only start a search when some minimum delay has past without receiving a new keystroke, and 2) when starting a search, if a search is already running, cancel it (right now you throw away any keystroke until the prior search completes) – Jason Feb 18 '23 at 13:41
  • @Jason I cannot see how to do it. Please need some help – hugo Feb 18 '23 at 15:13
  • if you don't understand how to do that, a simpler approach would be to stop checking for an existing search on each keystroke and start a new search each time – Jason Feb 18 '23 at 19:27
  • Any one for help please ? – hugo Feb 25 '23 at 18:47
  • Have you attempted to follow any of the suggestions already made? – Jason Feb 25 '23 at 19:32
  • @Jason I have update the code. I still have so delay and wrong search. I would like to search "Jason" sometimes it will search "Jaso" – hugo Feb 26 '23 at 22:21

0 Answers0