package net.lacolaco.smileessence.twitter.task; import net.lacolaco.smileessence.data.Account; import net.lacolaco.smileessence.entity.SavedSearch; import net.lacolaco.smileessence.entity.Tweet; import net.lacolaco.smileessence.util.BackgroundTask; import net.lacolaco.smileessence.util.ListUtils; import twitter4j.Query; import twitter4j.TwitterException; import java.util.List; public class Searches { public static class CreateSavedSearchTask extends BackgroundTask { private final Account account; private final String query; public CreateSavedSearchTask(Account account, String query) { this.account = account; this.query = query; } @Override protected SavedSearch doInBackground() throws Exception { return SavedSearch.fromTwitter(account.getTwitter().savedSearches().createSavedSearch(query)); } } public static class DestroySavedSearchTask extends BackgroundTask { private final Account account; private final long id; public DestroySavedSearchTask(Account account, long id) { this.account = account; this.id = id; } @Override protected Void doInBackground() throws Exception { account.getTwitter().savedSearches().destroySavedSearch(id); return null; } } public static class GetAllSavedSearchesTask extends BackgroundTask, Void> { private final Account account; public GetAllSavedSearchesTask(Account account) { this.account = account; } @Override protected List doInBackground() throws Exception { return ListUtils.map(account.getTwitter().savedSearches().getSavedSearches(), SavedSearch::fromTwitter); } } // TODO: List is not good here public static class SearchTask extends BackgroundTask, Void> { // ------------------------------ FIELDS ------------------------------ private final Account account; private final Query query; // --------------------------- CONSTRUCTORS --------------------------- public SearchTask(Account account, Query query) { this.account = account; this.query = query; } // ------------------------ OVERRIDE METHODS ------------------------ @Override protected List doInBackground() throws TwitterException { return Tweet.fromTwitter(account.getTwitter().search(query).getTweets(), account.getUserId()); } } }