Search in sources :

Example 26 with Submission

use of net.dean.jraw.models.Submission in project Slide by ccrama.

the class SubmissionCache method cacheInfo.

private static void cacheInfo(List<Submission> submissions, Context mContext, String baseSub) {
    if (titles == null)
        titles = new WeakHashMap<>();
    if (info == null)
        info = new WeakHashMap<>();
    if (crosspost == null)
        crosspost = new WeakHashMap<>();
    for (Submission submission : submissions) {
        titles.put(submission.getFullName(), getTitleSpannable(submission, mContext));
        info.put(submission.getFullName(), getInfoSpannable(submission, mContext, baseSub));
        crosspost.put(submission.getFullName(), getCrosspostLine(submission, mContext));
    }
}
Also used : Submission(net.dean.jraw.models.Submission) WeakHashMap(java.util.WeakHashMap)

Example 27 with Submission

use of net.dean.jraw.models.Submission in project Slide by ccrama.

the class CommentPage method doAdapter.

public void doAdapter(boolean load) {
    commentSorting = SettingValues.getCommentSorting(subreddit);
    if (load)
        doRefresh(true);
    if (load)
        loaded = true;
    if (!single && getActivity() instanceof CommentsScreen && ((CommentsScreen) getActivity()).subredditPosts != null && Authentication.didOnline && ((CommentsScreen) getActivity()).currentPosts != null && ((CommentsScreen) getActivity()).currentPosts.size() > page) {
        try {
            comments = new SubmissionComments(fullname, this, mSwipeRefreshLayout);
        } catch (IndexOutOfBoundsException e) {
            return;
        }
        Submission s = ((CommentsScreen) getActivity()).currentPosts.get(page);
        if (s != null && s.getDataNode().has("suggested_sort") && !s.getDataNode().get("suggested_sort").asText().equalsIgnoreCase("null")) {
            String sorting = s.getDataNode().get("suggested_sort").asText().toUpperCase();
            sorting = sorting.replace("İ", "I");
            commentSorting = CommentSort.valueOf(sorting);
        } else if (s != null) {
            commentSorting = SettingValues.getCommentSorting(s.getSubredditName());
        }
        if (load)
            comments.setSorting(commentSorting);
        if (adapter == null) {
            adapter = new CommentAdapter(this, comments, rv, s, getFragmentManager());
            rv.setAdapter(adapter);
        }
    } else if (getActivity() instanceof MainActivity) {
        if (Authentication.didOnline) {
            comments = new SubmissionComments(fullname, this, mSwipeRefreshLayout);
            Submission s = ((MainActivity) getActivity()).openingComments;
            if (s != null && s.getDataNode().has("suggested_sort") && !s.getDataNode().get("suggested_sort").asText().equalsIgnoreCase("null")) {
                String sorting = s.getDataNode().get("suggested_sort").asText().toUpperCase();
                sorting = sorting.replace("İ", "I");
                commentSorting = CommentSort.valueOf(sorting);
            } else if (s != null) {
                commentSorting = SettingValues.getCommentSorting(s.getSubredditName());
            }
            if (load)
                comments.setSorting(commentSorting);
            if (adapter == null) {
                adapter = new CommentAdapter(this, comments, rv, s, getFragmentManager());
                rv.setAdapter(adapter);
            }
        } else {
            Submission s = ((MainActivity) getActivity()).openingComments;
            doRefresh(false);
            comments = new SubmissionComments(fullname, this, mSwipeRefreshLayout, s);
            if (adapter == null) {
                adapter = new CommentAdapter(this, comments, rv, s, getFragmentManager());
                rv.setAdapter(adapter);
            }
        }
    } else {
        Submission s = null;
        try {
            s = OfflineSubreddit.getSubmissionFromStorage(fullname.contains("_") ? fullname : "t3_" + fullname, getContext(), !NetworkUtil.isConnected(getActivity()), new ObjectMapper().reader());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (s != null && s.getComments() != null) {
            doRefresh(false);
            comments = new SubmissionComments(fullname, this, mSwipeRefreshLayout, s);
            if (adapter == null) {
                adapter = new CommentAdapter(this, comments, rv, s, getFragmentManager());
                rv.setAdapter(adapter);
            }
        } else if (context.isEmpty()) {
            comments = new SubmissionComments(fullname, this, mSwipeRefreshLayout);
            comments.setSorting(commentSorting);
            if (adapter == null) {
                if (s != null) {
                    adapter = new CommentAdapter(this, comments, rv, s, getFragmentManager());
                }
                rv.setAdapter(adapter);
            }
        } else {
            if (context.equals(Reddit.EMPTY_STRING)) {
                comments = new SubmissionComments(fullname, this, mSwipeRefreshLayout);
                if (load)
                    comments.setSorting(commentSorting);
            } else {
                comments = new SubmissionComments(fullname, this, mSwipeRefreshLayout, context, contextNumber);
                if (load)
                    comments.setSorting(commentSorting);
            }
        }
    }
}
Also used : CommentsScreen(me.ccrama.redditslide.Activities.CommentsScreen) SubmissionComments(me.ccrama.redditslide.Adapters.SubmissionComments) Submission(net.dean.jraw.models.Submission) MainActivity(me.ccrama.redditslide.Activities.MainActivity) IOException(java.io.IOException) CommentAdapter(me.ccrama.redditslide.Adapters.CommentAdapter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 28 with Submission

use of net.dean.jraw.models.Submission in project Slide by ccrama.

the class NewsView method clearSeenPosts.

public List<Submission> clearSeenPosts(boolean forever) {
    if (adapter.dataSet.posts != null) {
        List<Submission> originalDataSetPosts = adapter.dataSet.posts;
        OfflineSubreddit o = OfflineSubreddit.getSubreddit(id.toLowerCase(Locale.ENGLISH), false, getActivity());
        for (int i = adapter.dataSet.posts.size(); i > -1; i--) {
            try {
                if (HasSeen.getSeen(adapter.dataSet.posts.get(i))) {
                    if (forever) {
                        Hidden.setHidden(adapter.dataSet.posts.get(i));
                    }
                    o.clearPost(adapter.dataSet.posts.get(i));
                    adapter.dataSet.posts.remove(i);
                    if (adapter.dataSet.posts.isEmpty()) {
                        adapter.notifyDataSetChanged();
                    } else {
                        rv.setItemAnimator(new AlphaInAnimator());
                        adapter.notifyItemRemoved(i + 1);
                    }
                }
            } catch (IndexOutOfBoundsException e) {
            // Let the loop reset itself
            }
        }
        adapter.notifyItemRangeChanged(0, adapter.dataSet.posts.size());
        o.writeToMemoryNoStorage();
        rv.setItemAnimator(new SlideUpAlphaAnimator());
        return originalDataSetPosts;
    }
    return null;
}
Also used : AlphaInAnimator(com.mikepenz.itemanimators.AlphaInAnimator) Submission(net.dean.jraw.models.Submission) OfflineSubreddit(me.ccrama.redditslide.OfflineSubreddit) SlideUpAlphaAnimator(com.mikepenz.itemanimators.SlideUpAlphaAnimator)

Example 29 with Submission

use of net.dean.jraw.models.Submission in project Slide by ccrama.

the class SubmissionNewsAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder2, final int pos) {
    int i = pos != 0 ? pos - 1 : pos;
    if (holder2 instanceof NewsViewHolder) {
        final NewsViewHolder holder = (NewsViewHolder) holder2;
        final Submission submission = dataSet.posts.get(i);
        CreateCardView.colorCard(submission.getSubredditName().toLowerCase(Locale.ENGLISH), holder.itemView, subreddit, (subreddit.equals("frontpage") || subreddit.equals("mod") || subreddit.equals("friends") || (subreddit.equals("all")) || subreddit.contains(".") || subreddit.contains("+")));
        holder.itemView.setOnClickListener(new OnSingleClickListener() {

            @Override
            public void onSingleClick(View v) {
                if (Authentication.didOnline || submission.getComments() != null) {
                    holder.title.setAlpha(0.54f);
                    if (context instanceof MainActivity) {
                        final MainActivity a = (MainActivity) context;
                        if (a.singleMode && a.commentPager && a.adapter instanceof MainActivity.OverviewPagerAdapterComment) {
                            if (a.openingComments != submission) {
                                clicked = holder2.getAdapterPosition();
                                a.openingComments = submission;
                                a.toOpenComments = a.pager.getCurrentItem() + 1;
                                a.currentComment = holder.getAdapterPosition() - 1;
                                ((MainActivity.OverviewPagerAdapterComment) (a).adapter).storedFragment = (a).adapter.getCurrentFragment();
                                ((MainActivity.OverviewPagerAdapterComment) (a).adapter).size = a.toOpenComments + 1;
                                try {
                                    a.adapter.notifyDataSetChanged();
                                } catch (Exception ignored) {
                                }
                            }
                            a.pager.postDelayed(new Runnable() {

                                @Override
                                public void run() {
                                    a.pager.setCurrentItem(a.pager.getCurrentItem() + 1, true);
                                }
                            }, 400);
                        } else {
                            Intent i2 = new Intent(context, CommentsScreen.class);
                            i2.putExtra(CommentsScreen.EXTRA_PAGE, holder2.getAdapterPosition() - 1);
                            i2.putExtra(CommentsScreen.EXTRA_SUBREDDIT, subreddit);
                            i2.putExtra("fullname", submission.getFullName());
                            context.startActivityForResult(i2, 940);
                            clicked = holder2.getAdapterPosition();
                        }
                    } else if (context instanceof SubredditView) {
                        final SubredditView a = (SubredditView) context;
                        if (a.singleMode && a.commentPager) {
                            if (a.openingComments != submission) {
                                clicked = holder2.getAdapterPosition();
                                a.openingComments = submission;
                                a.currentComment = holder.getAdapterPosition() - 1;
                                ((SubredditView.OverviewPagerAdapterComment) (a).adapter).storedFragment = (a).adapter.getCurrentFragment();
                                ((SubredditView.OverviewPagerAdapterComment) a.adapter).size = 3;
                                a.adapter.notifyDataSetChanged();
                            }
                            a.pager.postDelayed(new Runnable() {

                                @Override
                                public void run() {
                                    a.pager.setCurrentItem(a.pager.getCurrentItem() + 1, true);
                                }
                            }, 400);
                        } else {
                            Intent i2 = new Intent(context, CommentsScreen.class);
                            i2.putExtra(CommentsScreen.EXTRA_PAGE, holder2.getAdapterPosition() - 1);
                            i2.putExtra(CommentsScreen.EXTRA_SUBREDDIT, subreddit);
                            i2.putExtra("fullname", submission.getFullName());
                            context.startActivityForResult(i2, 940);
                            clicked = holder2.getAdapterPosition();
                        }
                    }
                } else {
                    if (!Reddit.appRestart.contains("offlinepopup")) {
                        new AlertDialogWrapper.Builder(context).setTitle(R.string.cache_no_comments_found).setMessage(R.string.cache_no_comments_found_message).setCancelable(false).setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Reddit.appRestart.edit().putString("offlinepopup", "").apply();
                            }
                        }).show();
                    } else {
                        Snackbar s = Snackbar.make(holder.itemView, R.string.cache_no_comments_found_snackbar, Snackbar.LENGTH_SHORT);
                        s.setAction(R.string.misc_more_info, new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                new AlertDialogWrapper.Builder(context).setTitle(R.string.cache_no_comments_found).setMessage(R.string.cache_no_comments_found_message).setCancelable(false).setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Reddit.appRestart.edit().putString("offlinepopup", "").apply();
                                    }
                                }).show();
                            }
                        });
                        View view = s.getView();
                        TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
                        tv.setTextColor(Color.WHITE);
                        s.show();
                    }
                }
            }
        });
        new PopulateNewsViewHolder().populateNewsViewHolder(holder, submission, context, false, false, dataSet.posts, listView, custom, dataSet.offline, dataSet.subreddit.toLowerCase(Locale.ENGLISH), null);
    }
    if (holder2 instanceof SubmissionFooterViewHolder) {
        Handler handler = new Handler();
        final Runnable r = new Runnable() {

            public void run() {
                notifyItemChanged(dataSet.posts.size() + // the loading spinner to replaced by nomoreposts.xml
                1);
            }
        };
        handler.post(r);
        if (holder2.itemView.findViewById(R.id.reload) != null) {
            holder2.itemView.findViewById(R.id.reload).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dataSet.loadMore(context, displayer, true);
                }
            });
        }
    }
    if (holder2 instanceof SpacerViewHolder) {
        View header = (context).findViewById(R.id.header);
        int height = header.getHeight();
        if (height == 0) {
            header.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            height = header.getMeasuredHeight();
            holder2.itemView.findViewById(R.id.height).setLayoutParams(new LinearLayout.LayoutParams(holder2.itemView.getWidth(), height));
            if (listView.getLayoutManager() instanceof CatchStaggeredGridLayoutManager) {
                CatchStaggeredGridLayoutManager.LayoutParams layoutParams = new CatchStaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
                layoutParams.setFullSpan(true);
                holder2.itemView.setLayoutParams(layoutParams);
            }
        } else {
            holder2.itemView.findViewById(R.id.height).setLayoutParams(new LinearLayout.LayoutParams(holder2.itemView.getWidth(), height));
            if (listView.getLayoutManager() instanceof CatchStaggeredGridLayoutManager) {
                CatchStaggeredGridLayoutManager.LayoutParams layoutParams = new CatchStaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
                layoutParams.setFullSpan(true);
                holder2.itemView.setLayoutParams(layoutParams);
            }
        }
    }
}
Also used : CatchStaggeredGridLayoutManager(me.ccrama.redditslide.Views.CatchStaggeredGridLayoutManager) DialogInterface(android.content.DialogInterface) OnSingleClickListener(me.ccrama.redditslide.util.OnSingleClickListener) SubredditView(me.ccrama.redditslide.Activities.SubredditView) MainActivity(me.ccrama.redditslide.Activities.MainActivity) AlertDialogWrapper(com.afollestad.materialdialogs.AlertDialogWrapper) TextView(android.widget.TextView) PopulateNewsViewHolder(me.ccrama.redditslide.SubmissionViews.PopulateNewsViewHolder) CommentsScreen(me.ccrama.redditslide.Activities.CommentsScreen) Submission(net.dean.jraw.models.Submission) Handler(android.os.Handler) Intent(android.content.Intent) SubmissionsView(me.ccrama.redditslide.Fragments.SubmissionsView) SubredditView(me.ccrama.redditslide.Activities.SubredditView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) CreateCardView(me.ccrama.redditslide.Views.CreateCardView) PopulateNewsViewHolder(me.ccrama.redditslide.SubmissionViews.PopulateNewsViewHolder) LinearLayout(android.widget.LinearLayout) Snackbar(android.support.design.widget.Snackbar)

