Skip to content

Commit

Permalink
Merge branch 'optional-dark-mode' into v-1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
medyo committed Jun 30, 2020
2 parents 853488d + ede23ad commit 6429eb0
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 84 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ This library allows to generate beautiful About Pages with less effort, it's ful
```java
View aboutPage = new AboutPage(this)
.isRTL(false)
.enableDarkMode(false)
.setCustomFont(String) // or Typeface
.setImage(R.drawable.dummy_image)
.addItem(versionElement)
.addItem(adsElement)
Expand All @@ -27,7 +29,7 @@ View aboutPage = new AboutPage(this)
Available on Jcenter, Maven and JitPack

```groovy
implementation 'com.github.medyo:android-about-page:1.2.6'
implementation 'com.github.medyo:android-about-page:1.3-beta'
```


Expand Down Expand Up @@ -77,13 +79,14 @@ addItem(versionElement)
| ------------- |:-------------:|
| setTitle(String) | Set title of the element|
| setIconTint(Int) | Set color of the element|
| setSkipTint(Boolean) | Skip tinting the icon (useful when using non vector drawables)|
| setIconDrawable(Int) | Set icon of the element|
| setValue(String) | Set Element value like Facebook ID|
| setIntent(Intent) | Set an intent to be called on `onClickListener` |
| setGravity(Gravity) | Set a Gravity for the element |
| setOnClickListener(View.OnClickListener) | If `intent` isn't suitable for you need, implement your custom behaviour by overriding the click listener|

### 5. How to use the library in a fragment
### 6. How to use the library in a fragment
```java
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Expand All @@ -99,6 +102,12 @@ addItem(versionElement)
```
snippet by [nrhoffmann](https://github.com/nrhoffmann)

### 6. Optional dark mode
```java
View aboutPage = new AboutPage(this)
.enableDarkMode(false) // if true, the dark mode is forced otherwise the default light one
.create()
```

## Sample Project
[medyo/android-about-page/app/](https://github.com/medyo/android-about-page/tree/master/app)
Expand Down
29 changes: 4 additions & 25 deletions app/src/main/java/mehdi/sakout/aboutpage/sample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package mehdi.sakout.aboutpage.sample;

import android.content.res.Configuration;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;

import mehdi.sakout.aboutpage.AboutPage;
import mehdi.sakout.aboutpage.Element;

Expand All @@ -18,12 +14,13 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
simulateDayNight(/* DAY */ 0);

Element adsElement = new Element();
adsElement.setTitle("Advertise with us");

View aboutPage = new AboutPage(this)
.isRTL(false)
.enableDarkMode(false)
.setImage(R.drawable.dummy_image)
.addItem(new Element().setTitle("Version 6.2"))
.addItem(adsElement)
Expand All @@ -48,6 +45,7 @@ Element getCopyRightsElement() {
final String copyrights = String.format(getString(R.string.copy_right), Calendar.getInstance().get(Calendar.YEAR));
copyRightsElement.setTitle(copyrights);
copyRightsElement.setIconDrawable(R.drawable.about_icon_copy_right);
copyRightsElement.setAutoApplyIconTint(true);
copyRightsElement.setIconTint(mehdi.sakout.aboutpage.R.color.about_item_icon_color);
copyRightsElement.setIconNightTint(android.R.color.white);
copyRightsElement.setGravity(Gravity.CENTER);
Expand All @@ -59,23 +57,4 @@ public void onClick(View v) {
});
return copyRightsElement;
}

void simulateDayNight(int currentSetting) {
final int DAY = 0;
final int NIGHT = 1;
final int FOLLOW_SYSTEM = 3;

int currentNightMode = getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK;
if (currentSetting == DAY && currentNightMode != Configuration.UI_MODE_NIGHT_NO) {
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_NO);
} else if (currentSetting == NIGHT && currentNightMode != Configuration.UI_MODE_NIGHT_YES) {
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_YES);
} else if (currentSetting == FOLLOW_SYSTEM) {
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
classpath 'com.android.tools.build:gradle:4.0.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
}
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Jan 09 10:58:07 WET 2019
#Tue Jun 30 17:26:38 WEST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 29
versionCode 18
versionName "1.2.6"
versionCode 19
versionName "1.3-beta"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
Expand Down
103 changes: 84 additions & 19 deletions library/src/main/java/mehdi/sakout/aboutpage/AboutPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.DrawableRes;
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.widget.TextViewCompat;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
Expand All @@ -25,6 +19,12 @@
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.widget.TextViewCompat;
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;

/**
* The main class of this library with many predefined methods to add Elements for common items in
* an About page. This class creates a {@link android.view.View} that can be passed as the root view
Expand All @@ -44,6 +44,11 @@ public class AboutPage {
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 @@ -56,6 +61,7 @@ public AboutPage(Context context) {
this.mContext = context;
this.mInflater = LayoutInflater.from(context);
this.mView = mInflater.inflate(R.layout.about_page, null);
this.enableDarkMode(false);
}

/**
Expand All @@ -70,6 +76,47 @@ public AboutPage setCustomFont(String path) {
return this;
}

/**
* Provide a typeface to use as custom font
*
* @param typeface
* @return this AboutPage instance for builder pattern support
*/
public AboutPage setCustomFont(Typeface typeface) {
mCustomFont = 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 Down Expand Up @@ -494,6 +541,9 @@ public AboutPage setDescription(CharSequence description) {
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);

if (mImage > 0) {
image.setImageResource(mImage);
}
Expand All @@ -503,11 +553,15 @@ public View create() {
}

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

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

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

return mView;
}

Expand Down Expand Up @@ -568,29 +622,38 @@ public void onClick(View view) {

Drawable wrappedDrawable = DrawableCompat.wrap(iconView.getDrawable());
wrappedDrawable = wrappedDrawable.mutate();
if (element.getAutoApplyIconTint()) {
int currentNightMode = mContext.getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK;
if (currentNightMode != Configuration.UI_MODE_NIGHT_YES) {
if (element.getIconTint() != null) {
DrawableCompat.setTint(wrappedDrawable, ContextCompat.getColor(mContext, element.getIconTint()));

if (!element.getSkipTint()) {
if (element.getAutoApplyIconTint()) {
if (mEnableDarkMode) {
if (element.getIconNightTint() != null) {
DrawableCompat.setTint(wrappedDrawable, ContextCompat.getColor(mContext, element.getIconNightTint()));
} else {
DrawableCompat.setTint(wrappedDrawable, mIconColor);
}
} else {
DrawableCompat.setTint(wrappedDrawable, ContextCompat.getColor(mContext, R.color.about_item_icon_color));
if (element.getIconTint() != null) {
DrawableCompat.setTint(wrappedDrawable, ContextCompat.getColor(mContext, element.getIconTint()));
} else {
DrawableCompat.setTint(wrappedDrawable, mIconColor);
}
}
} else if (element.getIconNightTint() != null) {
DrawableCompat.setTint(wrappedDrawable, ContextCompat.getColor(mContext, element.getIconNightTint()));
} else {
DrawableCompat.setTint(wrappedDrawable, AboutPageUtils.getThemeAccentColor(mContext));

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

}

} else {
int iconPadding = mContext.getResources().getDimensionPixelSize(R.dimen.about_icon_padding);
textView.setPadding(iconPadding, iconPadding, iconPadding, iconPadding);
}


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


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

private View getSeparator() {
return mInflater.inflate(R.layout.about_page_separator, null);
View separator = mInflater.inflate(R.layout.about_page_separator, null);
separator.setBackgroundColor(mSeparatorColor);
return separator;
}
}
15 changes: 14 additions & 1 deletion library/src/main/java/mehdi/sakout/aboutpage/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public class Element {
private String value;
private Intent intent;
private Integer gravity;
private Boolean autoIconColor = true;
private Boolean autoIconColor = false;
private Boolean skipTint = false;

private View.OnClickListener onClickListener;

Expand Down Expand Up @@ -216,4 +217,16 @@ public Element setAutoApplyIconTint(Boolean autoIconColor) {
this.autoIconColor = autoIconColor;
return this;
}

/**
* Skip Applying a custom tint usefull when the provided drawable isn't a vectorDrawable
* @param skipTint
*/
public void setSkipTint(Boolean skipTint) {
this.skipTint = skipTint;
}

public Boolean getSkipTint() {
return skipTint;
}
}
12 changes: 9 additions & 3 deletions library/src/main/res/layout/about_page.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/about_About.wrapper">
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout style="@style/about_About.wrapper">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/sub_wrapper"
style="@style/about_About.wrapper">

<LinearLayout style="@style/about_sub_wrapper">

Expand All @@ -15,7 +21,7 @@
style="@style/about_description" />
</LinearLayout>

<View style="@style/about_separator" />
<View android:id="@+id/description_separator" style="@style/about_separator" />

<LinearLayout
android:id="@+id/about_providers"
Expand Down
11 changes: 0 additions & 11 deletions library/src/main/res/values-night/colors.xml

This file was deleted.

Loading

0 comments on commit 6429eb0

Please sign in to comment.