-
Notifications
You must be signed in to change notification settings - Fork 0
Feature - Add dynamic image (Coil, Glide, Picasso) #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TomasValenta
wants to merge
5
commits into
master
Choose a base branch
from
feature/dynamic-image
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
90205ca
Add base dynamic-image module. Add dynamic-image Coil implementation.
TomasValenta 8ce6c10
Add initial dynamic-image Glide and Picasso implementation.
TomasValenta 089fb90
Rename dynamic-image to dynamic-image-base. Improve obtaining uri ima…
TomasValenta f73aaed
Remove unused buildType block from Gradle.
TomasValenta 5a5d544
Add initial state of DynamicImageCoilTest.
TomasValenta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| apply plugin: 'com.android.library' | ||
| apply plugin: 'kotlin-android' | ||
| apply plugin: 'kotlin-kapt' | ||
|
|
||
| ext.bintrayPublishVersion = libDynamicImageCoilVersion | ||
| apply from: '../../bintray-publish-config.gradle' | ||
|
|
||
| android { | ||
| compileSdkVersion androidCompileSdkVersion | ||
|
|
||
| defaultConfig { | ||
| minSdkVersion androidMinSdkVersion | ||
| targetSdkVersion androidTargerSdkVersion | ||
| versionName libDynamicImageCoilVersion | ||
|
|
||
| testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
| consumerProguardFiles 'consumer-rules.pro' | ||
| } | ||
|
|
||
| buildTypes { | ||
| release { | ||
| minifyEnabled false | ||
|
TomasValenta marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| buildFeatures { | ||
| dataBinding = true | ||
| } | ||
|
|
||
| compileOptions { | ||
| sourceCompatibility JavaVersion.VERSION_1_8 | ||
| targetCompatibility JavaVersion.VERSION_1_8 | ||
| } | ||
|
|
||
| kotlinOptions { | ||
| jvmTarget = JavaVersion.VERSION_1_8.toString() | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
|
|
||
| api project(':dynamic:image') | ||
|
|
||
| implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion" | ||
| implementation "androidx.annotation:annotation:$annotationVersion" | ||
| implementation "androidx.core:core-ktx:$androidXCoreVersion" | ||
| implementation "com.twocoders.extensions:common:$commonExtensionsVersion" | ||
|
|
||
| implementation "io.coil-kt:coil:$coilVersion" | ||
|
|
||
| androidTestImplementation "androidx.test:runner:$androidTestRunnerVersion" | ||
| androidTestImplementation "androidx.test.ext:junit:$androidJunitVersion" | ||
|
|
||
| testImplementation "junit:junit:$junitVersion" | ||
| } | ||
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <manifest package="com.twocoders.dynamic.image.coil" /> |
122 changes: 122 additions & 0 deletions
122
dynamic/image-coil/src/main/java/com/twocoders/dynamic/image/coil/DynamicImage.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| package com.twocoders.dynamic.image.coil | ||
|
|
||
| import android.content.Context | ||
| import android.graphics.drawable.Drawable | ||
| import android.graphics.drawable.VectorDrawable | ||
| import android.os.Parcel | ||
| import android.os.Parcelable | ||
| import android.widget.ImageView | ||
| import androidx.annotation.ColorInt | ||
| import androidx.annotation.DrawableRes | ||
| import coil.Coil | ||
| import coil.ImageLoader | ||
| import coil.load | ||
| import coil.request.ImageRequest | ||
| import com.twocoders.dynamic.image.BaseDynamicImage | ||
| import com.twocoders.dynamic.image.component.UriComponent | ||
|
|
||
| /** | ||
| * | ||
| * Handy class which can be used to bind image data to views using them. | ||
|
TomasValenta marked this conversation as resolved.
Outdated
|
||
| * Data can be in [DrawableRes], [Drawable], or [UriComponent] format. | ||
| * | ||
| * Use one of [DynamicImage.from] creator methods to create the data | ||
| * and [DynamicImage.getDrawable] to obtain the final [Drawable] output. Or you can | ||
| * provide target [ImageView] class into the [DynamicImage.loadDrawableInto] method. | ||
| * | ||
| * This [DynamicImage] implementation uses [Coil], for other implementations please visit our GitHub. | ||
|
TomasValenta marked this conversation as resolved.
Outdated
|
||
| * | ||
| */ | ||
| @Suppress("unused", "MemberVisibilityCanBePrivate") | ||
| open class DynamicImage : BaseDynamicImage { | ||
|
|
||
| protected constructor( | ||
| @ColorInt imageRes: Int? = null, | ||
| imageDrawable: Drawable? = null, | ||
| imageUri: UriComponent? = null | ||
| ) : super(imageRes, imageDrawable, imageUri) | ||
|
|
||
| protected constructor(parcel: Parcel) : super(parcel) | ||
|
|
||
| companion object { | ||
|
|
||
| val EMPTY = DynamicImage() | ||
|
|
||
| @JvmStatic | ||
| fun from(@DrawableRes imageRes: Int) = DynamicImage(imageRes = imageRes) | ||
|
|
||
| @JvmStatic | ||
| fun from(imageDrawable: Drawable) = DynamicImage(imageDrawable = imageDrawable) | ||
|
|
||
| @JvmStatic | ||
| fun from(imageUri: UriComponent) = DynamicImage(imageUri = imageUri) | ||
|
|
||
| @JvmField | ||
| val CREATOR = object : Parcelable.Creator<DynamicImage> { | ||
| override fun createFromParcel(parcel: Parcel) = DynamicImage(parcel) | ||
| override fun newArray(size: Int): Array<DynamicImage?> = arrayOfNulls(size) | ||
| } | ||
| } | ||
|
|
||
| override suspend fun getDrawable(context: Context): Drawable? { | ||
| imageUri?.let { uri -> | ||
| val request = ImageRequest.Builder(context) | ||
| .data(uri) | ||
| .build() | ||
| return ImageLoader(context).execute(request).drawable | ||
| } | ||
|
|
||
| return super.getDrawable(context) | ||
| } | ||
|
|
||
| override fun getDrawable(context: Context, callback: (drawable: Drawable) -> Unit) { | ||
| imageUri?.let { imageUriComponent -> | ||
| val request = ImageRequest.Builder(context) | ||
| .data(imageUriComponent.image) | ||
| .target { callback(it) } | ||
| .build() | ||
| ImageLoader(context).enqueue(request) | ||
| } | ||
|
|
||
| super.getDrawable(context, callback) | ||
| } | ||
|
|
||
| override fun loadDrawableInto( | ||
| imageView: ImageView, | ||
| withCrossFade: Boolean | ||
| ) { | ||
| imageUri?.let { imageUriComponent -> | ||
| imageView.load(imageUriComponent.image) { | ||
| crossfade(withCrossFade) | ||
| imageUriComponent.placeholderImage?.let { placeholder(it) } | ||
| imageUriComponent.errorImage?.let { error(it) } | ||
| } | ||
| } | ||
|
|
||
| imageDrawable?.let { | ||
| // Note: Coil has currently issue with loading vectors | ||
| if (it is VectorDrawable) { | ||
| imageView.setImageDrawable(it) | ||
| } else { | ||
| imageView.load(it) { | ||
| crossfade(withCrossFade) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| imageRes?.let { | ||
| // We need to get drawable before setting it to imageView through Coil, | ||
| // there is a problem with background tint from theme attribute | ||
| getDrawable(imageView.context) { drawable -> | ||
| // Note: Coil has currently issue with loading vectors | ||
| if (drawable is VectorDrawable) { | ||
| imageView.setImageDrawable(drawable) | ||
| } else { | ||
| imageView.load(drawable) { | ||
| crossfade(withCrossFade) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
8 changes: 8 additions & 0 deletions
8
...c/image-coil/src/main/java/com/twocoders/dynamic/image/coil/DynamicImageBindingAdapter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.twocoders.dynamic.image.coil | ||
|
|
||
| import android.widget.ImageView | ||
| import androidx.databinding.BindingAdapter | ||
|
|
||
| @BindingAdapter(value = ["android:src", "loadWithCrossFade"], requireAll = false) | ||
| fun ImageView.loadDynamicImage(image: DynamicImage, withCrossFade: Boolean = false) = | ||
| image.loadDrawableInto(this, withCrossFade) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| apply plugin: 'com.android.library' | ||
| apply plugin: 'kotlin-android' | ||
| apply plugin: 'kotlin-kapt' | ||
|
|
||
| ext.bintrayPublishVersion = libDynamicImageGlideVersion | ||
| apply from: '../../bintray-publish-config.gradle' | ||
|
|
||
| android { | ||
| compileSdkVersion androidCompileSdkVersion | ||
|
|
||
| defaultConfig { | ||
| minSdkVersion androidMinSdkVersion | ||
| targetSdkVersion androidTargerSdkVersion | ||
| versionName libDynamicImageGlideVersion | ||
|
|
||
| testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
| consumerProguardFiles 'consumer-rules.pro' | ||
| } | ||
|
|
||
| buildTypes { | ||
| release { | ||
| minifyEnabled false | ||
| } | ||
| } | ||
|
|
||
| buildFeatures { | ||
| dataBinding = true | ||
| } | ||
|
|
||
| compileOptions { | ||
| sourceCompatibility JavaVersion.VERSION_1_8 | ||
| targetCompatibility JavaVersion.VERSION_1_8 | ||
| } | ||
|
|
||
| kotlinOptions { | ||
| jvmTarget = JavaVersion.VERSION_1_8.toString() | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
|
|
||
| api project(':dynamic:image') | ||
|
|
||
| implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion" | ||
| implementation "androidx.annotation:annotation:$annotationVersion" | ||
| implementation "androidx.core:core-ktx:$androidXCoreVersion" | ||
| implementation "com.twocoders.extensions:common:$commonExtensionsVersion" | ||
|
|
||
| implementation "com.github.bumptech.glide:glide:$glideVersion" | ||
|
|
||
| androidTestImplementation "androidx.test:runner:$androidTestRunnerVersion" | ||
| androidTestImplementation "androidx.test.ext:junit:$androidJunitVersion" | ||
|
|
||
| testImplementation "junit:junit:$junitVersion" | ||
| } |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <manifest package="com.twocoders.dynamic.image.glide" /> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.