Skip to content

Instantly share code, notes, and snippets.

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
@Vove7
Vove7 / flutter_aar_upload.gradle
Created Mar 11, 2021
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")) {
@gmk57
gmk57 / 1 ViewBindingDelegates.kt
Last active Jan 30, 2023
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 Dec 28, 2022
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 Dec 5, 2020
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 Sep 10, 2022
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 Jan 25, 2023
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 Oct 12, 2022
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
@av
av / main.dart
Created Jan 13, 2020
Flutter: neu
View main.dart
import 'package:flutter/material.dart';
void main() => runApp(NeumorphicApp());
class NeumorphicApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Neumorphic App',
theme: ThemeData(