Skip to content

Instantly share code, notes, and snippets.

Avatar

Afzal Najam AfzalivE

View GitHub Profile
@AfzalivE
AfzalivE / LaunchCoroutines.kt
Last active Feb 4, 2021
Coroutines cancellation
View LaunchCoroutines.kt
fun main(args: Array<String>) {
val handler = CoroutineExceptionHandler { context, exception ->
println("CoroutineExceptionHandler got $exception with suppressed ${exception.suppressed.contentToString()}")
}
val parentScope = CoroutineScope(Dispatchers.Default + Job() + handler)
// val parentScope = MyScope()
parentScope.launch {
println("one job: " + this.coroutineContext[Job].toString())
println("Hello")
@AfzalivE
AfzalivE / ExplodingFab1.kt
Last active Dec 20, 2020
ExplodingFab Jetpack Compose
View ExplodingFab1.kt
// Using Jetpack Compose Transition v1
enum class FabState {
Initial,
Normal,
Exploded,
}
val fabSizeKey = DpPropKey()
val fabColorKey = ColorPropKey()
View ForegroundServiceLauncher.kt
class ForegroundServiceLauncher(private val serviceClass: Class<out Service>) {
private var isStarting = false
private var shouldStop = false
private var isCreated = false
@Synchronized
fun startService(context: Context, block: Intent.() -> Unit = {}) {
if (!isCreated) {
isStarting = true
@AfzalivE
AfzalivE / debug_from_qr.py
Created Oct 29, 2020 — forked from benigumocom/debug_from_qr.py
Connect Wireless Debug from Terminal on Android11
View debug_from_qr.py
#!/usr/bin/env python3
"""
Android11
Pair and connect devices for wireless debug on terminal
python-zeroconf: A pure python implementation of multicast DNS service discovery
https://github.com/jstasiak/python-zeroconf
"""
@AfzalivE
AfzalivE / Fake.kt
Created Oct 14, 2020
fake() for all the unit tests
View Fake.kt
val ALL_FAKES = listOf(
FakePostLoginUseCase::class,
// all fake classes go here
)
/**
* Finds and initializes an instance of
* class [T] if it exists in [ALL_FAKES].
*
* If it doesn't, a [FakeNotFoundException] is thrown
@AfzalivE
AfzalivE / BasicNav.kt
Last active Oct 14, 2020
jetpack compose navigation
View BasicNav.kt
@Composable
fun VeryBasicNav() {
NavHost(startDestination = "Profile") { // this: NavGraphBuilder
composable("Profile") {
Profile()
}
composable("Dashboard") {
Dashboard()
}
composable("Scrollable") {
@AfzalivE
AfzalivE / DatabaseSyncer.kt
Last active Aug 18, 2020
Syncing two local and remote db tables
View DatabaseSyncer.kt
package com.test
class DatabaseSyncer() {
// Updated thanks to @gildor
private fun findUpdatedNotes(
allLocalNotes: List<Note>,
allRemoteNotes: List<Note>
): List<Note> {
@AfzalivE
AfzalivE / ViewModelFactory.kt
Created Jul 29, 2019
ViewModelFactory for Dagger2
View ViewModelFactory.kt
/**
* Constructs a ViewModelFactory with dependencies
*/
@Singleton
class ViewModelFactory @Inject constructor(
private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
var creator = creators[modelClass]
@AfzalivE
AfzalivE / LocaleChanger.java
Last active Apr 7, 2019
Class to change android OS locale (works till API 24, M)
View LocaleChanger.java
public class LocaleChanger {
public static void setLocale(ArrayList<Locale> arrayList) {
try {
Class cls = Class.forName("android.app.ActivityManagerNative");
Method method = cls.getMethod("getDefault", new Class[0]);
method.setAccessible(true);
Object invoke = method.invoke(cls, new Object[0]);
method = cls.getMethod("getConfiguration", new Class[0]);
method.setAccessible(true);
@AfzalivE
AfzalivE / ConditionalTestRule.java
Last active Jan 28, 2019
Run test if a condition is true
View ConditionalTestRule.java
import com.azimolabs.conditionwatcher.Instruction;
import org.junit.Assume;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import timber.log.Timber;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;