Search in sources :

Example 11 with Comment

use of com.winsonchiu.reader.data.reddit.Comment in project Reader by TheKeeperOfPie.

the class ControllerComments method insertComments.

private void insertComments(int positionStart, List<Thing> comments, Listing listing) {
    for (int index = comments.size() - 1; index >= 0; index--) {
        Comment comment = (Comment) comments.get(index);
        listing.getChildren().add(positionStart, comment);
    }
}
Also used : Comment(com.winsonchiu.reader.data.reddit.Comment)

Example 12 with Comment

use of com.winsonchiu.reader.data.reddit.Comment in project Reader by TheKeeperOfPie.

the class ControllerComments method setLinkWithComments.

public void setLinkWithComments(Link link) {
    link.setBackgroundColor(this.link.getBackgroundColor());
    this.link = link;
    Listing listing = new Listing();
    // For some reason Reddit doesn't report the link author, so we'll do it manually
    for (Thing thing : link.getComments().getChildren()) {
        Comment comment = (Comment) thing;
        comment.setLinkAuthor(link.getAuthor());
    }
    // TODO: Make this logic cleaner
    if (link.getComments() != null) {
        listing.setChildren(new ArrayList<>(link.getComments().getChildren()));
    } else {
        listing.setChildren(new ArrayList<>());
    }
    listingComments = listing;
    if (sharedPreferences.getBoolean(AppSettings.PREF_COLLAPSE_COMMENT_THREADS, false)) {
        for (int index = listingComments.getChildren().size() - 1; index >= 0; index--) {
            if (((Comment) listingComments.getChildren().get(index)).getLevel() == 0) {
                collapseComment(index, false);
            }
        }
    }
    eventHolder.call(new RxAdapterEvent<>(getData()));
}
Also used : Comment(com.winsonchiu.reader.data.reddit.Comment) Listing(com.winsonchiu.reader.data.reddit.Listing) Thing(com.winsonchiu.reader.data.reddit.Thing)

Example 13 with Comment

use of com.winsonchiu.reader.data.reddit.Comment in project Reader by TheKeeperOfPie.

the class ControllerComments method isCommentExpanded.

public boolean isCommentExpanded(int position) {
    position = position - 1;
    if (position == listingComments.getChildren().size() - 1) {
        return false;
    }
    List<Thing> commentList = listingComments.getChildren();
    Comment comment = (Comment) commentList.get(position);
    Comment nextComment = (Comment) commentList.get(position + 1);
    if (comment.getLevel() == nextComment.getLevel()) {
        return false;
    } else if (comment.getLevel() < nextComment.getLevel()) {
        return true;
    }
    return false;
}
Also used : Comment(com.winsonchiu.reader.data.reddit.Comment) Thing(com.winsonchiu.reader.data.reddit.Thing)

Example 14 with Comment

use of com.winsonchiu.reader.data.reddit.Comment in project Reader by TheKeeperOfPie.

the class FragmentInbox method onCreateView.

