Skip to content

Instantly share code, notes, and snippets.

View marcardar's full-sized avatar

marcardar marcardar

View GitHub Profile
@handstandsam
handstandsam / InstallReferrerExt.kt
Created February 28, 2022 20:29
Install Referrer KTX - Kotlin Coroutine friendly wrapper for the Google Play's InstallReferrerClient API. This API is used to ask Google Play about where the installation originated.
import android.content.Context
import android.os.RemoteException
import com.android.installreferrer.api.InstallReferrerClient
import com.android.installreferrer.api.InstallReferrerStateListener
import com.android.installreferrer.api.ReferrerDetails
import kotlinx.coroutines.CompletableDeferred
/**
* https://developer.android.com/google/play/installreferrer/library
*
@handstandsam
handstandsam / MyLifecycleOwner.kt
Last active July 13, 2024 05:35
Jetpack Compose OverlayService. You have to have all the correct permissions granted and in your manifest, but if you do, this this will show a green box with "Hello" in it!
import android.os.Bundle
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleRegistry
import androidx.savedstate.SavedStateRegistry
import androidx.savedstate.SavedStateRegistryController
import androidx.savedstate.SavedStateRegistryOwner
internal class MyLifecycleOwner : SavedStateRegistryOwner {
private var mLifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
private var mSavedStateRegistryController: SavedStateRegistryController = SavedStateRegistryController.create(this)
@LouisCAD
LouisCAD / Companion object Powers example.kt
Last active January 21, 2024 07:45
Demonstration of the powers of companion objects when it comes to discoverability. Great alternative to java-style "factories".
package i_love_companion_objects
suspend fun main() {
// Discoverability is easy with the companion object
// (although autocomplete could use some performance improvements...)
doStuff(SomeInterface.createWithDefaults())
val someInstance: SomeInterface = SomeInterface.create(
parameterOne = 0,
parameterTwo = 1
)
@objcode
objcode / ConcurrencyHelpers.kt
Last active July 7, 2024 09:57
Helpers to control concurrency for one shot requests using Kotlin coroutines.
/* Copyright 2019 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
package com.geekorum.geekdroid.navigation
import android.content.Context
import android.os.Bundle
import android.util.AttributeSet
import androidx.core.content.res.use
import androidx.core.os.bundleOf
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
@Schadenfeude
Schadenfeude / BillingManagerProvider.kt
Last active August 18, 2020 12:22
A facade which provides an implementation of a billing manager depending on whether in-app billing via PlayStore is available
import android.content.Context
import android.util.Log
import com.android.billingclient.api.BillingClient
import com.android.billingclient.api.BillingClientStateListener
import com.android.billingclient.api.Purchase
import com.wacom.cloud.billing.manager.BillingManagerAliPay
import com.wacom.cloud.billing.manager.BillingManagerPlayStore
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.withContext
import kotlin.coroutines.experimental.suspendCoroutine
@zach-klippenstein
zach-klippenstein / SelectClauses.kt
Last active August 20, 2021 16:39
Helper methods to build select clauses for Kotlin coroutines
package com.zachklipp.coroutines
import kotlinx.coroutines.experimental.DisposableHandle
import kotlinx.coroutines.experimental.intrinsics.startCoroutineCancellable
import kotlinx.coroutines.experimental.selects.SelectClause0
import kotlinx.coroutines.experimental.selects.SelectClause1
import kotlinx.coroutines.experimental.selects.SelectClause2
import kotlinx.coroutines.experimental.selects.SelectInstance
/**
@LouisCAD
LouisCAD / LifecycleCoroutines.kt
Last active July 3, 2022 11:47
CoroutineScope and Job integration with Lifecycle for Android. Meant to be used for your coroutines in lifecycle aware components. OUTDATED. See up to date implementation here: https://github.com/LouisCAD/Splitties/tree/master/modules/lifecycle-coroutines
import android.arch.lifecycle.GenericLifecycleObserver
import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.Lifecycle.Event.ON_DESTROY
import android.arch.lifecycle.LifecycleOwner
import kotlinx.coroutines.experimental.CoroutineScope
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.android.Main
fun Lifecycle.createJob(cancelEvent: Lifecycle.Event = ON_DESTROY): Job {
@arekolek
arekolek / KotlinFunctions.md
Last active February 22, 2019 05:20 — forked from cbeyls/KotlinFunctions.md
Comparison of Kotlin functions: also, apply, let, run, with

Receiver vs. return value:

Returns block result Returns receiver
Receiver available as it let also
Receiver available as this run apply

How to read the table:

@mandybess
mandybess / ItemDecoration.java
Last active April 6, 2024 09:56
RecyclerView extension that "sticks" items in the center on scroll
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class ItemDecoration extends RecyclerView.ItemDecoration {
/**
*
* {@link #startPadding} and {@link #endPadding} are final and required on initialization
* because {@link android.support.v7.widget.RecyclerView.ItemDecoration} are drawn