Search in sources :

Example 1 with DomainPaginator

use of net.dean.jraw.paginators.DomainPaginator in project Slide by ccrama.

the class ListViewRemoteViewsFactory method onCreate.

// Initialize the data set.
public void onCreate() {
    // In onCreate() you set up any connections / cursors to your data source. Heavy lifting,
    // for example downloading or creating content etc, should be deferred to onDataSetChanged()
    // or getViewAt(). Taking more than 20 seconds in this call will result in an ANR.
    records = new ArrayList<>();
    if (NetworkUtil.isConnected(mContext)) {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                if (Authentication.reddit == null) {
                    new Authentication(mContext.getApplicationContext());
                    Authentication.me = Authentication.reddit.me();
                    Authentication.mod = Authentication.me.isMod();
                    Authentication.authentication.edit().putBoolean(Reddit.SHARED_PREF_IS_MOD, Authentication.mod).apply();
                    if (Reddit.notificationTime != -1) {
                        Reddit.notifications = new NotificationJobScheduler(mContext);
                        Reddit.notifications.start(mContext.getApplicationContext());
                    }
                    if (Reddit.cachedData.contains("toCache")) {
                        Reddit.autoCache = new AutoCacheScheduler(mContext);
                        Reddit.autoCache.start(mContext.getApplicationContext());
                    }
                    final String name = Authentication.me.getFullName();
                    Authentication.name = name;
                    LogUtil.v("AUTHENTICATED");
                    if (Authentication.reddit.isAuthenticated()) {
                        final Set<String> accounts = Authentication.authentication.getStringSet("accounts", new HashSet<String>());
                        if (accounts.contains(name)) {
                            // convert to new system
                            accounts.remove(name);
                            accounts.add(name + ":" + Authentication.refresh);
                            Authentication.authentication.edit().putStringSet("accounts", accounts).apply();
                        }
                        Authentication.isLoggedIn = true;
                        Reddit.notFirst = true;
                    }
                }
                String sub = SubredditWidgetProvider.getSubFromId(id, mContext);
                Paginator p;
                if (sub.equals("frontpage")) {
                    p = new SubredditPaginator(Authentication.reddit);
                } else if (!sub.contains(".")) {
                    p = new SubredditPaginator(Authentication.reddit, sub);
                } else {
                    p = new DomainPaginator(Authentication.reddit, sub);
                }
                p.setLimit(50);
                switch(SubredditWidgetProvider.getSorting(id, mContext)) {
                    case 0:
                        p.setSorting(Sorting.HOT);
                        break;
                    case 1:
                        p.setSorting(Sorting.NEW);
                        break;
                    case 2:
                        p.setSorting(Sorting.RISING);
                        break;
                    case 3:
                        p.setSorting(Sorting.TOP);
                        break;
                    case 4:
                        p.setSorting(Sorting.CONTROVERSIAL);
                        break;
                }
                switch(SubredditWidgetProvider.getSortingTime(id, mContext)) {
                    case 0:
                        p.setTimePeriod(TimePeriod.HOUR);
                        break;
                    case 1:
                        p.setTimePeriod(TimePeriod.DAY);
                        break;
                    case 2:
                        p.setTimePeriod(TimePeriod.WEEK);
                        break;
                    case 3:
                        p.setTimePeriod(TimePeriod.MONTH);
                        break;
                    case 4:
                        p.setTimePeriod(TimePeriod.YEAR);
                        break;
                    case 5:
                        p.setTimePeriod(TimePeriod.ALL);
                        break;
                }
                try {
                    ArrayList<Submission> s = new ArrayList<>(p.next());
                    records = new ArrayList<>();
                    for (Submission subm : s) {
                        if (!PostMatch.doesMatch(subm) && !subm.isStickied()) {
                            records.add(subm);
                        }
                    }
                } catch (Exception e) {
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                Intent widgetUpdateIntent = new Intent(mContext, SubredditWidgetProvider.class);
                widgetUpdateIntent.setAction(SubredditWidgetProvider.UPDATE_MEETING_ACTION);
                widgetUpdateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
                mContext.sendBroadcast(widgetUpdateIntent);
            }
        }.execute();
    } else {
        Intent widgetUpdateIntent = new Intent(mContext, SubredditWidgetProvider.class);
        widgetUpdateIntent.setAction(SubredditWidgetProvider.UPDATE_MEETING_ACTION);
        widgetUpdateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
        mContext.sendBroadcast(widgetUpdateIntent);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) AutoCacheScheduler(me.ccrama.redditslide.Autocache.AutoCacheScheduler) Submission(net.dean.jraw.models.Submission) ArrayList(java.util.ArrayList) DomainPaginator(net.dean.jraw.paginators.DomainPaginator) Intent(android.content.Intent) NotificationJobScheduler(me.ccrama.redditslide.Notifications.NotificationJobScheduler) DomainPaginator(net.dean.jraw.paginators.DomainPaginator) SubredditPaginator(net.dean.jraw.paginators.SubredditPaginator) Paginator(net.dean.jraw.paginators.Paginator) SubredditPaginator(net.dean.jraw.paginators.SubredditPaginator) Authentication(me.ccrama.redditslide.Authentication) HashSet(java.util.HashSet)

Aggregations

Intent (android.content.Intent)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Authentication (me.ccrama.redditslide.Authentication)1 AutoCacheScheduler (me.ccrama.redditslide.Autocache.AutoCacheScheduler)1 NotificationJobScheduler (me.ccrama.redditslide.Notifications.NotificationJobScheduler)1 Submission (net.dean.jraw.models.Submission)1 DomainPaginator (net.dean.jraw.paginators.DomainPaginator)1 Paginator (net.dean.jraw.paginators.Paginator)1 SubredditPaginator (net.dean.jraw.paginators.SubredditPaginator)1