Skip to content

Instantly share code, notes, and snippets.

View belinwu's full-sized avatar

吴上阿吉 belinwu

View GitHub Profile
@belinwu
belinwu / swap_arithmetic
Last active August 29, 2015 14:21
Swap Two Variables Without Using a Temp Variable (With Math!)
var a = 10, b = 20;
a = a + b;
b = a - b;
a = a - b;
console.log("a = " + a + ", b = " + b); // a = 20, b = 10
@belinwu
belinwu / AndroidBmpUtil.java
Last active October 25, 2018 09:03
Save Android Bitmap to .bmp file
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import android.graphics.Bitmap;
/**
@belinwu
belinwu / build.gradle
Created July 23, 2019 03:44 — forked from akperkins/build.gradle
Example of auto-generating the versionCode
apply plugin: 'com.android.application'
// version information
def versionMajor = 1
def versionMinor = 0
def versionPatch = 0
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
@belinwu
belinwu / build.gradle
Created March 23, 2020 14:08 — forked from wilik16/build.gradle
Archive/Copy debug or release APK / AAB (Android App Bundle) file and/or mapping.txt to a versioned folder
android {
applicationVariants.all { variant ->
variant.outputs.all {
def fileName = "app"
switch (variant.buildType.name) {
case "debug":
fileName = "${appNameDebug}-${variant.versionCode}"
break
case "release":
fileName = "${appNameRelease}-${variant.versionCode}"
./gradlew clean buildRelease publish
@belinwu
belinwu / LazyLifecycleManager.kt
Created June 23, 2022 11:03 — forked from vishalratna-microsoft/LazyLifecycleManager.kt
Code for LazyLifecycleManager
import android.app.Activity
import android.util.Log
import android.view.ViewTreeObserver
import androidx.annotation.MainThread
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import com.******************.Barrier
import com.******************.Closure
import com.*******************.Once
@belinwu
belinwu / Once.kt
Created June 23, 2022 11:04 — forked from vishalratna-microsoft/Once.kt
Code for Once
import java.util.concurrent.atomic.AtomicInteger
/**
* Construct that helps to restrict the code execution frequency to 1. It will not allow the snippet passed through the close to be executed more than once.
* The first client to execute run() will execute this and other calls to run() will not be respected till reset is called.
*/
class Once {
private val mCounter = AtomicInteger(0)
@belinwu
belinwu / Barrier.java
Created June 23, 2022 11:04 — forked from vishalratna-microsoft/Barrier.java
Code for Barrier
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
@belinwu
belinwu / LazyLifecycleCallbacks.kt
Created June 23, 2022 11:04 — forked from vishalratna-microsoft/LazyLifecycleCallbacks.kt
LazyLifecycleCallbacks contract
import android.view.View
interface LazyLifecycleCallbacks {
/**
* Lazy version of activity onCreate() callback. Should be used for one time initialisations that could be done after the
* screen has finished rendering. Should not be used for complementary calls that set/reset their state in
* onCreate/onDestroy
*/
fun onLazyCreate()
@belinwu
belinwu / BaseViewModel.kt
Created September 7, 2022 11:09 — forked from qwert2603/BaseViewModel.kt
ViewModelStateFlow
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
sealed class ViewModelStateFlow<T>(stateFlow: StateFlow<T>) : StateFlow<T> by stateFlow
private class ViewModelStateFlowImpl<T>(
initial: T,
val wrapped: MutableStateFlow<T> = MutableStateFlow(initial)
) : ViewModelStateFlow<T>(wrapped)