@SuppressWarnings("ResourceType")
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_inbox, container, false);
    listener = new ControllerInbox.Listener() {

        @Override
        public void setPage(Page page) {
            spinnerPage.setSelection(adapterInboxPage.getPages().indexOf(page));
        }

        @Override
        public RecyclerView.Adapter getAdapter() {
            return adapterInbox;
        }

        @Override
        public void setToolbarTitle(CharSequence title) {
            toolbar.setTitle(title);
        }

        @Override
        public void setRefreshing(boolean refreshing) {
            swipeRefreshInbox.setRefreshing(refreshing);
        }

        @Override
        public void post(Runnable runnable) {
            recyclerInbox.post(runnable);
        }
    };
    layoutCoordinator = (CoordinatorLayout) view.findViewById(R.id.layout_coordinator);
    layoutAppBar = (AppBarLayout) view.findViewById(R.id.layout_app_bar);
    int styleColorBackground = AppSettings.THEME_DARK.equals(mListener.getThemeBackground()) ? R.style.MenuDark : R.style.MenuLight;
    ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(new ThemeWrapper(activity, UtilsColor.getThemeForColor(getResources(), themer.getColorPrimary(), mListener)), styleColorBackground);
    toolbar = (Toolbar) activity.getLayoutInflater().cloneInContext(contextThemeWrapper).inflate(R.layout.toolbar, layoutAppBar, false);
    layoutAppBar.addView(toolbar);
    ((AppBarLayout.LayoutParams) toolbar.getLayoutParams()).setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
    toolbar.setTitleTextColor(themer.getColorFilterPrimary().getColor());
    toolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);
    toolbar.getNavigationIcon().mutate().setColorFilter(themer.getColorFilterPrimary());
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mListener.openDrawer();
        }
    });
    setUpOptionsMenu();
    floatingActionButtonNewMessage = (FloatingActionButton) view.findViewById(R.id.fab_new_message);
    floatingActionButtonNewMessage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            FragmentNewMessage fragmentNewMessage = FragmentNewMessage.newInstance();
            getFragmentManager().beginTransaction().hide(FragmentInbox.this).add(R.id.frame_fragment, fragmentNewMessage, FragmentNewMessage.TAG).addToBackStack(null).commit();
        }
    });
    floatingActionButtonNewMessage.setColorFilter(themer.getColorFilterAccent());
    behaviorFloatingActionButton = new ScrollAwareFloatingActionButtonBehavior(activity, null, new ScrollAwareFloatingActionButtonBehavior.OnVisibilityChangeListener() {

        @Override
        public void onStartHideFromScroll() {
        }

        @Override
        public void onEndHideFromScroll() {
        }
    });
    ((CoordinatorLayout.LayoutParams) floatingActionButtonNewMessage.getLayoutParams()).setBehavior(behaviorFloatingActionButton);
    adapterInboxPage = new AdapterInboxPage(activity);
    spinnerPage = new AppCompatSpinner(contextThemeWrapper);
    toolbar.addView(spinnerPage);
    ((Toolbar.LayoutParams) spinnerPage.getLayoutParams()).setMarginEnd((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics()));
    spinnerPage.setAdapter(adapterInboxPage);
    spinnerPage.setSelection(adapterInboxPage.getPages().indexOf(controllerInbox.getPage()));
    spinnerPage.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            controllerInbox.setPage(adapterInboxPage.getItem(position));
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
    swipeRefreshInbox = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_inbox);
    swipeRefreshInbox.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            controllerInbox.reload();
        }
    });
    linearLayoutManager = new LinearLayoutManager(activity);
    recyclerInbox = (RecyclerView) view.findViewById(R.id.recycler_inbox);
    recyclerInbox.setHasFixedSize(true);
    recyclerInbox.setItemAnimator(new DefaultItemAnimator());
    recyclerInbox.getItemAnimator().setRemoveDuration(UtilsAnimation.EXPAND_ACTION_DURATION);
    recyclerInbox.setLayoutManager(linearLayoutManager);
    recyclerInbox.addItemDecoration(new ItemDecorationDivider(activity, ItemDecorationDivider.VERTICAL_LIST));
    AdapterListener adapterListener = new AdapterListener() {

        @Override
        public void scrollAndCenter(int position, int height) {
            linearLayoutManager.scrollToPositionWithOffset(position, 0);
        }

        @Override
        public void hideToolbar() {
            AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
            behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
        }

        @Override
        public void clearDecoration() {
            behaviorFloatingActionButton.animateOut(floatingActionButtonNewMessage);
            AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
            behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
        }

        @Override
        public void requestMore() {
            controllerInbox.loadMore();
        }

        @Override
        public void requestDisallowInterceptTouchEventVertical(boolean disallow) {
            recyclerInbox.requestDisallowInterceptTouchEvent(disallow);
            swipeRefreshInbox.requestDisallowInterceptTouchEvent(disallow);
        }

        @Override
        public void requestDisallowInterceptTouchEventHorizontal(boolean disallow) {
        }
    };
    AdapterCommentList.ViewHolderComment.Listener listenerComments = new AdapterCommentList.ViewHolderComment.Listener() {

        @Override
        public void onToggleComment(Comment comment) {
        }

        @Override
        public void onShowReplyEditor(Comment comment) {
        }

        @Override
        public void onEditComment(Comment comment, String text) {
        }

        @Override
        public void onSendComment(Comment comment, String text) {
        }

        @Override
        public void onMarkRead(Comment comment) {
        }

        @Override
        public void onLoadNestedComments(Comment comment) {
        }

        @Override
        public void onJumpToParent(Comment comment) {
        }

        @Override
        public void onViewProfile(Comment comment) {
        }

        @Override
        public void onCopyText(Comment comment) {
        }

        @Override
        public void onDeleteComment(Comment comment) {
        }

        @Override
        public void onReport(Comment comment) {
        }

        @Override
        public void onVoteComment(Comment comment, AdapterCommentList.ViewHolderComment viewHolderComment, Likes vote) {
        }

        @Override
        public void onSave(Comment comment) {
        }
    };
    if (adapterInbox == null) {
        adapterInbox = new AdapterInbox(controllerInbox, controllerUser, adapterListener, listenerComments, mListener.getEventListenerBase());
    }
    recyclerInbox.setAdapter(adapterInbox);
    return view;
}
Also used : ItemDecorationDivider(com.winsonchiu.reader.utils.ItemDecorationDivider) AdapterListener(com.winsonchiu.reader.adapter.AdapterListener) Page(com.winsonchiu.reader.data.Page) FragmentNewMessage(com.winsonchiu.reader.FragmentNewMessage) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) DefaultItemAnimator(android.support.v7.widget.DefaultItemAnimator) ContextThemeWrapper(android.view.ContextThemeWrapper) ThemeWrapper(com.winsonchiu.reader.theme.ThemeWrapper) AdapterCommentList(com.winsonchiu.reader.comments.AdapterCommentList) ScrollAwareFloatingActionButtonBehavior(com.winsonchiu.reader.utils.ScrollAwareFloatingActionButtonBehavior) ScrollAwareFloatingActionButtonBehavior(com.winsonchiu.reader.utils.ScrollAwareFloatingActionButtonBehavior) AppBarLayout(android.support.design.widget.AppBarLayout) Comment(com.winsonchiu.reader.data.reddit.Comment) AdapterListener(com.winsonchiu.reader.adapter.AdapterListener) View(android.view.View) AdapterView(android.widget.AdapterView) RecyclerView(android.support.v7.widget.RecyclerView) AppCompatSpinner(android.support.v7.widget.AppCompatSpinner) CoordinatorLayout(android.support.design.widget.CoordinatorLayout) ContextThemeWrapper(android.view.ContextThemeWrapper) AdapterView(android.widget.AdapterView) Likes(com.winsonchiu.reader.data.reddit.Likes)

