This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The One-Shot Operation as a Cold Stream | |
//######################################################################## | |
class MyViewModel(private val repository: DataRepository) : ViewModel() { | |
private val _data = MutableStateFlow<UiState>(UiState.Loading) | |
val data: StateFlow<UiState> = _data | |
fun fetchData() { | |
viewModelScope.launch { | |
try { | |
_data.value = UiState.Success(repository.fetchSomeData()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun PriceScreen() { | |
val originalPrice = 150.0 | |
val addTax = { price: Double -> price * 1.08 } | |
val applyDiscount = { price: Double -> price * 0.85 } | |
val roundPrice = { price: Double -> kotlin.math.round(price) } | |
val finalPrice = processPrice(originalPrice, applyDiscount, addTax, roundPrice) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//########LaunchedEffect#################### | |
// first load in page: init vs Launched Effect | |
@Composable | |
fun MyScreen(userId: String) { | |
LaunchedEffect(userId) { | |
// هر بار userId تغییر کنه اجرا میشه | |
val user = userRepository.getUser(userId) | |
println(user.name) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// UiState بهبود یافته با generic type | |
sealed class UiState<out T> { | |
object Loading : UiState<Nothing>() | |
data class Success<T>(val data: T) : UiState<T>() | |
data class Error( | |
val throwable: Throwable, | |
val userMessage: String, | |
val isRetryable: Boolean = true | |
) : UiState<Nothing>() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SearchViewModel : ViewModel() { | |
private val _searchQuery = MutableSharedFlow<String>() | |
val searchResults: StateFlow<UiState<List<SearchResult>>> = _searchQuery | |
.debounce(300) // صبر میکنه تا کاربر تایپ کردن رو تموم کنه | |
.filter { it.length >= 3 } // حداقل 3 کاراکتر | |
.flatMapLatest { query -> | |
if (query.isEmpty()) { | |
flowOf(UiState.Success(emptyList())) | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun String.toAnnotatedTextWithArabicDiacritics( | |
diacriticsStyle: SpanStyle = SpanStyle(color = Color.Red) | |
): AnnotatedString { | |
val builder = AnnotatedString.Builder() | |
// اعراب عربی که باید قرمز شوند | |
val arabicDiacritics = setOf( | |
'\u064B', // فتحتان (ً) | |
'\u064C', // ضمتان (ٌ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Qualifier | |
@Retention(AnnotationRetention.BINARY) | |
annotation class SharedPreferencesFileNameKey | |
@Qualifier | |
@Retention(AnnotationRetention.BINARY) | |
annotation class LanguageCodeKey | |
@Module | |
@InstallIn(SingletonComponent::class) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ========== روش 1: استفاده از Interface ========== | |
fun interface OnItemClickListener { | |
fun onItemClick(item: String) | |
companion object { | |
fun withLogging( | |
tag: String = "ListItemClick", | |
messagePrefix: String = "Clicked on: ", | |
action: (String) -> Unit | |
): OnItemClickListener { |
NewerOlder