Skip to content

Instantly share code, notes, and snippets.

View patrick-elmquist's full-sized avatar

Patrick Elmquist patrick-elmquist

View GitHub Profile
@patrick-elmquist
patrick-elmquist / ring_buffer.sql
Created September 22, 2017 14:48 — forked from elyezer/ring_buffer.sql
How to create a ring buffer table in SQLite
-- Example table
CREATE TABLE ring_buffer (id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT);
-- Number 10 on where statement defines the ring buffer's size
CREATE TRIGGER delete_tail AFTER INSERT ON ring_buffer
BEGIN
DELETE FROM ring_buffer WHERE id%10=NEW.id%10 AND id!=NEW.id;
END;
@patrick-elmquist
patrick-elmquist / avd_loading_bar.xml
Created October 2, 2017 13:37 — forked from nickbutcher/avd_loading_bar.xml
A prototype of a loading indicator utilizing repeated gradients. See https://twitter.com/crafty/status/914830571196571648
<?xml version="1.0" encoding="utf-8"?>
<!--
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 distributed under the License
class ShowAdapter(private val items: List<Show>) : RecyclerView.Adapter<ShowViewHolder>() {
init {
setHasStableIds(true)
}
override fun getItemCount() = items.size
override fun getItemId(position: Int) = items[position].hashCode().toLong()
override fun onCreateViewHolder(parent: ViewGroup, vt: Int) = ShowViewHolder(parent.inflate(R.layout.item_show))
override fun onBindViewHolder(holder: ShowViewHolder, position: Int) = holder.bind(items[position])
}
data class Show(
@StringRes val title: Int,
@StringRes val description: Int,
@DrawableRes val image: Int
)
class ShowViewHolder(view: View) : ViewHolder(view) {
private val title = itemView.title
private val description = itemView.description
private val thumbnail = itemView.thumbnail
private val poster = itemView.poster
private val interpolator = FastOutLinearInInterpolator()
/**
* Offset the thumb and text with a factor [-1.0..1.0] of the total width
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clipChildren="false"
>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
... >
<ImageView
android:id="@+id/poster"
android:layout_width="match_parent"
recyclerView.apply {
// Create and attach the adapter
adapter = ShowAdapter(tvShows)
// Configure to be a horizontal list
layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
// Attach a snap helper, more on that below
PagerSnapHelper().attachToRecyclerView(this)
fun RecyclerView.setupParallaxScrollListener() {
addOnScrollListener(object : OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
val layoutManager = layoutManager as? LinearLayoutManager ?: return
val scrollOffset = recyclerView.computeHorizontalScrollOffset()
val offsetFactor = (scrollOffset % measuredWidth) / measuredWidth.toFloat()
val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()
findViewHolderForAdapterPosition(firstVisibleItemPosition)?.let {
@patrick-elmquist
patrick-elmquist / start_emulator.sh
Created March 4, 2019 06:42 — forked from itmammoth/start_emulator.sh
Start an android emulator with changing DNS server to Google public DNS
#!/bin/bash
EMULATOR=~/Library/Android/sdk/tools/emulator
if [ $# -ne 1 ]; then
echo "ERROR: Please specify the target AVD from the list below" 1>&2
$EMULATOR -list-avds 1>&2
exit 1
fi
$EMULATOR -avd $1 -dns-server "8.8.8.8,8.8.4.4"