mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 06:43:56 +01:00 
			
		
		
		
	Refactor : Fixed lint issues on EditActivity
- Using 'Log' instead of 'Timber' - Line is longer than allowed by code style (> 100 columns) - Use of getter method instead of property access syntax - Should be replaced with Kotlin function - Cascade 'if' should be replaced with 'when'
This commit is contained in:
		
							parent
							
								
									33548fa57d
								
							
						
					
					
						commit
						6ff21d8d6c
					
				
					 1 changed files with 59 additions and 53 deletions
				
			
		|  | @ -8,7 +8,6 @@ import android.graphics.BitmapFactory | ||||||
| import android.graphics.Matrix | import android.graphics.Matrix | ||||||
| import android.media.ExifInterface | import android.media.ExifInterface | ||||||
| import android.os.Bundle | import android.os.Bundle | ||||||
| import android.util.Log |  | ||||||
| import android.view.animation.AccelerateDecelerateInterpolator | import android.view.animation.AccelerateDecelerateInterpolator | ||||||
| import android.widget.ImageView | import android.widget.ImageView | ||||||
| import android.widget.Toast | import android.widget.Toast | ||||||
|  | @ -20,6 +19,7 @@ import androidx.lifecycle.ViewModelProvider | ||||||
| import fr.free.nrw.commons.databinding.ActivityEditBinding | import fr.free.nrw.commons.databinding.ActivityEditBinding | ||||||
| import timber.log.Timber | import timber.log.Timber | ||||||
| import java.io.File | import java.io.File | ||||||
|  | import kotlin.math.ceil | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * An activity class for editing and rotating images using LLJTran with EXIF attribute preservation. |  * An activity class for editing and rotating images using LLJTran with EXIF attribute preservation. | ||||||
|  | @ -42,7 +42,7 @@ class EditActivity : AppCompatActivity() { | ||||||
|         supportActionBar?.title = "" |         supportActionBar?.title = "" | ||||||
|         val intent = intent |         val intent = intent | ||||||
|         imageUri = intent.getStringExtra("image") ?: "" |         imageUri = intent.getStringExtra("image") ?: "" | ||||||
|         vm = ViewModelProvider(this).get(EditViewModel::class.java) |         vm = ViewModelProvider(this)[EditViewModel::class.java] | ||||||
|         val sourceExif = imageUri.toUri().path?.let { ExifInterface(it) } |         val sourceExif = imageUri.toUri().path?.let { ExifInterface(it) } | ||||||
|         val exifTags = |         val exifTags = | ||||||
|             arrayOf( |             arrayOf( | ||||||
|  | @ -88,8 +88,7 @@ class EditActivity : AppCompatActivity() { | ||||||
|     private fun init() { |     private fun init() { | ||||||
|         binding.iv.adjustViewBounds = true |         binding.iv.adjustViewBounds = true | ||||||
|         binding.iv.scaleType = ImageView.ScaleType.MATRIX |         binding.iv.scaleType = ImageView.ScaleType.MATRIX | ||||||
|         binding.iv.post( |         binding.iv.post { | ||||||
|             Runnable { |  | ||||||
|             val options = BitmapFactory.Options() |             val options = BitmapFactory.Options() | ||||||
|             options.inJustDecodeBounds = true |             options.inJustDecodeBounds = true | ||||||
|             BitmapFactory.decodeFile(imageUri, options) |             BitmapFactory.decodeFile(imageUri, options) | ||||||
|  | @ -118,8 +117,7 @@ class EditActivity : AppCompatActivity() { | ||||||
|                 binding.iv.layoutParams.height = (scale * bitmapHeight).toInt() |                 binding.iv.layoutParams.height = (scale * bitmapHeight).toInt() | ||||||
|                 binding.iv.imageMatrix = scaleMatrix(scale, scale) |                 binding.iv.imageMatrix = scaleMatrix(scale, scale) | ||||||
|             } |             } | ||||||
|             }, |         } | ||||||
|         ) |  | ||||||
|         binding.rotateBtn.setOnClickListener { |         binding.rotateBtn.setOnClickListener { | ||||||
|             animateImageHeight() |             animateImageHeight() | ||||||
|         } |         } | ||||||
|  | @ -143,15 +141,15 @@ class EditActivity : AppCompatActivity() { | ||||||
|         val drawableWidth: Float = |         val drawableWidth: Float = | ||||||
|             binding.iv |             binding.iv | ||||||
|                 .getDrawable() |                 .getDrawable() | ||||||
|                 .getIntrinsicWidth() |                 .intrinsicWidth | ||||||
|                 .toFloat() |                 .toFloat() | ||||||
|         val drawableHeight: Float = |         val drawableHeight: Float = | ||||||
|             binding.iv |             binding.iv | ||||||
|                 .getDrawable() |                 .getDrawable() | ||||||
|                 .getIntrinsicHeight() |                 .intrinsicHeight | ||||||
|                 .toFloat() |                 .toFloat() | ||||||
|         val viewWidth: Float = binding.iv.getMeasuredWidth().toFloat() |         val viewWidth: Float = binding.iv.measuredWidth.toFloat() | ||||||
|         val viewHeight: Float = binding.iv.getMeasuredHeight().toFloat() |         val viewHeight: Float = binding.iv.measuredHeight.toFloat() | ||||||
|         val rotation = imageRotation % 360 |         val rotation = imageRotation % 360 | ||||||
|         val newRotation = rotation + 90 |         val newRotation = rotation + 90 | ||||||
| 
 | 
 | ||||||
|  | @ -162,16 +160,23 @@ class EditActivity : AppCompatActivity() { | ||||||
|         Timber.d("Rotation $rotation") |         Timber.d("Rotation $rotation") | ||||||
|         Timber.d("new Rotation $newRotation") |         Timber.d("new Rotation $newRotation") | ||||||
| 
 | 
 | ||||||
|         if (rotation == 0 || rotation == 180) { |         when (rotation) { | ||||||
|  |             0, 180 -> { | ||||||
|                 imageScale = viewWidth / drawableWidth |                 imageScale = viewWidth / drawableWidth | ||||||
|                 newImageScale = viewWidth / drawableHeight |                 newImageScale = viewWidth / drawableHeight | ||||||
|                 newViewHeight = (drawableWidth * newImageScale).toInt() |                 newViewHeight = (drawableWidth * newImageScale).toInt() | ||||||
|         } else if (rotation == 90 || rotation == 270) { |             } | ||||||
|  |             90, 270 -> { | ||||||
|                 imageScale = viewWidth / drawableHeight |                 imageScale = viewWidth / drawableHeight | ||||||
|                 newImageScale = viewWidth / drawableWidth |                 newImageScale = viewWidth / drawableWidth | ||||||
|                 newViewHeight = (drawableHeight * newImageScale).toInt() |                 newViewHeight = (drawableHeight * newImageScale).toInt() | ||||||
|         } else { |             } | ||||||
|             throw UnsupportedOperationException("rotation can 0, 90, 180 or 270. \${rotation} is unsupported") |             else -> { | ||||||
|  |                 throw | ||||||
|  |                 UnsupportedOperationException( | ||||||
|  |                     "rotation can 0, 90, 180 or 270. \${rotation} is unsupported" | ||||||
|  |                 ) | ||||||
|  |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         val animator = ValueAnimator.ofFloat(0f, 1f).setDuration(1000L) |         val animator = ValueAnimator.ofFloat(0f, 1f).setDuration(1000L) | ||||||
|  | @ -204,7 +209,7 @@ class EditActivity : AppCompatActivity() { | ||||||
|                 (complementaryAnimVal * viewHeight + animVal * newViewHeight).toInt() |                 (complementaryAnimVal * viewHeight + animVal * newViewHeight).toInt() | ||||||
|             val animatedScale = complementaryAnimVal * imageScale + animVal * newImageScale |             val animatedScale = complementaryAnimVal * imageScale + animVal * newImageScale | ||||||
|             val animatedRotation = complementaryAnimVal * rotation + animVal * newRotation |             val animatedRotation = complementaryAnimVal * rotation + animVal * newRotation | ||||||
|             binding.iv.getLayoutParams().height = animatedHeight |             binding.iv.layoutParams.height = animatedHeight | ||||||
|             val matrix: Matrix = |             val matrix: Matrix = | ||||||
|                 rotationMatrix( |                 rotationMatrix( | ||||||
|                     animatedRotation, |                     animatedRotation, | ||||||
|  | @ -218,8 +223,8 @@ class EditActivity : AppCompatActivity() { | ||||||
|                 drawableHeight / 2, |                 drawableHeight / 2, | ||||||
|             ) |             ) | ||||||
|             matrix.postTranslate( |             matrix.postTranslate( | ||||||
|                 -(drawableWidth - binding.iv.getMeasuredWidth()) / 2, |                 -(drawableWidth - binding.iv.measuredWidth) / 2, | ||||||
|                 -(drawableHeight - binding.iv.getMeasuredHeight()) / 2, |                 -(drawableHeight - binding.iv.measuredHeight) / 2, | ||||||
|             ) |             ) | ||||||
|             binding.iv.setImageMatrix(matrix) |             binding.iv.setImageMatrix(matrix) | ||||||
|             binding.iv.requestLayout() |             binding.iv.requestLayout() | ||||||
|  | @ -267,9 +272,9 @@ class EditActivity : AppCompatActivity() { | ||||||
|      */ |      */ | ||||||
|     private fun copyExifData(editedImageExif: ExifInterface?) { |     private fun copyExifData(editedImageExif: ExifInterface?) { | ||||||
|         for (attr in sourceExifAttributeList) { |         for (attr in sourceExifAttributeList) { | ||||||
|             Log.d("Tag is  ${attr.first}", "Value is ${attr.second}") |             Timber.d("Value is ${attr.second}") | ||||||
|             editedImageExif!!.setAttribute(attr.first, attr.second) |             editedImageExif!!.setAttribute(attr.first, attr.second) | ||||||
|             Log.d("Tag is ${attr.first}", "Value is ${attr.second}") |             Timber.d("Value is ${attr.second}") | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         editedImageExif?.saveAttributes() |         editedImageExif?.saveAttributes() | ||||||
|  | @ -298,9 +303,10 @@ class EditActivity : AppCompatActivity() { | ||||||
|         var scaleFactor = 1 |         var scaleFactor = 1 | ||||||
| 
 | 
 | ||||||
|         if (originalWidth > maxSize || originalHeight > maxSize) { |         if (originalWidth > maxSize || originalHeight > maxSize) { | ||||||
|             // Calculate the largest power of 2 that is less than or equal to the desired width and height |             // Calculate the largest power of 2 that is less than or equal to the desired | ||||||
|             val widthRatio = Math.ceil((originalWidth.toDouble() / maxSize.toDouble())).toInt() |             // width and height | ||||||
|             val heightRatio = Math.ceil((originalHeight.toDouble() / maxSize.toDouble())).toInt() |             val widthRatio = ceil((originalWidth.toDouble() / maxSize.toDouble())).toInt() | ||||||
|  |             val heightRatio = ceil((originalHeight.toDouble() / maxSize.toDouble())).toInt() | ||||||
| 
 | 
 | ||||||
|             scaleFactor = if (widthRatio > heightRatio) widthRatio else heightRatio |             scaleFactor = if (widthRatio > heightRatio) widthRatio else heightRatio | ||||||
|         } |         } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Neel Doshi
						Neel Doshi