Skip to content

Instantly share code, notes, and snippets.

View belinwu's full-sized avatar

吴上阿吉 belinwu

View GitHub Profile
@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)
class MainViewModel : ViewModel() {
sealed class Event {
object NavigateToSettings: Event()
data class ShowSnackBar(val text: String): Event()
data class ShowToast(val text: String): Event()
}
private val eventChannel = Channel<Event>(Channel.BUFFERED)
val eventsFlow = eventChannel.receiveAsFlow()
@belinwu
belinwu / Type.kt
Created November 3, 2022 02:39 — forked from AsadLeo14/Type.kt
Access custom font from assets in Jetpack Compose
@OptIn(ExperimentalTextApi::class)
@Composable
fun fontFamily() = FontFamily(
Font(LocalContext.current.assets,"myfont.ttf")
)
@Composable
fun typography() = Typography(
h1 = TextStyle(