Improve comment deletion

feature/players
Aurelien Hubert 6 years ago
parent 9ad428152c
commit d90aaa72bc
  1. 4
      app/src/main/java/net/pokeranalytics/android/model/realm/Comment.kt
  2. 7
      app/src/main/java/net/pokeranalytics/android/model/realm/CustomField.kt
  3. 15
      app/src/main/java/net/pokeranalytics/android/model/realm/Player.kt
  4. 38
      app/src/main/java/net/pokeranalytics/android/ui/fragment/data/PlayerDataFragment.kt

@ -37,6 +37,9 @@ open class Comment : RealmObject(), Manageable, RowRepresentable {
@Ignore
override val inputFragmentType: InputFragmentType = InputFragmentType.EDIT_TEXT_MULTI_LINES
@Ignore
override val valueCanBeClearedWhenEditing: Boolean = false
override fun localizedTitle(context: Context): String {
return context.getString(R.string.comment)
}
@ -53,6 +56,7 @@ open class Comment : RealmObject(), Manageable, RowRepresentable {
InputFragment.buildAndShow(this, parent, data, isDeletable = true)
}
override fun updateValue(value: Any?, row: RowRepresentable) {
this.content = value as String? ?: ""
}

@ -19,8 +19,8 @@ import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource
import net.pokeranalytics.android.ui.fragment.components.input.InputFragment
import net.pokeranalytics.android.ui.fragment.components.input.InputFragmentType
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowEditableDataSource
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.ui.view.rowrepresentable.CustomFieldRow
import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable
@ -253,10 +253,11 @@ open class CustomField : RealmObject(), NameManageable, StaticRowRepresentableDa
updateRowRepresentation()
}
/**
* Cleanup deleted entries
*/
fun cleanupEntries() { // called when saving the custom field
val realm = Realm.getDefaultInstance()
realm.executeTransaction {
this.entriesToDelete.forEach { // entries are out of realm
realm.where<CustomFieldEntry>().equalTo("id", it.id).findFirst()?.deleteFromRealm()

@ -6,6 +6,7 @@ import io.realm.RealmList
import io.realm.RealmObject
import io.realm.annotations.Ignore
import io.realm.annotations.PrimaryKey
import io.realm.kotlin.where
import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.interfaces.Deletable
import net.pokeranalytics.android.model.interfaces.DeleteValidityStatus
@ -151,4 +152,18 @@ open class Player : RealmObject(), NameManageable, Deletable, StaticRowRepresent
updateRowRepresentation()
}
/**
* Clean up deleted entries
*/
fun cleanupComments() { // called when saving the custom field
val realm = Realm.getDefaultInstance()
realm.executeTransaction {
this.commentsToDelete.forEach { // entries are out of realm
realm.where<Comment>().equalTo("id", it.id).findFirst()?.deleteFromRealm()
}
}
realm.close()
this.commentsToDelete.clear()
}
}

@ -9,6 +9,10 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_player.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.realm.Comment
import net.pokeranalytics.android.model.realm.Player
@ -16,6 +20,7 @@ import net.pokeranalytics.android.ui.activity.ColorPickerActivity
import net.pokeranalytics.android.ui.activity.components.MediaActivity
import net.pokeranalytics.android.ui.adapter.RowRepresentableDataSource
import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource
import net.pokeranalytics.android.ui.extensions.showAlertDialog
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.ui.view.rowrepresentable.PlayerRow
@ -127,26 +132,15 @@ class PlayerDataFragment : EditableDataFragment(), StaticRowRepresentableDataSou
super.onRowDeleted(row)
when (row) {
is Comment -> {
if (!row.isValidForDelete(getRealm())) {
} else {
player.deleteComment(row)
rowRepresentableAdapter.notifyDataSetChanged()
}
/*
if (!row.isValidForDelete(getRealm())) {
val status = row.getDeleteStatus(requireContext(), getRealm())
val message = row.getFailedDeleteMessage(status)
showAlertDialog(requireContext(), R.string.cf_entry_delete_popup_title, message, showCancelButton = true, positiveAction = {
customField.deleteEntry(row)
rowRepresentableAdapter.notifyDataSetChanged()
})
} else {
customField.deleteEntry(row)
rowRepresentableAdapter.notifyDataSetChanged()
if (row.isValidForDelete(getRealm())) {
GlobalScope.launch(Dispatchers.Main) {
delay(300)
showAlertDialog(requireContext(), message = R.string.are_you_sure_you_want_to_delete, showCancelButton = true, positiveAction = {
player.deleteComment(row)
rowRepresentableAdapter.notifyDataSetChanged()
})
}
}
*/
}
}
}
@ -201,4 +195,10 @@ class PlayerDataFragment : EditableDataFragment(), StaticRowRepresentableDataSou
builder.show()
}
override fun onDataSaved() {
super.onDataSaved()
player.cleanupComments()
}
}
Loading…
Cancel
Save