Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Erik Hellman ErikHellman

🏠
Working from home
View GitHub Profile
@ErikHellman
ErikHellman / LitElementDialogExample.js
Last active Apr 8, 2021
Basic dialog example with LitElement
View LitElementDialogExample.js
import { LitElement, html } from '@polymer/lit-element'
import { classMap } from 'lit-html/directives/classMap'
class MyDialog extends LitElement {
constructor () {
super()
this.opened = false
}
View SimpleService.kt
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 Jan 16, 2018
Background Thread Function
View BackgroundThreadFunction.kt
fun launchBackgroundJob(job: () -> Unit) {
thread { job() }
}
@ErikHellman
ErikHellman / kotlin_coroutine_android_loader.kt
Last active Mar 20, 2020
Kotlin Coroutine Android Loader
View kotlin_coroutine_android_loader.kt
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 Nov 21, 2017
Twig - A logcat wrapper using Kotlin reified generics
View twig.kt
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) {
View RxJavaAndDiffUtil.java
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
View gist:fec71cc0635897a15d97f8bb050475e5
Verifying that "erikhellman.id" is my Blockstack ID. https://onename.com/erikhellman
@ErikHellman
ErikHellman / MainActivity.java
Created Apr 1, 2016
Async communication with Service
View MainActivity.java
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 Mar 23, 2016
Demo of using Handlers for async operations in Android
View MainActivity.java
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 Sep 24, 2015
A base for a wrapper for the Bluetooth LE API on Android
View RxGattWrapper.java
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) {