show the cached folders immediately as possible copy/move destinations at the dialog

This commit is contained in:
tibbi 2017-04-17 21:22:31 +02:00
parent 69a8a35016
commit 5a954febf4
4 changed files with 40 additions and 20 deletions

View file

@ -13,7 +13,6 @@ import android.support.v7.widget.GridLayoutManager
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.models.Release import com.simplemobiletools.commons.models.Release
import com.simplemobiletools.gallery.BuildConfig import com.simplemobiletools.gallery.BuildConfig
@ -21,10 +20,7 @@ import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.adapters.DirectoryAdapter import com.simplemobiletools.gallery.adapters.DirectoryAdapter
import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask
import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog
import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.extensions.launchAbout
import com.simplemobiletools.gallery.extensions.launchCamera
import com.simplemobiletools.gallery.extensions.launchSettings
import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.helpers.*
import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Directory
import com.simplemobiletools.gallery.views.MyScalableRecyclerView import com.simplemobiletools.gallery.views.MyScalableRecyclerView
@ -159,9 +155,8 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
return return
mIsGettingDirs = true mIsGettingDirs = true
val token = object : TypeToken<List<Directory>>() {}.type val dirs = getCachedDirectories()
val dirs = Gson().fromJson<ArrayList<Directory>>(config.directories, token) ?: ArrayList<Directory>(1) if (dirs.isNotEmpty() && !mLoadedInitialPhotos) {
if (dirs.size != 0 && !mLoadedInitialPhotos) {
gotDirectories(dirs) gotDirectories(dirs)
} }

View file

@ -221,7 +221,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
mIsGettingMedia = true mIsGettingMedia = true
val token = object : TypeToken<List<Medium>>() {}.type val token = object : TypeToken<List<Medium>>() {}.type
val media = Gson().fromJson<ArrayList<Medium>>(config.loadFolderMedia(mPath), token) ?: ArrayList<Medium>(1) val media = Gson().fromJson<ArrayList<Medium>>(config.loadFolderMedia(mPath), token) ?: ArrayList<Medium>(1)
if (media.size == 0 && !mLoadedInitialPhotos) { if (media.isNotEmpty() && !mLoadedInitialPhotos) {
gotMedia(media) gotMedia(media)
} }

View file

@ -11,33 +11,33 @@ import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.adapters.DirectoryAdapter import com.simplemobiletools.gallery.adapters.DirectoryAdapter
import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask
import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.getCachedDirectories
import com.simplemobiletools.gallery.models.Directory
import kotlinx.android.synthetic.main.dialog_album_picker.view.* import kotlinx.android.synthetic.main.dialog_album_picker.view.*
class PickAlbumDialog(val activity: SimpleActivity, val sourcePath: String, val callback: (path: String) -> Unit) { class PickAlbumDialog(val activity: SimpleActivity, val sourcePath: String, val callback: (path: String) -> Unit) {
var dialog: AlertDialog
var directoriesGrid: RecyclerView var directoriesGrid: RecyclerView
var shownDirectories: ArrayList<Directory> = ArrayList()
init { init {
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_album_picker, null) val view = LayoutInflater.from(activity).inflate(R.layout.dialog_album_picker, null)
directoriesGrid = view.directories_grid directoriesGrid = view.directories_grid
AlertDialog.Builder(activity) dialog = AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, null) .setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.setNeutralButton(R.string.other_folder, { dialogInterface, i -> showOtherFolder() }) .setNeutralButton(R.string.other_folder, { dialogInterface, i -> showOtherFolder() })
.create().apply { .create().apply {
activity.setupDialogStuff(view, this, R.string.select_destination) activity.setupDialogStuff(view, this, R.string.select_destination)
val dirs = activity.getCachedDirectories()
if (dirs.isNotEmpty()) {
gotDirectories(dirs)
}
GetDirectoriesAsynctask(activity, false, false) { GetDirectoriesAsynctask(activity, false, false) {
val adapter = DirectoryAdapter(activity, it, null) { gotDirectories(it)
if (it.path.trimEnd('/') == sourcePath) {
activity.toast(R.string.source_and_destination_same)
return@DirectoryAdapter
} else {
callback.invoke(it.path)
dismiss()
}
}
directoriesGrid.adapter = adapter
}.execute() }.execute()
} }
} }
@ -48,4 +48,21 @@ class PickAlbumDialog(val activity: SimpleActivity, val sourcePath: String, val
callback.invoke(it) callback.invoke(it)
} }
} }
private fun gotDirectories(directories: ArrayList<Directory>) {
if (directories.hashCode() == shownDirectories.hashCode())
return
shownDirectories = directories
val adapter = DirectoryAdapter(activity, directories, null) {
if (it.path.trimEnd('/') == sourcePath) {
activity.toast(R.string.source_and_destination_same)
return@DirectoryAdapter
} else {
callback.invoke(it.path)
dialog.dismiss()
}
}
directoriesGrid.adapter = adapter
}
} }

View file

@ -14,6 +14,8 @@ import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.signature.StringSignature import com.bumptech.glide.signature.StringSignature
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.gallery.BuildConfig import com.simplemobiletools.gallery.BuildConfig
@ -22,6 +24,7 @@ import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.helpers.NOMEDIA import com.simplemobiletools.gallery.helpers.NOMEDIA
import com.simplemobiletools.gallery.helpers.REQUEST_EDIT_IMAGE import com.simplemobiletools.gallery.helpers.REQUEST_EDIT_IMAGE
import com.simplemobiletools.gallery.helpers.REQUEST_SET_WALLPAPER import com.simplemobiletools.gallery.helpers.REQUEST_SET_WALLPAPER
import com.simplemobiletools.gallery.models.Directory
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import com.simplemobiletools.gallery.views.MySquareImageView import com.simplemobiletools.gallery.views.MySquareImageView
import java.io.File import java.io.File
@ -253,3 +256,8 @@ fun Activity.loadStaticGif(path: String, target: MySquareImageView) {
if (config.cropThumbnails) builder.centerCrop() else builder.fitCenter() if (config.cropThumbnails) builder.centerCrop() else builder.fitCenter()
builder.into(target) builder.into(target)
} }
fun Activity.getCachedDirectories(): ArrayList<Directory> {
val token = object : TypeToken<List<Directory>>() {}.type
return Gson().fromJson<ArrayList<Directory>>(config.directories, token) ?: ArrayList<Directory>(1)
}