Search in sources :

Example 16 with ContextThemeWrapper

use of androidx.appcompat.view.ContextThemeWrapper in project Slide by ccrama.

the class HeaderImageLinkView method onLinkLongClick.

public void onLinkLongClick(final String url, MotionEvent event) {
    popped = false;
    if (url == null) {
        return;
    }
    performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
    Activity activity = null;
    final Context context = getContext();
    if (context instanceof Activity) {
        activity = (Activity) context;
    } else if (context instanceof ContextThemeWrapper) {
        activity = (Activity) ((ContextThemeWrapper) context).getBaseContext();
    } else if (context instanceof ContextWrapper) {
        Context context1 = ((ContextWrapper) context).getBaseContext();
        if (context1 instanceof Activity) {
            activity = (Activity) context1;
        } else if (context1 instanceof ContextWrapper) {
            Context context2 = ((ContextWrapper) context1).getBaseContext();
            if (context2 instanceof Activity) {
                activity = (Activity) context2;
            } else if (context2 instanceof ContextWrapper) {
                activity = (Activity) ((ContextThemeWrapper) context2).getBaseContext();
            }
        }
    } else {
        throw new RuntimeException("Could not find activity from context:" + context);
    }
    if (activity != null && !activity.isFinishing()) {
        if (SettingValues.peek) {
            Peek.into(R.layout.peek_view_submission, new SimpleOnPeek() {

                @Override
                public void onInflated(final PeekView peekView, final View rootView) {
                    // do stuff
                    TextView text = rootView.findViewById(R.id.title);
                    text.setText(url);
                    text.setTextColor(Color.WHITE);
                    ((PeekMediaView) rootView.findViewById(R.id.peek)).setUrl(url);
                    peekView.addButton((R.id.share), new OnButtonUp() {

                        @Override
                        public void onButtonUp() {
                            Reddit.defaultShareText("", url, rootView.getContext());
                        }
                    });
                    peekView.addButton((R.id.upvoteb), new OnButtonUp() {

                        @Override
                        public void onButtonUp() {
                            ((View) getParent()).findViewById(R.id.upvote).callOnClick();
                        }
                    });
                    peekView.setOnRemoveListener(new OnRemove() {

                        @Override
                        public void onRemove() {
                            ((PeekMediaView) rootView.findViewById(R.id.peek)).doClose();
                        }
                    });
                    peekView.addButton((R.id.comments), new OnButtonUp() {

                        @Override
                        public void onButtonUp() {
                            ((View) getParent().getParent()).callOnClick();
                        }
                    });
                    peekView.setOnPop(new OnPop() {

                        @Override
                        public void onPop() {
                            popped = true;
                            callOnClick();
                        }
                    });
                }
            }).with(new PeekViewOptions().setFullScreenPeek(true)).show((PeekViewActivity) activity, event);
        } else {
            BottomSheet.Builder b = new BottomSheet.Builder(activity).title(url).grid();
            int[] attrs = new int[] { R.attr.tintColor };
            TypedArray ta = getContext().obtainStyledAttributes(attrs);
            int color = ta.getColor(0, Color.WHITE);
            Drawable open = getResources().getDrawable(R.drawable.ic_open_in_new);
            Drawable share = getResources().getDrawable(R.drawable.ic_share);
            Drawable copy = getResources().getDrawable(R.drawable.ic_content_copy);
            final List<Drawable> drawableSet = Arrays.asList(open, share, copy);
            BlendModeUtil.tintDrawablesAsSrcAtop(drawableSet, color);
            ta.recycle();
            b.sheet(R.id.open_link, open, getResources().getString(R.string.open_externally));
            b.sheet(R.id.share_link, share, getResources().getString(R.string.share_link));
            b.sheet(R.id.copy_link, copy, getResources().getString(R.string.submission_link_copy));
            final Activity finalActivity = activity;
            b.listener(new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch(which) {
                        case R.id.open_link:
                            LinkUtil.openExternally(url);
                            break;
                        case R.id.share_link:
                            Reddit.defaultShareText("", url, finalActivity);
                            break;
                        case R.id.copy_link:
                            LinkUtil.copyUrl(url, finalActivity);
                            break;
                    }
                }
            }).show();
        }
    }
}
Also used : DialogInterface(android.content.DialogInterface) PeekViewActivity(me.ccrama.redditslide.ForceTouch.PeekViewActivity) Activity(android.app.Activity) PeekMediaView(me.ccrama.redditslide.Views.PeekMediaView) TypedArray(android.content.res.TypedArray) PeekView(me.ccrama.redditslide.ForceTouch.PeekView) TransparentTagTextView(me.ccrama.redditslide.Views.TransparentTagTextView) TextView(android.widget.TextView) OnPop(me.ccrama.redditslide.ForceTouch.callback.OnPop) PeekViewOptions(me.ccrama.redditslide.ForceTouch.builder.PeekViewOptions) Context(android.content.Context) Drawable(android.graphics.drawable.Drawable) ImageView(android.widget.ImageView) PeekView(me.ccrama.redditslide.ForceTouch.PeekView) View(android.view.View) TransparentTagTextView(me.ccrama.redditslide.Views.TransparentTagTextView) TextView(android.widget.TextView) PeekMediaView(me.ccrama.redditslide.Views.PeekMediaView) OnButtonUp(me.ccrama.redditslide.ForceTouch.callback.OnButtonUp) ContextThemeWrapper(androidx.appcompat.view.ContextThemeWrapper) OnRemove(me.ccrama.redditslide.ForceTouch.callback.OnRemove) BottomSheet(com.cocosw.bottomsheet.BottomSheet) ContextWrapper(android.content.ContextWrapper) SimpleOnPeek(me.ccrama.redditslide.ForceTouch.callback.SimpleOnPeek)

