Skip to content

Commit 78d2b08

Browse files
authored
Merge pull request #7 from odaridavid/dark-variant
Add a dark variant
2 parents e6dfb7b + e9b2eca commit 78d2b08

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+545
-38
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,17 @@ will be required for a successful build.
2525

2626
## Screenshots
2727

28-
|<img src='art/s4.png' width='210'/>|<img src='art/s1.png' width='210'/>|<img src='art/s6.png' width='210'/>|<img src='art/s5.png' width='210'/>|
29-
|:--:|:--:|:--:|:--:|
28+
|<img src='art/s4.png' width='210'/>|<img src='art/s7.png' width='210'/>|<img src='art/s8.png' width='210'/>|
29+
|:--:|:--:|:--:|
30+
|Splash Activity|Settings(Q >)|Settings(< Q)|
31+
32+
|<img src='art/s9.png' width='210'/>|<img src='art/s10.png' width='210'/>|<img src='art/s11.png' width='210'/>|
33+
|:--:|:--:|:--:|
34+
|Dashboard(Dark)|Dashboard(Light)|Description(Light)|
35+
36+
|<img src='art/s12.png' width='210'/>|<img src='art/s13.png' width='210'/>|<img src='art/s14.png' width='210'/>|
37+
|:--:|:--:|:--:|
38+
|Description(Dark)|About(Light)|About(Dark)|
3039

3140
## Attributions
3241

