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 c8f16d81e..96080dfb6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -13,7 +13,6 @@ import android.support.v7.widget.GridLayoutManager import android.view.Menu import android.view.MenuItem import com.google.gson.Gson -import com.google.gson.reflect.TypeToken import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.models.Release import com.simplemobiletools.gallery.BuildConfig @@ -21,10 +20,7 @@ import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.adapters.DirectoryAdapter import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog -import com.simplemobiletools.gallery.extensions.config -import com.simplemobiletools.gallery.extensions.launchAbout -import com.simplemobiletools.gallery.extensions.launchCamera -import com.simplemobiletools.gallery.extensions.launchSettings +import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.views.MyScalableRecyclerView @@ -159,9 +155,8 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { return mIsGettingDirs = true - val token = object : TypeToken>() {}.type - val dirs = Gson().fromJson>(config.directories, token) ?: ArrayList(1) - if (dirs.size != 0 && !mLoadedInitialPhotos) { + val dirs = getCachedDirectories() + if (dirs.isNotEmpty() && !mLoadedInitialPhotos) { gotDirectories(dirs) } 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 cb300dd20..f18c096a8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -221,7 +221,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { mIsGettingMedia = true val token = object : TypeToken>() {}.type val media = Gson().fromJson>(config.loadFolderMedia(mPath), token) ?: ArrayList(1) - if (media.size == 0 && !mLoadedInitialPhotos) { + if (media.isNotEmpty() && !mLoadedInitialPhotos) { gotMedia(media) } 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 94f2e5162..9318120bb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickAlbumDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickAlbumDialog.kt @@ -11,33 +11,33 @@ import com.simplemobiletools.gallery.activities.SimpleActivity import com.simplemobiletools.gallery.adapters.DirectoryAdapter import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask 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.* class PickAlbumDialog(val activity: SimpleActivity, val sourcePath: String, val callback: (path: String) -> Unit) { + var dialog: AlertDialog var directoriesGrid: RecyclerView + var shownDirectories: ArrayList = ArrayList() init { val view = LayoutInflater.from(activity).inflate(R.layout.dialog_album_picker, null) directoriesGrid = view.directories_grid - AlertDialog.Builder(activity) + dialog = AlertDialog.Builder(activity) .setPositiveButton(R.string.ok, null) .setNegativeButton(R.string.cancel, null) .setNeutralButton(R.string.other_folder, { dialogInterface, i -> showOtherFolder() }) .create().apply { activity.setupDialogStuff(view, this, R.string.select_destination) + val dirs = activity.getCachedDirectories() + if (dirs.isNotEmpty()) { + gotDirectories(dirs) + } + GetDirectoriesAsynctask(activity, false, false) { - val adapter = DirectoryAdapter(activity, it, null) { - if (it.path.trimEnd('/') == sourcePath) { - activity.toast(R.string.source_and_destination_same) - return@DirectoryAdapter - } else { - callback.invoke(it.path) - dismiss() - } - } - directoriesGrid.adapter = adapter + gotDirectories(it) }.execute() } } @@ -48,4 +48,21 @@ class PickAlbumDialog(val activity: SimpleActivity, val sourcePath: String, val callback.invoke(it) } } + + private fun gotDirectories(directories: ArrayList) { + 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 + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt index ee9c44b50..62bdf7921 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt @@ -14,6 +14,8 @@ import com.bumptech.glide.Glide import com.bumptech.glide.load.DecodeFormat import com.bumptech.glide.load.engine.DiskCacheStrategy 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.helpers.* 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.REQUEST_EDIT_IMAGE import com.simplemobiletools.gallery.helpers.REQUEST_SET_WALLPAPER +import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.views.MySquareImageView import java.io.File @@ -253,3 +256,8 @@ fun Activity.loadStaticGif(path: String, target: MySquareImageView) { if (config.cropThumbnails) builder.centerCrop() else builder.fitCenter() builder.into(target) } + +fun Activity.getCachedDirectories(): ArrayList { + val token = object : TypeToken>() {}.type + return Gson().fromJson>(config.directories, token) ?: ArrayList(1) +}