Example 30 with Submission

use of net.dean.jraw.models.Submission in project Slide by ccrama.

the class SubredditPostsRealm method doNewsActivityOffline.

public void doNewsActivityOffline(final Context c, final SubmissionDisplay displayer) {
    LogUtil.v(subreddit);
    if (all == null) {
        all = OfflineSubreddit.getAll(subreddit);
    }
    // Move 0, or "submission only", to the end
    Collections.rotate(all, -1);
    offline = true;
    final String[] titles = new String[all.size()];
    final String[] base = new String[all.size()];
    int i = 0;
    for (String s : all) {
        String[] split = s.split(",");
        titles[i] = (Long.valueOf(split[1]) == 0 ? c.getString(R.string.settings_backup_submission_only) : TimeUtils.getTimeAgo(Long.valueOf(split[1]), c) + c.getString(R.string.settings_backup_comments));
        base[i] = s;
        i++;
    }
    ((NewsActivity) c).getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    ((NewsActivity) c).getSupportActionBar().setListNavigationCallbacks(new OfflineSubAdapter(c, android.R.layout.simple_list_item_1, titles), new ActionBar.OnNavigationListener() {

        @Override
        public boolean onNavigationItemSelected(int itemPosition, long itemId) {
            final String[] s2 = base[itemPosition].split(",");
            OfflineSubreddit.currentid = Long.valueOf(s2[1]);
            currentid = OfflineSubreddit.currentid;
            new AsyncTask<Void, Void, Void>() {

                OfflineSubreddit cached;

                @Override
                protected Void doInBackground(Void... params) {
                    cached = OfflineSubreddit.getSubreddit(subreddit, Long.valueOf(s2[1]), true, c);
                    List<Submission> finalSubs = new ArrayList<>();
                    for (Submission s : cached.submissions) {
                        if (!PostMatch.doesMatch(s, subreddit, force18)) {
                            finalSubs.add(s);
                        }
                    }
                    posts = finalSubs;
                    return null;
                }

                @Override
                protected void onPostExecute(Void aVoid) {
                    if (cached.submissions.isEmpty()) {
                        displayer.updateOfflineError();
                    }
                    // update offline
                    displayer.updateOffline(posts, Long.valueOf(s2[1]));
                }
            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;
        }
    });
}
Also used : Submission(net.dean.jraw.models.Submission) AsyncTask(android.os.AsyncTask) OfflineSubreddit(me.ccrama.redditslide.OfflineSubreddit) ArrayList(java.util.ArrayList) ActionBar(android.support.v7.app.ActionBar)

Aggregations

Submission (net.dean.jraw.models.Submission)32 Intent (android.content.Intent)15 View (android.view.View)14 DialogInterface (android.content.DialogInterface)10 Snackbar (android.support.design.widget.Snackbar)10 TextView (android.widget.TextView)10 AlertDialogWrapper (com.afollestad.materialdialogs.AlertDialogWrapper)10 ArrayList (java.util.ArrayList)10 RecyclerView (android.support.v7.widget.RecyclerView)9 SubmissionsView (me.ccrama.redditslide.Fragments.SubmissionsView)9 ImageView (android.widget.ImageView)8 SubredditView (me.ccrama.redditslide.Activities.SubredditView)8 OfflineSubreddit (me.ccrama.redditslide.OfflineSubreddit)8 CreateCardView (me.ccrama.redditslide.Views.CreateCardView)7 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)6 TypedArray (android.content.res.TypedArray)5 LinearLayout (android.widget.LinearLayout)5 DialogAction (com.afollestad.materialdialogs.DialogAction)5 BottomSheet (com.cocosw.bottomsheet.BottomSheet)5 CommentsScreen (me.ccrama.redditslide.Activities.CommentsScreen)5