Skip to content

Commit

Permalink
[Parent annotation] Added an annotation that will help bind and load …
Browse files Browse the repository at this point in the history
…data to nested views
  • Loading branch information
crypticminds committed Feb 19, 2020
1 parent db9ad6e commit 3a553e0
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 36 deletions.
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<img src="https://i.imgur.com/rEE8hUO.jpg"/>
</p>

**A lightweight caching library for android**
**A lightweight data loading and caching library for android**

[![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)

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

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

* Add the dependencies

implementation "com.github.crypticminds.ColdStorage:coldstoragecache:4.1.0"
kapt "com.github.crypticminds.ColdStorage:coldstoragecompiler:4.1.0"
implementation "com.github.crypticminds.ColdStorage:coldstorageannotation:4.1.0"
implementation "com.github.crypticminds.ColdStorage:coldstoragecache:4.1.1"
kapt "com.github.crypticminds.ColdStorage:coldstoragecompiler:4.1.1"
implementation "com.github.crypticminds.ColdStorage:coldstorageannotation:4.1.1"

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

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

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

## @Parent Annotation

An annotation that helps binding a nested view to a resource id.
Suppose you have a layout ** layout_1.xml ** which contains an ImageView. You have added this layout in your main layout using the
** <include/> ** tag.You can now use @LoadImage annotation on the ImageView by :

* Provide an id to the include tag

```xml
<include android:id="@+id/my_included_layout"
......
other attributes
/>
```

* Use Parent annotation along with LoadImage annotation

```kotlin
@Parent(R.id.my_included_layout)
@LoadImage(R.id.my_nested_image_view,"http://url.jpg"
lateinit var childImageView : ImageView
```

## @Freeze Annotation

Annotate your class using the freeze annotation to apply caching logic on top of all the public methods present in the class.
Expand Down
20 changes: 0 additions & 20 deletions app/src/main/res/layout/content_main.xml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.arcane.coldstorageannotation

/**
* An annotation that can be used to declare the parent of a view.
* This annotation is used in conjunction with other data loading and
* caching annotation such as @LoadImage.
*
* Usage
*
* @LoadImage(R.id.my_image_view,"myurl")
* @Parent(R.id.my_custom_view)
* lateinit var myImageViewInsideCustomView : ImageView
*
* This will load the image from "myrul" into the "my_image_view" inside "my_custom_view"
*
* @param resourceId the resource id of the parent view.
*
* @author Anurag.
*/
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.FIELD)
annotation class Parent(val resourceId: Int)
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,62 @@ class BindHelper {

companion object {

/**
* Binds an ImageView present inside a different view to the given resource id.g
*/
fun bindViewToResource(anyObject: Any, parentView: Int, resourceId: Int): ImageView {

return when (anyObject) {
is Activity -> {
bindView(bindView(anyObject, parentView), resourceId) as ImageView
}
is View -> {
bindView(bindView(anyObject, parentView), resourceId) as ImageView
}
is Fragment -> {
bindView(bindView(anyObject, parentView), resourceId) as ImageView
}
else -> {
throw Exception("Only views , activities and fragments are supported for the annotation")
}
}

}

/**
* Method that will bind views to the resource ids.
*/
fun bindViewToResource(anyObject: Any, resourceId: Int): ImageView {
return when (anyObject) {
is Activity -> {
bindView(anyObject, resourceId)
bindView(anyObject, resourceId) as ImageView
}
is View -> {
bindView(anyObject, resourceId)
bindView(anyObject, resourceId) as ImageView
}
is Fragment -> {
bindView(anyObject, resourceId)
bindView(anyObject, resourceId) as ImageView
}
else -> {
throw Exception("Only views , activities and fragments are supported for the annotation")
}
}
}

private fun bindView(fragment: Fragment, resourceId: Int): ImageView {
private fun bindView(fragment: Fragment, resourceId: Int): View {
if (fragment.view != null) {
return fragment.view!!.findViewById(resourceId)
} else {
throw Exception("Unable to get the root view of the fragment ${fragment.javaClass.simpleName}")
}
}

private fun bindView(activity: Activity, resourceId: Int): ImageView {
private fun bindView(activity: Activity, resourceId: Int): View {
return activity.window.decorView.findViewById(resourceId)
}


private fun bindView(view: View, resourceId: Int): ImageView {
private fun bindView(view: View, resourceId: Int): View {
return view.findViewById(resourceId)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.arcane.coldstoragecompiler

import com.arcane.coldstorageannotation.LoadImage
import com.arcane.coldstorageannotation.Parent
import com.arcane.coldstoragecompiler.helper.CodeGenerationHelper
import com.google.auto.service.AutoService
import com.squareup.kotlinpoet.*
Expand Down Expand Up @@ -183,12 +184,24 @@ class LoadImageProcessor : AbstractProcessor() {
// .beginControlFlow("$activityName.runOnUiThread ")
parameterList.forEach { parameter ->
val loadImage = parameter.getAnnotation(LoadImage::class.java)
val parent = parameter.getAnnotation(Parent::class.java)

if (parent != null) {
builder
.addStatement(
"$target.${parameter.simpleName} = " +
"%T.bindViewToResource($target ," +
"${parent.resourceId}," +
"${loadImage.imageViewResourceId})", bindHelper)
} else {
builder
.addStatement(
"$target.${parameter.simpleName} = " +
"%T.bindViewToResource($target ," +
" ${loadImage.imageViewResourceId})", bindHelper
)
}
builder
.addStatement(
"$target.${parameter.simpleName} = " +
"%T.bindViewToResource($target ," +
" ${loadImage.imageViewResourceId})", bindHelper
)
.addStatement(
"map.put($target.${parameter.simpleName}," +
"LoadImageConfig(\"${URLDecoder.decode(loadImage.url, "UTF-8")}\"," +
Expand Down

0 comments on commit 3a553e0

Please sign in to comment.