Skip to content

Instantly share code, notes, and snippets.

View objcode's full-sized avatar

Sean McQuillan objcode

  • Mountain View, CA
View GitHub Profile
@objcode
objcode / tiered_lights.py
Created September 16, 2017 19:25
Factorio tiered status lights on a tank
# A tiered light indicator is pretty easy to build and this will show you how to build scalable indicators using (2n + 1) decoders
# and one arithmetic. I made mine with three lights, which is enough to understand at a glance what's going on.
#
# You can also put the logic on the lights and use fewer deciders but I prefer this setup because it's easier to modify.
#
# Inputs:
# - R is a constant "ratio". It should be set to the integer value of (tank size - 1 / # of lights).
# - resource types
#
# Variables:
@objcode
objcode / inferrence.kt
Last active September 29, 2017 00:02
Kotlin type puzzle
// In the following declaration, what happens when you call fn()?
val fn: () -> Unit = { "I am not a string" }
// Due to inference, the lambda is evaluated to return Unit and the string is discarded. It is as if you typed,
fun fn(): Unit {
"I am not a string"
return
}
@objcode
objcode / build.gradle
Created April 16, 2018 17:53
1.1.0 Dependency for Constraint Layout
dependencies {
compile 'com.android.support.constraint:constraint-layout:1.1.0'
}
@objcode
objcode / percent_width.xml
Created April 16, 2018 21:35
70% width button example
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintWidth_percent="0.70" />
{"videos":[{"title":"Android Jetpack: EmojiCompat","description":"With the EmojiCompat library, part of Jetpack, your app can get backwards-compatible emoji support on devices with API level 19 and higher and get rid of the blank square characters. To use EmojiCompat, initialize the library when the application starts by using downloadable or bundled fonts. Start supporting emojis in your text views, either by integrating the provided widgets or update your custom views by using the provided helper classes. \n\nFor more information:\nEmojiCompat guide → https://goo.gl/7bEoXB \nEmojiCompat sample app → https://goo.gl/7bEoXB \nGoogle I/O 2017 talk on “What’s new in Support Library” → https://goo.gl/KeRvqB\n\nWatch more Android Jetpack videos here → https://goo.gl/kw8LPv\n\nSubscribe to the Android Developers channel here → https://goo.gl/vLYDU\n\n#jetpack #featured","url":"https://www.youtube.com/watch?v=sYGKUtM2ga8","updated":"2018-06-07T17:09:43+00:00","thumbnail":"https://i4.ytimg.com/vi/sYGKUtM2ga8/hqdefaul
private class DownloadActor(val scope: CoroutineScope) {
private val requested = mutableMapOf<Location, MutableList<Reference>>()
private val apiThread = newFixedThreadPoolContext(1, "DownloadActor")
private fun downloader(
references: ReceiveChannel<Reference>,
locations: SendChannel<Location>
) = scope.launch(apiThread) {
for (ref in references) {
package com.example.android.gdgfinder.libs
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
class RequestLimiter<R>(scope: CoroutineScope) {
val requestChannel = Channel<Pair<suspend () -> R, CompletableDeferred<R>>>()
@objcode
objcode / ConcurrencyHelpers.kt
Last active January 5, 2025 08:26
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,
@objcode
objcode / ViewModel.kt
Last active May 23, 2019 09:52
Implement a one shot request (ViewModel)
class ProductsViewModel(val productsRepository: ProductsRepository): ViewModel() {
private val _sortedProducts = MutableLiveData<List<ProductListing>>()
val sortedProducts: LiveData<List<ProductListing>> = _sortedProducts
/**
* Called by the UI when the user clicks the appropriate sort button
*/
fun onSortAscending() = sortPricesBy(ascending = true)
fun onSortDescending() = sortPricesBy(ascending = false)
@objcode
objcode / Repository.kt
Created May 21, 2019 01:47
Implement a one shot request with coroutines (Repository)
class ProductsRepository(val productsDao: ProductsDao) {
/**
* This is a "regular" suspending function, which means the caller must
* be in a coroutine. The repository is not responsible for starting or
* stopping coroutines since it doesn't have a natural lifecycle to cancel
* unnecessary work.
*
* This *may* be called from Dispatchers.Main and is main-safe because
* Room will take care of main-safety for us.