Skip to content

Commit

Permalink
add styling attributes, conventionally handle day-night modes
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshgngwr committed May 7, 2021
1 parent 719e9c2 commit 4dd2cd4
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ protected void onCreate(Bundle savedInstanceState) {

View aboutPage = new AboutPage(this)
.isRTL(false)
.enableDarkMode(false)
.setImage(R.drawable.dummy_image)
.addItem(new Element().setTitle("Version 6.2"))
.addItem(adsElement)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
Expand Down
99 changes: 32 additions & 67 deletions library/src/main/java/mehdi/sakout/aboutpage/AboutPage.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mehdi.sakout.aboutpage;

import android.app.Fragment;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
Expand All @@ -10,7 +11,7 @@
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -20,6 +21,8 @@
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.StyleRes;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.widget.TextViewCompat;
Expand All @@ -37,18 +40,15 @@
* @see Element
*/
public class AboutPage {
private static final String LOG_TAG = AboutPage.class.getSimpleName();

private final Context mContext;
private final LayoutInflater mInflater;
private final View mView;
private CharSequence mDescription;
private int mImage = 0;
private boolean mIsRTL = false;
private Typeface mCustomFont;
private Boolean mEnableDarkMode = false;
private int mBackgroundColor;
private int mIconColor;
private int mTextColor;
private int mSeparatorColor;

/**
* The AboutPage requires a context to perform it's functions. Give it a context associated to an
Expand All @@ -58,10 +58,17 @@ public class AboutPage {
* @param context
*/
public AboutPage(Context context) {
this.mContext = context;
this.mInflater = LayoutInflater.from(context);
this(context, AboutPageUtils.resolveResIdAttr(context, R.attr.aboutStyle, R.style.about_About));
}

public AboutPage(Context context, boolean forceEnableDarkMode) {
this(context, forceEnableDarkMode ? R.style.about_AboutBase_Dark : R.style.about_AboutBase_Light);
}

public AboutPage(Context context, @StyleRes int style) {
this.mContext = new ContextThemeWrapper(context, style);
this.mInflater = LayoutInflater.from(this.mContext);
this.mView = mInflater.inflate(R.layout.about_page, null);
this.enableDarkMode(false);
}

/**
Expand All @@ -87,36 +94,6 @@ public AboutPage setCustomFont(Typeface typeface) {
return this;
}

/**
* Provide a way to force dark mode or not
*
* @param enabled
* @return this AboutPage instance for builder pattern support
*/
public AboutPage enableDarkMode(Boolean enabled) {
mEnableDarkMode = enabled;
if (enabled) {
mBackgroundColor = ContextCompat
.getColor(mContext, R.color.about_background_dark_color);
mTextColor = ContextCompat
.getColor(mContext, R.color.about_text_dark_color);
mSeparatorColor = ContextCompat
.getColor(mContext, R.color.about_separator_dark_color);
mIconColor = AboutPageUtils.getThemeAccentColor(mContext);

} else {
mBackgroundColor = ContextCompat
.getColor(mContext, R.color.about_background_color);
mTextColor = ContextCompat
.getColor(mContext, R.color.about_text_color);
mSeparatorColor = ContextCompat
.getColor(mContext, R.color.about_separator_color);
mIconColor = ContextCompat
.getColor(mContext, R.color.about_item_icon_color);
}
return this;
}

/**
* Convenience method for {@link AboutPage#addEmail(java.lang.String, java.lang.String)} but with
* a predefined title string
Expand All @@ -140,7 +117,6 @@ public AboutPage addEmail(String email, String title) {
Element emailElement = new Element();
emailElement.setTitle(title);
emailElement.setIconDrawable(R.drawable.about_icon_email);
emailElement.setIconTint(R.color.about_item_icon_color);

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
Expand Down Expand Up @@ -430,7 +406,6 @@ public AboutPage addWebsite(String url, String title) {
Element websiteElement = new Element();
websiteElement.setTitle(title);
websiteElement.setIconDrawable(R.drawable.about_icon_link);
websiteElement.setIconTint(R.color.about_item_icon_color);
websiteElement.setValue(url);

Uri uri = Uri.parse(url);
Expand All @@ -450,7 +425,7 @@ public AboutPage addWebsite(String url, String title) {
* @see Element
*/
public AboutPage addItem(Element element) {
LinearLayout wrapper = (LinearLayout) mView.findViewById(R.id.about_providers);
LinearLayout wrapper = mView.findViewById(R.id.about_providers);
wrapper.addView(createItem(element));
wrapper.addView(getSeparator(), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mContext.getResources().getDimensionPixelSize(R.dimen.about_separator_height)));
return this;
Expand Down Expand Up @@ -492,7 +467,7 @@ public AboutPage addGroup(String name) {

TextView textView = new TextView(mContext);
textView.setText(name);
TextViewCompat.setTextAppearance(textView, R.style.about_groupTextAppearance);
TextViewCompat.setTextAppearance(textView, AboutPageUtils.resolveResIdAttr(mContext, R.attr.aboutGroupTextAppearance, R.style.about_groupTextAppearance));
LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

if (mCustomFont != null) {
Expand Down Expand Up @@ -539,10 +514,8 @@ public AboutPage setDescription(CharSequence description) {
* @return the inflated {@link View} of this AboutPage
*/
public View create() {
TextView description = (TextView) mView.findViewById(R.id.description);
ImageView image = (ImageView) mView.findViewById(R.id.image);
View subWrapper = mView.findViewById(R.id.sub_wrapper);
View descriptionSeparator = mView.findViewById(R.id.description_separator);
TextView description = mView.findViewById(R.id.description);
ImageView image = mView.findViewById(R.id.image);

if (mImage > 0) {
image.setImageResource(mImage);
Expand All @@ -552,16 +525,10 @@ public View create() {
description.setText(mDescription);
}

description.setGravity(Gravity.CENTER);
description.setTextColor(mTextColor);

if (mCustomFont != null) {
description.setTypeface(mCustomFont);
}

subWrapper.setBackgroundColor(mBackgroundColor);
descriptionSeparator.setBackgroundColor(mSeparatorColor);

return mView;
}

Expand All @@ -578,16 +545,15 @@ private View createItem(final Element element) {
public void onClick(View view) {
try {
mContext.startActivity(element.getIntent());
} catch (Exception e) {
} catch (ActivityNotFoundException e) {
Log.e(LOG_TAG, "failed to launch intent for '" + element.getTitle() + "' element", e);
}
}
});

}

TypedValue outValue = new TypedValue();
mContext.getTheme().resolveAttribute(R.attr.selectableItemBackground, outValue, true);
wrapper.setBackgroundResource(outValue.resourceId);
wrapper.setBackgroundResource(AboutPageUtils.resolveResIdAttr(mContext, R.attr.selectableItemBackground, android.R.color.transparent));

int padding = mContext.getResources().getDimensionPixelSize(R.dimen.about_text_padding);
wrapper.setPadding(padding, padding, padding, padding);
Expand All @@ -596,7 +562,7 @@ public void onClick(View view) {


TextView textView = new TextView(mContext);
TextViewCompat.setTextAppearance(textView, R.style.about_elementTextAppearance);
TextViewCompat.setTextAppearance(textView, AboutPageUtils.resolveResIdAttr(mContext, R.attr.aboutElementTextAppearance, R.style.about_elementTextAppearance));
LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(textParams);
if (mCustomFont != null) {
Expand All @@ -623,26 +589,28 @@ public void onClick(View view) {
Drawable wrappedDrawable = DrawableCompat.wrap(iconView.getDrawable());
wrappedDrawable = wrappedDrawable.mutate();

final boolean isNightModeEnabled = AboutPageUtils.isNightModeEnabled(mContext);
final int iconColor = AboutPageUtils.resolveColorAttr(mContext, R.attr.aboutElementIconTint);
if (!element.getSkipTint()) {
if (element.getAutoApplyIconTint()) {
if (mEnableDarkMode) {
if (isNightModeEnabled) {
if (element.getIconNightTint() != null) {
DrawableCompat.setTint(wrappedDrawable, ContextCompat.getColor(mContext, element.getIconNightTint()));
} else {
DrawableCompat.setTint(wrappedDrawable, mIconColor);
DrawableCompat.setTint(wrappedDrawable, iconColor);
}
} else {
if (element.getIconTint() != null) {
DrawableCompat.setTint(wrappedDrawable, ContextCompat.getColor(mContext, element.getIconTint()));
} else {
DrawableCompat.setTint(wrappedDrawable, mIconColor);
DrawableCompat.setTint(wrappedDrawable, iconColor);
}
}

} else if (element.getIconTint() != null) {
DrawableCompat.setTint(wrappedDrawable, ContextCompat.getColor(mContext, element.getIconTint()));
} else if (mEnableDarkMode) {
DrawableCompat.setTint(wrappedDrawable, mIconColor);
} else if (isNightModeEnabled) {
DrawableCompat.setTint(wrappedDrawable, iconColor);
}

}
Expand All @@ -653,7 +621,6 @@ public void onClick(View view) {
}

textView.setText(element.getTitle());
textView.setTextColor(mTextColor);


if (mIsRTL) {
Expand Down Expand Up @@ -683,8 +650,6 @@ public void onClick(View view) {
}

private View getSeparator() {
View separator = mInflater.inflate(R.layout.about_page_separator, null);
separator.setBackgroundColor(mSeparatorColor);
return separator;
return mInflater.inflate(R.layout.about_page_separator, null);
}
}
52 changes: 41 additions & 11 deletions library/src/main/java/mehdi/sakout/aboutpage/AboutPageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.TypedValue;

import androidx.annotation.AttrRes;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import java.util.List;

class AboutPageUtils {
Expand All @@ -25,16 +31,40 @@ static Boolean isAppInstalled(Context context, String appName) {
return installed;
}

static int getThemeAccentColor(Context context) {
int colorAttr;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
colorAttr = android.R.attr.colorAccent;
} else {
//Get colorAccent defined for AppCompat
colorAttr = context.getResources().getIdentifier("colorAccent", "attr", context.getPackageName());
@ColorInt
static int resolveColorAttr(@NonNull Context context, @AttrRes int attr) {
final TypedValue outValue = resolveAttr(context, attr);
if (outValue.type >= TypedValue.TYPE_FIRST_COLOR_INT && outValue.type <= TypedValue.TYPE_LAST_COLOR_INT) {
return outValue.data;
}
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(colorAttr, outValue, true);
return outValue.data;

return ContextCompat.getColor(context, outValue.resourceId);
}

static int resolveResIdAttr(@NonNull Context context, @AttrRes int attr, int defaultValue) {
try {
return resolveAttr(context, attr).resourceId;
} catch (Resources.NotFoundException e) {
return defaultValue;
}
}

@NonNull
private static TypedValue resolveAttr(@NonNull Context context, @AttrRes int attr) {
final TypedValue outValue = new TypedValue();
if (!context.getTheme().resolveAttribute(attr, outValue, true)) {
throw new Resources.NotFoundException("'" + context.getResources().getResourceName(attr) + "' is not set.");
}

if (outValue.type == TypedValue.TYPE_ATTRIBUTE) {
return resolveAttr(context, outValue.data);
}

return outValue;
}

static boolean isNightModeEnabled(@NonNull Context context) {
final int nightMode = context.getResources().getConfiguration().uiMode;
return (nightMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
}
}
1 change: 1 addition & 0 deletions library/src/main/res/layout/about_page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<TextView
android:id="@+id/description"
android:text="@string/about_page_description"
style="@style/about_description" />
</LinearLayout>

Expand Down
4 changes: 4 additions & 0 deletions library/src/main/res/values-night/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="about_About" parent="about_AboutBase_Dark" />
</resources>
10 changes: 10 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="aboutStyle" format="reference" />
<attr name="aboutBackground" format="reference|color" />
<attr name="aboutDescriptionTextAppearance" format="reference" />
<attr name="aboutSeparatorColor" format="color" />
<attr name="aboutElementTextAppearance" format="reference" />
<attr name="aboutElementIconTint" format="color" />
<attr name="aboutGroupTextAppearance" format="reference" />
</resources>
1 change: 1 addition & 0 deletions library/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<color name="about_text_color">#595959</color>

<!-- Dark Mode -->
<color name="about_item_icon_dark_color">?colorAccent</color>
<color name="about_background_dark_color">#303030</color>
<color name="about_separator_dark_color">#494949</color>

Expand Down
Loading

0 comments on commit 4dd2cd4

Please sign in to comment.