Skip to content

Commit

Permalink
Compression for single image selection (#17)
Browse files Browse the repository at this point in the history
Compression for single image selection
- Fixed permission issue in document selection
  • Loading branch information
malikmotani authored Jun 20, 2020
1 parent 60c9373 commit 3103bd5
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 33 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Lassi is simplest way to pick media (either image, video, audio or doc)
```groovy
dependencies {
...
implementation 'com.github.Mindinventory:Lassi:0.1.3'
implementation 'com.github.Mindinventory:Lassi:0.1.4'
}
```
Expand All @@ -53,6 +53,7 @@ Lassi is simplest way to pick media (either image, video, audio or doc)
.setMaxCount(5)
.setGridSize(3)
.setMediaType(MediaType.VIDEO) // MediaType : VIDEO IMAGE, AUDIO OR DOC
.setCompressionRation(10) // compress image for single item selection (can be 0 to 100)
.setMinTime(15) // for MediaType.VIDEO only
.setMaxTime(30) // for MediaType.VIDEO only
.setSupportedFileTypes("mp4", "mkv", "webm", "avi", "flv", "3gp") // Filter by limited media format (Optional)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/lassi/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
.setProgressBarColor(R.color.colorAccent)
.setCropType(CropImageView.CropShape.OVAL)
.setCropAspectRatio(1, 1)
.setCompressionRation(10)
.enableActualCircleCrop()
.setSupportedFileTypes("jpg", "jpeg", "png", "webp", "gif")
.enableFlip()
Expand Down
4 changes: 2 additions & 2 deletions lassi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
defaultConfig {
minSdkVersion 17
targetSdkVersion 29
versionCode 13
versionName "0.1.3"
versionCode 14
versionName "0.1.4"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
Expand Down
40 changes: 25 additions & 15 deletions lassi/src/main/java/com/lassi/common/utils/CropUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,33 @@ object CropUtils {
fun beginCrop(activity: FragmentActivity, source: Uri) {
with(LassiConfig.getConfig()) {
CropImage.activity(source)
.setGuidelines(CropImageView.Guidelines.ON)
.setOutputCompressQuality(70)
.setCropShape(cropType)
.setAspectRatio(cropAspectRatio)
.setOutputUri(createDirectory(activity))
.setAllowRotation(enableRotateImage)
.setAllowFlipping(enableFlipImage)
.start(activity)
.setGuidelines(CropImageView.Guidelines.ON)
.setOutputCompressQuality((100 - compressionRation))
.setCropShape(cropType)
.setAspectRatio(cropAspectRatio)
.setOutputUri(createDirectory(activity))
.setAllowRotation(enableRotateImage)
.setAllowFlipping(enableFlipImage)
.start(activity)
}
}

private fun getApplicationName(context: Context): String {
val applicationInfo = context.applicationInfo
val stringId = applicationInfo.labelRes
return if (stringId == 0) applicationInfo.nonLocalizedLabel.toString() else context.getString(stringId)
return if (stringId == 0) applicationInfo.nonLocalizedLabel.toString() else context.getString(
stringId
)
}

private fun getPath(context: Context): String {
return Environment.getExternalStorageDirectory().absolutePath + File.separator + getApplicationName(
context
)
}

private fun createDirectory(context: Context): Uri? {
val path = Environment.getExternalStorageDirectory().absolutePath + File.separator + getApplicationName(context)
path.let {
getPath(context).let {
val storageDir = File(it)
if (!storageDir.exists()) {
storageDir.mkdirs()
Expand Down Expand Up @@ -70,12 +77,15 @@ object CropUtils {
// Create an image file name
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(Date())
// val storageDir: File? = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
val storageDir = Environment.getExternalStorageDirectory().absolutePath + File.separator + getApplicationName(context)
val storageDir =
Environment.getExternalStorageDirectory().absolutePath + File.separator + getApplicationName(
context
)

return File.createTempFile(
"IMG-${timeStamp}_", /* prefix */
".jpeg", /* suffix */
File(storageDir) /* directory */
"IMG-${timeStamp}_", /* prefix */
".jpeg", /* suffix */
File(storageDir) /* directory */
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import java.io.File
import java.util.*
import kotlin.collections.ArrayList

class MediaDataRepository(private val context: Context) : MediaRepository {
class MediaRepositoryImpl(private val context: Context) : MediaRepository {

private val minTimeInMillis = LassiConfig.getConfig().minTime * 1000L
private val maxTimeInMillis = LassiConfig.getConfig().maxTime * 1000L
Expand Down
4 changes: 3 additions & 1 deletion lassi/src/main/java/com/lassi/domain/media/LassiConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ data class LassiConfig(
var cropAspectRatio: AspectRatio? = null,
var enableFlipImage: Boolean = false,
var enableRotateImage: Boolean = false,
var enableActualCircleCrop: Boolean = false
var enableActualCircleCrop: Boolean = false,
var compressionRation: Int = 0
) : Parcelable {
companion object {

Expand All @@ -57,6 +58,7 @@ data class LassiConfig(
enableFlipImage = lassiConfig.enableFlipImage
enableRotateImage = lassiConfig.enableRotateImage
enableActualCircleCrop = lassiConfig.enableActualCircleCrop
compressionRation = lassiConfig.compressionRation
}
}

Expand Down
13 changes: 13 additions & 0 deletions lassi/src/main/java/com/lassi/presentation/builder/Lassi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ class Lassi(private val context: Context) {
return this
}

/**
* Set compression ration between 0 to 100 (Only for single image selection)
*/
fun setCompressionRation(compressionRation: Int): Lassi {
val compression = if (compressionRation > 100) {
100
} else {
compressionRation
}
lassiConfig.compressionRation = compression
return this
}

/**
* Start LassiMediaPickerActivity with config
*/
Expand Down
69 changes: 68 additions & 1 deletion lassi/src/main/java/com/lassi/presentation/docs/DocsFragment.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package com.lassi.presentation.docs

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.PorterDuff
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.Menu
import androidx.appcompat.app.AlertDialog
import androidx.core.app.ActivityCompat
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.GridLayoutManager
import com.lassi.R
Expand Down Expand Up @@ -44,6 +51,35 @@ class DocsFragment : LassiBaseViewModelFragment<DocsViewModel>() {
mediaPickerConfig.progressBarColor,
PorterDuff.Mode.MULTIPLY
)
checkPermission()
}

private fun checkPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(
requireContext()
, Manifest.permission.READ_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(
requireContext()
, Manifest.permission.WRITE_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED
) {
this.requestPermissions(
arrayOf(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
, KeyUtils.REQUEST_PERMISSIONS_REQUEST_CODE
)
return
}
}
fetchDocs()
}

private fun fetchDocs() {
viewModel.fetchDocs()
}

override fun initLiveDataObservers() {
Expand All @@ -62,7 +98,6 @@ class DocsFragment : LassiBaseViewModelFragment<DocsViewModel>() {
}
}


private fun setImageAdapter() {
rvMedia.layoutManager = GridLayoutManager(context, mediaPickerConfig.gridSize)
rvMedia.adapter = mediaAdapter
Expand Down Expand Up @@ -97,4 +132,36 @@ class DocsFragment : LassiBaseViewModelFragment<DocsViewModel>() {
activity?.setResult(Activity.RESULT_OK, intent)
activity?.finish()
}

override fun onRequestPermissionsResult(
requestCode: Int, permissions: Array<out String>,
grantResults: IntArray
) {
if (requestCode == KeyUtils.REQUEST_PERMISSIONS_REQUEST_CODE
&& grantResults[0] == PackageManager.PERMISSION_GRANTED
) {
fetchDocs()
} else {
showPermissionDisableAlert()
}
}

private fun showPermissionDisableAlert() {
val alertDialog = AlertDialog.Builder(requireContext())
alertDialog.setMessage(R.string.storage_permission_rational)
alertDialog.setCancelable(false)
alertDialog.setPositiveButton(R.string.ok) { _, _ ->
val intent = Intent().apply {
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
data = Uri.fromParts("package", activity?.packageName, null)
}
startActivityForResult(intent, KeyUtils.SETTINGS_REQUEST_CODE)
}
alertDialog.setNegativeButton(R.string.cancel) { _, _ ->
activity?.onBackPressed()
}
val permissionDialog = alertDialog.create()
permissionDialog.setCancelable(false)
permissionDialog.show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ import java.util.*
class DocsViewModel(private val mediaRepository: MediaRepository) : LassiBaseViewModel() {
var fetchDocsLiveData = MutableLiveData<Response<ArrayList<MiMedia>>>()

override fun loadPage() {
super.loadPage()
fetchDocs()
}

private fun fetchDocs() {
fun fetchDocs() {
fetchDocsLiveData.value = Response.Loading()
mediaRepository.fetchDocs()
.subscribeOn(Schedulers.io())
Expand All @@ -28,5 +23,4 @@ class DocsViewModel(private val mediaRepository: MediaRepository) : LassiBaseVie
fetchDocsLiveData.value = Response.Error(it)
}).collect()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package com.lassi.presentation.docs
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.lassi.data.media.MediaDataRepository
import com.lassi.data.media.MediaRepositoryImpl
import com.lassi.domain.media.MediaRepository

@Suppress("UNCHECKED_CAST")
class DocsViewModelFactory(val context: Context) : ViewModelProvider.Factory {
private val mediaRepository: MediaRepository = MediaDataRepository(context)
private val mediaRepository: MediaRepository = MediaRepositoryImpl(context)

override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return DocsViewModel(mediaRepository) as T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package com.lassi.presentation.mediadirectory
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.lassi.data.media.MediaDataRepository
import com.lassi.data.media.MediaRepositoryImpl
import com.lassi.domain.media.MediaRepository

@Suppress("UNCHECKED_CAST")
class FolderViewModelFactory(val context: Context) : ViewModelProvider.Factory {
private val mediaRepository: MediaRepository =
MediaDataRepository(context)
private val mediaRepository: MediaRepository = MediaRepositoryImpl(context)

override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return FolderViewModel(mediaRepository) as T
Expand Down

0 comments on commit 3103bd5

Please sign in to comment.