replace some one function interfaces with anonymous functions

This commit is contained in:
tibbi 2016-11-22 23:12:15 +01:00
parent 65f870be3d
commit 9838cc1e2d
13 changed files with 70 additions and 120 deletions

View file

@ -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()

View file

@ -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<String>) {
@ -285,7 +284,7 @@ class MainActivity : SimpleActivity(), GetDirectoriesAsynctask.GetDirectoriesLis
}
}
override fun gotDirectories(dirs: ArrayList<Directory>) {
fun gotDirectories(dirs: ArrayList<Directory>) {
directories_holder.isRefreshing = false
mIsGettingDirs = false
if (dirs.toString() == mDirs.toString()) {

View file

@ -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<Medium>) {
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<Medium>) {
mIsGettingMedia = false
media_holder.isRefreshing = false
if (media.toString() == mMedia.toString()) {
return
}
mMedia = media
initializeGallery()
}
fun checkDelete() {
if (mSnackbar?.isShown == true) {
deleteFiles()

View file

@ -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() {

View file

@ -140,17 +140,15 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
return
}
RenameDirectoryDialog(activity, dir, object : RenameDirectoryDialog.OnRenameDirListener {
override fun onRenameDirSuccess(changedPaths: ArrayList<String>) {
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() {

View file

@ -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<String>, val listener: GetDirectoriesListener) : AsyncTask<Void, Void, ArrayList<Directory>>() {
val mToBeDeleted: List<String>, val callback: (dirs: ArrayList<Directory>) -> Unit) : AsyncTask<Void, Void, ArrayList<Directory>>() {
lateinit var mConfig: Config
lateinit var mListener: WeakReference<GetDirectoriesListener>
override fun onPreExecute() {
super.onPreExecute()
mConfig = Config.newInstance(context)
mListener = WeakReference(listener)
}
override fun doInBackground(vararg params: Void): ArrayList<Directory> {
@ -91,8 +88,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
override fun onPostExecute(dirs: ArrayList<Directory>) {
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<Directory>)
}
}

View file

@ -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<String>, val listener: GetMediaListener) : AsyncTask<Void, Void, ArrayList<Medium>>() {
val mToBeDeleted: List<String>, val callback: (media: ArrayList<Medium>) -> Unit) : AsyncTask<Void, Void, ArrayList<Medium>>() {
lateinit var mConfig: Config
lateinit var mListener: WeakReference<GetMediaListener>
override fun onPreExecute() {
super.onPreExecute()
mConfig = Config.newInstance(context)
mListener = WeakReference(listener)
}
override fun doInBackground(vararg params: Void): ArrayList<Medium> {
@ -76,11 +73,6 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo
override fun onPostExecute(media: ArrayList<Medium>) {
super.onPostExecute(media)
val listener = mListener.get()
listener?.gotMedia(media)
}
interface GetMediaListener {
fun gotMedia(media: ArrayList<Medium>)
callback.invoke(media)
}
}

View file

@ -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()
}
}

View file

@ -26,12 +26,10 @@ class CopyDialog(val activity: SimpleActivity, val files: ArrayList<File>, 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)

View file

@ -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<String>(), this).execute()
}
override fun gotDirectories(dirs: ArrayList<Directory>) {
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<String>()) {
val adapter = DirectoryAdapter(activity, it, null) {
callback.invoke(it.path)
dialog.dismiss()
}
directoriesGrid.adapter = adapter
}.execute()
}
}

View file

@ -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<String>) -> 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<String>)
callback.invoke(updatedFiles)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}