app/build.gradle

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ android {
1212
defaultConfig {
1313

1414
def versionMajor = 1
15-
def versionMinor = 0
15+
def versionMinor = 1
1616
def versionPatch = 0
1717

1818
applicationId "com.github.odaridavid.designpatterns"
@@ -30,7 +30,7 @@ android {
3030
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
3131
manifestPlaceholders = [crashlyticsEnabled: true]
3232
}
33-
debug{
33+
debug {
3434
manifestPlaceholders = [crashlyticsEnabled: false]
3535
}
3636
}
@@ -41,6 +41,7 @@ android {
4141
dependencies {
4242

4343
def material_design_version = "1.2.0-alpha06"
44+
def about_libraries_version = "8.1.2"
4445

4546
implementation fileTree(dir: 'libs', include: ['*.jar'])
4647
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
@@ -55,8 +56,8 @@ dependencies {
5556
implementation "com.google.android.material:material:$material_design_version"
5657

5758
//Libraries used info
58-
implementation "com.mikepenz:aboutlibraries-core:8.1.2"
59-
implementation "com.mikepenz:aboutlibraries:8.1.2"
59+
implementation "com.mikepenz:aboutlibraries-core:$about_libraries_version"
60+
implementation "com.mikepenz:aboutlibraries:$about_libraries_version"
6061

6162
//Animations
6263
implementation 'jp.wasabeef:recyclerview-animators:3.0.0'
@@ -67,10 +68,14 @@ dependencies {
6768
//Crashlytics
6869
implementation 'com.google.firebase:firebase-crashlytics:17.0.0'
6970

71+
//Preference
72+
implementation 'androidx.preference:preference-ktx:1.1.1'
73+
7074
//Markdown Support
7175
implementation 'us.feras.mdv:markdownview:1.1.0'
7276

7377
//Unit testing
78+
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
7479
testImplementation 'junit:junit:4.13'
7580
}
76-
apply plugin: 'com.google.gms.google-services'
81+
apply plugin: 'com.google.gms.google-services'

app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package="com.github.odaridavid.designpatterns">
55

66
<application
7+
android:name=".DesignPatternsApp"
78
android:allowBackup="false"
89
android:icon="@mipmap/ic_launcher"
910
android:label="@string/app_name"
@@ -13,6 +14,10 @@
1314
tools:ignore="AllowBackup"
1415
tools:replace="android:allowBackup">
1516
<activity android:name=".base.BaseActivity" />
17+
<activity
18+
android:name=".ui.SettingsActivity"
19+
android:label="@string/menu_settings"
20+
android:parentActivityName=".ui.MainActivity" />
1621
<activity
1722
android:name=".ui.AboutActivity"
1823
android:label="@string/menu_about"

app/src/main/assets/dark_kotlin.css

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
*
3+
* Copyright 2020 David Odari
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License. You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software distributed under the License
9+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10+
* or implied. See the License for the specific language governing permissions and limitations under
11+
* the License.
12+
*
13+
**/
14+
@charset "utf-8";
15+
16+
body {
17+
font-family: Helvetica, Arial, Freesans, clean, sans-serif;
18+
font-size:1.2em;
19+
padding:0.5em;
20+
margin:auto;
21+
color:#a69b97;
22+
max-width:42em;
23+
background:#494949;
24+
}
25+
code, pre {
26+
color: inherit;
27+
}
28+
pre {
29+
line-height: 1.25em;
30+
overflow: auto;
31+
padding: 6px 10px;
32+
}
33+
pre > code {
34+
border: 0;
35+
margin: 0;
36+
padding: 0;
37+
}
38+
code {
39+
margin: 0 2px;
40+
padding: 0 5px;
41+
}
42+
.keyword{
43+
color:#0097a7;
44+
}
45+
.types{
46+
color:#c25e00;
47+
}
48+
.generic{
49+
color:#c75b39;
50+
}
51+
.annotation{
52+
color:#c75b39;
53+
}
54+
.arguments{
55+
color:#D2691E;
56+
}
57+
.string{
58+
color:#99d066;
59+
}
60+
.comments{
61+
color:#8e8e8e;
62+
}
63+
.assertions{
64+
color:#b39ddb;
65+
}
66+
.stdlib{
67+
color:#c49000;
68+
}
69+
.literals{
70+
color:#1E90FF;
71+
}
72+
.static{
73+
color:#c49000;
74+
}
75+
.string-template{
76+
color:#c49000;
77+
}

app/src/main/assets/kotlin.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ body {
2222
background:#eeeeee;
2323
}
2424
code, pre {
25-
background-color: #F8F8F8;
2625
color: inherit;
2726
}
2827
pre {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
*
3+
* Copyright 2020 David Odari
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License. You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software distributed under the License
9+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10+
* or implied. See the License for the specific language governing permissions and limitations under
11+
* the License.
12+
*
13+
**/
14+
package com.github.odaridavid.designpatterns
15+
16+
import android.app.Application
17+
import androidx.preference.PreferenceManager
18+
import com.github.odaridavid.designpatterns.helpers.ThemeUtils
19+
20+
21+
internal class DesignPatternsApp : Application() {
22+
23+
override fun onCreate() {
24+
super.onCreate()
25+
val sp = PreferenceManager.getDefaultSharedPreferences(baseContext)
26+
PreferenceManager.setDefaultValues(this, R.xml.preferences, false)
27+
ThemeUtils.updateTheme(sp, getString(R.string.key_theme_preference))
28+
}
29+
30+
}
Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,86 @@
11
package com.github.odaridavid.designpatterns.base
22

3+
import android.content.Context
4+
import android.content.res.Configuration
35
import android.os.Build
4-
import android.os.Bundle
6+
import android.os.PowerManager
57
import android.view.View
8+
import androidx.annotation.RequiresApi
69
import androidx.appcompat.app.AppCompatActivity
10+
import androidx.preference.PreferenceManager
711
import com.github.odaridavid.designpatterns.R
8-
import com.github.odaridavid.designpatterns.helpers.SdkUtils
12+
import com.github.odaridavid.designpatterns.helpers.SdkUtils.versionFrom
13+
import com.github.odaridavid.designpatterns.helpers.SdkUtils.versionUntil
14+
import com.github.odaridavid.designpatterns.helpers.ThemeUtils
915

1016
abstract class BaseActivity : AppCompatActivity() {
1117

12-
override fun onCreate(savedInstanceState: Bundle?) {
13-
matchStatusBarWithBackground()
14-
super.onCreate(savedInstanceState)
18+
private val powerManager: PowerManager by lazy {
19+
getSystemService(Context.POWER_SERVICE) as PowerManager
1520
}
1621

17-
private fun matchStatusBarWithBackground() {
18-
if (SdkUtils.versionFrom(Build.VERSION_CODES.M)) {
22+
override fun onResume() {
23+
matchSystemBarsWithBackground()
24+
super.onResume()
25+
}
26+
27+
private fun matchSystemBarsWithBackground() {
28+
val sp = PreferenceManager.getDefaultSharedPreferences(baseContext)
29+
val theme = sp.getString(getString(R.string.key_theme_preference), ThemeUtils.THEME_LIGHT)
30+
if (versionFrom(Build.VERSION_CODES.M))
31+
handleSystemBars(theme)
32+
}
33+
34+
@RequiresApi(Build.VERSION_CODES.M)
35+
private fun handleSystemBars(theme: String?) {
36+
when (theme) {
37+
ThemeUtils.THEME_LIGHT -> setLightSystemBars()
38+
ThemeUtils.THEME_DARK -> setDarkSystemBars()
39+
ThemeUtils.THEME_SYSTEM -> {
40+
if (versionUntil(Build.VERSION_CODES.P)) {
41+
onPowerSaverModeChange()
42+
} else {
43+
onUiModeConfigChange()
44+
}
45+
}
46+
}
47+
}
48+
49+
@RequiresApi(Build.VERSION_CODES.M)
50+
private fun onPowerSaverModeChange() {
51+
if (powerManager.isPowerSaveMode)
52+
setDarkSystemBars()
53+
else
54+
setLightSystemBars()
55+
}
56+
57+
@RequiresApi(Build.VERSION_CODES.M)
58+
private fun onUiModeConfigChange() {
59+
when (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
60+
Configuration.UI_MODE_NIGHT_NO -> {
61+
setLightSystemBars()
62+
}
63+
Configuration.UI_MODE_NIGHT_YES -> {
64+
setDarkSystemBars()
65+
}
66+
}
67+
}
68+
69+
@RequiresApi(Build.VERSION_CODES.M)
70+
private fun setLightSystemBars() {
71+
if (versionFrom(Build.VERSION_CODES.O)) {
72+
window.decorView.systemUiVisibility =
73+
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
74+
} else {
1975
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
20-
window.statusBarColor = getColor(R.color.colorPrimary)
2176
}
77+
window.statusBarColor = getColor(android.R.color.background_light)
78+
window.navigationBarColor = getColor(android.R.color.background_light)
2279
}
2380

81+
@RequiresApi(Build.VERSION_CODES.M)
82+
private fun setDarkSystemBars() {
83+
window.statusBarColor = getColor(android.R.color.background_dark)
84+
window.navigationBarColor = getColor(android.R.color.background_dark)
85+
}
2486
}

app/src/main/java/com/github/odaridavid/designpatterns/helpers/CodeSamplePath.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
package com.github.odaridavid.designpatterns.helpers
1515

1616
const val BASE_PATH = "file:///android_asset"
17-
const val KOTLIN_CSS_PATH = "$BASE_PATH/kotlin.css"
17+
const val LIGHT_KOTLIN_CSS_PATH = "$BASE_PATH/kotlin.css"
18+
const val DARK_KOTLIN_CSS_PATH = "$BASE_PATH/dark_kotlin.css"
1819

1920
object CodeSample {
2021
const val ABSTRACT_FACTORY = "$BASE_PATH/abstract_factory.md"

app/src/main/java/com/github/odaridavid/designpatterns/helpers/Extensions.kt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
package com.github.odaridavid.designpatterns.helpers
1515

1616
import android.app.Activity
17+
import android.content.Context
1718
import android.content.Intent
19+
import android.content.SharedPreferences
1820
import android.widget.Toast
21+
import androidx.preference.PreferenceManager
22+
import com.github.odaridavid.designpatterns.R
1923
import us.feras.mdv.MarkdownView
2024

2125

@@ -27,13 +31,28 @@ inline fun <reified T> Activity.navigateTo(noinline intentExtras: ((Intent) -> U
2731
startActivity(intent)
2832
}
2933

30-
internal fun MarkdownView.loadWithKotlinCss(filePath: String) {
34+
internal fun MarkdownView.loadWithKotlinCss(context: Context, filePath: String) {
3135
loadMarkdownFile(
3236
filePath,
33-
KOTLIN_CSS_PATH
37+
getThemedCss(context)
3438
)
3539
}
3640

41+
internal fun getThemedCss(context: Context): String {
42+
val sp = PreferenceManager.getDefaultSharedPreferences(context)
43+
return when (getCurrentTheme(context, sp)) {
44+
ThemeUtils.THEME_DARK -> DARK_KOTLIN_CSS_PATH
45+
ThemeUtils.THEME_LIGHT -> LIGHT_KOTLIN_CSS_PATH
46+
ThemeUtils.THEME_SYSTEM -> LIGHT_KOTLIN_CSS_PATH
47+
else -> LIGHT_KOTLIN_CSS_PATH
48+
}
49+
}
50+
51+
internal fun getCurrentTheme(context: Context, sp: SharedPreferences?): String {
52+
return sp?.getString(context.getString(R.string.key_theme_preference), ThemeUtils.THEME_LIGHT)
53+
?: "default"
54+
}
55+
3756
internal fun String.checkUrlScheme(): String {
3857
return if (!startsWith("http://") && !startsWith("https://"))
3958
"http://$this"

app/src/main/java/com/github/odaridavid/designpatterns/helpers/SdkUtils.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ import android.os.Build
1717
import androidx.annotation.IntRange
1818

1919
internal object SdkUtils {
20-
fun versionFrom(@IntRange(from = 0, to = 29) versionCodes: Int): Boolean {
21-
return Build.VERSION.SDK_INT >= versionCodes
20+
fun versionFrom(@IntRange(from = 0, to = 29) versionCode: Int): Boolean {
21+
return Build.VERSION.SDK_INT >= versionCode
22+
}
23+
24+
fun versionUntil(@IntRange(from = 0, to = 29) versionCode: Int): Boolean {
25+
return Build.VERSION.SDK_INT <= versionCode
2226
}
2327
}
2428

0 commit comments

Comments
 (0)