Skip to content

Instantly share code, notes, and snippets.

View rygelouv's full-sized avatar

Rygel Louv rygelouv

View GitHub Profile
class DefaultService(
override val type: KClass<*>,
override val instance: Any
) : Service {
companion object {
fun createService(instance: Any) = DefaultService(instance::class, instance)
}
}
class Module {
val declarationRegistry: MutableMap<KClass<*>, Declaration<Any>> = ConcurrentHashMap()
inline fun <reified T: Any> factory(noinline declaration: Declaration<T>) {
declarationRegistry[T::class] = declaration
}
operator fun plus(module: Module) = listOf(module, this)
}
class ServiceLocator {
private val serviceMap: MutableMap<KClass<*>, Service> = ConcurrentHashMap()
fun <T : Any> getService(clz: KClass<T>): Service {
return serviceMap[clz] ?: error("Unable to find definition of $clz")
}
private fun addService(service: Service) {
serviceMap[service.type] = service
}
class AudioPlayerCustomView : MediaActionHandler {
...
init {
AudioPlayerLifecycleObserver.registerActionHandler(this)
}
override fun onStop() {
mediaPlayer.release() // we release the media player when the activity is stoped
...
AudioPlayerLifecycleObserver.registerLifecycle(getLifecycle())
...
interface ViewActionHandler {
fun onStart()
fun onStop()
}
object AudioPlayerLifecycleObserver : LifecycleObserver {
private var actionHandler: ViewActionHandler? = null
fun registerActionHandler(handler: ViewActionHandler) {
this.actionHandler = handler
}
fun registerLifecycle(lifecycle: Lifecycle) {
lifecycle.addObserver(this)
}
object AudioPlayerLifecycleObserver : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun start() {
Log.e("TAG", "================================>>>> lifecycle owner STARTED")
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun stop() {
Log.e("TAG", "================================>>>> lifecycle owner STOPED")
protected override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
// Stop media player
}
data class PostResponse(val data: List<Post>): BaseApiResponse(), DataResponse<List<Post>> {
override fun retrieveData(): List<Post> = data
}