Skip to content

Commit

Permalink
Added read all annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
devmike01 committed Feb 3, 2019
1 parent 8606654 commit 3b10f9f
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 33 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ A lite weight SharedPreference library which uses annotation processing to gener
* Write less code to configure JadeSharedPreference
* Save multiple values to JadeSharedPrefence at onces
* Read from JadeSharedPreference using just annotations `@Read...`)
* Listen to value changes in realtime
* Listen to value changes in real-time

```kotin
```kotlin
class InecBox @SharedPref("key") constructor(context: Context) {

val TAG: String = "InecBox"
Expand Down Expand Up @@ -54,8 +54,10 @@ allprojects {
kapt 'com.github.devmike01.JadeSharedPreference:compiler:1.2.15'
}
```
##### Note: Add `apply plugin: 'kotlin-kapt'` - if you don't already have it, to your app `build.gradle` to allow the the processor generates the necessary codes.
> Note: Add `apply plugin: 'kotlin-kapt'` - if you don't already have it, to your app `build.gradle` to allow the the processor generates the necessary codes.
Contributions are highly welcome. There're some features I intend to add in the future. They're listed below:
* Lint for checking

License
-------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package devmike.jade.com.annotations.read

@Target(AnnotationTarget.FIELD, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.SOURCE)
annotation class ReadAll {
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,27 @@ class MainActivity @SharedPref("sharedSetKey") constructor(): AppCompatActivity(

//Read string from JadeSharedPreference
@ReadString("string")
public lateinit var mString: String
lateinit var mString: String

//Read Integer from JadeSharedPreference
@ReadInt("integer_anything")
public var mInteger: Int =0
var mInteger: Int =0

//Read Long from JadeSharedPreference
@ReadLong("long_")
public var mLong: Long =0
var mLong: Long =0

//Read Float from JadeSharedPreference
@ReadFloat("float_key")
public var mFloat: Float =0f
var mFloat: Float =0f

//Read StringSet from JadeSharedPreference
@ReadStringSet("stringset__")
public var mStringSet: MutableSet<String> = mutableSetOf()
lateinit var mStringSet: MutableSet<String>

@ReadAll
lateinit var mMapAll: Map<String, *>


//@SharedPref("key")
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -60,9 +64,12 @@ class MainActivity @SharedPref("sharedSetKey") constructor(): AppCompatActivity(
save_btn4.setOnClickListener(this::btnTestOne)
save_btn5.setOnClickListener(this::btnTestOne)


Log.d("MainActivity", mMapAll.toString())

}

public fun btnTestOne(v: View){
fun btnTestOne(v: View){
/**
* Click to write to JadeSharedPreference
*/
Expand Down Expand Up @@ -106,4 +113,9 @@ class MainActivity @SharedPref("sharedSetKey") constructor(): AppCompatActivity(
fun stringSetChanges(set: MutableSet<String>){
read_5.text = set.toString()
}

@ReadAll
fun readAllItems(allItems: MutableMap<String, *>){
Log.d("MainActivity", allItems.toString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public object NameStore {
const val CLASS_VAR ="mClass"
const val SHAREDPREF_LISTENER ="sharedPrefListener"
const val ARGUMENT ="arg0"
const val READ_ALL = "Hey! Read All"

}

Expand Down
84 changes: 60 additions & 24 deletions compiler/src/main/java/devmike/jade/com/compiler/ProcessorHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import devmike.jade.com.annotations.SharedPref
import devmike.jade.com.annotations.read.*
import devmike.jade.com.processor.NameStore
import java.io.File
import java.lang.NullPointerException
import java.util.EnumSet.range
import java.util.HashSet
import javax.annotation.processing.ProcessingEnvironment
Expand All @@ -18,6 +19,7 @@ internal object ProcessorHelper {
const val KAPT_KOTLIN_GENERATED = "kapt.kotlin.generated"
private lateinit var buildClassAccessBuilder: FunSpec.Builder
private lateinit var sharedPrefListenerBuilder: FunSpec.Builder
private lateinit var readAllBuilder : FunSpec.Builder

public fun process(
processingEnv: ProcessingEnvironment,
Expand Down Expand Up @@ -170,7 +172,8 @@ internal object ProcessorHelper {
ReadLong::class.simpleName,
ReadStringSet::class.simpleName,
ReadFloat::class.simpleName,
ReadInt::class.simpleName
ReadInt::class.simpleName,
ReadAll::class.simpleName
)

//Select the annotations and use it to generate the appropriate method
Expand All @@ -195,44 +198,49 @@ internal object ProcessorHelper {
val readIntAn = annotatedMethod.getAnnotation(ReadInt::class.java)
val readLongAn = annotatedMethod.getAnnotation(ReadLong::class.java)
val readFloatAn = annotatedMethod.getAnnotation(ReadFloat::class.java)
val readAll = annotatedMethod.getAnnotation(ReadAll::class.java)


if (readFloatAn != null){
annotationBuilder(className,classBuilder,
annotationBuilder(className,
annotatedMethod, Float::class.simpleName, readFloatAn.defaultValue,
readFloatAn.key)
}

if(readStringAn != null){
annotationBuilder(className,classBuilder,
annotationBuilder(className,
annotatedMethod,
String::class.java.simpleName,
readStringAn.defaultValue,
readStringAn.key)
}

if (readStringSetAn != null){
annotationBuilder(className,classBuilder, annotatedMethod,
annotationBuilder(className, annotatedMethod,
NameStore.Types.STRINGSET,
"mutableSetOf(\"\")",
readStringSetAn.key)

}

if (readIntAn != null){
annotationBuilder(className,
classBuilder,
annotatedMethod,
Int::class.simpleName,
annotationBuilder(className, annotatedMethod, Int::class.simpleName,
readIntAn.defaultValue,
readIntAn.key)
}

if (readLongAn != null){

annotationBuilder(className,classBuilder, annotatedMethod,
annotationBuilder(className, annotatedMethod,
LONG.simpleName, readLongAn.defaultValue, readLongAn.key)
}

if (readAll != null){
annotationBuilder(className, annotatedMethod,
MutableMap::class.java.name,
null,
NameStore.Variable.READ_ALL)
}
}
}

Expand All @@ -251,31 +259,42 @@ internal object ProcessorHelper {
}
}

private fun annotationBuilder(className : ClassName,classBuilder: TypeSpec.Builder,
private fun annotationBuilder(className : ClassName,
annotatedMethod: Element, annotatedVarType: String?,
defaultValue: Any, key: String){
defaultValue: Any?, key: String){
if (annotatedVarType != null) {
listenerBuilder(className, classBuilder, annotatedMethod, annotatedVarType,
listenerBuilder(className, annotatedMethod, annotatedVarType,
defaultValue, key)
}
}

private fun listenerBuilder(className : ClassName, classBuilder: TypeSpec.Builder,
private fun listenerBuilder(className : ClassName,
annotatedMethod: Element,
annotatedVarType: String, defaultValue: Any, key: String){
annotatedVarType: String, defaultValue: Any?, key: String){

sharedPrefListenerBuilder
.addComment("Get $annotatedVarType value from SharedPreference")
.addStatement("if(%N.equals(%S)){(%L as %L).%L(%L.get%L(%S, ${placeHolder(annotatedVarType)}))}",
NameStore.Variable.SHARED_VALUE_KEY,
key,
NameStore.Variable.CLASS_VAR,
className,
annotatedMethod.simpleName.toString(),
NameStore.Variable.SHARED_PREF_VALUE,
annotatedVarType,
key,
defaultValue)
if (key != NameStore.Variable.READ_ALL && defaultValue !=null) {
sharedPrefListenerBuilder.addStatement(
"if(%N.equals(%S)){(%L as %L).%L(%L.get%L(%S, ${placeHolder(annotatedVarType)}))}",
NameStore.Variable.SHARED_VALUE_KEY,
key,
NameStore.Variable.CLASS_VAR,
className,
annotatedMethod.simpleName.toString(),
NameStore.Variable.SHARED_PREF_VALUE,
annotatedVarType,
key,
defaultValue
)
}else{
sharedPrefListenerBuilder.addStatement("(%L as %L).%L(%L.all)",
NameStore.Variable.CLASS_VAR,
className,
annotatedMethod.simpleName.toString(),
NameStore.Variable.SHARED_PREF_VALUE
)
}

}

Expand Down Expand Up @@ -370,6 +389,23 @@ internal object ProcessorHelper {

// }
}

if (an == ReadAll::class.simpleName){
val readAllAnnotation = annotatedParam.getAnnotation(ReadAll::class.java)
if(readAllAnnotation != null){
try {
buildReadSharedPrefValueBuilder.addStatement(
"(%N as %L).%L = %N.all",
NameStore.Variable.CLASS_VAR,
className,
annotatedParam.simpleName,
NameStore.Variable.SHARED_PREF_VALUE
)
}catch (npe: NullPointerException){
error(npe)
}
}
}
}
}

Expand Down

0 comments on commit 3b10f9f

Please sign in to comment.