Search in sources :

Example 1 with PojoComparator

use of fr.neamar.kiss.pojo.PojoComparator in project KISS by Neamar.

the class DataHandler method getResults.

/**
     * Get records for this query.
     *
     * @param context android context
     * @param query   query to run
     * @return ordered list of records
     */
public ArrayList<Pojo> getResults(Context context, String query) {
    query = query.toLowerCase().trim().replaceAll("<", "&lt;");
    currentQuery = query;
    // Have we ever made the same query and selected something ?
    List<ValuedHistoryRecord> lastIdsForQuery = DBHelper.getPreviousResultsForQuery(context, query);
    HashMap<String, Integer> knownIds = new HashMap<>();
    for (ValuedHistoryRecord id : lastIdsForQuery) {
        knownIds.put(id.record, id.value);
    }
    // Ask all providers for data
    ArrayList<Pojo> allPojos = new ArrayList<>();
    for (ProviderEntry entry : this.providers.values()) {
        if (entry.provider != null) {
            // Retrieve results for query:
            List<Pojo> pojos = entry.provider.getResults(query);
            // Add results to list
            for (Pojo pojo : pojos) {
                // Give a boost if item was previously selected for this query
                if (knownIds.containsKey(pojo.id)) {
                    pojo.relevance += 25 * Math.min(5, knownIds.get(pojo.id));
                }
                allPojos.add(pojo);
            }
        }
    }
    // Sort records according to relevance
    Collections.sort(allPojos, new PojoComparator());
    return allPojos;
}
Also used : ShortcutsPojo(fr.neamar.kiss.pojo.ShortcutsPojo) Pojo(fr.neamar.kiss.pojo.Pojo) HashMap(java.util.HashMap) PojoComparator(fr.neamar.kiss.pojo.PojoComparator) ValuedHistoryRecord(fr.neamar.kiss.db.ValuedHistoryRecord) ArrayList(java.util.ArrayList)

Aggregations

ValuedHistoryRecord (fr.neamar.kiss.db.ValuedHistoryRecord)1 Pojo (fr.neamar.kiss.pojo.Pojo)1 PojoComparator (fr.neamar.kiss.pojo.PojoComparator)1 ShortcutsPojo (fr.neamar.kiss.pojo.ShortcutsPojo)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1