Skip to content

Instantly share code, notes, and snippets.

View mahdiPourkazemi's full-sized avatar

Mahdi Pourkazemi mahdiPourkazemi

View GitHub Profile
@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
/**
@mahdiPourkazemi
mahdiPourkazemi / gist:dc03bd55375a7da36b49af8d4c83edc7
Created November 5, 2024 15:53
This Kotlin snippet showcases various ways to evaluate whether an integer is divisible by another integer using higher-order functions, member extension functions, and lambda references. It’s designed to illustrate Kotlin’s functional programming capabilities in a simple, clear manner.
// A higher-order function that executes a lambda function `f` on `arg1` with `arg2` as an argument.
fun exec(
arg1: Int, arg2: Int,
f: Int.(Int) -> Boolean
) = arg1.f(arg2) // Calls the lambda `f` with `arg2`.
// A lambda that checks if an integer is divisible by another integer.
val isDivisibleLambda: Int.(Int) -> Boolean = { this % it == 0 }
// An extension function for Int that checks if it is divisible by a given integer.
import os
import sys
import traceback
from functools import wraps
from multiprocessing import Process, Queue
def processify(func):
'''Decorator to run a function as a process.
Be sure that every argument and the return value
@mahdiPourkazemi
mahdiPourkazemi / plug_in.py
Created April 16, 2024 15:22
Register a function as plug-in, using decoration
import random
PLUGINS = dict()
def register(func):
"""Register a function as plug-in"""
#add function name and function to dictionary
PLUGINS[func.__name__] = func
return func
#example to put in dictionary
@register
@mahdiPourkazemi
mahdiPourkazemi / slow_down.py
Last active April 16, 2024 15:06
slow down your code with python decoration (used for network and etc)
import time
import functools
def slow_down(func):
"""Sleep 1 second before calling the function"""
@functools.wraps(func)
def wrapper_slow_down(*args, **kwargs):
#stop code to play one secend
time.sleep(1)
#return called functon
@mahdiPourkazemi
mahdiPourkazemi / debugger.py
Last active April 16, 2024 15:04
debugger tools to debug python code by decoration (monitoring input and output)
import functools
def debugger(func):
"""Print the functon singnature and return value"""
functools.wraps(func)
def debugger_wraper(*args,**kwargs):
#list of input args
args_repr = [repr(a) for a in args]
#list of input kwargs
kwargs_repr = [f'{k} = {v!r}' for k, v in kwargs.items()]
#sum of the input values