From 04da0fc1cfc9f5a8b529ecf664ce3f69c5a266b7 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 23 Jul 2018 00:15:48 +0200 Subject: [PATCH] flip between crop and default image view as necessary --- .../gallery/activities/EditActivity.kt | 65 ++++++++++++++----- 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt index 5d2085950..490b9e2ff 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt @@ -122,18 +122,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener } isCropIntent = intent.extras?.get(CROP) == "true" - Glide.with(this) - .asBitmap() - .load(uri) - .listener(object : RequestListener { - override fun onLoadFailed(e: GlideException?, model: Any?, target: Target?, isFirstResource: Boolean) = false - - override fun onResourceReady(bitmap: Bitmap?, model: Any?, target: Target?, dataSource: DataSource?, isFirstResource: Boolean): Boolean { - initialBitmap = bitmap - return false - } - }).into(default_image_view) - + loadDefaultImageView() setupBottomActions() } @@ -156,15 +145,41 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { - R.id.save_as -> crop_image_view.getCroppedImageAsync() + R.id.save_as -> saveImage() R.id.edit -> editWith() else -> return super.onOptionsItemSelected(item) } return true } + private fun loadDefaultImageView() { + default_image_view.beVisible() + crop_image_view.beGone() + Glide.with(this) + .asBitmap() + .load(uri) + .listener(object : RequestListener { + override fun onLoadFailed(e: GlideException?, model: Any?, target: Target?, isFirstResource: Boolean) = false + + override fun onResourceReady(bitmap: Bitmap?, model: Any?, target: Target?, dataSource: DataSource?, isFirstResource: Boolean): Boolean { + val currentFilter = getFiltersAdapter()?.getCurrentFilter() + if (initialBitmap != null && currentFilter != null && currentFilter.filter.name != getString(R.string.none)) { + default_image_view.onGlobalLayout { + applyFilter(currentFilter) + } + } else { + initialBitmap = bitmap + } + + return false + } + }).into(default_image_view) + } + private fun loadCropImageView() { + default_image_view.beGone() crop_image_view.apply { + beVisible() setOnCropImageCompleteListener(this@EditActivity) setImageUriAsync(uri) guidelines = CropImageView.Guidelines.ON @@ -177,6 +192,16 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener } } + private fun saveImage() { + if (crop_image_view.isVisible()) { + crop_image_view.getCroppedImageAsync() + } else { + val currentFilter = getFiltersAdapter()?.getCurrentFilter() + } + } + + private fun getFiltersAdapter() = bottom_actions_filter_list.adapter as? FiltersAdapter + private fun setupBottomActions() { setupPrimaryActionButtons() setupCropRotateActionButtons() @@ -255,6 +280,12 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener } private fun updatePrimaryActionButtons() { + if (crop_image_view.isGone() && currPrimaryAction == PRIMARY_ACTION_CROP_ROTATE) { + loadCropImageView() + } else if (default_image_view.isGone() && currPrimaryAction == PRIMARY_ACTION_FILTER) { + loadDefaultImageView() + } + arrayOf(bottom_primary_filter, bottom_primary_crop_rotate).forEach { it.applyColorFilter(Color.WHITE) } @@ -287,8 +318,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener val filterItems = filterThumbnailsManager.processThumbs() val adapter = FiltersAdapter(applicationContext, filterItems) { - val newBitmap = Bitmap.createBitmap(initialBitmap) - default_image_view.setImageBitmap(it.filter.processFilter(newBitmap)) + applyFilter(it) } bottom_actions_filter_list.adapter = adapter @@ -304,6 +334,11 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener } } + private fun applyFilter(filterItem: FilterItem) { + val newBitmap = Bitmap.createBitmap(initialBitmap) + default_image_view.setImageBitmap(filterItem.filter.processFilter(newBitmap)) + } + private fun updateAspectRatio(aspectRatio: Int) { currAspectRatio = aspectRatio updateAspectRatioButtons()