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 / 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()
@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)