Example 17 with ContextThemeWrapper

use of androidx.appcompat.view.ContextThemeWrapper in project Slide by ccrama.

the class NewsView method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), new ColorPreferences(inflater.getContext()).getThemeSubreddit(id));
    final View v = LayoutInflater.from(contextThemeWrapper).inflate(R.layout.fragment_verticalcontent, container, false);
    if (getActivity() instanceof MainActivity) {
        v.findViewById(R.id.back).setBackgroundResource(0);
    }
    rv = v.findViewById(R.id.vertical_content);
    rv.setHasFixedSize(true);
    final RecyclerView.LayoutManager mLayoutManager = createLayoutManager(LayoutUtils.getNumColumns(getResources().getConfiguration().orientation, getActivity()));
    if (!(getActivity() instanceof SubredditView)) {
        v.findViewById(R.id.back).setBackground(null);
    }
    rv.setLayoutManager(mLayoutManager);
    rv.setItemAnimator(new SlideUpAlphaAnimator().withInterpolator(new LinearOutSlowInInterpolator()));
    rv.getLayoutManager().scrollToPosition(0);
    mSwipeRefreshLayout = v.findViewById(R.id.activity_main_swipe_refresh_layout);
    mSwipeRefreshLayout.setColorSchemeColors(Palette.getColors(id, getContext()));
    /**
     * If using List view mode, we need to remove the start margin from the SwipeRefreshLayout.
     * The scrollbar style of "outsideInset" creates a 4dp padding around it. To counter this,
     * change the scrollbar style to "insideOverlay" when list view is enabled.
     * To recap: this removes the margins from the start/end so list view is full-width.
     */
    if (SettingValues.defaultCardView == CreateCardView.CardEnum.LIST) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        MarginLayoutParamsCompat.setMarginStart(params, 0);
        rv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        mSwipeRefreshLayout.setLayoutParams(params);
    }
    /**
     * If we use 'findViewById(R.id.header).getMeasuredHeight()', 0 is always returned.
     * So, we estimate the height of the header in dp.
     * If the view type is "single" (and therefore "commentPager"), we need a different offset
     */
    final int HEADER_OFFSET = (SettingValues.single || getActivity() instanceof SubredditView) ? Constants.SINGLE_HEADER_VIEW_OFFSET : Constants.TAB_HEADER_VIEW_OFFSET;
    mSwipeRefreshLayout.setProgressViewOffset(false, HEADER_OFFSET - Constants.PTR_OFFSET_TOP, HEADER_OFFSET + Constants.PTR_OFFSET_BOTTOM);
    if (SettingValues.fab) {
        fab = v.findViewById(R.id.post_floating_action_button);
        if (SettingValues.fabType == Constants.FAB_POST) {
            fab.setImageResource(R.drawable.ic_add);
            fab.setContentDescription(getString(R.string.btn_fab_post));
            fab.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent inte = new Intent(getActivity(), Submit.class);
                    inte.putExtra(Submit.EXTRA_SUBREDDIT, id);
                    getActivity().startActivity(inte);
                }
            });
        } else {
            fab.setImageResource(R.drawable.ic_visibility_off);
            fab.setContentDescription(getString(R.string.btn_fab_hide));
            fab.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (!Reddit.fabClear) {
                        new AlertDialog.Builder(getActivity()).setTitle(R.string.settings_fabclear).setMessage(R.string.settings_fabclear_msg).setPositiveButton(R.string.btn_ok, (dialog, which) -> {
                            Reddit.colors.edit().putBoolean(SettingValues.PREF_FAB_CLEAR, true).apply();
                            Reddit.fabClear = true;
                            clearSeenPosts(false);
                        }).show();
                    } else {
                        clearSeenPosts(false);
                    }
                }
            });
            final Handler handler = new Handler();
            fab.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    detector.onTouchEvent(event);
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        origY = event.getY();
                        handler.postDelayed(mLongPressRunnable, android.view.ViewConfiguration.getLongPressTimeout());
                    }
                    if (((event.getAction() == MotionEvent.ACTION_MOVE) && Math.abs(event.getY() - origY) > fab.getHeight() / 2.0f) || (event.getAction() == MotionEvent.ACTION_UP)) {
                        handler.removeCallbacks(mLongPressRunnable);
                    }
                    return false;
                }
            });
            mLongPressRunnable = new Runnable() {

                public void run() {
                    fab.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
                    if (!Reddit.fabClear) {
                        new AlertDialog.Builder(getActivity()).setTitle(R.string.settings_fabclear).setMessage(R.string.settings_fabclear_msg).setPositiveButton(R.string.btn_ok, (dialog, which) -> {
                            Reddit.colors.edit().putBoolean(SettingValues.PREF_FAB_CLEAR, true).apply();
                            Reddit.fabClear = true;
                            clearSeenPosts(true);
                        }).show();
                    } else {
                        clearSeenPosts(true);
                    }
                    Snackbar s = Snackbar.make(rv, getResources().getString(R.string.posts_hidden_forever), Snackbar.LENGTH_LONG);
                    /*Todo a way to unhide
                        s.setAction(R.string.btn_undo, new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {

                            }
                        });*/
                    LayoutUtils.showSnackbar(s);
                }
            };
        }
    } else {
        v.findViewById(R.id.post_floating_action_button).setVisibility(View.GONE);
    }
    if (fab != null)
        fab.show();
    header = getActivity().findViewById(R.id.header);
    // TODO, have it so that if the user clicks anywhere in the rv to hide and cancel GoToSubreddit?
    // final TextInputEditText GO_TO_SUB_FIELD = (TextInputEditText) getActivity().findViewById(R.id.toolbar_search);
    // final Toolbar TOOLBAR = ((Toolbar) getActivity().findViewById(R.id.toolbar));
    // final String PREV_TITLE = TOOLBAR.getTitle().toString();
    // final ImageView CLOSE_BUTTON = (ImageView) getActivity().findViewById(R.id.close);
    // 
    // rv.setOnTouchListener(new View.OnTouchListener() {
    // @Override
    // public boolean onTouch(View v, MotionEvent event) {
    // System.out.println("touched");
    // KeyboardUtil.hideKeyboard(getActivity(), v.getWindowToken(), 0);
    // 
    // GO_TO_SUB_FIELD.setText("");
    // GO_TO_SUB_FIELD.setVisibility(View.GONE);
    // CLOSE_BUTTON.setVisibility(View.GONE);
    // TOOLBAR.setTitle(PREV_TITLE);
    // 
    // return false;
    // }
    // });
    resetScroll();
    Reddit.isLoading = false;
    if (MainActivity.shouldLoad == null || id == null || (MainActivity.shouldLoad != null && MainActivity.shouldLoad.equals(id)) || !(getActivity() instanceof MainActivity)) {
        doAdapter();
    }
    return v;
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) SubredditView(me.ccrama.redditslide.Activities.SubredditView) MainActivity(me.ccrama.redditslide.Activities.MainActivity) SlideUpAlphaAnimator(com.mikepenz.itemanimators.SlideUpAlphaAnimator) LinearOutSlowInInterpolator(androidx.interpolator.view.animation.LinearOutSlowInInterpolator) Context(android.content.Context) ColorPreferences(me.ccrama.redditslide.Visuals.ColorPreferences) Handler(android.os.Handler) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler) Intent(android.content.Intent) SubredditView(me.ccrama.redditslide.Activities.SubredditView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) CreateCardView(me.ccrama.redditslide.Views.CreateCardView) MotionEvent(android.view.MotionEvent) ContextThemeWrapper(androidx.appcompat.view.ContextThemeWrapper) RelativeLayout(android.widget.RelativeLayout) Submit(me.ccrama.redditslide.Activities.Submit) RecyclerView(androidx.recyclerview.widget.RecyclerView) Snackbar(com.google.android.material.snackbar.Snackbar)

