Skip to content

Commit

Permalink
[REF] Generic ThemeModel class
Browse files Browse the repository at this point in the history
  • Loading branch information
manneohlund committed Mar 1, 2018
1 parent b66fba2 commit 2477864
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 93 deletions.
79 changes: 0 additions & 79 deletions jsonthemer/src/main/java/jsonthemer/model/BaseThemeModel.kt

This file was deleted.

85 changes: 75 additions & 10 deletions jsonthemer/src/main/java/jsonthemer/model/ThemeModel.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
6 changes: 3 additions & 3 deletions sample/src/main/java/com/sample/jsonthemer/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<variable
name="themeModel"
type="jsonthemer.model.BaseThemeModel" />
type="jsonthemer.model.ThemeModel" />
</data>

<android.support.design.widget.CoordinatorLayout xmlns:app="http://schemas.android.com/apk/res-auto"
Expand Down

0 comments on commit 2477864

Please sign in to comment.