Skip to content

Instantly share code, notes, and snippets.

View mahdiPourkazemi's full-sized avatar

Mahdi Pourkazemi mahdiPourkazemi

View GitHub Profile
@Composable
fun Float.dpToPx(): Float = with(LocalDensity.current) { this@dpToPx.dp.toPx() }
@Composable
fun Int.dpToPx(): Float = with(LocalDensity.current) { this@dpToPx.dp.toPx() }
val circleRadius = 12.dp.dpToPx()
fun <T> SnapshotStateList<T>.toggle(item: T) {
if (contains(item)) remove(item) else add(item)
@mahdiPourkazemi
mahdiPourkazemi / SpanStyleText.kt
Created July 31, 2025 08:44
quoran text style with color
fun String.toAnnotatedTextWithArabicDiacritics(
diacriticsStyle: SpanStyle = SpanStyle(color = Color.Red)
): AnnotatedString {
val builder = AnnotatedString.Builder()
// اعراب عربی که باید قرمز شوند
val arabicDiacritics = setOf(
'\u064B', // فتحتان (ً)
'\u064C', // ضمتان (ٌ)
@mahdiPourkazemi
mahdiPourkazemi / constants.kt
Created July 29, 2025 11:13
This snippet demonstrates a better way to inject constant values using Hilt in Android. Instead of relying on a centralized Constants.kt file and @nAmed strings (which are error-prone and hard to refactor), it uses custom @qualifier annotations. This approach provides compile-time safety, clearer dependency injection, and easier maintenance.
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class SharedPreferencesFileNameKey
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class LanguageCodeKey
@Module
@InstallIn(SingletonComponent::class)
@mahdiPourkazemi
mahdiPourkazemi / gist:2752ac97305a277da3928e23c47d2a04
Last active July 27, 2025 15:41
SAM applications in android
fun interface OnAction {
fun execute(context: Context)
companion object {
fun withLogging(tag: String = "MyAction",
message: String = "Button clicked",
action: (Context) -> Unit
): OnAction {
return OnAction { context ->
Log.d(tag, message)
@mahdiPourkazemi
mahdiPourkazemi / NavTypeParcelableMapper.kt
Created July 21, 2025 06:55
This Kotlin extension defines a custom NavType<T> mapper for use with Jetpack Navigation when passing Parcelable objects between destinations. It also integrates Kotlinx Serialization (@serializable) for safely encoding and decoding data into string format when navigating via routes. The mapper() function simplifies handling complex objects like…
import android.os.Build
import android.os.Bundle
import android.os.Parcelable
import androidx.navigation.NavType
import kotlinx.parcelize.Parcelize
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
@mahdiPourkazemi
mahdiPourkazemi / AvsB.kt
Last active February 24, 2025 19:49
I think using use-case with mvi in this approach is better (model A vs model B) #android #kotlin #mvvm #mvi
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChangedBy
import kotlinx.coroutines.flow.launchIn
@mahdiPourkazemi
mahdiPourkazemi / BaseDao.kt
Created January 1, 2025 06:52 — forked from florina-muntenescu/BaseDao.kt
Use Dao inheritance to reduce the amount of boilerplate code - https://medium.com/google-developers/7-pro-tips-for-room-fbadea4bfbd1
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@mahdiPourkazemi
mahdiPourkazemi / BaseDao.kt
Created January 1, 2025 06:52 — forked from florina-muntenescu/BaseDao.kt
Use Dao inheritance to reduce the amount of boilerplate code - https://medium.com/google-developers/7-pro-tips-for-room-fbadea4bfbd1
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@mahdiPourkazemi
mahdiPourkazemi / Data.kt
Created January 1, 2025 06:50 — forked from florina-muntenescu/Data.kt
Using RoomDatabase#Callback to pre-populate the database - https://medium.com/google-developers/7-pro-tips-for-room-fbadea4bfbd1
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@mahdiPourkazemi
mahdiPourkazemi / CalendarGrid.kt
Created December 28, 2024 18:04 — forked from pbk20191/CalendarGrid.kt
Calendar Grid style layout for Compose
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.SubcomposeLayout
import androidx.compose.ui.layout.SubcomposeLayoutState
import androidx.compose.ui.layout.SubcomposeSlotReusePolicy
import androidx.compose.ui.unit.*
import java.time.LocalDate
/**