Skip to content

Commit

Permalink
Improve compatibility, now support API level 4+.
Browse files Browse the repository at this point in the history
  • Loading branch information
vsnappy1 committed Dec 2, 2024
1 parent efe84be commit 9f456a5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 37 deletions.
38 changes: 18 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
# ResourceManager
ResourceManager is an Android plugin that simplifies accessing Android resources (strings, colors, drawables, etc.) in both Android and non-Android components (e.g., ViewModel) using generated code.

[![Gradle Plugin Portal](https://img.shields.io/maven-metadata/v?label=Gradle%20Plugin%20Portal&metadataUrl=https%3A%2F%2Fplugins.gradle.org%2Fm2%2Fdev%2Frandos%2Fresourcemanager%2Fdev.randos.resourcemanager.gradle.plugin%2Fmaven-metadata.xml)](https://plugins.gradle.org/plugin/dev.randos.resourcemanager)
![Platform Support](https://img.shields.io/badge/platform-Android-brightgreen.svg)
<p>
<a href="https://plugins.gradle.org/plugin/dev.randos.resourcemanager"><img alt="License" src="https://img.shields.io/maven-metadata/v?label=Gradle%20Plugin%20Portal&metadataUrl=https%3A%2F%2Fplugins.gradle.org%2Fm2%2Fdev%2Frandos%2Fresourcemanager%2Fdev.randos.resourcemanager.gradle.plugin%2Fmaven-metadata.xml"/></a>
<a href="https://android-arsenal.com/api?level=4"><img alt="API" src="https://img.shields.io/badge/API-4%2B-brightgreen.svg?style=flat"/></a>
<a href="https://github.com/vsnappy1/resourcemanager/actions"><img alt="Build Status" src="https://github.com/vsnappy1/resourcemanager/workflows/Android%20CI/badge.svg"/></a>
<a href="https://opensource.org/licenses/Apache-2.0"><img alt="License" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"/></a>
</p>

## Setup

### Step 1: Add ResourceManager Plugin
Add resourcemanager plugin to your project's root __build.gradle(.kts)__ file.
- If your project uses the plugins block.
```kotlin
// If your project uses the plugins block, add the following:
plugins {
id("com.android.application") version "8.0.1" apply false
id("org.jetbrains.kotlin.android") version "1.8.21" apply false
...
id("dev.randos.resourcemanager") version "0.0.9" apply false // Add ResourceManager plugin
id("dev.randos.resourcemanager") version "0.1.0" apply false
}
```
- If your project uses the buildscript block.
```kotlin

// Alternatively, if your project uses the buildscript block, include this:
buildScripts {
...
dependencies {
classpath "com.android.tools.build:gradle:8.0.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.21"
...
classpath 'dev.randos:resourcemanager:0.0.9' // Add ResourceManager plugin
classpath 'dev.randos:resourcemanager:0.1.0'
}
}
```

### Step 2: Apply ResourceManager Plugin
Apply the ResourceManager plugin in your module-level __build.gradle(.kts)__ file.
- If you are using the plugins block, add the following:
```kotlin
// If you are using the plugins block, add the following:
plugins {
id("com.android.application")
...
id("dev.randos.resourcemanager") // Apply ResourceManager plugin
id("dev.randos.resourcemanager")
}
```
- If your project uses the apply statement, include this:
```kotlin

// Alternatively, if your project uses the apply statement, include this:
apply plugin: 'com.android.application'
...
apply plugin: 'dev.randos.resourcemanager' // Apply ResourceManager plugin
apply plugin: 'dev.randos.resourcemanager'
```

### Step 3: Initialize ResourceManager
Expand All @@ -58,8 +56,8 @@ class MyApplication: Application() {

override fun onCreate() {
super.onCreate()
ResourceManager.initialize(this)
...
ResourceManager.initialize(this) // Initializes ResourceManager
}
}
```
Expand All @@ -78,7 +76,7 @@ class MyViewModel : ViewModel() {
}
```

## Migration
## Migration (Beta)
To streamline the transition to ResourceManager, plugin comes with a Gradle task to automate key aspects of the migration process. Please follow these steps carefully.

#### Important Warning ⚠️
Expand Down
2 changes: 1 addition & 1 deletion resourcemanager/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ apply(from = "../gradle/jacoco.gradle.kts")
val agp: String by project
val kotlinVersion: String by project
val pluginGroup = "dev.randos"
val pluginVersion = "0.0.9"
val pluginVersion = "0.1.0"
val pluginName = "resourcemanager"

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ internal object ClassFileGenerator {
appendLine("import android.app.Application")
appendLine("import android.graphics.drawable.Drawable")
appendLine("import android.content.res.Resources.Theme")
appendLine("import android.os.Build")
appendLine("import $namespace.R\n")
appendLine("object ResourceManager {\n")
appendLine("\tprivate var _application: Application? = null")
appendLine("\tprivate val application: Application")
appendLine("\t\tget() = _application ?: throw IllegalStateException(\"ResourceManager is not initialized. Please call ResourceManager.initialize(this) in your Application class.\")\n")
appendLine("\t\tget() = _application ?: throw IllegalStateException(\"ResourceManager is not initialized. Please invoke ResourceManager.initialize(this) in onCreate method of your Application class.\")\n")
appendLine("\t@JvmStatic")
appendLine("\tfun initialize(application: Application) {")
appendLine("\t\t_application = application")
Expand Down Expand Up @@ -72,6 +73,7 @@ internal object ClassFileGenerator {

private fun StringBuilder.generateObjectForDrawableResources(resources: List<Resource>) {
functionNames.clear()
appendLine("\t@Suppress(\"DEPRECATION\")")
appendLine("\tobject Drawables {")
resources.forEach { resource ->
resource.moduleDetails.resourceFiles.sorted().forEach { file ->
Expand Down Expand Up @@ -113,6 +115,9 @@ internal object ClassFileGenerator {
val defaultIndentation = "\t\t"
return StringBuilder().apply {
functionNames.clear()
if (name == "Colors") {
appendLine("\t@Suppress(\"DEPRECATION\")")
}
appendLine("\tobject $name {")
pairs.sortedBy { it.second.name }.forEach { (moduleDetails, resource) ->
when (resource.type) {
Expand Down Expand Up @@ -193,7 +198,7 @@ internal object ClassFileGenerator {
val moduleName = getDrawableMethodName(name, moduleDetails)
if (isMethodNameUsedBefore(moduleName)) return
appendLine(
"$defaultIndentation@JvmOverloads @JvmStatic fun $moduleName(theme: Theme = application.theme) : Drawable = application.resources.getDrawable(${namespaceString}R.drawable.$name, theme)"
"$defaultIndentation@JvmOverloads @JvmStatic fun $moduleName(theme: Theme = application.theme) : Drawable = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { application.resources.getDrawable(${namespaceString}R.drawable.$name, theme) } else { application.resources.getDrawable(${namespaceString}R.drawable.$name) }"
)
}

Expand All @@ -219,7 +224,7 @@ internal object ClassFileGenerator {
val methodName = getValueResourceMethodName(resource, moduleDetails)
if (isMethodNameUsedBefore(methodName)) return
appendLine(
"$defaultIndentation@JvmOverloads @JvmStatic fun $methodName(theme: Theme = application.theme) : Int = application.resources.getColor(${namespaceString}R.color.${resource.name}, theme)"
"$defaultIndentation@JvmOverloads @JvmStatic fun $methodName(theme: Theme = application.theme) : Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { application.resources.getColor(${namespaceString}R.color.${resource.name}, theme) } else { application.resources.getColor(${namespaceString}R.color.${resource.name}) }"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package com.example.application
import android.app.Application
import android.graphics.drawable.Drawable
import android.content.res.Resources.Theme
import android.os.Build
import com.example.application.R

object ResourceManager {

private var _application: Application? = null
private val application: Application
get() = _application ?: throw IllegalStateException("ResourceManager is not initialized. Please call ResourceManager.initialize(this) in your Application class.")
get() = _application ?: throw IllegalStateException("ResourceManager is not initialized. Please invoke ResourceManager.initialize(this) in onCreate method of your Application class.")

@JvmStatic
fun initialize(application: Application) {
Expand All @@ -22,14 +23,15 @@ object ResourceManager {
@JvmStatic fun boolResMod_lib1() : Boolean = application.resources.getBoolean(com.example.lib1.R.bool.bool_res_mod)
}

@Suppress("DEPRECATION")
object Colors {
@JvmOverloads @JvmStatic fun black(theme: Theme = application.theme) : Int = application.resources.getColor(R.color.black, theme)
@JvmOverloads @JvmStatic fun black_lib2(theme: Theme = application.theme) : Int = application.resources.getColor(com.example.lib2.R.color.black, theme)
@JvmOverloads @JvmStatic fun colorResCamelCase(theme: Theme = application.theme) : Int = application.resources.getColor(R.color.colorResCamelCase, theme)
@JvmOverloads @JvmStatic fun colorRes(theme: Theme = application.theme) : Int = application.resources.getColor(R.color.color_res, theme)
@JvmOverloads @JvmStatic fun colorResMod_lib1(theme: Theme = application.theme) : Int = application.resources.getColor(com.example.lib1.R.color.color_res_mod, theme)
@JvmOverloads @JvmStatic fun white(theme: Theme = application.theme) : Int = application.resources.getColor(R.color.white, theme)
@JvmOverloads @JvmStatic fun white_lib2(theme: Theme = application.theme) : Int = application.resources.getColor(com.example.lib2.R.color.white, theme)
@JvmOverloads @JvmStatic fun black(theme: Theme = application.theme) : Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { application.resources.getColor(R.color.black, theme) } else { application.resources.getColor(R.color.black) }
@JvmOverloads @JvmStatic fun black_lib2(theme: Theme = application.theme) : Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { application.resources.getColor(com.example.lib2.R.color.black, theme) } else { application.resources.getColor(com.example.lib2.R.color.black) }
@JvmOverloads @JvmStatic fun colorResCamelCase(theme: Theme = application.theme) : Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { application.resources.getColor(R.color.colorResCamelCase, theme) } else { application.resources.getColor(R.color.colorResCamelCase) }
@JvmOverloads @JvmStatic fun colorRes(theme: Theme = application.theme) : Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { application.resources.getColor(R.color.color_res, theme) } else { application.resources.getColor(R.color.color_res) }
@JvmOverloads @JvmStatic fun colorResMod_lib1(theme: Theme = application.theme) : Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { application.resources.getColor(com.example.lib1.R.color.color_res_mod, theme) } else { application.resources.getColor(com.example.lib1.R.color.color_res_mod) }
@JvmOverloads @JvmStatic fun white(theme: Theme = application.theme) : Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { application.resources.getColor(R.color.white, theme) } else { application.resources.getColor(R.color.white) }
@JvmOverloads @JvmStatic fun white_lib2(theme: Theme = application.theme) : Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { application.resources.getColor(com.example.lib2.R.color.white, theme) } else { application.resources.getColor(com.example.lib2.R.color.white) }
}

object Dimensions {
Expand Down Expand Up @@ -75,11 +77,12 @@ object ResourceManager {
}

// ----- DRAWABLES -----
@Suppress("DEPRECATION")
object Drawables {
@JvmOverloads @JvmStatic fun icArrow(theme: Theme = application.theme) : Drawable = application.resources.getDrawable(R.drawable.ic_arrow, theme)
@JvmOverloads @JvmStatic fun icGift(theme: Theme = application.theme) : Drawable = application.resources.getDrawable(R.drawable.ic_gift, theme)
@JvmOverloads @JvmStatic fun icLauncherForeground(theme: Theme = application.theme) : Drawable = application.resources.getDrawable(R.drawable.ic_launcher_foreground, theme)
@JvmOverloads @JvmStatic fun icCart_lib1(theme: Theme = application.theme) : Drawable = application.resources.getDrawable(com.example.lib1.R.drawable.ic_cart, theme)
@JvmOverloads @JvmStatic fun icDone_lib2(theme: Theme = application.theme) : Drawable = application.resources.getDrawable(com.example.lib2.R.drawable.ic_done, theme)
@JvmOverloads @JvmStatic fun icArrow(theme: Theme = application.theme) : Drawable = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { application.resources.getDrawable(R.drawable.ic_arrow, theme) } else { application.resources.getDrawable(R.drawable.ic_arrow) }
@JvmOverloads @JvmStatic fun icGift(theme: Theme = application.theme) : Drawable = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { application.resources.getDrawable(R.drawable.ic_gift, theme) } else { application.resources.getDrawable(R.drawable.ic_gift) }
@JvmOverloads @JvmStatic fun icLauncherForeground(theme: Theme = application.theme) : Drawable = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { application.resources.getDrawable(R.drawable.ic_launcher_foreground, theme) } else { application.resources.getDrawable(R.drawable.ic_launcher_foreground) }
@JvmOverloads @JvmStatic fun icCart_lib1(theme: Theme = application.theme) : Drawable = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { application.resources.getDrawable(com.example.lib1.R.drawable.ic_cart, theme) } else { application.resources.getDrawable(com.example.lib1.R.drawable.ic_cart) }
@JvmOverloads @JvmStatic fun icDone_lib2(theme: Theme = application.theme) : Drawable = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { application.resources.getDrawable(com.example.lib2.R.drawable.ic_done, theme) } else { application.resources.getDrawable(com.example.lib2.R.drawable.ic_done) }
}
}

0 comments on commit 9f456a5

Please sign in to comment.