Skip to content

Instantly share code, notes, and snippets.

View khahani's full-sized avatar
💻
Happy coding...

Mohammadreza Khahani khahani

💻
Happy coding...
View GitHub Profile
@khahani
khahani / ResultKtTest.kt
Created September 17, 2023 17:08
ResultKtTest
import app.cash.turbine.test
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.test.runTest
import org.junit.Test
import kotlin.test.assertEquals
class ResultKtTest {
@Test
fun Result_catches_errors() = runTest {
@khahani
khahani / Result.kt
Created September 17, 2023 17:03
asResult.kt
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
fun <T> Flow<T>.asResult(): Flow<Result<T>> {
return this
.map<T, Result<T>> {
Result.Success(it)
}
@khahani
khahani / build.gradle
Created September 17, 2023 16:50
Update Kotlin version
subprojects {
tasks.withType(KotlinCompile).configureEach {
kotlinOptions {
languageVersion = "1.9"
}
}
}
@khahani
khahani / Result.kt
Created September 17, 2023 16:43
Result.kt
sealed interface Result<out T> {
data class Success<T>(val data: T) : Result<T>
data class Error(val exception: Throwable? = null) : Result<Nothing>
data object Loading : Result<Nothing>
}
@khahani
khahani / UpdateCoroutineScope.kt
Created September 17, 2023 16:36
UpdateCoroutineScope.kt
@Module
@InstallIn(SingletonComponent::class)
object CoroutineScopesModule {
@Provides
@Singleton
@ApplicationScope
fun providesApplicationScopeWithIODiapatcher(
@Dispatcher(IO) ioDispatcher: CoroutineDispatcher,
@ApplicationScope scope: CoroutineScope,
): CoroutineScope = CoroutineScope(scope.coroutineContext + ioDispatcher) // <- What I meant by updating dispatcher
@khahani
khahani / CoroutineScopesModule.kt
Created September 17, 2023 16:27
CoroutineScopesModule.kt
import [your_app_package].core.network.Dispatcher
import [your_app_package].core.network.AppDispatchers.Default
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import javax.inject.Qualifier
@khahani
khahani / DispatcherModules.kt
Last active September 17, 2023 16:13
DispatcherModules.kt
import [your_app_package].common.network.AppDispatchers.Default
import [your_app_package].common.network.AppDispatchers.IO
import [your_app_package].common.network.Dispatcher
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
@khahani
khahani / AppDispatcher.kt
Created September 17, 2023 16:05
AppDispatcher.kt
import javax.inject.Qualifier
@Qualifier
annotation class Dispatcher(val appDispatcher: AppDispatchers)
enum class AppDispatchers {
Default,
IO,
}
@khahani
khahani / Sumup.kt
Created August 4, 2023 09:06
Lambda and method reference
class Button(
private val onClick: () -> Unit
) {
fun performClick() {
onClick()
}
}
class ButtonClickListener(
var name: String
@khahani
khahani / CheckSignature.java
Created December 24, 2020 10:25
How to check Android app signature at runtime
public class foo {
private boolean isOldSignature() {
Context context = this;
final String SIGNATURE = "Put your app signature after retrive in log";
final boolean VALID = true;
final boolean INVALID = false;
try {
PackageInfo packageInfo = context.getPackageManager()
.getPackageInfo(context.getPackageName(),