This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DefaultService( | |
override val type: KClass<*>, | |
override val instance: Any | |
) : Service { | |
companion object { | |
fun createService(instance: Any) = DefaultService(instance::class, instance) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AudioPlayerCustomView : MediaActionHandler { | |
... | |
init { | |
AudioPlayerLifecycleObserver.registerActionHandler(this) | |
} | |
override fun onStop() { | |
mediaPlayer.release() // we release the media player when the activity is stoped |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
AudioPlayerLifecycleObserver.registerLifecycle(getLifecycle()) | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ViewActionHandler { | |
fun onStart() | |
fun onStop() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object AudioPlayerLifecycleObserver : LifecycleObserver { | |
private var actionHandler: ViewActionHandler? = null | |
fun registerActionHandler(handler: ViewActionHandler) { | |
this.actionHandler = handler | |
} | |
fun registerLifecycle(lifecycle: Lifecycle) { | |
lifecycle.addObserver(this) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected override fun onDetachedFromWindow() { | |
super.onDetachedFromWindow() | |
// Stop media player | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class PostResponse(val data: List<Post>): BaseApiResponse(), DataResponse<List<Post>> { | |
override fun retrieveData(): List<Post> = data | |
} |