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 / JWTHelper.kt
Created February 18, 2024 15:28
Creating a Custom JWT Token Utility in Spring Boot. #springboot #kotlin #jwt #jwttoken #jwtutil #customjwt
object JwtHelper {
/**
* Enum class representing JWT algorithms with their corresponding values.
*/
enum class JwtAlgorithm(val value: String) {
ALGORITHM_HS256("HS256"), ALGORITHM_HS384("HS384"), ALGORITHM_HS512("HS512")
}
/**
* Enum class representing HMAC signature algorithms with their corresponding values.
@rommansabbir
rommansabbir / Hilt.kt
Last active November 10, 2023 21:08
Android - Hilt: Inject multiple instances of SameType Object to the Dependent Module
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ViewModelComponent
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import javax.inject.Named
@rommansabbir
rommansabbir / WithCoroutineExt.kt
Last active October 16, 2023 08:54
Simplifying concurrency with Kotlin Coroutines : A utility function | Executes a coroutine with configurable dispatchers for subscription and observation.
/**
* Executes a coroutine with configurable dispatchers for subscription and observation.
*
* @param subscribeOn The dispatcher context for subscribing to the asynchronous operation (default: Dispatchers.IO).
* @param observeOn The dispatcher context for observing the result (default: Dispatchers.Main).
* @param block A suspend function representing the asynchronous operation to be executed.
* @param onSuccess A suspend function that is invoked with the result of the asynchronous operation when it succeeds.
* @param onError A function that is invoked if the asynchronous operation encounters an error.
*/
fun <T> withCoroutine(
/**
* Default implementation of [LocaleHelperKt].
*/
class DefaultLocaleHelper private constructor(context: Context) : BaseLocaleHelper(context) {
companion object {
/* Mark the instance as Volatile*/
@Volatile
private var instance: LocaleHelperKt? = null
private var LOCK: Any = Any()
@rommansabbir
rommansabbir / AndroidRetrofitOkHttp.kt
Created July 18, 2022 06:19
Android : Mock Response with Retrofit & OkHttp
class LoginInterceptor: Interceptor{
override fun intercept(chain: Interceptor.Chain): Response {
//If requested endpoint matched to targeted endpoint, we will return an mocked response.
if (chain.request().url.toUri().toString().endsWith("fake-login")) {
val responseString = "OUR_JSON_RESPONSE_FROM_ASSET_OR_OTHER_SOURCE"
return chain.proceed(chain.request())
.newBuilder()
.code(200)
.protocol(Protocol.HTTP_2)
.message(responseString)
/**
* 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 / UsagesOfSealedClass.kt
Created December 9, 2020 18:37
How to use Kotlin's Sealed Class in Android Development for better & clean code
sealed class AppFailure {
/**
* Global Failure classes
* These failures will be used across all over the app including Data Layer, Domain Layer, Framework Layer
*/
class GeneralFailure(var message: String, var errorCode: Int? = null) : AppFailure()
class UnauthorizedFailure(var message: String = "Unauthorized", errorCode: Int? = null) : AppFailure()
class LoginFailure(var message: String = "Unable to login", errorCode: Int? = null) : AppFailure()
class ServerFailure(var message: String = "Unable to connect to the server side", errorCode: Int? = null) : AppFailure()
class NoInternetFailure(var message: String = "Device is not connected to the internet", errorCode: Int? = null) : AppFailure()
@rommansabbir
rommansabbir / IntentExt.kt
Created October 10, 2022 15:46
Intent Debug Ext
fun Intent?.toDebugString(): String {
val intent = this ?: return ""
return StringBuilder().apply {
appendLine("--- Intent ---")
appendLine("type: ${intent.type}")
appendLine("package: ${intent.`package`}")
appendLine("scheme: ${intent.scheme}")
appendLine("component: ${intent.component}")
appendLine("flags: ${intent.flags}")
appendLine("categories: ${intent.categories}")
@rommansabbir
rommansabbir / LoadJSONFromAssests.kt
Last active August 25, 2022 11:54
Android: Load JSON from `src/main/assets/`
/* Android: Load JSON from `src/main/assets/` */
/* Extension function where `Context` passed explicitly */
fun loadJSONFromAsset(context: Context, fileName: String): String {
val inputStream: InputStream = context.assets.open(fileName)
val size: Int = inputStream.available()
val buffer = ByteArray(size)
inputStream.read(buffer)
inputStream.close()
@rommansabbir
rommansabbir / Failure.kt
Created March 22, 2022 04:02
Sealed Class that return HTTP Failure (4xx, 5xx)
sealed class Failure {
/*Network Error*/
class NetworkConnection(var additionalData: Any? = null) : Failure()
/*Exception*/
class Exception(var additionalData: Any? = null) : Failure()
/**
* Although the HTTP standard specifies "unauthorized",
* semantically this response means "unauthenticated".