Search in sources :

Example 1 with ValuedHistoryRecord

use of fr.neamar.kiss.db.ValuedHistoryRecord in project KISS by Neamar.

the class DataHandler method getHistory.

/**
     * Return previously selected items.<br />
     * May return null if no items were ever selected (app first use)<br />
     * May return an empty set if the providers are not done building records,
     * in this case it is probably a good idea to call this function 500ms after
     *
     * @param context        android context
     * @param itemCount      max number of items to retrieve, total number may be less (search or calls are not returned for instance)
     * @param smartHistory   Recency vs Frecency
     * @param itemsToExclude Items to exclude from history
     * @return pojos in recent history
     */
public ArrayList<Pojo> getHistory(Context context, int itemCount, boolean smartHistory, ArrayList<Pojo> itemsToExclude) {
    // Pre-allocate array slots that are likely to be used based on the current maximum item
    // count
    ArrayList<Pojo> history = new ArrayList<>(Math.min(itemCount, 256));
    // Read history
    List<ValuedHistoryRecord> ids = DBHelper.getHistory(context, itemCount, smartHistory);
    // Find associated items
    for (int i = 0; i < ids.size(); i++) {
        // Ask all providers if they know this id
        Pojo pojo = getPojo(ids.get(i).record);
        if (pojo != null) {
            //Look if the pojo should get excluded
            boolean exclude = false;
            for (int j = 0; j < itemsToExclude.size(); j++) {
                if (itemsToExclude.get(j).id.equals(pojo.id)) {
                    exclude = true;
                    break;
                }
            }
            if (!exclude) {
                history.add(pojo);
            }
        }
    }
    return history;
}
Also used : ShortcutsPojo(fr.neamar.kiss.pojo.ShortcutsPojo) Pojo(fr.neamar.kiss.pojo.Pojo) ValuedHistoryRecord(fr.neamar.kiss.db.ValuedHistoryRecord) ArrayList(java.util.ArrayList)

Example 2 with ValuedHistoryRecord

use of fr.neamar.kiss.db.ValuedHistoryRecord 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)2 Pojo (fr.neamar.kiss.pojo.Pojo)2 ShortcutsPojo (fr.neamar.kiss.pojo.ShortcutsPojo)2 ArrayList (java.util.ArrayList)2 PojoComparator (fr.neamar.kiss.pojo.PojoComparator)1 HashMap (java.util.HashMap)1