Skip to content

Commit 74bf49c

Browse files
committed
fixing listeners
1 parent 2b79489 commit 74bf49c

File tree

8 files changed

+245
-26
lines changed

8 files changed

+245
-26
lines changed

app/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ dependencies {
3131
implementation project(':binder')
3232
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
3333

34+
// Optional -- Mockito framework
35+
testImplementation 'org.mockito:mockito-core:2.11.0'
36+
3437
implementation 'io.reactivex.rxjava2:rxjava:2.2.6'
3538
implementation fileTree(dir: 'libs', include: ['*.jar'])
3639
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
@@ -39,4 +42,5 @@ dependencies {
3942
testImplementation 'junit:junit:4.12'
4043
androidTestImplementation 'com.android.support.test:runner:1.0.2'
4144
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
45+
testImplementation "org.robolectric:robolectric:4.2"
4246
}

app/src/main/java/devmike/jade/com/jadesharedpreference/MainActivity.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package devmike.jade.com.jadesharedpreference
33
import android.support.v7.app.AppCompatActivity
44
import android.os.Bundle
55
import android.preference.PreferenceManager
6+
import android.support.annotation.VisibleForTesting
67
import android.util.Log
78
import android.view.View
89
import devmike.jade.com.annotations.SettingsPreference
@@ -14,7 +15,6 @@ import kotlinx.android.synthetic.main.activity_main.*
1415

1516
class MainActivity @SharedPref("setting") constructor(): AppCompatActivity() {
1617

17-
1818
private lateinit var jsp :JadeSharedPreference
1919

2020
//Read string from JadeSharedPreference
@@ -40,15 +40,11 @@ class MainActivity @SharedPref("setting") constructor(): AppCompatActivity() {
4040
@ReadAll
4141
lateinit var mMapAll: Map<String, *>
4242

43-
44-
private val settingPref = PreferenceManager.getDefaultSharedPreferences(this)
45-
46-
4743
//@SharedPref("key")
4844
override fun onCreate(savedInstanceState: Bundle?) {
4945
super.onCreate(savedInstanceState)
5046
setContentView(R.layout.activity_main)
51-
jsp =JadeSharedPreference.plug(this, this)
47+
jsp =JadeSharedPreference.apply(this, this)
5248

5349

5450
read_1.text = mString
@@ -67,7 +63,6 @@ class MainActivity @SharedPref("setting") constructor(): AppCompatActivity() {
6763
//vClass.insertValue("hello", "World Of Django")
6864

6965
Log.d("MainActivity", mMapAll.toString())
70-
7166
}
7267

7368
fun btnTestOne(v: View){
@@ -119,4 +114,10 @@ class MainActivity @SharedPref("setting") constructor(): AppCompatActivity() {
119114
fun readAllItems(allItems: MutableMap<String, *>){
120115
Log.d("MainActivity", allItems.toString())
121116
}
117+
118+
@VisibleForTesting
119+
fun writeString(key: String){
120+
jsp.insert(key, TestConstants.FAKE_TEST_STRING)
121+
}
122+
122123
}

app/src/main/java/devmike/jade/com/jadesharedpreference/TestClass.kt

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,35 @@ import android.content.Context
44
import devmike.jade.com.annotations.SettingsPreference
55
import devmike.jade.com.annotations.SharedPref
66
import devmike.jade.com.annotations.preference.ReadPrefString
7-
import devmike.jade.com.annotations.sharedpreference.ReadString
87
import devmike.jade.com.binder.JadeSharedPreference
8+
import java.util.concurrent.CountDownLatch
99

10-
class TestClass @SettingsPreference constructor(context: Context){
10+
open class TestClass @SettingsPreference @SharedPref constructor(val context: Context){
1111

12-
var jsp: JadeSharedPreference = JadeSharedPreference.plug(this, context)
12+
lateinit var jsp: JadeSharedPreference
13+
14+
val countDown = CountDownLatch(1)
1315

1416
@ReadPrefString("string")
15-
var TEST_KEY: String ="test_key"
17+
lateinit var mString: String
1618

17-
fun init(context: Context){
18-
jsp.plug(this, context)
19+
fun init(){
20+
jsp =JadeSharedPreference.preference(this, context)
1921
}
2022

2123

22-
public fun writeTest(value: Int){
23-
jsp.insert("test", 200)
24+
public fun writeTest(value: String){
25+
jsp.insert("test", value)
26+
//countDown.await()
2427
}
2528

29+
@ReadPrefString("string")
30+
fun listenToStringChanges(value: String){
31+
// countDown.countDown()
32+
}
33+
34+
public fun getString(): String{
35+
return mString
36+
}
2637

2738
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package devmike.jade.com.jadesharedpreference
2+
3+
import android.support.annotation.VisibleForTesting
4+
5+
/**
6+
* Constant for testing
7+
*/
8+
object TestConstants {
9+
10+
@VisibleForTesting
11+
const val FAKE_TEST_STRING ="Hello World"
12+
13+
@VisibleForTesting
14+
const val FAKE_TEST_INT =200
15+
16+
17+
@VisibleForTesting
18+
const val FAKE_TEST_FLOAT =20f
19+
20+
21+
@VisibleForTesting
22+
const val FAKE_TEST_LONG =220
23+
24+
25+
@VisibleForTesting
26+
val FAKE_TEST_SET = arrayOf("Hello", "World")
27+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package devmike.jade.com.jadesharedpreference
2+
3+
import android.app.Instrumentation
4+
import android.content.Context
5+
import junit.framework.TestCase.assertEquals
6+
import org.junit.Before
7+
import org.junit.Test
8+
import org.junit.runner.RunWith
9+
import org.junit.runners.JUnit4
10+
import org.mockito.Mockito.mock
11+
import org.robolectric.Robolectric
12+
13+
@RunWith(JUnit4::class)
14+
class SettingsPreferenceTest {
15+
16+
17+
lateinit var testClass : TestClass
18+
19+
lateinit var context: Context
20+
21+
private var TEST_STR ="Testing this annotation"
22+
23+
@Before
24+
fun init(){
25+
context = mock(Context::class.java)
26+
}
27+
28+
@Test
29+
fun testWrite(){
30+
val testClas = TestClass(context)
31+
testClas.init();
32+
testClas.writeTest(TEST_STR)
33+
}
34+
35+
@Test
36+
fun readString(){
37+
assertEquals(testClass.getString(), TEST_STR)
38+
}
39+
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package devmike.jade.com.jadesharedpreference
2+
3+
import org.junit.Assert.assertNotEquals
4+
import org.junit.Assert.assertThat
5+
import org.junit.Before
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
import org.robolectric.Robolectric
9+
import org.robolectric.RobolectricTestRunner
10+
import org.robolectric.annotation.Config
11+
12+
@Config(manifest= Config.NONE)
13+
@RunWith(RobolectricTestRunner::class)
14+
class SharedPreferenceTest {
15+
16+
lateinit var mainActivity: MainActivity
17+
18+
@Before
19+
fun init(){
20+
mainActivity = Robolectric.buildActivity(MainActivity::class.java).get()
21+
}
22+
23+
@Test
24+
fun testread(){
25+
assertNotEquals(mainActivity.mFloat, 0)
26+
}
27+
}

binder/src/main/java/devmike/jade/com/binder/JadeSharedPreference.kt

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,82 @@ package devmike.jade.com.binder
33
import android.app.Activity
44
import android.content.Context
55
import android.util.Log
6+
import devmike.jade.com.annotations.SettingsPreference
7+
import devmike.jade.com.annotations.SharedPref
68
import java.lang.Error
9+
import java.lang.Exception
710
import java.lang.IllegalArgumentException
811
import java.lang.NullPointerException
12+
import java.lang.annotation.ElementType
13+
import java.lang.reflect.Constructor
914
import java.lang.reflect.InvocationTargetException
15+
import java.lang.reflect.Method
16+
import kotlin.reflect.KFunction
17+
import kotlin.reflect.full.findAnnotation
18+
import kotlin.reflect.full.memberProperties
19+
import kotlin.reflect.full.primaryConstructor
20+
import kotlin.reflect.jvm.javaField
1021

1122
public object JadeSharedPreference {
1223

1324
private var bindingClassNewInstance: Any? =null
1425
private var bindingClass: Class<*>? =null
26+
private var method: Method? =null
1527

16-
fun <T : Any> plug(targetClas: T, context: Context) : JadeSharedPreference{
17-
val className ="${targetClas::class.java.`package`.name}.JSP${targetClas::class.simpleName}"
18-
this.bindingClass = targetClas.javaClass.classLoader?.loadClass( className)
19-
val method = bindingClass?.getMethod("plug", Context::class.java, Any::class.java)
28+
fun <T : Any> preference(targetClass: T, context: Context) : JadeSharedPreference{
29+
30+
val className ="${targetClass::class.java.`package`.name}.JSP_Preference${targetClass::class.simpleName}"
31+
32+
// val className ="${targetClass::class.java.`package`.name}JSP_Preference${targetClass::class.simpleName}"
33+
34+
try {
35+
36+
//Wire up plug() method to generated SharedPreference file
37+
this.bindingClass = targetClass.javaClass.classLoader?.loadClass( className)
38+
method = bindingClass?.getMethod("plug", Context::class.java, Any::class.java)
39+
40+
this.bindingClassNewInstance =bindingClass?.newInstance()
41+
method?.invoke(bindingClassNewInstance, context, targetClass)
42+
43+
}catch (ite: Exception){
44+
if(ite is NoSuchMethodException) {
45+
throw NullPointerException("${method?.toString()} does not exist")
46+
}else if (ite is ClassNotFoundException){
47+
throw ClassNotFoundException("$className was not generated. Please fix the error and try again!")
48+
}
49+
}
50+
51+
return this
52+
}
53+
54+
//Access point for SharedPreference
55+
fun <T : Any> apply(targetClass: T, context: Context) : JadeSharedPreference{
56+
57+
val className ="${targetClass::class.java.`package`.name}.JSP${targetClass::class.simpleName}"
58+
59+
// val className ="${targetClass::class.java.`package`.name}JSP_Preference${targetClass::class.simpleName}"
2060

21-
this.bindingClassNewInstance =bindingClass?.newInstance()
2261
try {
23-
method?.invoke(bindingClassNewInstance, context, targetClas)
2462

25-
}catch (ite: InvocationTargetException){
63+
//Wire up plug() method to generated SharedPreference file
64+
this.bindingClass = targetClass.javaClass.classLoader?.loadClass( className)
65+
method = bindingClass?.getMethod("plug", Context::class.java, Any::class.java)
66+
67+
this.bindingClassNewInstance =bindingClass?.newInstance()
68+
method?.invoke(bindingClassNewInstance, context, targetClass)
69+
70+
}catch (ite: Exception){
2671
if(ite is NoSuchMethodException) {
27-
throw NullPointerException("${method.toString()} does not exist")
72+
throw NullPointerException("${method?.toString()} does not exist")
2873
}else if (ite is ClassNotFoundException){
29-
throw NullPointerException("$className was not generated. Please fix the error and try again!")
74+
throw ClassNotFoundException("$className was not generated. Please fix the error and try again!")
3075
}
3176
}
3277

3378
return this
3479
}
3580

81+
3682
fun <T : Any> insert(key: String, value: T){
3783
if (value is String) {
3884
val insertStringFunc = bindingClass?.getMethod("insertValue", String::class.java,

compiler/src/main/java/devmike/jade/com/compiler/sharedpreference/ProcessorHelper.kt

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class ProcessorHelper {
137137
.addStatement("%N()", NameStore.Method.SHARED_PREF_READ_VALUE)
138138
// .addComment("Read the saved value from the SharedPreference")
139139

140-
140+
/*
141141
142142
if (readFloatAn != null) {
143143
annotationBuilder(
@@ -192,7 +192,70 @@ class ProcessorHelper {
192192
)
193193
}
194194
195-
195+
*/
196+
197+
198+
for (annotatedMethod in ElementFilter.methodsIn(typeElement.enclosedElements)) {
199+
if (annotatedMethod != null) {
200+
201+
//Add all the annotation to a set
202+
203+
//Loop through all the annotated methods
204+
205+
if (readFloatAn != null) {
206+
annotationBuilder(
207+
className,
208+
annotatedMethod, Float::class.simpleName, readFloatAn.defaultValue,
209+
readFloatAn.key
210+
)
211+
}
212+
213+
if (readStringAn != null) {
214+
annotationBuilder(
215+
className,
216+
annotatedMethod,
217+
String::class.java.simpleName,
218+
readStringAn.defaultValue,
219+
readStringAn.key
220+
)
221+
}
222+
223+
if (readStringSetAn != null) {
224+
annotationBuilder(
225+
className, annotatedMethod,
226+
NameStore.Types.STRINGSET,
227+
"mutableSetOf(\"\")",
228+
readStringSetAn.key
229+
)
230+
231+
}
232+
233+
if (readIntAn != null) {
234+
annotationBuilder(
235+
className, annotatedMethod, Int::class.simpleName,
236+
readIntAn.defaultValue,
237+
readIntAn.key
238+
)
239+
}
240+
241+
if (readLongAn != null) {
242+
243+
annotationBuilder(
244+
className, annotatedMethod,
245+
LONG.simpleName, readLongAn.defaultValue, readLongAn.key
246+
)
247+
}
248+
249+
if (readAll != null) {
250+
annotationBuilder(
251+
className, annotatedMethod,
252+
MutableMap::class.java.name,
253+
null,
254+
NameStore.Variable.READ_ALL
255+
)
256+
}
257+
}
258+
}
196259

197260
val buildReadSharedPrefValueBuilder = FunSpec.builder(NameStore.Method.SHARED_PREF_READ_VALUE)
198261
.addModifiers(KModifier.PRIVATE)

0 commit comments

Comments
 (0)