From 9838cc1e2d991c667ae607e035fd24becd8f8ab7 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 22 Nov 2016 23:12:15 +0100 Subject: [PATCH] replace some one function interfaces with anonymous functions --- .../gallery/activities/EditActivity.kt | 24 ++++++------- .../gallery/activities/MainActivity.kt | 17 +++++----- .../gallery/activities/MediaActivity.kt | 34 +++++++++---------- .../gallery/activities/ViewPagerActivity.kt | 10 +++--- .../gallery/adapters/DirectoryAdapter.kt | 16 ++++----- .../asynctasks/GetDirectoriesAsynctask.kt | 12 ++----- .../gallery/asynctasks/GetMediaAsynctask.kt | 12 ++----- .../gallery/dialogs/ChangeSortingDialog.kt | 8 ++--- .../gallery/dialogs/CopyDialog.kt | 10 +++--- .../gallery/dialogs/PickAlbumDialog.kt | 23 +++++-------- .../gallery/dialogs/RenameDirectoryDialog.kt | 8 ++--- .../gallery/dialogs/RenameFileDialog.kt | 8 ++--- .../gallery/dialogs/SaveAsDialog.kt | 8 ++--- 13 files changed, 70 insertions(+), 120 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 4ebf3a009..4feaade51 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt @@ -72,23 +72,19 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener override fun onCropImageComplete(view: CropImageView, result: CropImageView.CropResult) { if (result.error == null) { if (uri.scheme == "file") { - SaveAsDialog(this, uri.path, object : SaveAsDialog.OnSaveAsListener { - override fun onSaveAsSuccess(filename: String) { - val parent = File(uri.path).parent - val path = File(parent, filename).absolutePath - saveBitmapToFile(result.bitmap, path) - } - }) + SaveAsDialog(this, uri.path) { + val parent = File(uri.path).parent + val path = File(parent, it).absolutePath + saveBitmapToFile(result.bitmap, path) + } } else if (uri.scheme == "content") { val newPath = applicationContext.getRealPathFromURI(uri) ?: "" if (!newPath.isEmpty()) { - SaveAsDialog(this, newPath, object : SaveAsDialog.OnSaveAsListener { - override fun onSaveAsSuccess(filename: String) { - val parent = File(uri.path).parent - val path = File(parent, filename).absolutePath - saveBitmapToFile(result.bitmap, path) - } - }) + SaveAsDialog(this, newPath) { + val parent = File(uri.path).parent + val path = File(parent, it).absolutePath + saveBitmapToFile(result.bitmap, path) + } } else { toast(R.string.image_editing_failed) finish() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index c9cc1aad0..3ed48516d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -25,7 +25,7 @@ import kotlinx.android.synthetic.main.activity_main.* import java.io.File import java.util.* -class MainActivity : SimpleActivity(), GetDirectoriesAsynctask.GetDirectoriesListener, DirectoryAdapter.DirOperationsListener { +class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { companion object { private val STORAGE_PERMISSION = 1 private val PICK_MEDIA = 2 @@ -134,16 +134,15 @@ class MainActivity : SimpleActivity(), GetDirectoriesAsynctask.GetDirectoriesLis return mIsGettingDirs = true - GetDirectoriesAsynctask(applicationContext, mIsPickVideoIntent || mIsGetVideoContentIntent, mIsPickImageIntent || mIsGetImageContentIntent, - mToBeDeleted, this).execute() + GetDirectoriesAsynctask(applicationContext, mIsPickVideoIntent || mIsGetVideoContentIntent, mIsPickImageIntent || mIsGetImageContentIntent, mToBeDeleted) { + gotDirectories(it) + }.execute() } private fun showSortingDialog() { - ChangeSortingDialog(this, true, object : ChangeSortingDialog.OnChangeSortingListener { - override fun sortingChanged() { - getDirectories() - } - }) + ChangeSortingDialog(this, true) { + getDirectories() + } } override fun prepareForDeleting(paths: ArrayList) { @@ -285,7 +284,7 @@ class MainActivity : SimpleActivity(), GetDirectoriesAsynctask.GetDirectoriesLis } } - override fun gotDirectories(dirs: ArrayList) { + fun gotDirectories(dirs: ArrayList) { directories_holder.isRefreshing = false mIsGettingDirs = false if (dirs.toString() == mDirs.toString()) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index 3c96af1d7..412c05aec 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -128,11 +128,9 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } private fun showSortingDialog() { - ChangeSortingDialog(this, false, object : ChangeSortingDialog.OnChangeSortingListener { - override fun sortingChanged() { - getMedia() - } - }) + ChangeSortingDialog(this, false) { + getMedia() + } } private fun hideDirectory() { @@ -161,18 +159,9 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { return mIsGettingMedia = true - GetMediaAsynctask(applicationContext, mPath, mIsGetVideoIntent, mIsGetImageIntent, mToBeDeleted, object : GetMediaAsynctask.GetMediaListener { - override fun gotMedia(media: ArrayList) { - mIsGettingMedia = false - media_holder.isRefreshing = false - if (media.toString() == mMedia.toString()) { - return - } - - mMedia = media - initializeGallery() - } - }).execute() + GetMediaAsynctask(applicationContext, mPath, mIsGetVideoIntent, mIsGetImageIntent, mToBeDeleted) { + gotMedia(it) + }.execute() } private fun isDirEmpty(): Boolean { @@ -304,6 +293,17 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } } + fun gotMedia(media: ArrayList) { + mIsGettingMedia = false + media_holder.isRefreshing = false + if (media.toString() == mMedia.toString()) { + return + } + + mMedia = media + initializeGallery() + } + fun checkDelete() { if (mSnackbar?.isShown == true) { deleteFiles() 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 908a2ef5c..65f536492 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -315,12 +315,10 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } private fun editMedium() { - RenameFileDialog(this, getCurrentFile(), object : RenameFileDialog.OnRenameFileListener { - override fun onRenameFileSuccess(newFile: File) { - mMedia!![view_pager.currentItem].path = newFile.absolutePath - updateActionbarTitle() - } - }) + RenameFileDialog(this, getCurrentFile()) { + mMedia!![view_pager.currentItem].path = it.absolutePath + updateActionbarTitle() + } } private fun reloadViewPager() { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt index bb4a71316..a1d6ceb9a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -140,17 +140,15 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList) { - activity.scanPaths(changedPaths) { - activity.runOnUiThread { - actMode?.finish() - listener?.refreshItems() - activity.toast(R.string.rename_folder_ok) - } + RenameDirectoryDialog(activity, dir) { + activity.scanPaths(it) { + activity.runOnUiThread { + actMode?.finish() + listener?.refreshItems() + activity.toast(R.string.rename_folder_ok) } } - }) + } } private fun hideDirs() { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt index a22c56687..abd01ae59 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -12,18 +12,15 @@ import com.simplemobiletools.gallery.SORT_DESCENDING import com.simplemobiletools.gallery.extensions.getHumanizedFilename import com.simplemobiletools.gallery.models.Directory import java.io.File -import java.lang.ref.WeakReference import java.util.* class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, val isPickImage: Boolean, - val mToBeDeleted: List, val listener: GetDirectoriesListener) : AsyncTask>() { + val mToBeDeleted: List, val callback: (dirs: ArrayList) -> Unit) : AsyncTask>() { lateinit var mConfig: Config - lateinit var mListener: WeakReference override fun onPreExecute() { super.onPreExecute() mConfig = Config.newInstance(context) - mListener = WeakReference(listener) } override fun doInBackground(vararg params: Void): ArrayList { @@ -91,8 +88,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va override fun onPostExecute(dirs: ArrayList) { super.onPostExecute(dirs) - val listener = mListener.get() - listener?.gotDirectories(dirs) + callback.invoke(dirs) } // sort the files at querying too, just to get the correct thumbnail @@ -135,8 +131,4 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va dirs.removeAll(ignoreDirs) } - - interface GetDirectoriesListener { - fun gotDirectories(dirs: ArrayList) - } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt index 01e9f6336..31585ead5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt @@ -8,19 +8,16 @@ import com.simplemobiletools.filepicker.extensions.scanFiles import com.simplemobiletools.gallery.Config import com.simplemobiletools.gallery.models.Medium import java.io.File -import java.lang.ref.WeakReference import java.util.* import java.util.regex.Pattern class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo: Boolean, val isPickImage: Boolean, - val mToBeDeleted: List, val listener: GetMediaListener) : AsyncTask>() { + val mToBeDeleted: List, val callback: (media: ArrayList) -> Unit) : AsyncTask>() { lateinit var mConfig: Config - lateinit var mListener: WeakReference override fun onPreExecute() { super.onPreExecute() mConfig = Config.newInstance(context) - mListener = WeakReference(listener) } override fun doInBackground(vararg params: Void): ArrayList { @@ -76,11 +73,6 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo override fun onPostExecute(media: ArrayList) { super.onPostExecute(media) - val listener = mListener.get() - listener?.gotMedia(media) - } - - interface GetMediaListener { - fun gotMedia(media: ArrayList) + callback.invoke(media) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt index f71261cb9..c5b939178 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/ChangeSortingDialog.kt @@ -8,7 +8,7 @@ import android.view.View import com.simplemobiletools.gallery.* import kotlinx.android.synthetic.main.dialog_change_sorting.view.* -class ChangeSortingDialog(val activity: Activity, val isDirectorySorting: Boolean, val listener: OnChangeSortingListener) : DialogInterface.OnClickListener { +class ChangeSortingDialog(val activity: Activity, val isDirectorySorting: Boolean, val callback: () -> Unit) : DialogInterface.OnClickListener { companion object { private var currSorting = 0 @@ -76,10 +76,6 @@ class ChangeSortingDialog(val activity: Activity, val isDirectorySorting: Boolea config.sorting = sorting } } - listener.sortingChanged() - } - - interface OnChangeSortingListener { - fun sortingChanged() + callback.invoke() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/CopyDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/CopyDialog.kt index daae39ba4..cba853a19 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/CopyDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/CopyDialog.kt @@ -26,12 +26,10 @@ class CopyDialog(val activity: SimpleActivity, val files: ArrayList, val c view.source.text = activity.humanizePath(sourcePath) view.destination.setOnClickListener { - PickAlbumDialog(activity, object : PickAlbumDialog.OnPickAlbumListener { - override fun onSuccess(path: String) { - destinationPath = path - view.destination.text = activity.humanizePath(path) - } - }) + PickAlbumDialog(activity) { + destinationPath = it + view.destination.text = activity.humanizePath(it) + } } AlertDialog.Builder(activity) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickAlbumDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickAlbumDialog.kt index da25e292f..1272ea837 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickAlbumDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickAlbumDialog.kt @@ -7,11 +7,10 @@ import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.activities.SimpleActivity import com.simplemobiletools.gallery.adapters.DirectoryAdapter import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask -import com.simplemobiletools.gallery.models.Directory import kotlinx.android.synthetic.main.dialog_album_picker.view.* import java.util.* -class PickAlbumDialog(val activity: SimpleActivity, val listener: OnPickAlbumListener) : GetDirectoriesAsynctask.GetDirectoriesListener { +class PickAlbumDialog(val activity: SimpleActivity, val callback: (path: String) -> Unit) { var dialog: AlertDialog var directoriesGrid: RecyclerView @@ -27,18 +26,12 @@ class PickAlbumDialog(val activity: SimpleActivity, val listener: OnPickAlbumLis .create() dialog.show() - GetDirectoriesAsynctask(activity, false, false, ArrayList(), this).execute() - } - - override fun gotDirectories(dirs: ArrayList) { - val adapter = DirectoryAdapter(activity, dirs, null) { - listener.onSuccess(it.path) - dialog.dismiss() - } - directoriesGrid.adapter = adapter - } - - interface OnPickAlbumListener { - fun onSuccess(path: String) + GetDirectoriesAsynctask(activity, false, false, ArrayList()) { + val adapter = DirectoryAdapter(activity, it, null) { + callback.invoke(it.path) + dialog.dismiss() + } + directoriesGrid.adapter = adapter + }.execute() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameDirectoryDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameDirectoryDialog.kt index 03b0d3209..101b9fc55 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameDirectoryDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameDirectoryDialog.kt @@ -11,7 +11,7 @@ import kotlinx.android.synthetic.main.rename_directory.view.* import java.io.File import java.util.* -class RenameDirectoryDialog(val activity: SimpleActivity, val dir: File, val listener: OnRenameDirListener) { +class RenameDirectoryDialog(val activity: SimpleActivity, val dir: File, val callback: (changedPaths: ArrayList) -> Unit) { init { val view = LayoutInflater.from(activity).inflate(R.layout.rename_directory, null) @@ -72,10 +72,6 @@ class RenameDirectoryDialog(val activity: SimpleActivity, val dir: File, val lis files.mapTo(updatedFiles) { it.absolutePath } updatedFiles.add(newDir.absolutePath) - listener.onRenameDirSuccess(updatedFiles) - } - - interface OnRenameDirListener { - fun onRenameDirSuccess(changedPaths: ArrayList) + callback.invoke(updatedFiles) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt index b135e15d9..ebb7470f4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt @@ -10,7 +10,7 @@ import com.simplemobiletools.gallery.activities.SimpleActivity import kotlinx.android.synthetic.main.rename_file.view.* import java.io.File -class RenameFileDialog(val activity: SimpleActivity, val file: File, val listener: OnRenameFileListener) { +class RenameFileDialog(val activity: SimpleActivity, val file: File, val callback: (newFile: File) -> Unit) { init { val view = LayoutInflater.from(activity).inflate(R.layout.rename_file, null) @@ -73,10 +73,6 @@ class RenameFileDialog(val activity: SimpleActivity, val file: File, val listene private fun sendSuccess(currFile: File, newFile: File) { val changedFiles = arrayListOf(currFile, newFile) activity.scanFiles(changedFiles) {} - listener.onRenameFileSuccess(newFile) - } - - interface OnRenameFileListener { - fun onRenameFileSuccess(newFile: File) + callback.invoke(newFile) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/SaveAsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/SaveAsDialog.kt index 8aae9029b..627ae71f6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/SaveAsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/SaveAsDialog.kt @@ -11,7 +11,7 @@ import com.simplemobiletools.filepicker.extensions.value import com.simplemobiletools.gallery.R import kotlinx.android.synthetic.main.rename_file.view.* -class SaveAsDialog(val activity: Activity, val path: String, val listener: OnSaveAsListener) { +class SaveAsDialog(val activity: Activity, val path: String, val callback: (filename: String) -> Unit) { init { val view = LayoutInflater.from(activity).inflate(R.layout.dialog_save_as, null) @@ -38,13 +38,9 @@ class SaveAsDialog(val activity: Activity, val path: String, val listener: OnSav return@setOnClickListener } - listener.onSaveAsSuccess(filename) + callback.invoke(filename) dismiss() }) } } - - interface OnSaveAsListener { - fun onSaveAsSuccess(filename: String) - } }