Skip to content

Instantly share code, notes, and snippets.

@NonCoderF
Created July 17, 2023 16:29
Show Gist options
  • Save NonCoderF/1b75836a7ae7929342e4ab3e912b6f13 to your computer and use it in GitHub Desktop.
Save NonCoderF/1b75836a7ae7929342e4ab3e912b6f13 to your computer and use it in GitHub Desktop.
Modified Observer with single JSON Object design
import org.json.JSONObject
typealias JsonObserver = (jsonObject: JSONObject) -> Unit
class JSONSubject {
companion object {
fun instance() = JSONSubject()
}
private var observer: JsonObserver? = null
private val jsonObject: JSONObject = JSONObject()
fun registerObserver(observer: JsonObserver) { this.observer = observer }
fun updateKey(key: String, value: Any) {
jsonObject.put(key, value)
observer?.let { it(jsonObject) }
}
fun removeKey(key: String){
jsonObject.remove(key)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment