rewrite the mediapicker dialog, make it work like picking directories

This commit is contained in:
tibbi 2017-10-19 17:24:14 +02:00
parent 906168c6b5
commit 021a1cbf74
5 changed files with 88 additions and 24 deletions

View file

@ -18,7 +18,6 @@ import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition import com.bumptech.glide.request.transition.Transition
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
@ -343,8 +342,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
return return
mIsGettingMedia = true mIsGettingMedia = true
val token = object : TypeToken<List<Medium>>() {}.type val media = getCachedMedia(mPath)
val media = Gson().fromJson<ArrayList<Medium>>(config.loadFolderMedia(mPath), token) ?: ArrayList(1)
if (media.isNotEmpty() && !mLoadedInitialPhotos) { if (media.isNotEmpty() && !mLoadedInitialPhotos) {
gotMedia(media, true) gotMedia(media, true)
} else { } else {

View file

@ -3,7 +3,6 @@ package com.simplemobiletools.gallery.dialogs
import android.support.v7.app.AlertDialog import android.support.v7.app.AlertDialog
import android.support.v7.widget.GridLayoutManager import android.support.v7.widget.GridLayoutManager
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View
import com.simplemobiletools.commons.dialogs.FilePickerDialog import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.extensions.beGoneIf import com.simplemobiletools.commons.extensions.beGoneIf
import com.simplemobiletools.commons.extensions.beVisibleIf import com.simplemobiletools.commons.extensions.beVisibleIf
@ -22,8 +21,8 @@ import kotlinx.android.synthetic.main.dialog_directory_picker.view.*
class PickDirectoryDialog(val activity: SimpleActivity, val sourcePath: String, val callback: (path: String) -> Unit) { class PickDirectoryDialog(val activity: SimpleActivity, val sourcePath: String, val callback: (path: String) -> Unit) {
var dialog: AlertDialog var dialog: AlertDialog
var shownDirectories: ArrayList<Directory> = ArrayList() var shownDirectories = ArrayList<Directory>()
var view: View = LayoutInflater.from(activity).inflate(R.layout.dialog_directory_picker, null) var view = LayoutInflater.from(activity).inflate(R.layout.dialog_directory_picker, null)
var isGridViewType = activity.config.viewTypeFolders == VIEW_TYPE_GRID var isGridViewType = activity.config.viewTypeFolders == VIEW_TYPE_GRID
init { init {

View file

@ -1,27 +1,32 @@
package com.simplemobiletools.gallery.dialogs package com.simplemobiletools.gallery.dialogs
import android.support.v7.app.AlertDialog import android.support.v7.app.AlertDialog
import android.support.v7.widget.RecyclerView import android.support.v7.widget.GridLayoutManager
import android.view.LayoutInflater import android.view.LayoutInflater
import com.google.gson.Gson import com.simplemobiletools.commons.extensions.beGoneIf
import com.google.gson.reflect.TypeToken import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.setupDialogStuff import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SimpleActivity import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.adapters.MediaAdapter import com.simplemobiletools.gallery.adapters.MediaAdapter
import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask
import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.getCachedMedia
import com.simplemobiletools.gallery.helpers.VIEW_TYPE_GRID
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import kotlinx.android.synthetic.main.dialog_medium_picker.view.* import kotlinx.android.synthetic.main.dialog_medium_picker.view.*
class PickMediumDialog(val activity: SimpleActivity, val path: String, val callback: (path: String) -> Unit) { class PickMediumDialog(val activity: SimpleActivity, val path: String, val callback: (path: String) -> Unit) {
var dialog: AlertDialog var dialog: AlertDialog
var mediaGrid: RecyclerView var shownMedia = ArrayList<Medium>()
var shownMedia: ArrayList<Medium> = ArrayList() val view = LayoutInflater.from(activity).inflate(R.layout.dialog_medium_picker, null)
var isGridViewType = activity.config.viewTypeFiles == VIEW_TYPE_GRID
init { init {
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_medium_picker, null) (view.media_grid.layoutManager as GridLayoutManager).apply {
mediaGrid = view.media_grid orientation = if (activity.config.scrollHorizontally && isGridViewType) GridLayoutManager.HORIZONTAL else GridLayoutManager.VERTICAL
spanCount = if (isGridViewType) activity.config.mediaColumnCnt else 1
}
dialog = AlertDialog.Builder(activity) dialog = AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, null) .setPositiveButton(R.string.ok, null)
@ -29,10 +34,7 @@ class PickMediumDialog(val activity: SimpleActivity, val path: String, val callb
.create().apply { .create().apply {
activity.setupDialogStuff(view, this, R.string.select_photo) activity.setupDialogStuff(view, this, R.string.select_photo)
val token = object : TypeToken<List<Medium>>() {}.type val media = activity.getCachedMedia(path).filter { !it.video } as ArrayList
var media = Gson().fromJson<ArrayList<Medium>>(activity.config.loadFolderMedia(path), token) ?: ArrayList(1)
media = media.filter { !it.video } as ArrayList
if (media.isNotEmpty()) { if (media.isNotEmpty()) {
gotMedia(media) gotMedia(media)
} }
@ -52,6 +54,22 @@ class PickMediumDialog(val activity: SimpleActivity, val path: String, val callb
callback(it.path) callback(it.path)
dialog.dismiss() dialog.dismiss()
} }
mediaGrid.adapter = adapter
val scrollHorizontally = activity.config.scrollHorizontally && isGridViewType
view.apply {
media_grid.adapter = adapter
media_vertical_fastscroller.isHorizontal = false
media_vertical_fastscroller.beGoneIf(scrollHorizontally)
media_horizontal_fastscroller.isHorizontal = true
media_horizontal_fastscroller.beVisibleIf(scrollHorizontally)
if (scrollHorizontally) {
media_horizontal_fastscroller.setViews(media_grid)
} else {
media_vertical_fastscroller.setViews(media_grid)
}
}
} }
} }

View file

@ -337,3 +337,8 @@ fun Activity.getCachedDirectories(): ArrayList<Directory> {
val token = object : TypeToken<List<Directory>>() {}.type val token = object : TypeToken<List<Directory>>() {}.type
return Gson().fromJson<ArrayList<Directory>>(config.directories, token) ?: ArrayList<Directory>(1) return Gson().fromJson<ArrayList<Directory>>(config.directories, token) ?: ArrayList<Directory>(1)
} }
fun Activity.getCachedMedia(path: String): ArrayList<Medium> {
val token = object : TypeToken<List<Medium>>() {}.type
return Gson().fromJson<ArrayList<Medium>>(config.loadFolderMedia(path), token) ?: ArrayList(1)
}

View file

@ -1,10 +1,54 @@
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/media_grid" android:id="@+id/media_grid_holder"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingTop="@dimen/activity_margin" android:paddingTop="@dimen/activity_margin">
app:layoutManager="android.support.v7.widget.GridLayoutManager"
app:spanCount="@integer/media_columns_vertical_scroll"/> <com.simplemobiletools.commons.views.MyScalableRecyclerView
android:id="@+id/media_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
app:spanCount="@integer/media_columns_vertical_scroll"/>
<com.simplemobiletools.commons.views.FastScroller
android:id="@+id/media_vertical_fastscroller"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:paddingLeft="@dimen/normal_margin"
android:paddingStart="@dimen/normal_margin">
<ImageView
android:id="@+id/fastscroller_handle_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:alpha="0"
android:background="@drawable/fastscroller_handle_vertical"/>
</com.simplemobiletools.commons.views.FastScroller>
<com.simplemobiletools.commons.views.FastScroller
android:id="@+id/media_horizontal_fastscroller"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingTop="@dimen/normal_margin">
<ImageView
android:id="@+id/fastscroller_handle_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:alpha="0"
android:background="@drawable/fastscroller_handle_horizontal"/>
</com.simplemobiletools.commons.views.FastScroller>
</RelativeLayout>