Skip to content

Commit 3a553e0

Browse files
author
crypticminds
committed
[Parent annotation] Added an annotation that will help bind and load data to nested views
1 parent db9ad6e commit 3a553e0

File tree

5 files changed

+96
-36
lines changed

5 files changed

+96
-36
lines changed

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<img src="https://i.imgur.com/rEE8hUO.jpg"/>
55
</p>
66

7-
**A lightweight caching library for android**
7+
**A lightweight data loading and caching library for android**
88

99
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) ![Downloads](https://jitpack.io/v/crypticminds/ColdStorage/month.svg) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/946075aa2cc14be3af73eb8a9fc2352b)](https://www.codacy.com/manual/crypticminds/ColdStorage?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=crypticminds/ColdStorage&amp;utm_campaign=Badge_Grade) [![Build Status](https://travis-ci.com/crypticminds/ColdStorage.svg?branch=master)](https://travis-ci.com/crypticminds/ColdStorage) [![Gitter](https://badges.gitter.im/ColdStorageCache/community.svg)](https://gitter.im/ColdStorageCache/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
1010

@@ -21,7 +21,7 @@
2121
* [Caching in general](https://medium.com/@crypticmindscom_5258/caching-made-easy-with-kotlin-part-1-53433c756978)
2222
* [@Refrigerate annotation usage](https://medium.com/@crypticmindscom_5258/caching-made-easy-in-android-with-kotlin-part-2-61bb476063b4)
2323
* [@Freeze annotation usage](https://medium.com/@crypticmindscom_5258/caching-made-easy-on-android-with-kotlin-part-3-3d4cfcb57df0)
24-
* [@LoadImage anniration usage](https://medium.com/@crypticmindscom_5258/caching-made-easy-on-android-with-kotlin-part-4-18e7b066e9c2)
24+
* [@LoadImage annotation usage](https://medium.com/@crypticmindscom_5258/caching-made-easy-on-android-with-kotlin-part-4-18e7b066e9c2)
2525
# Setup
2626

2727
* Add kotlin-kapt gradle plugin to **app build.gradle** file
@@ -30,9 +30,9 @@
3030

3131
* Add the dependencies
3232

33-
implementation "com.github.crypticminds.ColdStorage:coldstoragecache:4.1.0"
34-
kapt "com.github.crypticminds.ColdStorage:coldstoragecompiler:4.1.0"
35-
implementation "com.github.crypticminds.ColdStorage:coldstorageannotation:4.1.0"
33+
implementation "com.github.crypticminds.ColdStorage:coldstoragecache:4.1.1"
34+
kapt "com.github.crypticminds.ColdStorage:coldstoragecompiler:4.1.1"
35+
implementation "com.github.crypticminds.ColdStorage:coldstorageannotation:4.1.1"
3636

3737
***Check the latest release to get the newest features.***
3838

@@ -101,6 +101,29 @@ In an activity, the method should be called after setContentView and in a fragem
101101

102102
Currently the cache can only be bound to an Activity , fragment or view.
103103

104+
## @Parent Annotation
105+
106+
An annotation that helps binding a nested view to a resource id.
107+
Suppose you have a layout ** layout_1.xml ** which contains an ImageView. You have added this layout in your main layout using the
108+
** <include/> ** tag.You can now use @LoadImage annotation on the ImageView by :
109+
110+
* Provide an id to the include tag
111+
112+
```xml
113+
<include android:id="@+id/my_included_layout"
114+
......
115+
other attributes
116+
/>
117+
```
118+
119+
* Use Parent annotation along with LoadImage annotation
120+
121+
```kotlin
122+
@Parent(R.id.my_included_layout)
123+
@LoadImage(R.id.my_nested_image_view,"http://url.jpg"
124+
lateinit var childImageView : ImageView
125+
```
126+
104127
## @Freeze Annotation
105128

106129
Annotate your class using the freeze annotation to apply caching logic on top of all the public methods present in the class.

app/src/main/res/layout/content_main.xml

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.arcane.coldstorageannotation
2+
3+
/**
4+
* An annotation that can be used to declare the parent of a view.
5+
* This annotation is used in conjunction with other data loading and
6+
* caching annotation such as @LoadImage.
7+
*
8+
* Usage
9+
*
10+
* @LoadImage(R.id.my_image_view,"myurl")
11+
* @Parent(R.id.my_custom_view)
12+
* lateinit var myImageViewInsideCustomView : ImageView
13+
*
14+
* This will load the image from "myrul" into the "my_image_view" inside "my_custom_view"
15+
*
16+
* @param resourceId the resource id of the parent view.
17+
*
18+
* @author Anurag.
19+
*/
20+
@Retention(AnnotationRetention.SOURCE)
21+
@Target(AnnotationTarget.FIELD)
22+
annotation class Parent(val resourceId: Int)

coldstoragecache/src/main/java/com/arcane/coldstoragecache/helper/BindHelper.kt

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,62 @@ class BindHelper {
1515

1616
companion object {
1717

18+
/**
19+
* Binds an ImageView present inside a different view to the given resource id.g
20+
*/
21+
fun bindViewToResource(anyObject: Any, parentView: Int, resourceId: Int): ImageView {
22+
23+
return when (anyObject) {
24+
is Activity -> {
25+
bindView(bindView(anyObject, parentView), resourceId) as ImageView
26+
}
27+
is View -> {
28+
bindView(bindView(anyObject, parentView), resourceId) as ImageView
29+
}
30+
is Fragment -> {
31+
bindView(bindView(anyObject, parentView), resourceId) as ImageView
32+
}
33+
else -> {
34+
throw Exception("Only views , activities and fragments are supported for the annotation")
35+
}
36+
}
37+
38+
}
39+
1840
/**
1941
* Method that will bind views to the resource ids.
2042
*/
2143
fun bindViewToResource(anyObject: Any, resourceId: Int): ImageView {
2244
return when (anyObject) {
2345
is Activity -> {
24-
bindView(anyObject, resourceId)
46+
bindView(anyObject, resourceId) as ImageView
2547
}
2648
is View -> {
27-
bindView(anyObject, resourceId)
49+
bindView(anyObject, resourceId) as ImageView
2850
}
2951
is Fragment -> {
30-
bindView(anyObject, resourceId)
52+
bindView(anyObject, resourceId) as ImageView
3153
}
3254
else -> {
3355
throw Exception("Only views , activities and fragments are supported for the annotation")
3456
}
3557
}
3658
}
3759

38-
private fun bindView(fragment: Fragment, resourceId: Int): ImageView {
60+
private fun bindView(fragment: Fragment, resourceId: Int): View {
3961
if (fragment.view != null) {
4062
return fragment.view!!.findViewById(resourceId)
4163
} else {
4264
throw Exception("Unable to get the root view of the fragment ${fragment.javaClass.simpleName}")
4365
}
4466
}
4567

46-
private fun bindView(activity: Activity, resourceId: Int): ImageView {
68+
private fun bindView(activity: Activity, resourceId: Int): View {
4769
return activity.window.decorView.findViewById(resourceId)
4870
}
4971

5072

51-
private fun bindView(view: View, resourceId: Int): ImageView {
73+
private fun bindView(view: View, resourceId: Int): View {
5274
return view.findViewById(resourceId)
5375
}
5476
}

coldstoragecompiler/src/main/java/com/arcane/coldstoragecompiler/LoadImageProcessor.kt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.arcane.coldstoragecompiler
22

33
import com.arcane.coldstorageannotation.LoadImage
4+
import com.arcane.coldstorageannotation.Parent
45
import com.arcane.coldstoragecompiler.helper.CodeGenerationHelper
56
import com.google.auto.service.AutoService
67
import com.squareup.kotlinpoet.*
@@ -183,12 +184,24 @@ class LoadImageProcessor : AbstractProcessor() {
183184
// .beginControlFlow("$activityName.runOnUiThread ")
184185
parameterList.forEach { parameter ->
185186
val loadImage = parameter.getAnnotation(LoadImage::class.java)
187+
val parent = parameter.getAnnotation(Parent::class.java)
188+
189+
if (parent != null) {
190+
builder
191+
.addStatement(
192+
"$target.${parameter.simpleName} = " +
193+
"%T.bindViewToResource($target ," +
194+
"${parent.resourceId}," +
195+
"${loadImage.imageViewResourceId})", bindHelper)
196+
} else {
197+
builder
198+
.addStatement(
199+
"$target.${parameter.simpleName} = " +
200+
"%T.bindViewToResource($target ," +
201+
" ${loadImage.imageViewResourceId})", bindHelper
202+
)
203+
}
186204
builder
187-
.addStatement(
188-
"$target.${parameter.simpleName} = " +
189-
"%T.bindViewToResource($target ," +
190-
" ${loadImage.imageViewResourceId})", bindHelper
191-
)
192205
.addStatement(
193206
"map.put($target.${parameter.simpleName}," +
194207
"LoadImageConfig(\"${URLDecoder.decode(loadImage.url, "UTF-8")}\"," +

0 commit comments

Comments
 (0)