Skip to content

Instantly share code, notes, and snippets.

@cse-ariful
Created November 14, 2022 03:18
Show Gist options
  • Save cse-ariful/cdce67212ef2ac8cfbb50feaccc96130 to your computer and use it in GitHub Desktop.
Save cse-ariful/cdce67212ef2ac8cfbb50feaccc96130 to your computer and use it in GitHub Desktop.
A Extension of baseFragment which support register and unregister and notifying the listener from context.
import androidx.viewbinding.ViewBinding
abstract class BaseObservableFragment<V : ViewBinding, ListenerType>(inflater: Inflate<V>) :
BaseFragment<V>(inflater) {
private val listeners = hashSetOf<ListenerType>()
fun notify(data: (ListenerType) -> Unit) {
listeners.forEach(data)
}
fun registerObserver(it: ListenerType) {
if (listeners.contains(it).not()) listeners.add(it)
}
fun unRegisterObserver(it: ListenerType) {
if (listeners.contains(it)) listeners.remove(it)
}
override fun onDestroy() {
super.onDestroy()
listeners.clear()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment