make "media" a global variable at getMediaAsyncTask

This commit is contained in:
tibbi 2017-04-04 21:40:12 +02:00
parent 9c0cecc76f
commit 259f517a18
2 changed files with 13 additions and 15 deletions

View file

@ -32,7 +32,7 @@ android {
} }
dependencies { dependencies {
compile 'com.simplemobiletools:commons:2.15.2' compile 'com.simplemobiletools:commons:2.15.3'
compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.6.0' compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.6.0'
compile 'com.theartofdev.edmodo:android-image-cropper:2.3.1' compile 'com.theartofdev.edmodo:android-image-cropper:2.3.1'
compile 'com.bignerdranch.android:recyclerview-multiselect:0.2' compile 'com.bignerdranch.android:recyclerview-multiselect:0.2'

View file

@ -16,11 +16,12 @@ import java.util.*
class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo: Boolean = false, val isPickImage: Boolean = false, class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo: Boolean = false, val isPickImage: Boolean = false,
val showAll: Boolean, val callback: (media: ArrayList<Medium>) -> Unit) : val showAll: Boolean, val callback: (media: ArrayList<Medium>) -> Unit) :
AsyncTask<Void, Void, ArrayList<Medium>>() { AsyncTask<Void, Void, Unit>() {
var config = context.config var config = context.config
var showMedia = IMAGES_AND_VIDEOS var showMedia = IMAGES_AND_VIDEOS
var fileSorting = 0 var fileSorting = 0
var shouldStop = false var shouldStop = false
var media = ArrayList<Medium>()
override fun onPreExecute() { override fun onPreExecute() {
super.onPreExecute() super.onPreExecute()
@ -28,27 +29,24 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo
fileSorting = config.getFileSorting(mPath) fileSorting = config.getFileSorting(mPath)
} }
override fun doInBackground(vararg params: Void): ArrayList<Medium> { override fun doInBackground(vararg params: Void): Unit {
val media = ArrayList<Medium>()
if (showAll) { if (showAll) {
val parents = context.getParents() val parents = context.getParents()
for (parent in parents) { for (parent in parents) {
media.addAll(getFilesFrom(parent)) getFilesFrom(parent)
} }
} else { } else {
media.addAll(getFilesFrom(mPath)) getFilesFrom(mPath)
} }
Medium.sorting = fileSorting Medium.sorting = fileSorting
media.sort() media.sort()
return media return Unit
} }
private fun getFilesFrom(path: String): ArrayList<Medium> { private fun getFilesFrom(path: String) {
val media = ArrayList<Medium>()
val dir = File(path) val dir = File(path)
val filenames = dir.list() ?: return media val filenames = dir.list() ?: return
for (filename in filenames) { for (filename in filenames) {
if (shouldStop) if (shouldStop)
cancel(true) cancel(true)
@ -71,13 +69,13 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo
continue continue
val dateModified = file.lastModified() val dateModified = file.lastModified()
media.add(Medium(filename, file.absolutePath, isVideo, dateModified, dateModified, size)) val medium = Medium(filename, file.absolutePath, isVideo, dateModified, dateModified, size)
media.add(medium)
} }
return media
} }
override fun onPostExecute(media: ArrayList<Medium>) { override fun onPostExecute(result: Unit?) {
super.onPostExecute(media) super.onPostExecute(result)
callback.invoke(media) callback.invoke(media)
} }
} }