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 / SearchViewExtensions.kt
Last active June 24, 2021 09:30
SearchView Extension Functions
/**
* Add an action which will be invoked when the text is changing.
*
* @return the [SearchView.OnQueryTextListener] added to the [SearchView]
*/
inline fun SearchView.doAfterTextChanged(
delay: Long = 500,
crossinline onTextChangedDelayed: (text: String) -> Unit
) = doOnQueryTextListener(delay, onTextChangedDelayed)
@rommansabbir
rommansabbir / ExecutionLocker.kt
Created October 13, 2021 11:19
Simple & useful util class to manage execution according to locking state for Android
object ExecutionLocker {
private const val LOGGER = "ExecutionLocker:: isExecutionLocked -> "
// Hold the state of locking
private var isLockingEnabled: Boolean = false
// Get locking status
val isLocked: Boolean
get() = isLockingEnabled
@rommansabbir
rommansabbir / APIExceptionHandler.kt
Created October 16, 2021 06:46
Handle API exception in Spring Boot
@ControllerAdvice
class APIExceptionHandler {
companion object {
/**
* Register all kind of Exceptions here
*/
private var exceptionListHolder: MutableMap<Class<*>, HttpStatus> = HashMap<Class<*>, HttpStatus>().apply {
put(NullPointerException::class.java, HttpStatus.NOT_FOUND)
put(NoSuchElementException::class.java, HttpStatus.NOT_FOUND)
put(MethodArgumentTypeMismatchException::class.java, HttpStatus.BAD_REQUEST)
/**
* Add an action which will be invoked when the text is changing.
*
* @return the [EditText.onTextChangeListener] added to the [EditText]
*/
inline fun EditText.doAfterTextChanged(
delay: Long = 500,
crossinline onTextChangedDelayed: (text: String) -> Unit
) = onTextChangeListener(delay, onTextChangedDelayed)
@rommansabbir
rommansabbir / Connectivity.java
Created November 1, 2021 09:21 — forked from emil2k/Connectivity.java
Android utility class for checking device's network connectivity and speed.
/*
* Copyright (c) 2017 Emil Davtyan
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
@rommansabbir
rommansabbir / FrameworkExtensions.kt
Last active January 13, 2022 12:30
Extension functions that fire [CoroutineScope] (support: [Dispatchers.IO], [Dispatchers.Main], [Dispatchers.Default]) respective to [AppCompatActivity]/[Fragment]/[ViewModel]. Functions are called under the parent [CoroutineScope] of respective [AppCompatActivity]/[Fragment]/[ViewModel], So if the parent is no longer exists, cancel any ongoing o…
/**
* Extension functions that fire [CoroutineScope] (support: [Dispatchers.IO], [Dispatchers.Main], [Dispatchers.Default]) respective to [AppCompatActivity]/[Fragment]/[ViewModel].
* Functions are called under the parent [CoroutineScope] of respective [AppCompatActivity]/[Fragment]/[ViewModel],
* So if the parent is no longer exists, cancel any ongoing operation automatically.
*/
private suspend fun CoroutineScope.executeBody(block: suspend CoroutineScope.() -> Unit) {
try {
block.invoke(this)
} catch (e: Exception) {
@rommansabbir
rommansabbir / AdapterExtensions.kt
Created January 4, 2022 10:35
Android AdapterView Extension Functions
inline fun <T : Adapter> AdapterView<T>.registerForAdapterEventListener(
crossinline itemListener: (parent: AdapterView<*>, view: View, position: Int, id: Long) -> Unit,
) = doOnItemSelectedListener(itemListener, {})
inline fun <T : Adapter> AdapterView<T>.registerForAdapterEventListener(
crossinline itemListener: (parent: AdapterView<*>, view: View, position: Int, id: Long) -> Unit,
crossinline nothingListener: (parent: AdapterView<*>) -> Unit = {},
) = doOnItemSelectedListener(itemListener, nothingListener)
@rommansabbir
rommansabbir / FacadeDesignPattern.kt
Created January 22, 2022 10:21
Facade Design Pattern - Kotlin
class FacadeDesignPattern {
companion object {
@JvmStatic
fun main(args: Array<String>) {
try {
if (FacadeOrderManager.getInstance.createOrder("1")) {
println("Order Successful")
}
} catch (e: Exception) {
println("Error while placing order: ${e.message}")
@rommansabbir
rommansabbir / AdapterDesignPattern.kt
Created January 23, 2022 01:20
Adapter Design Pattern - Kotlin
class AdapterDesignPattern {
companion object {
@JvmStatic
fun main(args: Array<String>) {
println(CSVAdapterImpl(CSVFormatterImpl()).formatText("Hello. Romman Sabbir. How are you?."))
}
}
}
/**
@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()