diff --git a/README.md b/README.md index 09a2f52..44af1f0 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@

-**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&utm_medium=referral&utm_content=crypticminds/ColdStorage&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) @@ -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 @@ -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.*** @@ -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 +** ** tag.You can now use @LoadImage annotation on the ImageView by : + +* Provide an id to the include tag + + ```xml + + ``` + +* 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. diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml deleted file mode 100644 index 926c02e..0000000 --- a/app/src/main/res/layout/content_main.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/coldstorageannotation/src/main/java/com/arcane/coldstorageannotation/Parent.kt b/coldstorageannotation/src/main/java/com/arcane/coldstorageannotation/Parent.kt new file mode 100644 index 0000000..9e855be --- /dev/null +++ b/coldstorageannotation/src/main/java/com/arcane/coldstorageannotation/Parent.kt @@ -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) \ No newline at end of file diff --git a/coldstoragecache/src/main/java/com/arcane/coldstoragecache/helper/BindHelper.kt b/coldstoragecache/src/main/java/com/arcane/coldstoragecache/helper/BindHelper.kt index 3a7bfe7..a50cb1b 100644 --- a/coldstoragecache/src/main/java/com/arcane/coldstoragecache/helper/BindHelper.kt +++ b/coldstoragecache/src/main/java/com/arcane/coldstoragecache/helper/BindHelper.kt @@ -15,19 +15,41 @@ 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") @@ -35,7 +57,7 @@ class BindHelper { } } - 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 { @@ -43,12 +65,12 @@ class BindHelper { } } - 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) } } diff --git a/coldstoragecompiler/src/main/java/com/arcane/coldstoragecompiler/LoadImageProcessor.kt b/coldstoragecompiler/src/main/java/com/arcane/coldstoragecompiler/LoadImageProcessor.kt index 22b1c59..a8c1d25 100644 --- a/coldstoragecompiler/src/main/java/com/arcane/coldstoragecompiler/LoadImageProcessor.kt +++ b/coldstoragecompiler/src/main/java/com/arcane/coldstoragecompiler/LoadImageProcessor.kt @@ -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.* @@ -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")}\"," +