aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/twitter/task/Searches.java
blob: 91f6fbcc421d69cbb4946353c0b4c924ca6274bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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<SavedSearch, Void> {
        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<Void, Void> {
        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<List<SavedSearch>, Void> {
        private final Account account;

        public GetAllSavedSearchesTask(Account account) {
            this.account = account;
        }

        @Override
        protected List<SavedSearch> doInBackground() throws Exception {
            return ListUtils.map(account.getTwitter().savedSearches().getSavedSearches(), SavedSearch::fromTwitter);
        }
    }

    // TODO: List<Tweet> is not good here
    public static class SearchTask extends BackgroundTask<List<Tweet>, 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<Tweet> doInBackground() throws TwitterException {
            return Tweet.fromTwitter(account.getTwitter().search(query).getTweets(), account.getUserId());
        }
    }
}