Skip to content

Instantly share code, notes, and snippets.

Avatar

Abhi Muktheeswarar abhimuktheeswarar

View GitHub Profile
@abhimuktheeswarar
abhimuktheeswarar / ViewAnimationsExtensions.kt
Last active Mar 23, 2022
View reveal animation for view visibility
View ViewAnimationsExtensions.kt
import android.graphics.Rect
import android.view.View
import android.view.ViewTreeObserver
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.view.Gravity
import android.view.ViewAnimationUtils
import android.view.ViewGroup
import androidx.core.view.isVisible
@abhimuktheeswarar
abhimuktheeswarar / AndroidManifest.xml
Last active Nov 22, 2021
Logic to support back navigation to parent activity properly when returning from PiP mode
View AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
xmlns:tools="http://schemas.android.com/tools"
package="com.app.yourapp">
<dist:module
dist:instant="false"
dist:title="@string/feature_title">
<dist:fusing dist:include="true" />
@abhimuktheeswarar
abhimuktheeswarar / TaskManager.java
Created Nov 22, 2021 — forked from chuangx/TaskManager.java
Bring your launcher task to front
View TaskManager.java
public class TaskManager {
/**
* Bring up launcher task to front
*/
public void navToLauncherTask(@Nonnull Context appContext) {
ActivityManager activityManager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
// iterate app tasks available and navigate to launcher task (browse task)
final List<ActivityManager.AppTask> appTasks = activityManager.getAppTasks();
for (ActivityManager.AppTask task : appTasks) {
@abhimuktheeswarar
abhimuktheeswarar / DownloadAndOpenPdf.kt
Last active Sep 15, 2021
Code snippet for downloading and opening a PDF file
View DownloadAndOpenPdf.kt
class DownloadAndOpenPdf(
private val applicationContext: Context,
private val okHttpClient: OkHttpClient,
private val scope: CoroutineScope
) {
private val tag = "DownloadAndOpenPdf"
private var inProgress: Boolean = false
private lateinit var file: File
@abhimuktheeswarar
abhimuktheeswarar / customCounterStateMachine.kt
Last active Aug 1, 2021
customCounterStateMachine for blog
View customCounterStateMachine.kt
fun CoroutineScope.customCounterStateMachine(channel : ReceiveChannel<CounterMessage>)
= launch {
var counter = 0 // actor state
channel.consumeEach { message ->
ensureActive()
when (message) {
is IncrementCounter -> counter++
is GetCounter -> message.response.complete(counter)
}
}
View CounterMessage.kt
sealed interface CounterMessage
object IncrementCounter : CounterMessage
object DecrementCounter : CounterMessage
class GetCounterState(val deferred: CompletableDeferred<CounterState>) : CounterMessage
View CounterViewModel.kt
class CounterViewModel(private val counterStateStore: CounterStateStore) : ViewModel() {
val stateFlow: Flow<CounterState> = counterStateStore.stateFlow
fun dispatch(message: CounterMessage) {
counterStateStore.dispatch(message)
}
override fun onCleared() {
super.onCleared()
@abhimuktheeswarar
abhimuktheeswarar / CounterSideEffect.kt
Last active Aug 6, 2021
CounterSideEffect for blog
View CounterSideEffect.kt
class Repository {
suspend fun update(count: Int): Boolean {
TODO("Save to DB")
}
}
class CounterSideEffect(
private val repository: Repository,
private val counterStateStore: CounterStateStore,
@abhimuktheeswarar
abhimuktheeswarar / counterStateMachine.kt
Last active Aug 6, 2021
counterStateMachine for blog
View counterStateMachine.kt
fun CoroutineScope.counterStateMachine(
initialState: CounterState,
mutableStateFlow: MutableStateFlow<CounterState>,
mutableMessages: MutableSharedFlow<CounterMessage>,
) =
actor<CounterMessage> {
var state: CounterState = initialState
channel.consumeEach { message ->
when (message) {
is IncrementCounter -> {
View CounterActivity.kt
class CounterActivity : AppCompatActivity() {
private val viewModel by viewModels<CounterViewModel>()
private lateinit var binding: ActivityCounterBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityCounterBinding.inflate(layoutInflater)
setContentView(binding.root)