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 / 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 / PlaceExtension.kt
Last active January 8, 2021 05:19
Transform Place API to Address API - Android
/**
* This function transform [Place] into [Address]
*/
fun Place.toAddress(): Address {
val address = Address(Locale.ENGLISH)
this.latLng?.let {
address.latitude = it.latitude
address.longitude = it.longitude
}
when (addressComponents) {
@rommansabbir
rommansabbir / TextViewExtensions.kt
Last active February 13, 2021 18:19
TextView after text changed with delay Extension function
inline fun TextView.afterTextChangedDelayed(crossinline afterTextChanged: (String) -> Unit): TextWatcher {
val watcher = object : TextWatcher {
private var timer: Timer = Timer()
// Change the delay based on your requirement
private val DELAY: Long = 1000
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun afterTextChanged(editable: Editable?) {
timer.cancel()
inline fun Spinner.onItemSelectedListenerCustom(crossinline changed: (AdapterView<*>?, View?, Int, Long) -> Unit): AdapterView.OnItemSelectedListener {
val listener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
changed.invoke(parent, view, position, id)
}
@rommansabbir
rommansabbir / ContextExtension.kt
Created March 10, 2021 18:08
Check if a specific Service is running or not (Extension Function)
fun Context.isMyServiceRunning(serviceClass: Class<*>): Boolean {
val manager = this.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
return manager.getRunningServices(Integer.MAX_VALUE)
.any { it.service.className == serviceClass.name }
}
@rommansabbir
rommansabbir / AsyncDiffUtilExample.kt
Created April 11, 2021 14:09
Android RecyclerView: Async DiffUtill
@Singleton
class PatientUserChatAdapter @Inject constructor() :
RecyclerView.Adapter<PatientUserChatViewHolder>() {
//
private var dataSet: MutableList<UserChatDataModel> = mutableListOf()
private var callback: PatientUserChatSelectionCallback? = null
//Register for action callback
fun setActionCallback(callback: PatientUserChatSelectionCallback? = null) {
// If you have a list of data to pass through intent
// Don't do this:
intent.put("first", "something")
intent.put("second", 100.5)
intent.put("third", Object)
// Else, do this.
// Create a model
@rommansabbir
rommansabbir / Extensions.kt
Last active April 28, 2021 08:56
Simply check if two list is the exact same or not.
fun <T> MutableList<out T>.contentDeepEquals(
other: MutableList<out T>
): Boolean {
return Gson().toJson(this).toString() == Gson().toJson(other).toString()
}
var handlerDelayTimer: Timer = Timer()
inline fun handlerPost(crossinline onSuccess: () -> Unit) {
Handler(Looper.getMainLooper()).post {
onSuccess.invoke()
}
}
inline fun handlerPostDelayed(delay: Long, crossinline onSuccess: () -> Unit) {
handlerDelayTimer.cancel()
@rommansabbir
rommansabbir / *Actions.kt
Last active June 22, 2021 14:41
Create a blueprint of your business logic for respective class/fragment/activity and clear the concern.
/*
Define all actions for a respective class/fragment/activity.
The reason to put all actions into a respective interface is to clear the concern.
So that we can easily identify the all business logic handle by this class/fragment/activity.
This interface will work like a blueprint of business logic for your class/fragment/activity.
*/
interface ProfileActions {
fun onBMETCard()
fun onMyDocuments()
fun onSettings()