parent
7cb63575ac
commit
9bf524b1e3
@ -0,0 +1,134 @@ |
|||||||
|
package net.pokeranalytics.android.ui.modules.data |
||||||
|
|
||||||
|
import android.content.Context |
||||||
|
import io.realm.Realm |
||||||
|
import io.realm.kotlin.where |
||||||
|
import net.pokeranalytics.android.R |
||||||
|
import net.pokeranalytics.android.model.realm.Comment |
||||||
|
import net.pokeranalytics.android.model.realm.Player |
||||||
|
import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource |
||||||
|
import net.pokeranalytics.android.ui.view.RowRepresentable |
||||||
|
import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor |
||||||
|
import net.pokeranalytics.android.ui.view.RowViewType |
||||||
|
import net.pokeranalytics.android.ui.view.rows.CustomizableRowRepresentable |
||||||
|
import net.pokeranalytics.android.ui.view.rows.PlayerPropertiesRow |
||||||
|
import net.pokeranalytics.android.ui.view.rows.SeparatorRow |
||||||
|
import net.pokeranalytics.android.ui.viewmodel.DataManagerViewModel |
||||||
|
import net.pokeranalytics.android.util.NULL_TEXT |
||||||
|
import net.pokeranalytics.android.util.extensions.isSameDay |
||||||
|
import net.pokeranalytics.android.util.extensions.mediumDate |
||||||
|
import java.util.* |
||||||
|
|
||||||
|
class PlayerDataViewModel : DataManagerViewModel(), StaticRowRepresentableDataSource { |
||||||
|
|
||||||
|
private var rowRepresentation: List<RowRepresentable> = mutableListOf() |
||||||
|
|
||||||
|
private var commentsToDelete: ArrayList<Comment> = ArrayList() |
||||||
|
|
||||||
|
private val player: Player |
||||||
|
get() { |
||||||
|
return this.item as Player |
||||||
|
} |
||||||
|
|
||||||
|
override fun adapterRows(): List<RowRepresentable> { |
||||||
|
return this.rowRepresentation |
||||||
|
} |
||||||
|
|
||||||
|
override fun charSequenceForRow( |
||||||
|
row: RowRepresentable, |
||||||
|
context: Context, |
||||||
|
tag: Int |
||||||
|
): CharSequence { |
||||||
|
return when (row) { |
||||||
|
PlayerPropertiesRow.NAME -> this.player.name.ifEmpty { NULL_TEXT } |
||||||
|
else -> return super.charSequenceForRow(row, context, 0) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fun updateRowRepresentation() { |
||||||
|
this.rowRepresentation = this.updatedRowRepresentationForCurrentState() |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Update the row representation |
||||||
|
*/ |
||||||
|
private fun updatedRowRepresentationForCurrentState(): List<RowRepresentable> { |
||||||
|
val rows = ArrayList<RowRepresentable>() |
||||||
|
rows.add(PlayerPropertiesRow.IMAGE) |
||||||
|
rows.add(PlayerPropertiesRow.NAME) |
||||||
|
rows.add(PlayerPropertiesRow.SUMMARY) |
||||||
|
|
||||||
|
if (this.player.comments.size > 0) { |
||||||
|
// Adds Comments section |
||||||
|
rows.add(CustomizableRowRepresentable(RowViewType.HEADER_TITLE, R.string.comments)) |
||||||
|
|
||||||
|
val currentCommentCalendar = Calendar.getInstance() |
||||||
|
val currentDateCalendar = Calendar.getInstance() |
||||||
|
|
||||||
|
val commentsToDisplay = ArrayList<Comment>() |
||||||
|
commentsToDisplay.addAll(this.player.comments) |
||||||
|
commentsToDisplay.sortByDescending { it.date } |
||||||
|
|
||||||
|
commentsToDisplay.forEachIndexed { index, comment -> |
||||||
|
currentCommentCalendar.time = comment.date |
||||||
|
|
||||||
|
if (!currentCommentCalendar.isSameDay(currentDateCalendar) || index == 0) { |
||||||
|
currentDateCalendar.time = currentCommentCalendar.time |
||||||
|
// Adds day sub section |
||||||
|
rows.add(CustomizableRowRepresentable(RowViewType.HEADER_SUBTITLE, title = currentDateCalendar.time.mediumDate())) |
||||||
|
} |
||||||
|
|
||||||
|
// Adds comment |
||||||
|
rows.add(comment) |
||||||
|
} |
||||||
|
|
||||||
|
rows.add(SeparatorRow()) |
||||||
|
} |
||||||
|
|
||||||
|
return rows |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Add an entry |
||||||
|
*/ |
||||||
|
fun addComment(): Comment { |
||||||
|
val entry = Comment() |
||||||
|
this.player.comments.add(entry) |
||||||
|
updateRowRepresentation() |
||||||
|
return entry |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Delete an entry |
||||||
|
*/ |
||||||
|
fun deleteComment(comment: Comment) { |
||||||
|
commentsToDelete.add(comment) |
||||||
|
this.player.comments.remove(comment) |
||||||
|
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() |
||||||
|
} |
||||||
|
|
||||||
|
override fun editDescriptors(row: RowRepresentable): List<RowRepresentableEditDescriptor>? { |
||||||
|
|
||||||
|
when (row) { |
||||||
|
PlayerPropertiesRow.NAME -> return row.editingDescriptors(mapOf("defaultValue" to this.player.name)) |
||||||
|
PlayerPropertiesRow.SUMMARY -> return row.editingDescriptors(mapOf("defaultValue" to this.player.summary)) |
||||||
|
} |
||||||
|
|
||||||
|
return null |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue