Search in sources :

Example 1 with SearchController

use of com.cmput301w18t05.taskzilla.controller.SearchController in project Taskzilla by CMPUT301W18T05.

the class SearchFragment method onViewCreated.

/**
 * Determines what is done on the view once it has been created
 *
 * @param view                  The current activity
 * @param savedInstanceState    The state of the screen before interrupts appear, such as leaving the app
 */
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // expand search bar by default
    searchField = view.findViewById(R.id.searchView);
    /*
         * Listens for changes in the searchview
         * OnQueryTextChange invokes whenever the user types, while on the other hand
         * OnQueryTextSubmit invokes only when the user submits the keywords.
         *
         * There are 3 scenarios:
         *      1. if(sentence.length == 0)  and if(searchController.getKeywords().isEmpty() == true)
         *          sentence is the words the user types in.
         *
         *          These conditions check if the user entered in previous search words,
         *          if not then nothing happens since all tasks are shown to begin with
         *
         *      2. if(sentence.length == 0)  and if(searchController.getKeywords().isEmpty() == false)
         *
         *          These conditions are for when the user had previously searched for keywords and now wants
         *          to see all the available tasks. It first clears the listview then does a search returning all
         *          available tasks.
         *
         *      3. The last condition is when the user is searching for tasks by entering in keyword(s).
         *          This clears the listview and then proceeds to make a search request using the keyword(s)
         *          as a parameter. It then returns the tasks containing the keywords in the descriptions.
         *
         */
    searchField.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String text) {
            String sentence;
            sentence = text.toLowerCase();
            if (sentence.length() == 0) {
                // Checks if user entered text in search bar
                if (!searchController.getKeywords().isEmpty()) {
                    // Checks if keywords is empty, if yes return already loaded array of tasks
                    searchController.clearKeywords();
                    searchController.getAllRequest();
                }
            } else {
                // Adds keyword to list and loads new set of tasks based on keywords
                searchController.clearKeywords();
                searchController.addKeywords(sentence);
                searchController.searchRequest(sentence);
            }
            notifyChange();
            return false;
        }
    });
    mButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            viewMap();
        }
    });
    // Set up listview and adapter
    searchResults = new ArrayList<>();
    searchController = new SearchController(this, getActivity());
    adapterText = new TaskCustomAdapter(getActivity(), R.layout.tasks_list_view2, searchResults);
    availableTasksText.setAdapter(adapterText);
    /*
         *  Listens for user tapping on a task in the listview
         *
         *  Sets currentTask variable to the current task tapped, which
         *  is used later on to determine which item to remove from the listview
         *  if the item was deleted.
         */
    availableTasksText.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            currentTask = searchResults.get(i);
            viewTask(searchResults.get(i).getId());
        }
    });
    // get all available tasks
    searchController.getAllRequest();
}
Also used : SearchView(android.widget.SearchView) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) SearchController(com.cmput301w18t05.taskzilla.controller.SearchController) TaskCustomAdapter(com.cmput301w18t05.taskzilla.TaskCustomAdapter) SearchView(android.widget.SearchView) AdapterView(android.widget.AdapterView)

Aggregations

View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 SearchView (android.widget.SearchView)1 TaskCustomAdapter (com.cmput301w18t05.taskzilla.TaskCustomAdapter)1 SearchController (com.cmput301w18t05.taskzilla.controller.SearchController)1