diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SimpleActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SimpleActivity.kt index 935ef564f..08056c4fb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SimpleActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/SimpleActivity.kt @@ -1,10 +1,72 @@ package com.simplemobiletools.gallery.activities import android.os.Bundle +import android.support.v4.util.Pair import com.simplemobiletools.commons.activities.BaseSimpleActivity +import com.simplemobiletools.commons.asynctasks.CopyMoveTask +import com.simplemobiletools.commons.extensions.isPathOnSD +import com.simplemobiletools.commons.extensions.scanFiles +import com.simplemobiletools.commons.extensions.toast +import com.simplemobiletools.gallery.R +import com.simplemobiletools.gallery.dialogs.PickAlbumDialog +import java.io.File +import java.util.* open class SimpleActivity : BaseSimpleActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } + + protected fun copyMoveFilesTo(source: String, files: ArrayList, isCopyOperation: Boolean, copyMoveListener: CopyMoveTask.CopyMoveListener) { + val currPath = source.trimEnd('/') + PickAlbumDialog(this, currPath) { + val destinationFolder = File(it) + if (currPath == it.trimEnd('/')) { + toast(R.string.source_and_destination_same) + return@PickAlbumDialog + } + + if (!destinationFolder.exists()) { + toast(R.string.invalid_destination) + return@PickAlbumDialog + } + + if (files.size == 1) { + if (File(destinationFolder.absolutePath, files[0].name).exists()) { + toast(R.string.name_taken) + return@PickAlbumDialog + } + } + + handleSAFDialog(destinationFolder) { + if (isCopyOperation) { + toast(R.string.copying) + val pair = Pair, File>(files, destinationFolder) + CopyMoveTask(this, isCopyOperation, true, copyMoveListener).execute(pair) + } else { + if (isPathOnSD(currPath) || isPathOnSD(destinationFolder.absolutePath)) { + handleSAFDialog(files[0]) { + toast(R.string.moving) + val pair = Pair, File>(files, destinationFolder) + CopyMoveTask(this, isCopyOperation, true, copyMoveListener).execute(pair) + } + } else { + val updatedFiles = ArrayList(files.size * 2) + updatedFiles.addAll(files) + for (file in files) { + val destination = File(destinationFolder, file.name) + if (!destination.exists() && file.renameTo(destination)) + updatedFiles.add(destination) + } + + scanFiles(updatedFiles) { + runOnUiThread { + copyMoveListener.copySucceeded(true, files.size * 2 == updatedFiles.size) + } + } + } + } + } + } + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt index b39ba52b8..aa47b6de2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -13,7 +13,6 @@ import android.net.Uri import android.os.Build import android.os.Bundle import android.provider.MediaStore -import android.support.v4.util.Pair import android.support.v4.view.ViewPager import android.util.DisplayMetrics import android.view.Menu @@ -27,7 +26,6 @@ import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.adapters.MyPagerAdapter import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask -import com.simplemobiletools.gallery.dialogs.PickAlbumDialog import com.simplemobiletools.gallery.dialogs.SaveAsDialog import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.fragments.PhotoFragment @@ -190,56 +188,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private fun copyMoveTo(isCopyOperation: Boolean) { val currPath = File(getCurrentPath()).parent.trimEnd('/') val files = ArrayList(1).apply { add(getCurrentFile()) } - - PickAlbumDialog(this, currPath) { - val destinationFolder = File(it) - if (currPath == it.trimEnd('/')) { - toast(R.string.source_and_destination_same) - return@PickAlbumDialog - } - - if (!destinationFolder.exists()) { - toast(R.string.invalid_destination) - return@PickAlbumDialog - } - - if (files.size == 1) { - if (File(destinationFolder.absolutePath, files[0].name).exists()) { - toast(R.string.name_taken) - return@PickAlbumDialog - } - } - - handleSAFDialog(destinationFolder) { - if (isCopyOperation) { - toast(R.string.copying) - val pair = Pair, File>(files, destinationFolder) - CopyMoveTask(this, isCopyOperation, true, copyMoveListener).execute(pair) - } else { - if (isPathOnSD(currPath) || isPathOnSD(destinationFolder.absolutePath)) { - handleSAFDialog(files[0]) { - toast(R.string.moving) - val pair = Pair, File>(files, destinationFolder) - CopyMoveTask(this, isCopyOperation, true, copyMoveListener).execute(pair) - } - } else { - val updatedFiles = ArrayList(files.size * 2) - updatedFiles.addAll(files) - for (file in files) { - val destination = File(destinationFolder, file.name) - if (!destination.exists() && file.renameTo(destination)) - updatedFiles.add(destination) - } - - scanFiles(updatedFiles) { - runOnUiThread { - copyMoveListener.copySucceeded(true, files.size * 2 == updatedFiles.size) - } - } - } - } - } - } + copyMoveFilesTo(currPath, files, isCopyOperation, copyMoveListener) } private val copyMoveListener = object : CopyMoveTask.CopyMoveListener {