Skip to content

Commit

Permalink
Merge pull request #2 from jt-gilkeson/feature/support_delete
Browse files Browse the repository at this point in the history
Added ability to remove photos
  • Loading branch information
jt-gilkeson authored Jun 17, 2019
2 parents 5d055a5 + 4b4ec0d commit cd0d67a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
1 change: 1 addition & 0 deletions gallery/src/main/java/com/jt/gallery/FullScreenView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ interface FullScreenView {
fun showSystemUI()
fun hideSystemUI()
fun isNavigationVisible(): Boolean
fun resetAdapter(index: Int)
}
25 changes: 19 additions & 6 deletions gallery/src/main/java/com/jt/gallery/GalleryActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>, currentImage: Int = 0, useFullBrightness: Boolean = false) =
fun newIntent(context: Context, imageList: List<String>, 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)
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
29 changes: 28 additions & 1 deletion gallery/src/main/java/com/jt/gallery/GalleryAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,7 +18,8 @@ import uk.co.senab.photoview.PhotoViewAttacher

class GalleryAdapter(
private val view: FullScreenView,
private val images: ArrayList<String>
private val images: ArrayList<String>,
private val allowDelete: Boolean = false
) : PagerAdapter() {
private val views: SparseArray<View> = SparseArray()

Expand All @@ -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
}

Expand All @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions gallery/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="delete_message">Delete item?</string>
<string name="delete">Delete</string>
</resources>

0 comments on commit cd0d67a

Please sign in to comment.