Skip to content

Instantly share code, notes, and snippets.

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

Erik Hellman ErikHellman

🏠
Working from home
View GitHub Profile
@ErikHellman
ErikHellman / LitElementDialogExample.js
Last active May 18, 2023 13:57
Basic dialog example with LitElement
import { LitElement, html } from '@polymer/lit-element'
import { classMap } from 'lit-html/directives/classMap'
class MyDialog extends LitElement {
constructor () {
super()
this.opened = false
}
class SimpleService : Service() {
companion object {
@JvmStatic fun launchService(context: Context) {
context.startService(Intent(context, SimpleService::class.java))
}
}
override fun onBind(intent: Intent?): IBinder {
return Binder()
}
@ErikHellman
ErikHellman / BackgroundThreadFunction.kt
Last active January 16, 2018 07:09
Background Thread Function
fun launchBackgroundJob(job: () -> Unit) {
thread { job() }
}
@ErikHellman
ErikHellman / kotlin_coroutine_android_loader.kt
Last active March 20, 2020 20:42
Kotlin Coroutine Android Loader
internal class CoroutineLifecycleListener(private val deferred: Deferred<*>) : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun cancelCoroutine() {
deferred.cancel()
}
}
internal var POOL = newFixedThreadPoolContext(2, "loader")
fun <T> LifecycleOwner.load(loader: () -> T): Deferred<T> {
@ErikHellman
ErikHellman / twig.kt
Last active November 21, 2017 09:31
Twig - A logcat wrapper using Kotlin reified generics
package se.hellsoft.twig
import android.util.Log
import se.hellsoft.kotlinhacks.BuildConfig
/**
* Verbose logging. Will be stripped from release builds.
*/
inline fun <reified T> T.logv(error: Throwable? = null, m: () -> String) {
if (BuildConfig.DEBUG) {
@ErikHellman
ErikHellman / RxJavaAndDiffUtil.java
Created April 2, 2017 13:41
RxJava & DiffUtil
private void subscribeToData() {
Flowable<List<Data>> dataLists = Flowable
.interval(0, 3, TimeUnit.SECONDS, Schedulers.computation())
.map(val -> {
Log.d(TAG, "Shuffling original list...");
return Data.shuffle(originalList).subList(0, (int) (0.8 * originalList.size()));
})
.share();
Flowable<List<Data>> startWith = dataLists
Verifying that "erikhellman.id" is my Blockstack ID. https://onename.com/erikhellman
@ErikHellman
ErikHellman / MainActivity.java
Created April 1, 2016 10:07
Async communication with Service
package se.hellsoft.handlerthreadingdemo;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
@ErikHellman
ErikHellman / MainActivity.java
Created March 23, 2016 13:46
Demo of using Handlers for async operations in Android
public class MainActivity extends AppCompatActivity {
private static final int MSG_LONG_RUNNING_OPERATION = 101;
private static final int MSG_UPDATE_UI = 102;
private static final long THRITY_SECONDS = 30000;
private Handler bgHandler;
private Handler uiHandler;
private Handler.Callback callback;
private boolean doAgain = false;
@ErikHellman
ErikHellman / RxGattWrapper.java
Created September 24, 2015 08:22
A base for a wrapper for the Bluetooth LE API on Android
public final class RxGattWrapper {
private SerializedSubject<BluetoothGattEvent, BluetoothGattEvent> eventSubject = new SerializedSubject<>(PublishSubject.<BluetoothGattEvent>create());
private BluetoothDevice bluetoothDevice;
private BluetoothGatt bluetoothGatt;
public RxGattWrapper(BluetoothDevice bluetoothDevice) {
this.bluetoothDevice = bluetoothDevice;
}
public void connect(Context context) {