Skip to content

Commit

Permalink
Fix DataModel crash after deserialization (#249)
Browse files Browse the repository at this point in the history
Fixes #248
  • Loading branch information
thusson13 authored Feb 15, 2021
1 parent dc39575 commit 814e250
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ import java.io.Serializable
*/
abstract class DataModel : Serializable {
@Transient
private val updateListeners = mutableSetOf<DataModelUpdateListener>()
private var updateListeners: MutableSet<DataModelUpdateListener>? = null
get() {
if (field == null) {
field = mutableSetOf()
}
return field
}

/**
* Add a [DataModelUpdateListener] to the list of listeners.
*
* @param updateListener the listener to add
*/
open fun addUpdateListener(updateListener: DataModelUpdateListener) {
updateListeners.add(updateListener)
updateListeners!!.add(updateListener)
}

/**
Expand All @@ -28,7 +34,7 @@ abstract class DataModel : Serializable {
* @param updateListener the listener to remove
*/
open fun removeUpdateListener(updateListener: DataModelUpdateListener) {
updateListeners.remove(updateListener)
updateListeners!!.remove(updateListener)
}

/**
Expand All @@ -37,7 +43,7 @@ abstract class DataModel : Serializable {
open fun notifyChange() {
val handler = Handler(Looper.getMainLooper())

updateListeners.forEach { updateListener ->
updateListeners!!.forEach { updateListener ->
handler.post { updateListener.notifyChange() }
}
}
Expand Down

0 comments on commit 814e250

Please sign in to comment.