From 3049e8f1e8060588269aded1da85965b5daf57e5 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 19 Nov 2016 19:57:08 +0100 Subject: [PATCH] add Share functionality to images --- .../gallery/activities/MediaActivity.kt | 39 +------------------ .../gallery/adapters/MediaAdapter.kt | 24 ++++++++++++ .../gallery/extensions/activity.kt | 15 +++++++ 3 files changed, 40 insertions(+), 38 deletions(-) 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 0c74c4d0d..3b7ecbc16 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -231,40 +231,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On false } - private fun shareMedia() { - /*val selectedMedia = getSelectedMedia() - if (selectedMedia.size <= 1) { - shareMedium(selectedMedia[0]) - } else { - shareMedia(selectedMedia) - }*/ - } - - private fun shareMedia(media: List) { - val shareTitle = resources.getString(R.string.share_via) - Intent().apply { - action = Intent.ACTION_SEND_MULTIPLE - type = "image/* video/*" - val uris = ArrayList(media.size) - media.map { File(it.path) } - .mapTo(uris) { Uri.fromFile(it) } - - putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris) - startActivity(Intent.createChooser(this, shareTitle)) - } - } - - /*private fun getSelectedMedia(): List { - val items = media_grid.checkedItemPositions - val cnt = items.size() - val media = (0..cnt - 1) - .filter { items.valueAt(it) } - .map { mMedia[items.keyAt(it)] } - - return media - } - - private fun prepareForDeleting() { + /*private fun prepareForDeleting() { if (isShowingPermDialog(File(mPath))) return @@ -423,10 +390,6 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On /*override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { when (item.itemId) { - R.id.cab_share -> { - shareMedia() - return true - } R.id.cab_delete -> { prepareForDeleting() mode.finish() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt index 1e8996a20..a08476fd5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt @@ -15,6 +15,8 @@ import com.simplemobiletools.gallery.Config import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.activities.SimpleActivity import com.simplemobiletools.gallery.extensions.beVisibleIf +import com.simplemobiletools.gallery.extensions.shareMedia +import com.simplemobiletools.gallery.extensions.shareMedium import com.simplemobiletools.gallery.models.Medium import kotlinx.android.synthetic.main.photo_video_item.view.* import kotlinx.android.synthetic.main.photo_video_tmb.view.* @@ -45,6 +47,10 @@ class MediaAdapter(val activity: SimpleActivity, val media: MutableList, showProperties() true } + R.id.cab_share -> { + shareMedia() + return true + } else -> false } } @@ -75,6 +81,24 @@ class MediaAdapter(val activity: SimpleActivity, val media: MutableList, } } + private fun shareMedia() { + val selections = multiSelector.selectedPositions + if (selections.size <= 1) { + activity.shareMedium(getSelectedMedia()[0]) + } else { + activity.shareMedia(getSelectedMedia()) + } + } + + private fun getSelectedMedia(): List { + val selections = multiSelector.selectedPositions + val cnt = selections.size + val selectedMedia = (0..cnt - 1) + .map { media[selections[it]] } + + return selectedMedia + } + override fun onBindViewHolder(holder: ViewHolder, position: Int) { views.add(holder.bindView(activity, multiSelectorMode, multiSelector, media[position])) } 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 7907ab375..d61a9d903 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt @@ -6,6 +6,7 @@ import android.net.Uri import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.models.Medium import java.io.File +import java.util.* fun Activity.shareMedium(medium: Medium) { val shareTitle = resources.getString(R.string.share_via) @@ -18,3 +19,17 @@ fun Activity.shareMedium(medium: Medium) { startActivity(Intent.createChooser(this, shareTitle)) } } + +fun Activity.shareMedia(media: List) { + val shareTitle = resources.getString(R.string.share_via) + Intent().apply { + action = Intent.ACTION_SEND_MULTIPLE + type = "image/* video/*" + val uris = ArrayList(media.size) + media.map { File(it.path) } + .mapTo(uris) { Uri.fromFile(it) } + + putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris) + startActivity(Intent.createChooser(this, shareTitle)) + } +}