Skip to content

Commit

Permalink
Handle displaying selected image
Browse files Browse the repository at this point in the history
  • Loading branch information
EslamElMeniawy committed Feb 1, 2020
1 parent 4199b51 commit 4e5517c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 37 deletions.
13 changes: 7 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,14 @@ dependencies {
// For HTTP logging.
implementation 'com.squareup.okhttp3:logging-interceptor:4.3.1'

// Picasso.
// Images add much-needed context and visual flair to Android applications.
// Picasso allows for hassle-free image loading in your application
// often in one line of code.
implementation 'com.squareup.picasso:picasso:2.71828'

// Shape Image View.
// Custom shaped android image view components.
implementation 'com.github.siyamed:android-shape-imageview:0.9.3'

// Glide.
// Glide is a fast and efficient open source media management and image loading framework for Android
// that wraps media decoding, memory and disk caching,
// and resource pooling into a simple and easy to use interface.
implementation "com.github.bumptech.glide:glide:$glide_version"
kapt "com.github.bumptech.glide:compiler:$glide_version"
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ class MainFragment : Fragment() {
if (resultCode == Activity.RESULT_OK) {
val queryImageUrl = if (data?.data != null) {
//Photo from gallery.
val imageUri = data.data
imageUri?.path ?: ""
//val imageUri = data.data
//imageUri?.path ?: ""
data.data.toString()
} else {
//Photo from camera.
getImagePath(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import elmeniawy.eslam.imagepicker.model.api.ApiRepo
import elmeniawy.eslam.imagepicker.utils.CAMERA_PERMISSION_REQUEST_CODE
import elmeniawy.eslam.imagepicker.utils.IMAGE_REQUEST_CODE
import timber.log.Timber
import java.io.File

/**
* MainViewModel
Expand All @@ -19,14 +18,14 @@ class MainViewModel(
private val apiRepository: ApiRepo,
private val moshi: Moshi
) : ViewModel() {
val imageUri: MutableLiveData<File?> = MutableLiveData()
val imageUri: MutableLiveData<String> = MutableLiveData()
val checkCameraPermission: MutableLiveData<Boolean> = MutableLiveData()
val requestCameraPermission: MutableLiveData<Boolean> = MutableLiveData()
val showPicker: MutableLiveData<Boolean> = MutableLiveData()
val errorMessageId: MutableLiveData<Int> = MutableLiveData()

init {
imageUri.value = null
imageUri.value = ""
}

fun addClicked() {
Expand Down Expand Up @@ -57,7 +56,7 @@ class MainViewModel(
when (requestCode) {
IMAGE_REQUEST_CODE -> {
Timber.d("ReturnedPath: $queryImageUrl")
//imageUri.value = file
imageUri.value = queryImageUrl
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package elmeniawy.eslam.imagepicker.utils

import android.graphics.drawable.Drawable
import android.net.Uri
import android.graphics.Bitmap
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.BindingAdapter
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.squareup.picasso.Callback
import com.squareup.picasso.Picasso
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import elmeniawy.eslam.imagepicker.R
import elmeniawy.eslam.imagepicker.utils.extension.getParentActivity
import timber.log.Timber
import java.io.File

/**
* BindingAdapters
Expand All @@ -29,29 +31,49 @@ import java.io.File
)
fun bindingAdapterIV(
view: ImageView,
imageUri: MutableLiveData<File?>?
imageUri: MutableLiveData<String>?
) {
val parentActivity: AppCompatActivity? = view.getParentActivity()

parentActivity?.let {
imageUri?.let {
imageUri.observe(parentActivity, Observer { value ->
// if (value==null) {
// view.setImageResource(R.drawable.placeholder)
// } else {
// //view.setImageDrawable(Drawable.createFromPath(value))
// //view.setImageURI(Uri.fromFile(value))
// //view.setImageDrawable(Drawable.createFromPath(value.absolutePath))
// Picasso.get().load(value).into(view, object : Callback {
// override fun onSuccess() {
// Timber.v("Successfully loaded image: $value")
// }
//
// override fun onError(e: Exception?) {
// Timber.w(e, "Failed to load image: $value")
// }
// })
// }
value?.let {
if (value.isNullOrBlank()) {
view.setImageResource(R.drawable.placeholder)
} else {
Glide.with(view.context)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder)
.load(value)
.listener(object : RequestListener<Bitmap> {
override fun onResourceReady(
resource: Bitmap?,
model: Any?,
target: Target<Bitmap>?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
Timber.v("Successfully loaded image: %s", value)
return false
}

override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Bitmap>?,
isFirstResource: Boolean
): Boolean {
Timber.w(e, "Failed to load image: %s", value)
return false
}
})
.into(view)
}
}
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ private fun getImageFile(context: Context?): File? {

val file = File(folder, "Image_Tmp.jpg")

if (file.exists()) {
file.delete()
}
// if (file.exists()) {
// file.delete()
// }

Timber.d("ImageFile: $file")
file.createNewFile()
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ buildscript {
ext.navigation_version = '1.0.0'
ext.retrofit_version = '2.7.1'
ext.moshi_version = '1.9.2'
ext.glide_version = '4.11.0'

repositories {
google()
Expand Down

0 comments on commit 4e5517c

Please sign in to comment.