Example 18 with ContextThemeWrapper

use of androidx.appcompat.view.ContextThemeWrapper in project Slide by ccrama.

the class SubmissionsView method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), new ColorPreferences(inflater.getContext()).getThemeSubreddit(id));
    final View v = LayoutInflater.from(contextThemeWrapper).inflate(R.layout.fragment_verticalcontent, container, false);
    if (getActivity() instanceof MainActivity) {
        v.findViewById(R.id.back).setBackgroundResource(0);
    }
    rv = v.findViewById(R.id.vertical_content);
    rv.setHasFixedSize(true);
    final RecyclerView.LayoutManager mLayoutManager = createLayoutManager(LayoutUtils.getNumColumns(getResources().getConfiguration().orientation, getActivity()));
    if (!(getActivity() instanceof SubredditView)) {
        v.findViewById(R.id.back).setBackground(null);
    }
    rv.setLayoutManager(mLayoutManager);
    rv.setItemAnimator(new SlideUpAlphaAnimator().withInterpolator(new LinearOutSlowInInterpolator()));
    rv.getLayoutManager().scrollToPosition(0);
    mSwipeRefreshLayout = v.findViewById(R.id.activity_main_swipe_refresh_layout);
    mSwipeRefreshLayout.setColorSchemeColors(Palette.getColors(id, getContext()));
    /**
     * If using List view mode, we need to remove the start margin from the SwipeRefreshLayout.
     * The scrollbar style of "outsideInset" creates a 4dp padding around it. To counter this,
     * change the scrollbar style to "insideOverlay" when list view is enabled.
     * To recap: this removes the margins from the start/end so list view is full-width.
     */
    if (SettingValues.defaultCardView == CreateCardView.CardEnum.LIST) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        MarginLayoutParamsCompat.setMarginStart(params, 0);
        rv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        mSwipeRefreshLayout.setLayoutParams(params);
    }
    /**
     * If we use 'findViewById(R.id.header).getMeasuredHeight()', 0 is always returned.
     * So, we estimate the height of the header in dp.
     * If the view type is "single" (and therefore "commentPager"), we need a different offset
     */
    final int HEADER_OFFSET = (SettingValues.single || getActivity() instanceof SubredditView) ? Constants.SINGLE_HEADER_VIEW_OFFSET : Constants.TAB_HEADER_VIEW_OFFSET;
    mSwipeRefreshLayout.setProgressViewOffset(false, HEADER_OFFSET - Constants.PTR_OFFSET_TOP, HEADER_OFFSET + Constants.PTR_OFFSET_BOTTOM);
    if (SettingValues.fab) {
        fab = v.findViewById(R.id.post_floating_action_button);
        if (SettingValues.fabType == Constants.FAB_POST) {
            fab.setImageResource(R.drawable.ic_add);
            fab.setContentDescription(getString(R.string.btn_fab_post));
            fab.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent inte = new Intent(getActivity(), Submit.class);
                    inte.putExtra(Submit.EXTRA_SUBREDDIT, id);
                    getActivity().startActivity(inte);
                }
            });
        } else if (SettingValues.fabType == Constants.FAB_SEARCH) {
            fab.setImageResource(R.drawable.ic_search);
            fab.setContentDescription(getString(R.string.btn_fab_search));
            fab.setOnClickListener(new View.OnClickListener() {

                String term;

                @Override
                public void onClick(View v) {
                    MaterialDialog.Builder builder = new MaterialDialog.Builder(getActivity()).title(R.string.search_title).alwaysCallInputCallback().input(getString(R.string.search_msg), "", new MaterialDialog.InputCallback() {

                        @Override
                        public void onInput(MaterialDialog materialDialog, CharSequence charSequence) {
                            term = charSequence.toString();
                        }
                    });
                    // Add "search current sub" if it is not frontpage/all/random
                    if (!id.equalsIgnoreCase("frontpage") && !id.equalsIgnoreCase("all") && !id.contains(".") && !id.contains("/m/") && !id.equalsIgnoreCase("friends") && !id.equalsIgnoreCase("random") && !id.equalsIgnoreCase("popular") && !id.equalsIgnoreCase("myrandom") && !id.equalsIgnoreCase("randnsfw")) {
                        builder.positiveText(getString(R.string.search_subreddit, id)).onPositive(new MaterialDialog.SingleButtonCallback() {

                            @Override
                            public void onClick(@NonNull MaterialDialog materialDialog, @NonNull DialogAction dialogAction) {
                                Intent i = new Intent(getActivity(), Search.class);
                                i.putExtra(Search.EXTRA_TERM, term);
                                i.putExtra(Search.EXTRA_SUBREDDIT, id);
                                startActivity(i);
                            }
                        });
                        builder.neutralText(R.string.search_all).onNeutral(new MaterialDialog.SingleButtonCallback() {

                            @Override
                            public void onClick(@NonNull MaterialDialog materialDialog, @NonNull DialogAction dialogAction) {
                                Intent i = new Intent(getActivity(), Search.class);
                                i.putExtra(Search.EXTRA_TERM, term);
                                startActivity(i);
                            }
                        });
                    } else {
                        builder.positiveText(R.string.search_all).onPositive(new MaterialDialog.SingleButtonCallback() {

                            @Override
                            public void onClick(@NonNull MaterialDialog materialDialog, @NonNull DialogAction dialogAction) {
                                Intent i = new Intent(getActivity(), Search.class);
                                i.putExtra(Search.EXTRA_TERM, term);
                                startActivity(i);
                            }
                        });
                    }
                    builder.show();
                }
            });
        } else {
            fab.setImageResource(R.drawable.ic_visibility_off);
            fab.setContentDescription(getString(R.string.btn_fab_hide));
            fab.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (!Reddit.fabClear) {
                        new AlertDialog.Builder(getActivity()).setTitle(R.string.settings_fabclear).setMessage(R.string.settings_fabclear_msg).setPositiveButton(R.string.btn_ok, (dialog, which) -> {
                            Reddit.colors.edit().putBoolean(SettingValues.PREF_FAB_CLEAR, true).apply();
                            Reddit.fabClear = true;
                            clearSeenPosts(false);
                        }).show();
                    } else {
                        clearSeenPosts(false);
                    }
                }
            });
            final Handler handler = new Handler();
            fab.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    detector.onTouchEvent(event);
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        origY = event.getY();
                        handler.postDelayed(mLongPressRunnable, android.view.ViewConfiguration.getLongPressTimeout());
                    }
                    if (((event.getAction() == MotionEvent.ACTION_MOVE) && Math.abs(event.getY() - origY) > fab.getHeight() / 2.0f) || (event.getAction() == MotionEvent.ACTION_UP)) {
                        handler.removeCallbacks(mLongPressRunnable);
                    }
                    return false;
                }
            });
            mLongPressRunnable = new Runnable() {

                public void run() {
                    fab.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
                    if (!Reddit.fabClear) {
                        new AlertDialog.Builder(getActivity()).setTitle(R.string.settings_fabclear).setMessage(R.string.settings_fabclear_msg).setPositiveButton(R.string.btn_ok, (dialog, which) -> {
                            Reddit.colors.edit().putBoolean(SettingValues.PREF_FAB_CLEAR, true).apply();
                            Reddit.fabClear = true;
                            clearSeenPosts(true);
                        }).show();
                    } else {
                        clearSeenPosts(true);
                    }
                    Snackbar s = Snackbar.make(rv, getResources().getString(R.string.posts_hidden_forever), Snackbar.LENGTH_LONG);
                    /*Todo a way to unhide
                        s.setAction(R.string.btn_undo, new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {

                            }
                        });*/
                    LayoutUtils.showSnackbar(s);
                }
            };
        }
    } else {
        v.findViewById(R.id.post_floating_action_button).setVisibility(View.GONE);
    }
    if (fab != null)
        fab.show();
    header = getActivity().findViewById(R.id.header);
    // TODO, have it so that if the user clicks anywhere in the rv to hide and cancel GoToSubreddit?
    // final TextInputEditText GO_TO_SUB_FIELD = (TextInputEditText) getActivity().findViewById(R.id.toolbar_search);
    // final Toolbar TOOLBAR = ((Toolbar) getActivity().findViewById(R.id.toolbar));
    // final String PREV_TITLE = TOOLBAR.getTitle().toString();
    // final ImageView CLOSE_BUTTON = (ImageView) getActivity().findViewById(R.id.close);
    // 
    // rv.setOnTouchListener(new View.OnTouchListener() {
    // @Override
    // public boolean onTouch(View v, MotionEvent event) {
    // System.out.println("touched");
    // KeyboardUtil.hideKeyboard(getActivity(), v.getWindowToken(), 0);
    // 
    // GO_TO_SUB_FIELD.setText("");
    // GO_TO_SUB_FIELD.setVisibility(View.GONE);
    // CLOSE_BUTTON.setVisibility(View.GONE);
    // TOOLBAR.setTitle(PREV_TITLE);
    // 
    // return false;
    // }
    // });
    resetScroll();
    Reddit.isLoading = false;
    if (MainActivity.shouldLoad == null || id == null || (MainActivity.shouldLoad != null && MainActivity.shouldLoad.equals(id)) || !(getActivity() instanceof MainActivity)) {
        doAdapter();
    }
    return v;
}
Also used : Bundle(android.os.Bundle) R(me.ccrama.redditslide.R) NonNull(androidx.annotation.NonNull) MainActivity(me.ccrama.redditslide.Activities.MainActivity) Locale(java.util.Locale) Handler(android.os.Handler) FloatingActionButton(com.google.android.material.floatingactionbutton.FloatingActionButton) Fragment(androidx.fragment.app.Fragment) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) SubredditPosts(me.ccrama.redditslide.Adapters.SubredditPosts) MarginLayoutParamsCompat(androidx.core.view.MarginLayoutParamsCompat) Palette(me.ccrama.redditslide.Visuals.Palette) ViewGroup(android.view.ViewGroup) CatchStaggeredGridLayoutManager(me.ccrama.redditslide.Views.CatchStaggeredGridLayoutManager) HapticFeedbackConstants(android.view.HapticFeedbackConstants) OfflineSubreddit(me.ccrama.redditslide.OfflineSubreddit) List(java.util.List) RelativeLayout(android.widget.RelativeLayout) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Snackbar(com.google.android.material.snackbar.Snackbar) LayoutUtils(me.ccrama.redditslide.util.LayoutUtils) Context(android.content.Context) LinearOutSlowInInterpolator(androidx.interpolator.view.animation.LinearOutSlowInInterpolator) GestureDetector(android.view.GestureDetector) BaseActivity(me.ccrama.redditslide.Activities.BaseActivity) SettingValues(me.ccrama.redditslide.SettingValues) AlertDialog(androidx.appcompat.app.AlertDialog) HasSeen(me.ccrama.redditslide.HasSeen) Submission(net.dean.jraw.models.Submission) Intent(android.content.Intent) Constants(me.ccrama.redditslide.Constants) Hidden(me.ccrama.redditslide.Hidden) MotionEvent(android.view.MotionEvent) Search(me.ccrama.redditslide.Activities.Search) SubredditView(me.ccrama.redditslide.Activities.SubredditView) SubmissionAdapter(me.ccrama.redditslide.Adapters.SubmissionAdapter) AlphaInAnimator(com.mikepenz.itemanimators.AlphaInAnimator) LayoutInflater(android.view.LayoutInflater) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) Reddit(me.ccrama.redditslide.Reddit) DialogAction(com.afollestad.materialdialogs.DialogAction) Submit(me.ccrama.redditslide.Activities.Submit) ColorPreferences(me.ccrama.redditslide.Visuals.ColorPreferences) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler) ContextThemeWrapper(androidx.appcompat.view.ContextThemeWrapper) SubmissionDisplay(me.ccrama.redditslide.Adapters.SubmissionDisplay) SlideUpAlphaAnimator(com.mikepenz.itemanimators.SlideUpAlphaAnimator) Configuration(android.content.res.Configuration) CreateCardView(me.ccrama.redditslide.Views.CreateCardView) SubredditView(me.ccrama.redditslide.Activities.SubredditView) MainActivity(me.ccrama.redditslide.Activities.MainActivity) SlideUpAlphaAnimator(com.mikepenz.itemanimators.SlideUpAlphaAnimator) LinearOutSlowInInterpolator(androidx.interpolator.view.animation.LinearOutSlowInInterpolator) Search(me.ccrama.redditslide.Activities.Search) Context(android.content.Context) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) ColorPreferences(me.ccrama.redditslide.Visuals.ColorPreferences) Handler(android.os.Handler) ToolbarScrollHideHandler(me.ccrama.redditslide.handler.ToolbarScrollHideHandler) Intent(android.content.Intent) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) SubredditView(me.ccrama.redditslide.Activities.SubredditView) CreateCardView(me.ccrama.redditslide.Views.CreateCardView) MotionEvent(android.view.MotionEvent) ContextThemeWrapper(androidx.appcompat.view.ContextThemeWrapper) DialogAction(com.afollestad.materialdialogs.DialogAction) RelativeLayout(android.widget.RelativeLayout) Submit(me.ccrama.redditslide.Activities.Submit) RecyclerView(androidx.recyclerview.widget.RecyclerView) Snackbar(com.google.android.material.snackbar.Snackbar)

