Unverified Commit c448359d authored by Lucio Maciel's avatar Lucio Maciel Committed by GitHub

Merge pull request #1384 from RocketChat/feature/compress-image

[NEW] Compress image
parents 18e06ae0 590aedde
......@@ -5,26 +5,32 @@ import android.net.Uri
import chat.rocket.android.util.extensions.*
import javax.inject.Inject
class UriInteractor @Inject constructor(private val context: Context) {
/**
* Gets the file name from an [Uri].
* Returns the file name from the [Uri].
*/
fun getFileName(uri: Uri): String? = uri.getFileName(context)
/**
* Gets the MimeType of an [Uri]
* Returns the MimeType from the [Uri].
*/
fun getMimeType(uri: Uri): String = uri.getMimeType(context)
/**
* Gets the real path of an [Uri]
* Returns the file size from the [Uri].
*/
fun getRealPath(uri: Uri): String? = uri.getRealPathFromURI(context)
fun getFileSize(uri: Uri) = uri.getFileSize(context)
/**
* Returns the InputStream from the [Uri].
*/
fun getInputStream(uri: Uri) = uri.getInputStream(context)
/**
* Returns the Bitmap from the [Uri].
*
* Note: It should be an image.
*/
fun getBitmap(uri: Uri) = uri.getBitmpap(context)
}
\ No newline at end of file
......@@ -8,11 +8,11 @@ import android.content.Context
import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator
import androidx.fragment.app.Fragment
import android.view.View
import android.view.ViewAnimationUtils
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import androidx.fragment.app.Fragment
fun View.rotateBy(value: Float, duration: Long = 100) {
animate()
......@@ -31,12 +31,12 @@ fun View.fadeIn(startValue: Float = 0f, finishValue: Float = 1f, duration: Long
.alpha(startValue)
.setDuration(duration / 2)
.setInterpolator(DecelerateInterpolator())
.withEndAction({
.withEndAction {
animate()
.alpha(finishValue)
.setDuration(duration / 2)
.setInterpolator(AccelerateInterpolator()).start()
}).start()
}.start()
setVisible(true)
}
......@@ -51,18 +51,25 @@ fun View.fadeOut(startValue: Float = 1f, finishValue: Float = 0f, duration: Long
.alpha(startValue)
.setDuration(duration)
.setInterpolator(DecelerateInterpolator())
.withEndAction({
.withEndAction {
animate()
.alpha(finishValue)
.setDuration(duration)
.setInterpolator(AccelerateInterpolator()).start()
}).start()
}.start()
setVisible(false)
}
fun View.circularRevealOrUnreveal(centerX: Int, centerY: Int, startRadius: Float, endRadius: Float, duration: Long = 200) {
val anim = ViewAnimationUtils.createCircularReveal(this, centerX, centerY, startRadius, endRadius)
fun View.circularRevealOrUnreveal(
centerX: Int,
centerY: Int,
startRadius: Float,
endRadius: Float,
duration: Long = 200
) {
val anim =
ViewAnimationUtils.createCircularReveal(this, centerX, centerY, startRadius, endRadius)
anim.duration = duration
if (startRadius < endRadius) {
......@@ -74,7 +81,7 @@ fun View.circularRevealOrUnreveal(centerX: Int, centerY: Int, startRadius: Float
anim.start()
}
fun View.shake(x: Float = 2F, num: Int = 0){
fun View.shake(x: Float = 2F, num: Int = 0) {
if (num == 6) {
this.translationX = 0.toFloat()
return
......@@ -104,5 +111,4 @@ fun Fragment.vibrateSmartPhone() {
} else {
vibrator.vibrate(200)
}
}
\ No newline at end of file
package chat.rocket.android.util.extensions
import android.graphics.Bitmap
fun Bitmap.getCompressFormat(mimeType: String): Bitmap.CompressFormat {
return when {
mimeType.contains("jpeg") -> Bitmap.CompressFormat.JPEG
mimeType.contains("webp") -> Bitmap.CompressFormat.WEBP
else -> Bitmap.CompressFormat.PNG
}
}
\ No newline at end of file
......@@ -2,17 +2,15 @@ package chat.rocket.android.util.extensions
import android.app.Activity
import android.content.Context
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import chat.rocket.android.R
// TODO: Remove. Use KTX instead.
......@@ -88,13 +86,4 @@ fun Fragment.showToast(@StringRes resource: Int, duration: Int = Toast.LENGTH_SH
showToast(getString(resource), duration)
fun Fragment.showToast(message: String, duration: Int = Toast.LENGTH_SHORT) =
activity?.showToast(message, duration)
fun RecyclerView.isAtBottom(): Boolean {
val manager: RecyclerView.LayoutManager? = layoutManager
if (manager is LinearLayoutManager) {
return manager.findFirstVisibleItemPosition() == 0
}
return false // or true??? we can't determine the first visible item.
}
\ No newline at end of file
activity?.showToast(message, duration)
\ No newline at end of file
......@@ -3,6 +3,7 @@ package chat.rocket.android.util.extensions
import android.annotation.TargetApi
import android.content.ContentResolver
import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.provider.DocumentsContract
......@@ -57,25 +58,17 @@ fun Uri.getMimeType(context: Context): String {
}
}
fun Uri.getRealPathFromURI(context: Context): String? {
val cursor = context.contentResolver.query(this, arrayOf(MediaStore.Images.Media.DATA), null, null, null)
cursor.use { cursor ->
val columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
cursor.moveToFirst()
return cursor.getString(columnIndex)
}
}
@TargetApi(Build.VERSION_CODES.N)
fun Uri.isVirtualFile(context: Context): Boolean {
if (!DocumentsContract.isDocumentUri(context, this)) {
return false
}
val cursor = context.contentResolver.query(this,
arrayOf(DocumentsContract.Document.COLUMN_FLAGS),
null, null, null)
val cursor = context.contentResolver.query(
this,
arrayOf(DocumentsContract.Document.COLUMN_FLAGS),
null, null, null
)
var flags = 0
if (cursor.moveToFirst()) {
......@@ -97,8 +90,8 @@ fun Uri.getInputStreamForVirtualFile(context: Context, mimeTypeFilter: String):
throw FileNotFoundException()
}
return resolver.openTypedAssetFileDescriptor(this, openableMimeTypes[0],
null)?.createInputStream()
return resolver.openTypedAssetFileDescriptor(this, openableMimeTypes[0], null)
?.createInputStream()
}
fun Uri.getInputStream(context: Context): InputStream? {
......@@ -107,4 +100,8 @@ fun Uri.getInputStream(context: Context): InputStream? {
}
return context.contentResolver.openInputStream(this)
}
\ No newline at end of file
}
fun Uri.getBitmpap(context: Context): Bitmap? {
return MediaStore.Images.Media.getBitmap(context.contentResolver, this)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment