Fixed a crash under API level 21 on the main timeline due to trying to tint the status buttons with selectors.

This commit is contained in:
Vavassor 2017-02-17 23:10:46 -05:00
commit 0439fabd79
15 changed files with 67 additions and 132 deletions

View file

@ -626,11 +626,13 @@ public class ComposeActivity extends BaseActivity {
private void enableMediaPicking() {
mediaPick.setEnabled(true);
ThemeUtils.setImageViewTint(mediaPick, R.attr.compose_media_button_tint);
mediaPick.setImageResource(R.drawable.ic_media);
}
private void disableMediaPicking() {
mediaPick.setEnabled(false);
ThemeUtils.setImageViewTint(mediaPick, R.attr.compose_media_button_disabled_tint);
mediaPick.setImageResource(R.drawable.ic_media_disabled);
}

View file

@ -1,54 +0,0 @@
/* Copyright 2017 Andrew Dawson
*
* This file is part of Tusky.
*
* Tusky is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky. If not, see
* <http://www.gnu.org/licenses/>. */
package com.keylesspalace.tusky;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.AppCompatImageButton;
import android.util.AttributeSet;
public class StatusButton extends AppCompatImageButton {
private static final int[] STATE_MARKED = { R.attr.state_marked };
private boolean marked;
public StatusButton(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
TypedArray array = context.getTheme().obtainStyledAttributes(
attributeSet, R.styleable.StatusButton, 0, 0);
try {
marked = array.getBoolean(R.styleable.StatusButton_state_marked, false);
} finally {
array.recycle();
}
}
@Override
public int[] onCreateDrawableState(int extraSpace) {
if (marked) {
extraSpace += 1;
}
int[] drawableState = super.onCreateDrawableState(extraSpace);
if (marked) {
mergeDrawableStates(drawableState, STATE_MARKED);
}
return drawableState;
}
public void setMarked(boolean marked) {
this.marked = marked;
}
}

View file

@ -44,8 +44,8 @@ public class StatusViewHolder extends RecyclerView.ViewHolder {
private View rebloggedBar;
private TextView rebloggedByDisplayName;
private ImageButton replyButton;
private StatusButton reblogButton;
private StatusButton favouriteButton;
private ImageButton reblogButton;
private ImageButton favouriteButton;
private ImageButton moreButton;
private boolean favourited;
private boolean reblogged;
@ -71,8 +71,8 @@ public class StatusViewHolder extends RecyclerView.ViewHolder {
rebloggedBar = itemView.findViewById(R.id.status_reblogged_bar);
rebloggedByDisplayName = (TextView) itemView.findViewById(R.id.status_reblogged);
replyButton = (ImageButton) itemView.findViewById(R.id.status_reply);
reblogButton = (StatusButton) itemView.findViewById(R.id.status_reblog);
favouriteButton = (StatusButton) itemView.findViewById(R.id.status_favourite);
reblogButton = (ImageButton) itemView.findViewById(R.id.status_reblog);
favouriteButton = (ImageButton) itemView.findViewById(R.id.status_favourite);
moreButton = (ImageButton) itemView.findViewById(R.id.status_more);
reblogged = false;
favourited = false;
@ -189,21 +189,33 @@ public class StatusViewHolder extends RecyclerView.ViewHolder {
public void setReblogged(boolean reblogged) {
this.reblogged = reblogged;
reblogButton.setMarked(reblogged);
int attribute;
if (reblogged) {
attribute = R.attr.status_reblog_button_marked_tint;
} else {
attribute = R.attr.status_reblog_button_tint;
}
ThemeUtils.setImageViewTint(reblogButton, attribute);
}
/** This should only be called after setReblogged, in order to override the tint correctly. */
public void setRebloggingEnabled(boolean enabled) {
reblogButton.setEnabled(enabled);
if (enabled) {
reblogButton.setImageResource(R.drawable.ic_reblog);
} else {
if (!enabled) {
ThemeUtils.setImageViewTint(reblogButton, R.attr.status_reblog_button_disabled_tint);
reblogButton.setImageResource(R.drawable.ic_reblog_disabled);
}
}
public void setFavourited(boolean favourited) {
this.favourited = favourited;
favouriteButton.setMarked(favourited);
int attribute;
if (favourited) {
attribute = R.attr.status_favourite_button_marked_tint;
} else {
attribute = R.attr.status_favourite_button_tint;
}
ThemeUtils.setImageViewTint(favouriteButton, attribute);
}
public void setMediaPreviews(final Status.MediaAttachment[] attachments,

View file

@ -16,9 +16,11 @@
package com.keylesspalace.tusky;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;
import android.widget.ImageView;
public class ThemeUtils {
public static Drawable getDrawable(Context context, int attribute, int fallbackDrawable) {
@ -49,4 +51,8 @@ public class ThemeUtils {
return android.R.color.black;
}
}
public static void setImageViewTint(ImageView view, int attribute) {
view.setColorFilter(getColor(view.getContext(), attribute), PorterDuff.Mode.SRC_IN);
}
}