Skip to content

Instantly share code, notes, and snippets.

View yongjhih's full-sized avatar
🏠
Working from home

Andrew Chen yongjhih

🏠
Working from home
View GitHub Profile
View NetworkStatusTracker_full.kt
import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
@Krosxx
Krosxx / flutter_aar_upload.gradle
Created March 11, 2021 10:55
Flutter module upload aar to maven
View flutter_aar_upload.gradle
// This script is used to initialize the build in a module or plugin project.
// During this phase, the script applies the Maven plugin and configures the
// destination of the local repository.
// The local repository will contain the AAR and POM files.
void configureProject(Project project, String mavenUrl, String mavenUser, String mavenPwd, String version) {
if (!project.hasProperty("android")) {
throw new GradleException("Android property not found.")
}
if (!project.android.hasProperty("libraryVariants")) {
@DavidIbrahim
DavidIbrahim / DashedBorder.kt
Last active September 27, 2023 12:17
dashedBorder modifier for android compose
View DashedBorder.kt
import androidx.compose.foundation.BorderStroke
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
@gmk57
gmk57 / 1 ViewBindingDelegates.kt
Last active August 28, 2023 20:19
Kotlin delegates for Android View Binding with usage examples
View 1 ViewBindingDelegates.kt
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.viewbinding.ViewBinding
@manuelvicnt
manuelvicnt / LocationRepository.kt
Last active December 28, 2022 04:17
LocationFlow shareIn
View LocationRepository.kt
val FusedLocationProviderClient.locationFlow() = callbackFlow<Location> {
...
}.shareIn(
// Make the flow follow the applicationScope
applicationScope,
// Emit the last emitted element to new collectors
replay = 1,
// Keep the producer active while there are active subscribers
started = SharingStarted.WhileSubscribed()
)
@serhii-pokrovskyi
serhii-pokrovskyi / OnDestroyNullable.kt
Created December 5, 2020 08:03
Property that will be destroyed in onDestoy
View OnDestroyNullable.kt
fun <T> LifecycleOwner.onDestroyNullable(): ReadWriteProperty<LifecycleOwner, T> =
object : ReadWriteProperty<LifecycleOwner, T>, DefaultLifecycleObserver {
private var value: T? = null
init {
this@onDestroyNullable
.lifecycle
.addObserver(this)
}
@jamiesanson
jamiesanson / ViewLifecycleLazy.kt
Last active May 29, 2023 23:10
A Kotlin lazy implementation which automatically clears itself at appropriate times in the View Lifecycle, with a Fragment example
View ViewLifecycleLazy.kt
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
fun <T> Fragment.viewLifecycleLazy(initialise: () -> T): ReadOnlyProperty<Fragment, T> =
object : ReadOnlyProperty<Fragment, T>, DefaultLifecycleObserver {
View FragmentExt.kt
/*
* Based on https://gist.github.com/jamiesanson/d1a3ed0910cd605e928572ce245bafc4
*
* Refactored to use the preferred `DefaultLifecycleObserver` which does not rely on the annotation processor.
* See https://developer.android.com/reference/kotlin/androidx/lifecycle/Lifecycle#init
*
* Usage:
* `private var binding: TheViewBinding by viewLifecycle()`
*/
@Zhuinden
Zhuinden / FragmentViewBindingDelegate.kt
Last active August 18, 2023 08:54
Fragment view binding delegate
View FragmentViewBindingDelegate.kt
// https://github.com/Zhuinden/fragmentviewbindingdelegate-kt
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.viewbinding.ViewBinding
import kotlin.properties.ReadOnlyProperty
@jamiesanson
jamiesanson / ViewLifecycleBinding.kt
Last active April 18, 2023 11:25
Kotlin Property Delegate for Fragment view lifecycle binding
View ViewLifecycleBinding.kt
fun <T> Fragment.viewLifecycle(bindUntilEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY): ReadWriteProperty<Fragment, T> =
object: ReadWriteProperty<Fragment, T>, LifecycleObserver {
// A backing property to hold our value
private var binding: T? = null
private var viewLifecycleOwner: LifecycleOwner? = null
init {
// Observe the View Lifecycle of the Fragment