commit
595bea05a6
@ -0,0 +1,114 @@ |
||||
package net.pokeranalytics.android.ui.fragment |
||||
|
||||
import android.os.Bundle |
||||
import android.view.View |
||||
import androidx.appcompat.app.AlertDialog |
||||
import androidx.coordinatorlayout.widget.CoordinatorLayout |
||||
import com.google.android.material.snackbar.Snackbar |
||||
import io.realm.RealmObject |
||||
import net.pokeranalytics.android.R |
||||
import net.pokeranalytics.android.model.interfaces.Deletable |
||||
import net.pokeranalytics.android.model.interfaces.Identifiable |
||||
import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter |
||||
import net.pokeranalytics.android.ui.fragment.components.RealmFragment |
||||
|
||||
/** |
||||
* Deletable Item Fragment |
||||
* Don't forget to add a CoordinatorLayout at the top of your XML if you want to display correctly the snack bar |
||||
*/ |
||||
open class DeletableItemFragment : RealmFragment() { |
||||
|
||||
private var deletedItem: RealmObject? = null |
||||
private var lastDeletedItemPosition: Int = 0 |
||||
private var dataListAdapter: RowRepresentableAdapter? = null |
||||
private var coordinatorLayout: CoordinatorLayout? = null |
||||
private var snackBar: Snackbar? = null |
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
||||
super.onViewCreated(view, savedInstanceState) |
||||
this.coordinatorLayout = view.findViewById(R.id.coordinatorLayout) |
||||
} |
||||
|
||||
override fun onPause() { |
||||
super.onPause() |
||||
snackBar?.dismiss() |
||||
} |
||||
|
||||
/** |
||||
* Delete item |
||||
* [dataListAdapter]: Adapter to update |
||||
* [items]: List of items which contains the element to delete |
||||
* [itemId]: Id of the item to delete |
||||
* [container]: View to display the Snackbar |
||||
*/ |
||||
fun deleteItem(dataListAdapter: RowRepresentableAdapter, items: List<*>, itemId: String) { |
||||
|
||||
if (isDetached || activity == null) { |
||||
return |
||||
} |
||||
|
||||
this.dataListAdapter = dataListAdapter |
||||
|
||||
// Save the delete position & create a copy of the object |
||||
val itemPosition = items.indexOfFirst { (it as Identifiable).id == itemId } |
||||
val itemToDelete = items.find { (it as Identifiable).id == itemId } |
||||
|
||||
if (itemToDelete is RealmObject && itemPosition != -1) { |
||||
|
||||
val deletableItem = (itemToDelete as Deletable) |
||||
|
||||
// Check if the object is valid for the deletion |
||||
if (deletableItem.isValidForDelete(this.getRealm())) { |
||||
deletedItem = getRealm().copyFromRealm(itemToDelete) |
||||
lastDeletedItemPosition = itemPosition |
||||
getRealm().executeTransaction { |
||||
itemToDelete.deleteFromRealm() |
||||
} |
||||
updateUIAfterDeletion(itemPosition) |
||||
showUndoSnackBar() |
||||
} else { |
||||
dataListAdapter.notifyItemChanged(itemPosition) |
||||
val status = deletableItem.getDeleteStatus(this.getRealm()) |
||||
val message = deletableItem.getFailedDeleteMessage(status) |
||||
val builder = AlertDialog.Builder(requireContext()) |
||||
.setMessage(message) |
||||
.setNegativeButton(R.string.ok, null) |
||||
builder.show() |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Show undo snack bar |
||||
*/ |
||||
private fun showUndoSnackBar() { |
||||
val message = String.format(getString(R.string.data_deleted)) |
||||
this.coordinatorLayout?.let { view -> |
||||
snackBar = Snackbar.make(view, message, Snackbar.LENGTH_INDEFINITE) |
||||
snackBar?.setAction(R.string.cancel) { |
||||
getRealm().executeTransaction { realm -> |
||||
deletedItem?.let { |
||||
val item = realm.copyToRealmOrUpdate(it) |
||||
updateUIAfterUndoDeletion(item) |
||||
} |
||||
} |
||||
} |
||||
snackBar?.show() |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Called once the object has been deleted |
||||
*/ |
||||
open fun updateUIAfterDeletion(itemPosition: Int) { |
||||
dataListAdapter?.notifyItemRemoved(itemPosition) |
||||
} |
||||
|
||||
/** |
||||
* Called once the object has been restored |
||||
*/ |
||||
open fun updateUIAfterUndoDeletion(newItem: RealmObject) { |
||||
dataListAdapter?.notifyItemInserted(lastDeletedItemPosition) |
||||
} |
||||
|
||||
} |
||||
@ -1,6 +1,33 @@ |
||||
package net.pokeranalytics.android.ui.view.rowrepresentable |
||||
|
||||
import net.pokeranalytics.android.R |
||||
import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetType |
||||
import net.pokeranalytics.android.ui.view.DefaultEditDataSource |
||||
import net.pokeranalytics.android.ui.view.RowRepresentable |
||||
import net.pokeranalytics.android.ui.view.RowViewType |
||||
|
||||
enum class TransactionTypeRow : RowRepresentable, DefaultEditDataSource |
||||
enum class TransactionTypeRow : RowRepresentable, DefaultEditDataSource { |
||||
TRANSACTION_ADDITIVE; |
||||
|
||||
override val resId: Int? |
||||
get() { |
||||
return when (this) { |
||||
TRANSACTION_ADDITIVE -> R.string.additive |
||||
} |
||||
} |
||||
|
||||
override val viewType: Int |
||||
get() { |
||||
return when (this) { |
||||
TRANSACTION_ADDITIVE -> RowViewType.TITLE_SWITCH.ordinal |
||||
} |
||||
} |
||||
|
||||
override val bottomSheetType: BottomSheetType |
||||
get() { |
||||
return when (this) { |
||||
TRANSACTION_ADDITIVE -> BottomSheetType.NONE |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
Loading…
Reference in new issue