Skip to content

Commit

Permalink
Merge pull request #7 from odaridavid/dark-variant
Browse files Browse the repository at this point in the history
Add a dark variant
  • Loading branch information
odaridavid authored May 9, 2020
2 parents e6dfb7b + e9b2eca commit 78d2b08
Show file tree
Hide file tree
Showing 44 changed files with 545 additions and 38 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@ will be required for a successful build.

## Screenshots

|<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'/>|
|:--:|:--:|:--:|:--:|
|<img src='art/s4.png' width='210'/>|<img src='art/s7.png' width='210'/>|<img src='art/s8.png' width='210'/>|
|:--:|:--:|:--:|
|Splash Activity|Settings(Q >)|Settings(< Q)|

|<img src='art/s9.png' width='210'/>|<img src='art/s10.png' width='210'/>|<img src='art/s11.png' width='210'/>|
|:--:|:--:|:--:|
|Dashboard(Dark)|Dashboard(Light)|Description(Light)|

|<img src='art/s12.png' width='210'/>|<img src='art/s13.png' width='210'/>|<img src='art/s14.png' width='210'/>|
|:--:|:--:|:--:|
|Description(Dark)|About(Light)|About(Dark)|

## Attributions

Expand Down
15 changes: 10 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
defaultConfig {

def versionMajor = 1
def versionMinor = 0
def versionMinor = 1
def versionPatch = 0

applicationId "com.github.odaridavid.designpatterns"
Expand All @@ -30,7 +30,7 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
manifestPlaceholders = [crashlyticsEnabled: true]
}
debug{
debug {
manifestPlaceholders = [crashlyticsEnabled: false]
}
}
Expand All @@ -41,6 +41,7 @@ android {
dependencies {

def material_design_version = "1.2.0-alpha06"
def about_libraries_version = "8.1.2"

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

//Libraries used info
implementation "com.mikepenz:aboutlibraries-core:8.1.2"
implementation "com.mikepenz:aboutlibraries:8.1.2"
implementation "com.mikepenz:aboutlibraries-core:$about_libraries_version"
implementation "com.mikepenz:aboutlibraries:$about_libraries_version"

//Animations
implementation 'jp.wasabeef:recyclerview-animators:3.0.0'
Expand All @@ -67,10 +68,14 @@ dependencies {
//Crashlytics
implementation 'com.google.firebase:firebase-crashlytics:17.0.0'

//Preference
implementation 'androidx.preference:preference-ktx:1.1.1'

//Markdown Support
implementation 'us.feras.mdv:markdownview:1.1.0'

//Unit testing
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.13'
}
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.gms.google-services'
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package="com.github.odaridavid.designpatterns">

<application
android:name=".DesignPatternsApp"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand All @@ -13,6 +14,10 @@
tools:ignore="AllowBackup"
tools:replace="android:allowBackup">
<activity android:name=".base.BaseActivity" />
<activity
android:name=".ui.SettingsActivity"
android:label="@string/menu_settings"
android:parentActivityName=".ui.MainActivity" />
<activity
android:name=".ui.AboutActivity"
android:label="@string/menu_about"
Expand Down
77 changes: 77 additions & 0 deletions app/src/main/assets/dark_kotlin.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
*
* Copyright 2020 David Odari
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
**/
@charset "utf-8";

body {
font-family: Helvetica, Arial, Freesans, clean, sans-serif;
font-size:1.2em;
padding:0.5em;
margin:auto;
color:#a69b97;
max-width:42em;
background:#494949;
}
code, pre {
color: inherit;
}
pre {
line-height: 1.25em;
overflow: auto;
padding: 6px 10px;
}
pre > code {
border: 0;
margin: 0;
padding: 0;
}
code {
margin: 0 2px;
padding: 0 5px;
}
.keyword{
color:#0097a7;
}
.types{
color:#c25e00;
}
.generic{
color:#c75b39;
}
.annotation{
color:#c75b39;
}
.arguments{
color:#D2691E;
}
.string{
color:#99d066;
}
.comments{
color:#8e8e8e;
}
.assertions{
color:#b39ddb;
}
.stdlib{
color:#c49000;
}
.literals{
color:#1E90FF;
}
.static{
color:#c49000;
}
.string-template{
color:#c49000;
}
1 change: 0 additions & 1 deletion app/src/main/assets/kotlin.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ body {
background:#eeeeee;
}
code, pre {
background-color: #F8F8F8;
color: inherit;
}
pre {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
*
* Copyright 2020 David Odari
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
**/
package com.github.odaridavid.designpatterns

import android.app.Application
import androidx.preference.PreferenceManager
import com.github.odaridavid.designpatterns.helpers.ThemeUtils


internal class DesignPatternsApp : Application() {

override fun onCreate() {
super.onCreate()
val sp = PreferenceManager.getDefaultSharedPreferences(baseContext)
PreferenceManager.setDefaultValues(this, R.xml.preferences, false)
ThemeUtils.updateTheme(sp, getString(R.string.key_theme_preference))
}

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,86 @@
package com.github.odaridavid.designpatterns.base

import android.content.Context
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.os.PowerManager
import android.view.View
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
import com.github.odaridavid.designpatterns.R
import com.github.odaridavid.designpatterns.helpers.SdkUtils
import com.github.odaridavid.designpatterns.helpers.SdkUtils.versionFrom
import com.github.odaridavid.designpatterns.helpers.SdkUtils.versionUntil
import com.github.odaridavid.designpatterns.helpers.ThemeUtils

abstract class BaseActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
matchStatusBarWithBackground()
super.onCreate(savedInstanceState)
private val powerManager: PowerManager by lazy {
getSystemService(Context.POWER_SERVICE) as PowerManager
}

private fun matchStatusBarWithBackground() {
if (SdkUtils.versionFrom(Build.VERSION_CODES.M)) {
override fun onResume() {
matchSystemBarsWithBackground()
super.onResume()
}

private fun matchSystemBarsWithBackground() {
val sp = PreferenceManager.getDefaultSharedPreferences(baseContext)
val theme = sp.getString(getString(R.string.key_theme_preference), ThemeUtils.THEME_LIGHT)
if (versionFrom(Build.VERSION_CODES.M))
handleSystemBars(theme)
}

@RequiresApi(Build.VERSION_CODES.M)
private fun handleSystemBars(theme: String?) {
when (theme) {
ThemeUtils.THEME_LIGHT -> setLightSystemBars()
ThemeUtils.THEME_DARK -> setDarkSystemBars()
ThemeUtils.THEME_SYSTEM -> {
if (versionUntil(Build.VERSION_CODES.P)) {
onPowerSaverModeChange()
} else {
onUiModeConfigChange()
}
}
}
}

@RequiresApi(Build.VERSION_CODES.M)
private fun onPowerSaverModeChange() {
if (powerManager.isPowerSaveMode)
setDarkSystemBars()
else
setLightSystemBars()
}

@RequiresApi(Build.VERSION_CODES.M)
private fun onUiModeConfigChange() {
when (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
Configuration.UI_MODE_NIGHT_NO -> {
setLightSystemBars()
}
Configuration.UI_MODE_NIGHT_YES -> {
setDarkSystemBars()
}
}
}

@RequiresApi(Build.VERSION_CODES.M)
private fun setLightSystemBars() {
if (versionFrom(Build.VERSION_CODES.O)) {
window.decorView.systemUiVisibility =
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
} else {
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
window.statusBarColor = getColor(R.color.colorPrimary)
}
window.statusBarColor = getColor(android.R.color.background_light)
window.navigationBarColor = getColor(android.R.color.background_light)
}

@RequiresApi(Build.VERSION_CODES.M)
private fun setDarkSystemBars() {
window.statusBarColor = getColor(android.R.color.background_dark)
window.navigationBarColor = getColor(android.R.color.background_dark)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
package com.github.odaridavid.designpatterns.helpers

const val BASE_PATH = "file:///android_asset"
const val KOTLIN_CSS_PATH = "$BASE_PATH/kotlin.css"
const val LIGHT_KOTLIN_CSS_PATH = "$BASE_PATH/kotlin.css"
const val DARK_KOTLIN_CSS_PATH = "$BASE_PATH/dark_kotlin.css"

object CodeSample {
const val ABSTRACT_FACTORY = "$BASE_PATH/abstract_factory.md"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
package com.github.odaridavid.designpatterns.helpers

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.widget.Toast
import androidx.preference.PreferenceManager
import com.github.odaridavid.designpatterns.R
import us.feras.mdv.MarkdownView


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

internal fun MarkdownView.loadWithKotlinCss(filePath: String) {
internal fun MarkdownView.loadWithKotlinCss(context: Context, filePath: String) {
loadMarkdownFile(
filePath,
KOTLIN_CSS_PATH
getThemedCss(context)
)
}

internal fun getThemedCss(context: Context): String {
val sp = PreferenceManager.getDefaultSharedPreferences(context)
return when (getCurrentTheme(context, sp)) {
ThemeUtils.THEME_DARK -> DARK_KOTLIN_CSS_PATH
ThemeUtils.THEME_LIGHT -> LIGHT_KOTLIN_CSS_PATH
ThemeUtils.THEME_SYSTEM -> LIGHT_KOTLIN_CSS_PATH
else -> LIGHT_KOTLIN_CSS_PATH
}
}

internal fun getCurrentTheme(context: Context, sp: SharedPreferences?): String {
return sp?.getString(context.getString(R.string.key_theme_preference), ThemeUtils.THEME_LIGHT)
?: "default"
}

internal fun String.checkUrlScheme(): String {
return if (!startsWith("http://") && !startsWith("https://"))
"http://$this"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ import android.os.Build
import androidx.annotation.IntRange

internal object SdkUtils {
fun versionFrom(@IntRange(from = 0, to = 29) versionCodes: Int): Boolean {
return Build.VERSION.SDK_INT >= versionCodes
fun versionFrom(@IntRange(from = 0, to = 29) versionCode: Int): Boolean {
return Build.VERSION.SDK_INT >= versionCode
}

fun versionUntil(@IntRange(from = 0, to = 29) versionCode: Int): Boolean {
return Build.VERSION.SDK_INT <= versionCode
}
}

Expand Down
Loading

0 comments on commit 78d2b08

Please sign in to comment.