Example 15 with Comment

use of com.winsonchiu.reader.data.reddit.Comment in project Reader by TheKeeperOfPie.

the class Receiver method checkInbox.

public void checkInbox(final Context context, @Nullable final ArrayList<String> names) {
    final ArrayList<String> readNames;
    if (names == null) {
        readNames = new ArrayList<>();
    } else {
        readNames = names;
    }
    new Thread(new Runnable() {

        @Override
        public void run() {
            final Listing messages = new Listing();
            Account[] accounts = accountManager.getAccountsByType(Reddit.ACCOUNT_TYPE);
            for (Account account : accounts) {
                final AccountManagerFuture<Bundle> futureAuth = accountManager.getAuthToken(account, Reddit.AUTH_TOKEN_FULL_ACCESS, null, true, null, null);
                try {
                    Bundle bundle = futureAuth.getResult();
                    final String tokenAuth = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                    Request request = new Request.Builder().url(Reddit.OAUTH_URL + "/message/unread").header(Reddit.USER_AGENT, Reddit.CUSTOM_USER_AGENT).header(Reddit.AUTHORIZATION, Reddit.BEARER + tokenAuth).header(Reddit.CONTENT_TYPE, Reddit.CONTENT_TYPE_APP_JSON).get().build();
                    String response = okHttpClient.newCall(request).execute().body().string();
                    Listing listing = Listing.fromJson(ComponentStatic.getObjectMapper().readValue(response, JsonNode.class));
                    messages.addChildren(listing.getChildren());
                    Log.d(TAG, account.name + " checkInbox response: " + response);
                } catch (OperationCanceledException | AuthenticatorException | IOException e) {
                    e.printStackTrace();
                }
            }
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
            Thing thing = null;
            for (int index = 0; index < messages.getChildren().size(); index++) {
                thing = messages.getChildren().get(index);
                if (readNames.contains(thing.getName())) {
                    reddit.markRead(thing.getName()).subscribe(new ObserverEmpty<>());
                    thing = null;
                } else {
                    readNames.add(thing.getName());
                    break;
                }
            }
            if (thing == null) {
                notificationManager.cancel(NOTIFICATION_INBOX);
                return;
            }
            int titleSuffixResource = messages.getChildren().size() == 1 ? R.string.new_message : R.string.new_messages;
            CharSequence content = "";
            CharSequence author = "";
            CharSequence dest = "";
            if (thing instanceof Message) {
                content = ((Message) thing).getBodyHtml();
                author = ((Message) thing).getAuthor();
                dest = ((Message) thing).getDest();
            } else if (thing instanceof Comment) {
                content = ((Comment) thing).getBodyHtml();
                author = ((Comment) thing).getAuthor();
                dest = ((Comment) thing).getDest();
            }
            Intent intentActivity = new Intent(context, ActivityMain.class);
            intentActivity.putExtra(ActivityMain.ACCOUNT, dest);
            intentActivity.putExtra(ActivityMain.NAV_ID, R.id.item_inbox);
            intentActivity.putExtra(ActivityMain.NAV_PAGE, ControllerInbox.UNREAD);
            PendingIntent pendingIntentActivity = PendingIntent.getActivity(context, 0, intentActivity, PendingIntent.FLAG_CANCEL_CURRENT);
            Intent intentRecheckInbox = new Intent(INTENT_INBOX);
            intentRecheckInbox.putExtra(READ_NAMES, readNames);
            PendingIntent pendingIntentRecheckInbox = PendingIntent.getBroadcast(context, 0, intentRecheckInbox, PendingIntent.FLAG_CANCEL_CURRENT);
            Themer themer = new Themer(context);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setSmallIcon(R.mipmap.app_icon_white_outline).setContentTitle(messages.getChildren().size() + " " + context.getResources().getString(titleSuffixResource)).setContentText(context.getString(R.string.expand_to_read_first_message)).setStyle(new NotificationCompat.BigTextStyle().setSummaryText(context.getString(R.string.from) + " /u/" + author).bigText(content)).setContentIntent(pendingIntentActivity).addAction(new NotificationCompat.Action(R.drawable.ic_check_white_24dp, context.getString(R.string.mark_read), pendingIntentRecheckInbox)).setDeleteIntent(pendingIntentRecheckInbox).setAutoCancel(true).setCategory(NotificationCompat.CATEGORY_EMAIL).setColor(themer.getColorPrimary()).setLights(themer.getColorPrimary(), LED_MS_ON, LED_MS_OFF);
            notificationManager.notify(NOTIFICATION_INBOX, builder.build());
        }
    }).start();
}
Also used : Account(android.accounts.Account) Message(com.winsonchiu.reader.data.reddit.Message) OperationCanceledException(android.accounts.OperationCanceledException) NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat) JsonNode(com.fasterxml.jackson.databind.JsonNode) NotificationCompat(android.support.v4.app.NotificationCompat) Thing(com.winsonchiu.reader.data.reddit.Thing) Comment(com.winsonchiu.reader.data.reddit.Comment) Bundle(android.os.Bundle) Request(okhttp3.Request) AuthenticatorException(android.accounts.AuthenticatorException) Themer(com.winsonchiu.reader.theme.Themer) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) IOException(java.io.IOException) Listing(com.winsonchiu.reader.data.reddit.Listing) PendingIntent(android.app.PendingIntent)

Aggregations

Comment (com.winsonchiu.reader.data.reddit.Comment)17 Thing (com.winsonchiu.reader.data.reddit.Thing)7 Listing (com.winsonchiu.reader.data.reddit.Listing)4 Bundle (android.os.Bundle)3 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)3 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)3 RecyclerView (android.support.v7.widget.RecyclerView)3 View (android.view.View)3 AdapterListener (com.winsonchiu.reader.adapter.AdapterListener)3 Likes (com.winsonchiu.reader.data.reddit.Likes)3 Link (com.winsonchiu.reader.data.reddit.Link)3 Intent (android.content.Intent)2 AppBarLayout (android.support.design.widget.AppBarLayout)2 CoordinatorLayout (android.support.design.widget.CoordinatorLayout)2 AppCompatSpinner (android.support.v7.widget.AppCompatSpinner)2 ContextThemeWrapper (android.view.ContextThemeWrapper)2 AdapterView (android.widget.AdapterView)2 ControllerListener (com.winsonchiu.reader.utils.ControllerListener)2 LinkedList (java.util.LinkedList)2 Account (android.accounts.Account)1