Skip to content

Instantly share code, notes, and snippets.

View LouisCAD's full-sized avatar

Louis CAD LouisCAD

View GitHub Profile
@LouisCAD
LouisCAD / generate-pushid.js
Created November 24, 2016 13:55 — forked from mikelehen/generate-pushid.js
JavaScript code for generating Firebase Push IDs
/**
* Fancy ID generator that creates 20-character string identifiers with the following properties:
*
* 1. They're based on timestamp so that they sort *after* any existing ids.
* 2. They contain 72-bits of random data after the timestamp so that IDs won't collide with other clients' IDs.
* 3. They sort *lexicographically* (so the timestamp is converted to characters that will sort properly).
* 4. They're monotonically increasing. Even if you generate more than one in the same timestamp, the
* latter ones will sort after the former ones. We do this by using the previous random bits
* but "incrementing" them by 1 (only in the case of a timestamp collision).
*/
@LouisCAD
LouisCAD / MyPreferences.kt
Last active September 14, 2020 17:12
Make Android's SharedPreferences as simple as a field in kotlin.
package com.mypackage.myapp.prefs
import android.content.Context
import com.mypackage.myapp.appCtx
object MyPreferences : Preferences(name = "MyPrefsFileName") {
var userWantsFancyUi by BoolPref(Keys.FANCY_UI, false)
var isFcmTokenSent by BoolPref(Keys.FCM_TOKEN_SENT_TO_SERVER, false)
var eggTapsCount by LongPref(Keys.EGG_TAPS_COUNT, 0L)
@LouisCAD
LouisCAD / CursorCallback.java
Created December 20, 2016 10:42 — forked from NikolaDespotoski/CursorCallback.java
Cursor Diff Util callback.
import android.database.Cursor;
import android.support.annotation.Nullable;
import android.support.v7.util.DiffUtil;
/**
* Created by Nikola on 9/29/2016.
*/
public abstract class CursorCallback<C extends Cursor> extends DiffUtil.Callback {
private final C newCursor;
@LouisCAD
LouisCAD / BitFlags.kt
Last active December 24, 2022 16:28
Allows simple bit flags operation on int values in kotlin. Now available in Splitties: https://github.com/LouisCAD/Splitties/tree/master/modules/bitflags Inspired by: http://stackoverflow.com/a/40588216/4433326
@file:Suppress("NOTHING_TO_INLINE")
import kotlin.experimental.and // Used for Byte
import kotlin.experimental.inv // Used for Byte
import kotlin.experimental.or // Used for Byte
inline fun Int.hasFlag(flag: Int) = flag and this == flag
inline fun Int.withFlag(flag: Int) = this or flag
inline fun Int.minusFlag(flag: Int) = this and flag.inv()
import android.annotation.SuppressLint
import android.content.ContentResolver
import android.database.Cursor
import android.net.Uri
/**
* @see ContentResolver.query
*/
@SuppressLint("Recycle")
@Suppress("NOTHING_TO_INLINE")
@LouisCAD
LouisCAD / installAll.gradle
Last active March 27, 2019 06:01
Installs all the release variants of a library to the local maven repository and bintray. Useful if you want to deploy an android library using productFlavors, and push all the flavors to bintray and jcenter. You may need to edit the info specific to my library in the pomConfig, pom and pkg closures. Example here: https://github.com/LouisCAD/Spl…
/*
* Copyright (c) 2017. Louis Cognault Ayeva Derman
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
@LouisCAD
LouisCAD / AppInitProvider.kt
Created January 23, 2017 10:08
Use ContentProvider to init your application. This example sets up Timber logging and a "crash shield" that can do what you want 5 seconds after an uncaught exception.
import android.app.AlarmManager
import android.app.AlarmManager.ELAPSED_REALTIME_WAKEUP
import android.app.PendingIntent
import android.content.Context
import android.os.SystemClock
import android.support.annotation.IntRange
import com.google.firebase.provider.FirebaseInitProvider
import org.jetbrains.anko.alarmManager
import timber.log.Timber
@LouisCAD
LouisCAD / JobDispatcherReschedulerWorkaroundService.kt
Last active January 9, 2018 06:11
A fix for Firebase Job Dispatcher issue #6 (https://github.com/firebase/firebase-jobdispatcher-android/issues/6). You need to extend PersistedJobService instead of JobService for this to work, and call Jobs.schedulePresisted(tag)
import com.google.android.gms.gcm.GcmTaskService
import com.google.android.gms.gcm.TaskParams
import com.example.androidapp.jobs.Jobs
import com.example.androidapp.jobs.tagsOfScheduledJobs
import timber.log.Timber
/**
* See [this issue](https://github.com/firebase/firebase-jobdispatcher-android/issues/6).
*/
class JobDispatcherReschedulerWorkaroundService : GcmTaskService() {
@LouisCAD
LouisCAD / TextWatcher.kt
Last active June 4, 2018 13:00
EditText TextWatcher, without the override me again ceremony
import android.text.Editable
import android.widget.EditText
import splitties.collections.forEachByIndex
import android.text.TextWatcher as AndroidTextWatcher
interface TextWatcher : AndroidTextWatcher {
override fun afterTextChanged(s: Editable) = Unit
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit
}
@LouisCAD
LouisCAD / gmail_email.py
Created October 8, 2017 19:08 — forked from erans/gmail_email.py
Check if an Email address is Gmail or Google Apps for your domain
import sys
import re
import dns.resolver # Requires dnspython
email_host_regex = re.compile(".*@(.*)$")
gmail_servers_regex = re.compile("(.google.com.|.googlemail.com.)$", re.IGNORECASE)
def is_gmail(email):
""" Returns True if the supplied Email address is a @gmail.com Email or is a Google Apps for your domain - hosted Gmail address
Checks are performed by checking the DNS MX records """