Skip to content

Instantly share code, notes, and snippets.

View rommansabbir's full-sized avatar
👓
Only Development

Romman Sabbir rommansabbir

👓
Only Development
View GitHub Profile
@rommansabbir
rommansabbir / KotlinAysncToSync.kt
Created February 28, 2022 04:17
Kotlin: Convert Async APIs into Sync
class Worker {
/*Async API*/
fun doSomething(listener: Listener, throwError: Boolean) {
when (throwError) {
true -> {
Thread.sleep(3000)
listener.onError(Exception("Just a random exception..."))
}
else -> {
Thread.sleep(3000)
@rommansabbir
rommansabbir / BaseCustomView.kt
Created February 24, 2022 05:07
Custom Base View to support Kotlin's Coroutine according to View's Lifecycle (On Detach, kill Coroutine).
/**
* Custom Base View to support Kotlin's Coroutine according to View's Lifecycle (On Detach, kill Coroutine).
*
* @param context [Context]
* @param attrs [AttributeSet]
*/
open class BaseCustomView(context: Context, attrs: AttributeSet) :
ConstraintLayout(context, attrs) {
private val tag by lazy { this::class.java.canonicalName ?: "BaseCustomView" }
@rommansabbir
rommansabbir / GenericBodyExecutor.kt
Last active March 10, 2022 18:44
A generic API to execute a given body and return an object [T] type. If exception occur return null else the object [T].
class GenericBodyExecutor {
companion object {
@JvmStatic
fun main(args: Array<String>) {
println(executeBodyOrReturnNull<Int> { GenericData("0").data.toInt() }) // Result -> 0
println(executeBodyOrReturnNull<Int> { GenericData("Hello").data.toInt() }) // Result -> null
}
}
}
/**
* Our interface that has only one method [Delegation.print].
* Who inherit [Delegation] might have different implementation.
* So we are declaring [Delegation.print] without any kind of parameter.
*/
interface Delegation {
fun print()
}
/**
@rommansabbir
rommansabbir / CoroutineExtensions.kt
Created February 17, 2022 05:10
Kotlin's Coroutine extension functions to switch from one context to another one. Supports [Dispatchers.IO], [Dispatchers.Main], [Dispatchers.Default], [Dispatchers.Unconfined].
/**
* Kotlin's Coroutine extension functions to switch from one context to another one.
* Supports [Dispatchers.IO], [Dispatchers.Main], [Dispatchers.Default], [Dispatchers.Unconfined].
*/
suspend inline fun CoroutineScope.toIO(crossinline scope: suspend CoroutineScope.() -> Unit) {
withContext(Dispatchers.IO) { scope.invoke(this) }
}
suspend inline fun CoroutineScope.toMain(crossinline scope: suspend CoroutineScope.() -> Unit) {
withContext(Dispatchers.Main) { scope.invoke(this) }
@rommansabbir
rommansabbir / FlyweightDesignPattern.kt
Created February 9, 2022 06:06
Flyweight - Design Pattern
class FlyweightExample {
companion object {
@JvmStatic
fun main(args: Array<String>) {
val raceCars = arrayOf(
RaceCarClient("Midget"),
RaceCarClient("Midget"),
RaceCarClient("Midget"),
RaceCarClient("Sprint"),
RaceCarClient("Sprint"),
class SafeExecutorExample {
companion object {
@JvmStatic
fun main(args: Array<String>) {
withSafeExecutor<Any> {
Either.Right(Any()) }
/*
Test by throwing exception.
*/
withSafeExecutor<Any> { throw Exception("Test") }.either(
@rommansabbir
rommansabbir / CompositeDesignPattern.kt
Created January 24, 2022 01:19
Composite Design Pattern - Kotlin
class CompositeDesignPattern {
companion object {
@JvmStatic
fun main(args: Array<String>) {
/*Create primary products for main catalog*/
val mJeanProduct: CatalogComponent = Product("M: Jeans 32", 65.00)
val mTShirtProduct: CatalogComponent = Product("M: T Shirt 38", 45.00)
/*Create a composite product catalog and add female products to it*/
val newCatalog: CatalogComponent = ProductCatalog("Female Products")
val fJeans: CatalogComponent = Product("F: Jeans 32", 65.00)
@rommansabbir
rommansabbir / BridgeDesignPattern.kt
Created January 23, 2022 15:46
Bridge Design Pattern - Kotlin
class BridgeDesignPattern {
companion object {
@JvmStatic
fun main(args: Array<String>) {
// Send new text message
val textMessageSender: MessageSender = TextMessageSender()
val textMessage: Message = TextMessage(textMessageSender)
textMessage.send()
// Send new email message
@rommansabbir
rommansabbir / DecoratorDesignPattern.kt
Created January 23, 2022 08:59
Decorator Design Pattern - Kotlin
class DecoratorDesignPattern {
companion object {
@JvmStatic
fun main(args: Array<String>) {
val rose = RoseBouquet()
println("Without customization = ${rose.description}, ${rose.cost()}")
val roseWrapper = PaperWrapper(rose)
println("With customization = ${roseWrapper.description}, ${roseWrapper.cost()}")
val orchid = OrchidBouquet()