Example 19 with ContextThemeWrapper

use of androidx.appcompat.view.ContextThemeWrapper in project Slide by ccrama.

the class SubredditListView method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), new ColorPreferences(inflater.getContext()).getThemeSubreddit(where));
    View v = LayoutInflater.from(contextThemeWrapper).inflate(R.layout.fragment_verticalcontent, container, false);
    rv = v.findViewById(R.id.vertical_content);
    final RecyclerView.LayoutManager mLayoutManager = new PreCachingLayoutManager(getActivity());
    rv.setLayoutManager(mLayoutManager);
    rv.setItemAnimator(new SlideUpAlphaAnimator().withInterpolator(new LinearOutSlowInInterpolator()));
    mSwipeRefreshLayout = v.findViewById(R.id.activity_main_swipe_refresh_layout);
    mSwipeRefreshLayout.setColorSchemeColors(Palette.getColors("no sub", getContext()));
    // If we use 'findViewById(R.id.header).getMeasuredHeight()', 0 is always returned.
    // So, we estimate the height of the header in dp
    mSwipeRefreshLayout.setProgressViewOffset(false, Constants.TAB_HEADER_VIEW_OFFSET - Constants.PTR_OFFSET_TOP, Constants.TAB_HEADER_VIEW_OFFSET + Constants.PTR_OFFSET_BOTTOM);
    v.findViewById(R.id.post_floating_action_button).setVisibility(View.GONE);
    doAdapter();
    return v;
}
Also used : Context(android.content.Context) LinearOutSlowInInterpolator(androidx.interpolator.view.animation.LinearOutSlowInInterpolator) ContextThemeWrapper(androidx.appcompat.view.ContextThemeWrapper) ColorPreferences(me.ccrama.redditslide.Visuals.ColorPreferences) RecyclerView(androidx.recyclerview.widget.RecyclerView) PreCachingLayoutManager(me.ccrama.redditslide.Views.PreCachingLayoutManager) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) SlideUpAlphaAnimator(com.mikepenz.itemanimators.SlideUpAlphaAnimator)

