Labeled separator view styling refactoring.

This commit is contained in:
Anton Chekulaev
2020-08-21 17:10:49 +10:00
parent a5408fa4f4
commit 9d7d7689e2
8 changed files with 40 additions and 33 deletions

View File

@@ -2,13 +2,10 @@ package org.thoughtcrime.securesms.components;
import android.content.Context;
import androidx.annotation.AttrRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageButton;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import org.thoughtcrime.securesms.TransportOption;
@@ -116,7 +113,7 @@ public class SendButton extends AppCompatImageButton
case SMS:
case TEXTSECURE:
default: {
sendDrawable = ThemeUtil.getDrawableResWithAttribute(
sendDrawable = ThemeUtil.getThemedDrawableResId(
getContext(), R.attr.conversation_transport_sms_indicator);
}
}

View File

@@ -9,21 +9,21 @@ import android.view.LayoutInflater
import android.widget.RelativeLayout
import kotlinx.android.synthetic.main.view_separator.view.*
import network.loki.messenger.R
import org.thoughtcrime.securesms.loki.utilities.getColorWithID
import org.thoughtcrime.securesms.loki.utilities.toPx
import org.thoughtcrime.securesms.util.ThemeUtil
class LabeledSeparatorView : RelativeLayout {
private val path = Path()
private val paint: Paint = {
private val paint: Paint by lazy{
val result = Paint()
result.style = Paint.Style.STROKE
result.color = resources.getColorWithID(R.color.separator, context.theme)
result.color = ThemeUtil.getThemedColor(context, R.attr.dividerHorizontal)
result.strokeWidth = toPx(1, resources).toFloat()
result.isAntiAlias = true
result
}()
}
// region Lifecycle
constructor(context: Context) : super(context) {
@@ -43,7 +43,7 @@ class LabeledSeparatorView : RelativeLayout {
}
private fun setUpViewHierarchy() {
val inflater = context.applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val contentView = inflater.inflate(R.layout.view_separator, null)
val layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
addView(contentView, layoutParams)
@@ -56,7 +56,7 @@ class LabeledSeparatorView : RelativeLayout {
super.onDraw(c)
val w = width.toFloat()
val h = height.toFloat()
val hMargin = toPx(10, resources).toFloat()
val hMargin = toPx(16, resources).toFloat()
path.reset()
path.moveTo(0.0f, h / 2)
path.lineTo(titleTextView.left - hMargin, h / 2)

View File

@@ -4,6 +4,7 @@ import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import androidx.annotation.AttrRes;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
@@ -19,17 +20,33 @@ public class ThemeUtil {
private static final String TAG = ThemeUtil.class.getSimpleName();
public static boolean isDarkTheme(@NonNull Context context) {
return getAttribute(context, R.attr.theme_type, "light").equals("dark");
return getAttributeText(context, R.attr.theme_type, "light").equals("dark");
}
@ColorInt
public static int getThemedColor(@NonNull Context context, @AttrRes int attr) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
if (theme.resolveAttribute(attr, typedValue, true)) {
return typedValue.data;
} else {
Log.e(TAG, "Couldn't find a color attribute with id: " + attr);
return Color.RED;
}
}
@DrawableRes
public static int getThemedDrawableResId(@NonNull Context context, @AttrRes int attr) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
if (theme.resolveAttribute(attr, typedValue, true)) {
return typedValue.resourceId;
} else {
Log.e(TAG, "Couldn't find a drawable attribute with id: " + attr);
return 0;
}
return Color.RED;
}
public static LayoutInflater getThemedInflater(@NonNull Context context, @NonNull LayoutInflater inflater, @StyleRes int theme) {
@@ -37,7 +54,7 @@ public class ThemeUtil {
return inflater.cloneInContext(contextThemeWrapper);
}
private static String getAttribute(Context context, int attribute, String defaultValue) {
private static String getAttributeText(Context context, int attribute, String defaultValue) {
TypedValue outValue = new TypedValue();
if (context.getTheme().resolveAttribute(attribute, outValue, true)) {
@@ -49,15 +66,4 @@ public class ThemeUtil {
return defaultValue;
}
@DrawableRes
public static int getDrawableResWithAttribute(Context context, @AttrRes int attributeId) {
TypedValue resolvedValue = new TypedValue();
context.getTheme().resolveAttribute(attributeId, resolvedValue, true);
if (resolvedValue.type != TypedValue.TYPE_STRING) {
Log.e(TAG, "Cannot resolve a drawable resource from an attribute ID: " + attributeId);
return 0;
}
return resolvedValue.resourceId;
}
}