Skip to content

Commit

Permalink
Make it possible to change FullScreenDialogFragment's Toolbar theme
Browse files Browse the repository at this point in the history
  • Loading branch information
renanferrari committed Jul 10, 2020
1 parent bd9706c commit 81b74ba
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.annotation.StyleRes;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.view.ViewCompat;
import androidx.fragment.app.DialogFragment;
Expand All @@ -31,6 +31,8 @@
import androidx.fragment.app.FragmentTransaction;

import org.wordpress.android.R;
import org.wordpress.android.util.ColorUtils;
import org.wordpress.android.util.ContextExtensionsKt;

/**
* A {@link DialogFragment} implementing the full-screen dialog pattern defined in the
Expand All @@ -48,12 +50,14 @@ public class FullScreenDialogFragment extends DialogFragment {
private String mTitle;
private Toolbar mToolbar;
private boolean mHideActivityBar;
private int mToolbarTheme;
private int mToolbarColor;

private static final String ARG_ACTION = "ARG_ACTION";
private static final String ARG_HIDE_ACTIVITY_BAR = "ARG_HIDE_ACTIVITY_BAR";
private static final String ARG_SUBTITLE = "ARG_SUBTITLE";
private static final String ARG_TITLE = "ARG_TITLE";
private static final String ARG_TOOLBAR_THEME = "ARG_TOOLBAR_THEME";
private static final String ARG_TOOLBAR_COLOR = "ARG_TOOLBAR_COLOR";
private static final int ID_ACTION = 1;

Expand Down Expand Up @@ -98,6 +102,7 @@ private static Bundle setArguments(Builder builder) {
bundle.putString(ARG_ACTION, builder.mAction);
bundle.putString(ARG_TITLE, builder.mTitle);
bundle.putString(ARG_SUBTITLE, builder.mSubtitle);
bundle.putInt(ARG_TOOLBAR_THEME, builder.mToolbarTheme);
bundle.putInt(ARG_TOOLBAR_COLOR, builder.mToolbarColor);
bundle.putBoolean(ARG_HIDE_ACTIVITY_BAR, builder.mHideActivityBar);
return bundle;
Expand Down Expand Up @@ -251,6 +256,7 @@ private void initBuilderArguments() {
mAction = bundle.getString(ARG_ACTION);
mTitle = bundle.getString(ARG_TITLE);
mSubtitle = bundle.getString(ARG_SUBTITLE);
mToolbarTheme = bundle.getInt(ARG_TOOLBAR_THEME);
mToolbarColor = bundle.getInt(ARG_TOOLBAR_COLOR);
mHideActivityBar = bundle.getBoolean(ARG_HIDE_ACTIVITY_BAR);
}
Expand All @@ -262,9 +268,17 @@ private void initBuilderArguments() {
*/
private void initToolbar(View view) {
mToolbar = view.findViewById(R.id.toolbar_main);

if (mToolbarTheme > 0) {
mToolbar.getContext().setTheme(mToolbarTheme);
}

final Context context = mToolbar.getContext();

mToolbar.setTitle(mTitle);
mToolbar.setSubtitle(mSubtitle);
mToolbar.setNavigationIcon(ContextCompat.getDrawable(view.getContext(), R.drawable.ic_close_white_24dp));
mToolbar.setNavigationIcon(ColorUtils.applyTintToDrawable(context, R.drawable.ic_close_white_24dp,
ContextExtensionsKt.getColorResIdFromAttribute(context, R.attr.colorControlNormal)));
mToolbar.setNavigationContentDescription(R.string.close_dialog_button_desc);
mToolbar.setNavigationOnClickListener(v -> onDismissClicked());

Expand Down Expand Up @@ -415,6 +429,7 @@ public static class Builder {
String mSubtitle = "";
String mTitle = "";
boolean mHideActivityBar = false;
int mToolbarTheme = 0;
int mToolbarColor = 0;

/**
Expand Down Expand Up @@ -530,6 +545,11 @@ public Builder setTitle(@StringRes int textId) {
return this;
}

public Builder setToolbarTheme(@StyleRes int themeId) {
this.mToolbarTheme = themeId;
return this;
}

/**
* Set {@link FullScreenDialogFragment} toolbar color.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ protected void launchDialog() {
mDialog = new FullScreenDialogFragment.Builder(getContext())
.setTitle(R.string.username_changer_title)
.setAction(R.string.username_changer_action)
.setToolbarTheme(R.style.ThemeOverlay_LoginFlow_Toolbar)
.setOnConfirmListener(this)
.setOnDismissListener(this)
.setContent(UsernameChangerFullScreenDialogFragment.class, bundle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.widget.ImageViewCompat

object ColorUtils {
@JvmStatic
fun applyTintToDrawable(context: Context, @DrawableRes drawableResId: Int, @ColorRes colorResId: Int): Drawable {
val drawable = context.resources.getDrawable(drawableResId, context.theme)
val color = ContextCompat.getColor(context, colorResId)
Expand All @@ -20,6 +21,7 @@ object ColorUtils {
return drawable
}

@JvmStatic
fun setImageResourceWithTint(imageView: ImageView, @DrawableRes drawableResId: Int, @ColorRes colorResId: Int) {
imageView.setImageDrawable(ContextCompat.getDrawable(imageView.context, drawableResId))
ImageViewCompat.setImageTintList(
Expand Down
9 changes: 9 additions & 0 deletions WordPress/src/main/res/drawable/ic_close_white_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="@android:color/white"
android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z" />
</vector>
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
android:paddingTop="@dimen/margin_extra_large"
android:paddingBottom="@dimen/margin_extra_large"
android:src="@drawable/ic_search_white_24dp"
app:tint="@color/login_on_surface_medium_selector" />
app:tint="?attr/wpColorOnSurfaceMedium" />

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/username"
Expand Down

0 comments on commit 81b74ba

Please sign in to comment.