Example 20 with ContextThemeWrapper

use of androidx.appcompat.view.ContextThemeWrapper in project Slide by ccrama.

the class SubredditView method doSubSidebar.

public void doSubSidebar(final String subOverride) {
    findViewById(R.id.loader).setVisibility(View.VISIBLE);
    invalidateOptionsMenu();
    if (!subOverride.equalsIgnoreCase("all") && !subOverride.equalsIgnoreCase("frontpage") && !subOverride.equalsIgnoreCase("random") && !subOverride.equalsIgnoreCase("popular") && !subOverride.equalsIgnoreCase("myrandom") && !subOverride.equalsIgnoreCase("randnsfw") && !subOverride.equalsIgnoreCase("friends") && !subOverride.equalsIgnoreCase("mod") && !subOverride.contains("+") && !subOverride.contains(".") && !subOverride.contains("/m/")) {
        if (drawerLayout != null) {
            drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, GravityCompat.END);
        }
        loaded = true;
        final View dialoglayout = findViewById(R.id.sidebarsub);
        {
            View submit = (dialoglayout.findViewById(R.id.submit));
            if (!Authentication.isLoggedIn || !Authentication.didOnline) {
                submit.setVisibility(View.GONE);
            }
            if (SettingValues.fab && SettingValues.fabType == Constants.FAB_POST) {
                submit.setVisibility(View.GONE);
            }
            submit.setOnClickListener(new OnSingleClickListener() {

                @Override
                public void onSingleClick(View view) {
                    Intent inte = new Intent(SubredditView.this, Submit.class);
                    if (!subOverride.contains("/m/") && canSubmit) {
                        inte.putExtra(Submit.EXTRA_SUBREDDIT, subOverride);
                    }
                    SubredditView.this.startActivity(inte);
                }
            });
        }
        dialoglayout.findViewById(R.id.wiki).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(SubredditView.this, Wiki.class);
                i.putExtra(Wiki.EXTRA_SUBREDDIT, subOverride);
                startActivity(i);
            }
        });
        dialoglayout.findViewById(R.id.syncflair).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ImageFlairs.syncFlairs(SubredditView.this, subreddit);
            }
        });
        dialoglayout.findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(SubredditView.this, Submit.class);
                if ((!subOverride.contains("/m/") || !subOverride.contains(".")) && canSubmit) {
                    i.putExtra(Submit.EXTRA_SUBREDDIT, subOverride);
                }
                startActivity(i);
            }
        });
        final TextView sort = dialoglayout.findViewById(R.id.sort);
        Sorting sortingis = Sorting.HOT;
        if (SettingValues.hasSort(subreddit)) {
            sortingis = SettingValues.getBaseSubmissionSort(subreddit);
            sort.setText(sortingis.name() + ((sortingis == Sorting.CONTROVERSIAL || sortingis == Sorting.TOP) ? " of " + SettingValues.getBaseTimePeriod(subreddit).name() : ""));
        } else {
            sort.setText("Set default sorting");
        }
        final int sortid = SortingUtil.getSortingId(sortingis);
        dialoglayout.findViewById(R.id.sorting).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                final DialogInterface.OnClickListener l2 = new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        switch(i) {
                            case 0:
                                sorts = Sorting.HOT;
                                break;
                            case 1:
                                sorts = Sorting.NEW;
                                break;
                            case 2:
                                sorts = Sorting.RISING;
                                break;
                            case 3:
                                sorts = Sorting.TOP;
                                askTimePeriod(sorts, subreddit, dialoglayout);
                                return;
                            case 4:
                                sorts = Sorting.CONTROVERSIAL;
                                askTimePeriod(sorts, subreddit, dialoglayout);
                                return;
                        }
                        SettingValues.setSubSorting(sorts, time, subreddit);
                        Sorting sortingis = SettingValues.getBaseSubmissionSort(subreddit);
                        sort.setText(sortingis.name() + ((sortingis == Sorting.CONTROVERSIAL || sortingis == Sorting.TOP) ? " of " + SettingValues.getBaseTimePeriod(subreddit).name() : ""));
                        reloadSubs();
                    }
                };
                new AlertDialog.Builder(SubredditView.this).setTitle(R.string.sorting_choose).setSingleChoiceItems(SortingUtil.getSortingStrings(), sortid, l2).setNegativeButton("Reset default sorting", (dialog, which) -> {
                    SettingValues.prefs.edit().remove("defaultSort" + subreddit.toLowerCase(Locale.ENGLISH)).apply();
                    SettingValues.prefs.edit().remove("defaultTime" + subreddit.toLowerCase(Locale.ENGLISH)).apply();
                    final TextView sort1 = dialoglayout.findViewById(R.id.sort);
                    if (SettingValues.hasSort(subreddit)) {
                        Sorting sortingis1 = SettingValues.getBaseSubmissionSort(subreddit);
                        sort1.setText(sortingis1.name() + ((sortingis1 == Sorting.CONTROVERSIAL || sortingis1 == Sorting.TOP) ? " of " + SettingValues.getBaseTimePeriod(subreddit).name() : ""));
                    } else {
                        sort1.setText("Set default sorting");
                    }
                    reloadSubs();
                }).show();
            }
        });
        dialoglayout.findViewById(R.id.theme).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int style = new ColorPreferences(SubredditView.this).getThemeSubreddit(subOverride);
                final Context contextThemeWrapper = new ContextThemeWrapper(SubredditView.this, style);
                LayoutInflater localInflater = getLayoutInflater().cloneInContext(contextThemeWrapper);
                final View dialoglayout = localInflater.inflate(R.layout.colorsub, null);
                ArrayList<String> arrayList = new ArrayList<>();
                arrayList.add(subOverride);
                SettingsSubAdapter.showSubThemeEditor(arrayList, SubredditView.this, dialoglayout);
            }
        });
        dialoglayout.findViewById(R.id.mods).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                final Dialog d = new MaterialDialog.Builder(SubredditView.this).title(R.string.sidebar_findingmods).cancelable(true).content(R.string.misc_please_wait).progress(true, 100).show();
                new AsyncTask<Void, Void, Void>() {

                    ArrayList<UserRecord> mods;

                    @Override
                    protected Void doInBackground(Void... params) {
                        mods = new ArrayList<>();
                        UserRecordPaginator paginator = new UserRecordPaginator(Authentication.reddit, subOverride, "moderators");
                        paginator.setSorting(Sorting.HOT);
                        paginator.setTimePeriod(TimePeriod.ALL);
                        while (paginator.hasNext()) {
                            mods.addAll(paginator.next());
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        final ArrayList<String> names = new ArrayList<>();
                        for (UserRecord rec : mods) {
                            names.add(rec.getFullName());
                        }
                        d.dismiss();
                        new MaterialDialog.Builder(SubredditView.this).title(getString(R.string.sidebar_submods, subreddit)).items(names).itemsCallback(new MaterialDialog.ListCallback() {

                            @Override
                            public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                                Intent i = new Intent(SubredditView.this, Profile.class);
                                i.putExtra(Profile.EXTRA_PROFILE, names.get(which));
                                startActivity(i);
                            }
                        }).positiveText(R.string.btn_message).onPositive(new MaterialDialog.SingleButtonCallback() {

                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                Intent i = new Intent(SubredditView.this, SendMessage.class);
                                i.putExtra(SendMessage.EXTRA_NAME, "/r/" + subOverride);
                                startActivity(i);
                            }
                        }).show();
                    }
                }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            }
        });
        dialoglayout.findViewById(R.id.flair).setVisibility(View.GONE);
        if (Authentication.didOnline && Authentication.isLoggedIn) {
            new AsyncTask<View, Void, View>() {

                List<FlairTemplate> flairs;

                ArrayList<String> flairText;

                String current;

                AccountManager m;

                @Override
                protected View doInBackground(View... params) {
                    try {
                        m = new AccountManager(Authentication.reddit);
                        JsonNode node = m.getFlairChoicesRootNode(subOverride, null);
                        flairs = m.getFlairChoices(subOverride, node);
                        FlairTemplate currentF = m.getCurrentFlair(subOverride, node);
                        if (currentF != null) {
                            if (currentF.getText().isEmpty()) {
                                current = ("[" + currentF.getCssClass() + "]");
                            } else {
                                current = (currentF.getText());
                            }
                        }
                        flairText = new ArrayList<>();
                        for (FlairTemplate temp : flairs) {
                            if (temp.getText().isEmpty()) {
                                flairText.add("[" + temp.getCssClass() + "]");
                            } else {
                                flairText.add(temp.getText());
                            }
                        }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                    return params[0];
                }

                @Override
                protected void onPostExecute(View flair) {
                    if (flairs != null && !flairs.isEmpty() && flairText != null && !flairText.isEmpty()) {
                        flair.setVisibility(View.VISIBLE);
                        if (current != null) {
                            ((TextView) dialoglayout.findViewById(R.id.flair_text)).setText(getString(R.string.sidebar_flair, current));
                        }
                        flair.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                new MaterialDialog.Builder(SubredditView.this).items(flairText).title(R.string.sidebar_select_flair).itemsCallback(new MaterialDialog.ListCallback() {

                                    @Override
                                    public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                                        final FlairTemplate t = flairs.get(which);
                                        if (t.isTextEditable()) {
                                            new MaterialDialog.Builder(SubredditView.this).title(R.string.sidebar_select_flair_text).input(getString(R.string.mod_flair_hint), t.getText(), true, (dialog1, input) -> {
                                            }).positiveText(R.string.btn_set).onPositive(new MaterialDialog.SingleButtonCallback() {

                                                @Override
                                                public void onClick(MaterialDialog dialog, DialogAction which) {
                                                    final String flair = dialog.getInputEditText().getText().toString();
                                                    new AsyncTask<Void, Void, Boolean>() {

                                                        @Override
                                                        protected Boolean doInBackground(Void... params) {
                                                            try {
                                                                new ModerationManager(Authentication.reddit).setFlair(subOverride, t, flair, Authentication.name);
                                                                FlairTemplate currentF = m.getCurrentFlair(subOverride);
                                                                if (currentF.getText().isEmpty()) {
                                                                    current = ("[" + currentF.getCssClass() + "]");
                                                                } else {
                                                                    current = (currentF.getText());
                                                                }
                                                                return true;
                                                            } catch (Exception e) {
                                                                e.printStackTrace();
                                                                return false;
                                                            }
                                                        }

                                                        @Override
                                                        protected void onPostExecute(Boolean done) {
                                                            Snackbar s;
                                                            if (done) {
                                                                if (current != null) {
                                                                    ((TextView) dialoglayout.findViewById(R.id.flair_text)).setText(getString(R.string.sidebar_flair, current));
                                                                }
                                                                s = Snackbar.make(mToolbar, R.string.snackbar_flair_success, Snackbar.LENGTH_SHORT);
                                                            } else {
                                                                s = Snackbar.make(mToolbar, R.string.snackbar_flair_error, Snackbar.LENGTH_SHORT);
                                                            }
                                                            if (s != null) {
                                                                LayoutUtils.showSnackbar(s);
                                                            }
                                                        }
                                                    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                                                }
                                            }).negativeText(R.string.btn_cancel).show();
                                        } else {
                                            new AsyncTask<Void, Void, Boolean>() {

                                                @Override
                                                protected Boolean doInBackground(Void... params) {
                                                    try {
                                                        new ModerationManager(Authentication.reddit).setFlair(subOverride, t, null, Authentication.name);
                                                        FlairTemplate currentF = m.getCurrentFlair(subOverride);
                                                        if (currentF.getText().isEmpty()) {
                                                            current = ("[" + currentF.getCssClass() + "]");
                                                        } else {
                                                            current = (currentF.getText());
                                                        }
                                                        return true;
                                                    } catch (Exception e) {
                                                        e.printStackTrace();
                                                        return false;
                                                    }
                                                }

                                                @Override
                                                protected void onPostExecute(Boolean done) {
                                                    Snackbar s;
                                                    if (done) {
                                                        if (current != null) {
                                                            ((TextView) dialoglayout.findViewById(R.id.flair_text)).setText(getString(R.string.sidebar_flair, current));
                                                        }
                                                        s = Snackbar.make(mToolbar, R.string.snackbar_flair_success, Snackbar.LENGTH_SHORT);
                                                    } else {
                                                        s = Snackbar.make(mToolbar, R.string.snackbar_flair_error, Snackbar.LENGTH_SHORT);
                                                    }
                                                    if (s != null) {
                                                        LayoutUtils.showSnackbar(s);
                                                    }
                                                }
                                            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                                        }
                                    }
                                }).show();
                            }
                        });
                    }
                }
            }.execute((View) dialoglayout.findViewById(R.id.flair));
        }
    } else {
        if (drawerLayout != null) {
            drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END);
        }
    }
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) UserRecordPaginator(net.dean.jraw.paginators.UserRecordPaginator) DialogInterface(android.content.DialogInterface) OnSingleClickListener(me.ccrama.redditslide.util.OnSingleClickListener) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) AlertDialog(androidx.appcompat.app.AlertDialog) Dialog(android.app.Dialog) NonNull(androidx.annotation.NonNull) SpoilerRobotoTextView(me.ccrama.redditslide.SpoilerRobotoTextView) TextView(android.widget.TextView) Context(android.content.Context) ModerationManager(net.dean.jraw.managers.ModerationManager) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) ColorPreferences(me.ccrama.redditslide.Visuals.ColorPreferences) FlairTemplate(net.dean.jraw.models.FlairTemplate) AsyncTask(android.os.AsyncTask) Intent(android.content.Intent) ImageView(android.widget.ImageView) SpoilerRobotoTextView(me.ccrama.redditslide.SpoilerRobotoTextView) HorizontalScrollView(android.widget.HorizontalScrollView) SubmissionsView(me.ccrama.redditslide.Fragments.SubmissionsView) View(android.view.View) TextView(android.widget.TextView) ApiException(net.dean.jraw.ApiException) NetworkException(net.dean.jraw.http.NetworkException) Sorting(net.dean.jraw.paginators.Sorting) ContextThemeWrapper(androidx.appcompat.view.ContextThemeWrapper) UserRecord(net.dean.jraw.models.UserRecord) DialogAction(com.afollestad.materialdialogs.DialogAction) LayoutInflater(android.view.LayoutInflater) AccountManager(net.dean.jraw.managers.AccountManager) Snackbar(com.google.android.material.snackbar.Snackbar)

Aggregations

ContextThemeWrapper (androidx.appcompat.view.ContextThemeWrapper)21 Context (android.content.Context)14 View (android.view.View)12 ColorPreferences (me.ccrama.redditslide.Visuals.ColorPreferences)10 TextView (android.widget.TextView)9 LayoutInflater (android.view.LayoutInflater)7 Intent (android.content.Intent)6 ImageView (android.widget.ImageView)6 ArrayList (java.util.ArrayList)6 DialogInterface (android.content.DialogInterface)5 AlertDialog (androidx.appcompat.app.AlertDialog)5 RecyclerView (androidx.recyclerview.widget.RecyclerView)5 Snackbar (com.google.android.material.snackbar.Snackbar)5 SpoilerRobotoTextView (me.ccrama.redditslide.SpoilerRobotoTextView)5 Activity (android.app.Activity)4 ContextWrapper (android.content.ContextWrapper)4 TypedArray (android.content.res.TypedArray)4 HorizontalScrollView (android.widget.HorizontalScrollView)4 NonNull (androidx.annotation.NonNull)4 DialogAction (com.afollestad.materialdialogs.DialogAction)4