From 247786408ff45b5a3f9442a7a9fe8374865ec84c Mon Sep 17 00:00:00 2001 From: manneohlund Date: Thu, 1 Mar 2018 11:45:25 +0100 Subject: [PATCH] [REF] Generic ThemeModel class --- .../java/jsonthemer/model/BaseThemeModel.kt | 79 ----------------- .../main/java/jsonthemer/model/ThemeModel.kt | 85 ++++++++++++++++--- .../com/sample/jsonthemer/MainActivity.kt | 6 +- sample/src/main/res/layout/activity_main.xml | 2 +- 4 files changed, 79 insertions(+), 93 deletions(-) delete mode 100644 jsonthemer/src/main/java/jsonthemer/model/BaseThemeModel.kt diff --git a/jsonthemer/src/main/java/jsonthemer/model/BaseThemeModel.kt b/jsonthemer/src/main/java/jsonthemer/model/BaseThemeModel.kt deleted file mode 100644 index 9b4ec7c..0000000 --- a/jsonthemer/src/main/java/jsonthemer/model/BaseThemeModel.kt +++ /dev/null @@ -1,79 +0,0 @@ -package jsonthemer.model - -import android.graphics.Color -import android.graphics.drawable.ColorDrawable -import jsonthemer.annotation.* - -/** - * Created by Manne Öhlund on 2018-02-23. - * Copyright © 2018. All rights reserved. - */ - -class BaseThemeModel { - val LIGHT = 0 - val DARK = 1 - - var theme = LIGHT - @StatusBar - var statusBarColor = "#FFFF5722" - @NavigationBar - var navigationBarColor = "#FFFF5722" - - // Theme overlays - private var toolbarThemeOverlay: Int = LIGHT - private var popupThemeOverlay: Int = LIGHT - - // Other - @ToolbarBar - private var toolbarColor: String = "#FFFF5722" - @AccentColor - private var accentColor: String = "#FFFF5722" - private var windowBackgroundColor: String = "#FF000000" - - @Theme - fun getModelTheme(): Int { - return when (theme) { - DARK -> return android.support.v7.appcompat.R.style.Theme_AppCompat_NoActionBar - else -> android.support.v7.appcompat.R.style.Theme_AppCompat_Light_NoActionBar - } - } - - @ToolbarThemeOverlay - fun getToolbarThemeOverlay(): Int { - return when (toolbarThemeOverlay) { - DARK -> return android.support.v7.appcompat.R.style.ThemeOverlay_AppCompat_Dark_ActionBar - else -> android.support.v7.appcompat.R.style.ThemeOverlay_AppCompat_ActionBar - } - } - - fun getPopupThemeOverlay(): Int { - return when (popupThemeOverlay) { - DARK -> return android.support.v7.appcompat.R.style.Theme_AppCompat - else -> android.support.v7.appcompat.R.style.Theme_AppCompat_Light - } - } - - fun getStatusBarColor(): Int { - return Color.parseColor(statusBarColor) - } - - fun getStatusBarColorDrawable(): ColorDrawable { - return ColorDrawable(getStatusBarColor()) - } - - fun getToolbarColor(): Int { - return Color.parseColor(toolbarColor) - } - - fun getAccentColor(): Int { - return Color.parseColor(accentColor) - } - - fun getNavigationBarColor(): Int { - return Color.parseColor(navigationBarColor) - } - - fun getWindowBackgroundColor(): Int { - return Color.parseColor(windowBackgroundColor) - } -} \ No newline at end of file diff --git a/jsonthemer/src/main/java/jsonthemer/model/ThemeModel.kt b/jsonthemer/src/main/java/jsonthemer/model/ThemeModel.kt index 487aebe..bfa015b 100644 --- a/jsonthemer/src/main/java/jsonthemer/model/ThemeModel.kt +++ b/jsonthemer/src/main/java/jsonthemer/model/ThemeModel.kt @@ -1,16 +1,81 @@ package jsonthemer.model +import android.graphics.Color +import android.graphics.drawable.ColorDrawable +import jsonthemer.annotation.* + /** - * Created by Manne Öhlund on 2018-02-27. + * Created by Manne Öhlund on 2018-02-23. * Copyright © 2018. All rights reserved. */ -interface ThemeModel { - var theme: Int - var statusBarColor: String - var navigationBarColor: String - - fun getModelTheme(): Int - fun getStatusBarColor(): Int - fun getToolbarColor(): Int - fun getNavigationBarColor(): Int + +class ThemeModel { + val LIGHT = 0 + val DARK = 1 + + var theme = LIGHT + @StatusBar + var statusBarColor = "#FFFF5722" + @NavigationBar + var navigationBarColor = "#FFFF5722" + + // Theme overlays + + private var toolbarThemeOverlay: Int = LIGHT + private var popupThemeOverlay: Int = LIGHT + + // Other + + @ToolbarBar + private var toolbarColor: String = "#FFFF5722" + @AccentColor + private var accentColor: String = "#FFFF5722" + private var windowBackgroundColor: String = "#FF000000" + + @Theme + fun getModelTheme(): Int { + return when (theme) { + DARK -> return android.support.v7.appcompat.R.style.Theme_AppCompat_NoActionBar + else -> android.support.v7.appcompat.R.style.Theme_AppCompat_Light_NoActionBar + } + } + + @ToolbarThemeOverlay + fun getToolbarThemeOverlay(): Int { + return when (toolbarThemeOverlay) { + DARK -> return android.support.v7.appcompat.R.style.ThemeOverlay_AppCompat_Dark_ActionBar + else -> android.support.v7.appcompat.R.style.ThemeOverlay_AppCompat_ActionBar + } + } + + fun getPopupThemeOverlay(): Int { + return when (popupThemeOverlay) { + DARK -> return android.support.v7.appcompat.R.style.Theme_AppCompat + else -> android.support.v7.appcompat.R.style.Theme_AppCompat_Light + } + } + + fun getStatusBarColor(): Int { + return Color.parseColor(statusBarColor) + } + + fun getStatusBarColorDrawable(): ColorDrawable { + return ColorDrawable(getStatusBarColor()) + } + + fun getToolbarColor(): Int { + return Color.parseColor(toolbarColor) + } + + fun getAccentColor(): Int { + return Color.parseColor(accentColor) + } + + fun getNavigationBarColor(): Int { + return Color.parseColor(navigationBarColor) + } + + fun getWindowBackgroundColor(): Int { + return Color.parseColor(windowBackgroundColor) + } } \ No newline at end of file diff --git a/sample/src/main/java/com/sample/jsonthemer/MainActivity.kt b/sample/src/main/java/com/sample/jsonthemer/MainActivity.kt index 81357e8..1852feb 100644 --- a/sample/src/main/java/com/sample/jsonthemer/MainActivity.kt +++ b/sample/src/main/java/com/sample/jsonthemer/MainActivity.kt @@ -9,7 +9,7 @@ import android.view.MenuItem import android.view.View import com.sample.jsonthemer.databinding.ActivityMainBinding import jsonthemer.JsonThemer -import jsonthemer.model.BaseThemeModel +import jsonthemer.model.ThemeModel class MainActivity : AppCompatActivity() { @@ -26,14 +26,14 @@ class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { - val theme: BaseThemeModel = let { + val theme: ThemeModel = let { try { JsonThemer.setup(this, currentTheme) } catch (e: Exception) { Log.e(MainActivity::class.simpleName, "Error: AssetFileNotFound, " + e.message) // Fallback on base model - BaseThemeModel() + ThemeModel() } } super.onCreate(savedInstanceState) diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 8c51f33..79acfe4 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -5,7 +5,7 @@ + type="jsonthemer.model.ThemeModel" />