Skip to content

Instantly share code, notes, and snippets.

View xrubioj's full-sized avatar

Xavier Rubio Jansana xrubioj

View GitHub Profile
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.PowerManager;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewCompat;
import android.view.View;
@dazwin
dazwin / pullrequests.py
Last active November 16, 2020 14:03
Looks at all pull requests across all repos in a Bitbucket (http://bitbucket.org) team and is able to decline if there are not enough reviewers and/or merge if all/required participants have approved
#!/usr/bin/env python
# -*- coding: utf-8
# Example usage:
# BB_USERNAME=myuser BB_PASSWORD=###### python pullrequests.py --owner=myteam --auto-merge --auto-decline
import urllib2
import base64
import json
import re
@chrisbanes
chrisbanes / KotterKnife.kt
Last active February 7, 2021 15:25
LifecycleAware KotterKnife
package kotterknife
import android.app.Activity
import android.app.Dialog
import android.app.DialogFragment
import android.app.Fragment
import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleObserver
import android.arch.lifecycle.LifecycleOwner
import android.arch.lifecycle.OnLifecycleEvent
@romannurik
romannurik / CharsPerLineActivity.java
Last active December 17, 2021 03:15
Demonstrates how to identify and avoid line-length issues with TextView. The measure, or characters per line, of a block of text plays a key role in how comfortable it is to read (sometimes referred to as readability). A widely accepted optimal range for a text block's measure is between 45 and 75 characters. This code demonstrates two phases of…
/*
* Copyright 2013 Google Inc.
*
* 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
@chrisbanes
chrisbanes / ScopedViewModel.kt
Last active October 25, 2022 21:29
ScopedViewModel
open class ScopedViewModel : ViewModel() {
private val job = Job()
protected val scope: CoroutineScope = job + Dispatchers.Main
override fun onCleared() {
super.onCleared()
job.cancel()
}
}
@tomaszpolanski
tomaszpolanski / Comparison.txt
Last active October 25, 2022 21:29
Kotlin Standard comparison
╔══════════╦═════════════════╦═══════════════╦═══════════════╗
║ Function ║ Receiver (this) ║ Argument (it) ║ Result ║
╠══════════╬═════════════════╬═══════════════╬═══════════════╣
║ let ║ this@MyClass ║ String("...") ║ Int(42) ║
║ run ║ String("...") ║ N\A ║ Int(42) ║
║ run* ║ this@MyClass ║ N\A ║ Int(42) ║
║ with* ║ String("...") ║ N\A ║ Int(42) ║
║ apply ║ String("...") ║ N\A ║ String("...") ║
║ also ║ this@MyClass ║ String("...") ║ String("...") ║
╚══════════╩═════════════════╩═══════════════╩═══════════════╝
@nickbutcher
nickbutcher / timestamp.sh
Created April 3, 2017 11:31
A script for setting creation and modification dates on all files within a folder. Specify the first date in the script then all files are stamped in alphabetical order, increasing by 1s.
USAGE="Set the desired date/time in the script and then call: timestamp /path/to/folder"
DATE="022109002017" # month day hour min year
if (( $# != 1 )); then
echo "Illegal parameters"
echo $USAGE
exit 2
fi
if [ ! -d $1 ]; then
@danielocampo2
danielocampo2 / MultiDistinctBy.kt
Last active January 13, 2023 21:20
multiDistinctBy function for Kotlin: Like stdlib distinctBy but for multiple fields
fun <T, K> Iterable<T>.multiDistinctBy(vararg selectors: (T) -> K): List<T> {
require(selectors.isNotEmpty())
val set = HashSet<Int>()
val distinct = ArrayList<T>()
for (element in this) {
val key = selectors.fold(0) { sum, selector ->
sum.plus(selector(element)?.hashCode() ?: 0) }
if (set.add(key))
@chrisbanes
chrisbanes / code.kt
Last active August 10, 2023 10:46
Night Mode inflater
/*
* Copyright 2017 Google, Inc.
*
* 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
@algal
algal / ScaleAspectFitImageView.swift
Last active September 24, 2023 10:19
UIImageView subclass that works with Auto Layout to express its desired aspect ratio
import UIKit
// known-good: Xcode 8.2.1
/**
UIImageView subclass which works with Auto Layout to try
to maintain the same aspect ratio as the image it displays.
This is unlike the usual behavior of UIImageView, where the