diff --git a/gallery/src/main/java/com/jt/gallery/FullScreenView.kt b/gallery/src/main/java/com/jt/gallery/FullScreenView.kt index 0e03ed3..2f37e49 100644 --- a/gallery/src/main/java/com/jt/gallery/FullScreenView.kt +++ b/gallery/src/main/java/com/jt/gallery/FullScreenView.kt @@ -4,4 +4,5 @@ interface FullScreenView { fun showSystemUI() fun hideSystemUI() fun isNavigationVisible(): Boolean + fun resetAdapter(index: Int) } \ No newline at end of file diff --git a/gallery/src/main/java/com/jt/gallery/GalleryActivity.kt b/gallery/src/main/java/com/jt/gallery/GalleryActivity.kt index 5d32d28..a4127db 100644 --- a/gallery/src/main/java/com/jt/gallery/GalleryActivity.kt +++ b/gallery/src/main/java/com/jt/gallery/GalleryActivity.kt @@ -11,20 +11,23 @@ import kotlin.collections.ArrayList private const val PAGER_OFFSCREEN_LIMIT = 10 -private const val IMAGE_LIST = "imageList" private const val POSITION = "position" private const val FULL_BRIGHTNESS = "fullBrightness" +private const val ALLOW_DELETE = "allowDelete" class GalleryActivity : AppCompatActivity(), FullScreenView { companion object { + const val IMAGE_LIST = "imageList" + @JvmStatic @JvmOverloads - fun newIntent(context: Context, imageList: List, currentImage: Int = 0, useFullBrightness: Boolean = false) = + fun newIntent(context: Context, imageList: List, currentImage: Int = 0, useFullBrightness: Boolean = false, allowDelete: Boolean = false) = Intent(context, GalleryActivity::class.java).apply { putExtra(IMAGE_LIST, ArrayList(imageList)) putExtra(POSITION, currentImage) putExtra(FULL_BRIGHTNESS, useFullBrightness) + putExtra(ALLOW_DELETE, allowDelete) } } @@ -37,19 +40,18 @@ class GalleryActivity : AppCompatActivity(), FullScreenView { setContentView(R.layout.activity_gallery) val currentImage: Int - val adjustBrightness: Boolean + val adjustBrightness = intent.getBooleanExtra(FULL_BRIGHTNESS, false) + val allowDelete = intent.getBooleanExtra(ALLOW_DELETE, false) if (savedInstanceState != null) { imageList = savedInstanceState.getStringArrayList(IMAGE_LIST) ?: arrayListOf() currentImage = savedInstanceState.getInt(POSITION) - adjustBrightness = intent.getBooleanExtra(FULL_BRIGHTNESS, false) } else { imageList = intent.getStringArrayListExtra(IMAGE_LIST) currentImage = intent.getIntExtra(POSITION, 0) - adjustBrightness = intent.getBooleanExtra(FULL_BRIGHTNESS, false) } - val galleryAdapter = GalleryAdapter(this, imageList) + val galleryAdapter = GalleryAdapter(this, imageList, allowDelete) galleryViewPager.apply { offscreenPageLimit = PAGER_OFFSCREEN_LIMIT @@ -95,6 +97,17 @@ class GalleryActivity : AppCompatActivity(), FullScreenView { override fun isNavigationVisible(): Boolean = window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_HIDE_NAVIGATION == 0 + override fun resetAdapter(index: Int) { + // Need to reset the adapter to update it + galleryViewPager.adapter = GalleryAdapter(this, imageList, true) + galleryViewPager.currentItem = index + } + + override fun onBackPressed() { + setResult(RESULT_OK, Intent().putStringArrayListExtra(IMAGE_LIST, imageList)) + super.onBackPressed() + } + private fun setScreenBrightnessTo(brightness: Float) { val lp = window.attributes if (lp.screenBrightness == brightness) { diff --git a/gallery/src/main/java/com/jt/gallery/GalleryAdapter.kt b/gallery/src/main/java/com/jt/gallery/GalleryAdapter.kt index 58de453..97a1250 100644 --- a/gallery/src/main/java/com/jt/gallery/GalleryAdapter.kt +++ b/gallery/src/main/java/com/jt/gallery/GalleryAdapter.kt @@ -2,6 +2,7 @@ package com.jt.gallery import android.graphics.drawable.Drawable import android.support.v4.view.PagerAdapter +import android.support.v7.app.AlertDialog import android.util.SparseArray import android.view.LayoutInflater import android.view.View @@ -17,7 +18,8 @@ import uk.co.senab.photoview.PhotoViewAttacher class GalleryAdapter( private val view: FullScreenView, - private val images: ArrayList + private val images: ArrayList, + private val allowDelete: Boolean = false ) : PagerAdapter() { private val views: SparseArray = SparseArray() @@ -42,6 +44,23 @@ class GalleryAdapter( val photoView = PhotoViewAttacher(rootView.galleryImage) photoView.onViewTapListener = tapListener photoView.scaleType = ImageView.ScaleType.FIT_CENTER + + if (allowDelete) { + photoView.setOnLongClickListener { + AlertDialog.Builder(it.context) + .setMessage(R.string.delete_message) + .setPositiveButton(R.string.delete) { dialog, _ -> + deleteItem(position) + dialog.dismiss() + } + .setNegativeButton(android.R.string.cancel) { dialog, _ -> dialog.dismiss() } + .create() + .show() + + true + } + } + return false } @@ -67,6 +86,14 @@ class GalleryAdapter( override fun getCount() = images.size + private fun deleteItem(position: Int) { + images.removeAt(position) + views.remove(position) + notifyDataSetChanged() + + view.resetAdapter(if (position > images.size - 1) images.size - 1 else position) + } + private val tapListener = PhotoViewAttacher.OnViewTapListener { _, _, _ -> if (view.isNavigationVisible()) { view.hideSystemUI() diff --git a/gallery/src/main/res/values/strings.xml b/gallery/src/main/res/values/strings.xml new file mode 100644 index 0000000..a8fad66 --- /dev/null +++ b/gallery/src/main/res/values/strings.xml @@ -0,0 +1,5 @@ + + + Delete item? + Delete +