Skip to content

Instantly share code, notes, and snippets.

View riggaroo's full-sized avatar
🌍

Rebecca Franks riggaroo

🌍
View GitHub Profile
@riggaroo
riggaroo / RestServiceMockUtils.java
Last active May 1, 2021 17:52
Mocking API Responses using a Retrofit Client in Android
public class RestServiceMockUtils {
public static String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
@riggaroo
riggaroo / MainActivity.java
Last active July 15, 2020 12:30
Online Presence with Firebase and Android based off article https://firebase.googleblog.com/2013/06/how-to-build-presence-system.html . Read the article as it explains the whole .onDisconnect().removeValue() nicely.
private void initialiseOnlinePresence() {
final DatabaseReference onlineRef = databaseReference.child(".info/connected");
final DatabaseReference currentUserRef = databaseReference.child("/presence/" + userId);
onlineRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
Log.d(TAG, "DataSnapshot:" + dataSnapshot);
if (dataSnapshot.getValue(Boolean.class)){
currentUserRef.onDisconnect().removeValue();
currentUserRef.setValue(true);
@riggaroo
riggaroo / pull_screenshots.sh
Created April 30, 2020 08:34
pull_screenshots.sh script from Chiu-ki Chan's video on terminal tricks https://youtu.be/1N90lU1xn2w
#! /bin/bash
today=$(date +%Y%m%d)
for path in $(adb shell ls /sdcard/Pictures/Screenshots/*"${today}"*); do
name=$(basename "$path")
if [ ! -f "$name" ]; then
adb pull "$path"
fi
done
@riggaroo
riggaroo / InlineClasses.kt
Last active March 2, 2020 06:04
Have you heard about inline classes in Kotlin?
// Why use inline classes? 🤔
// 🎯 Compile time safety
// 🎯 Less runtime overhead than a normal wrapper class as it "inlines" the data into its usages
// More info : https://kotlinlang.org/docs/reference/inline-classes.html
// Without inline classes 😞
data class Recipe(id: UUID)
data class Ingredient(id: UUID, recipeId: UUID)
@riggaroo
riggaroo / SpringAnimationVelocity.kt
Created March 28, 2019 13:00
Example setting start velocity of a spring animation.
SpringAnimation(this, floatPropertyAnimY, point.y).apply {
setStartVelocity(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5000f, resources.displayMetrics))
start()
}
@riggaroo
riggaroo / SpringAnimation.kt
Created March 28, 2019 12:58
Example using SpringAnimation to animate a custom view using FloatPropertyCompat properties.
private fun animateToPoint(point: Point) {
SpringAnimation(this, floatPropertyAnimX, point.x).apply {
spring.stiffness = SpringForce.STIFFNESS_MEDIUM
spring.dampingRatio = SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY
start()
}
SpringAnimation(this, floatPropertyAnimY, point.y).apply {
spring.stiffness = SpringForce.STIFFNESS_MEDIUM
@riggaroo
riggaroo / FloatPropertyCompatExamples.kt
Created March 28, 2019 12:57
Example of using FloatPropertyCompat for SpringAnimations.
private val floatPropertyAnimX = object : FloatPropertyCompat<ColorDropperView>(PROPERTY_X) {
override fun setValue(dropper: ColorDropperView?, value: Float) {
dropper?.setDropperX(value)
}
override fun getValue(dropper: ColorDropperView?): Float {
return dropper?.getDropperX() ?: 0f
}
}
@riggaroo
riggaroo / ValueAnimator.kt
Created March 28, 2019 12:56
Example showing how to use PropertyValuesHolder to animate a custom views properties.
private fun animateToPoint(point: Point) {
val propertyX = PropertyValuesHolder.ofFloat(ColorDropperView.PROPERTY_X, dropperPoint.x, point.x)
val propertyY = PropertyValuesHolder.ofFloat(ColorDropperView.PROPERTY_Y, dropperPoint.y, point.y)
val animator = ValueAnimator()
animator.setValues(propertyX, propertyY)
animator.interpolator = OvershootInterpolator()
animator.duration = 100
animator.addUpdateListener { animation ->
val animatedX = animation.getAnimatedValue(ColorDropperView.PROPERTY_X) as Float
@riggaroo
riggaroo / CustomView.kt
Last active March 21, 2019 15:36
Custom Canvas Drawing with KTX
canvas.withTranslation(200f, 300f) {
drawCircle(150f, 150f, RADIUS, circlePaint)
withRotation(45f) {
drawRect(rect, rectPaint)
}
}
@riggaroo
riggaroo / CustomView-Snippet6.kt
Created March 21, 2019 15:29
Using extension function to draw to canvas.
canvas.withTranslate(200f, 300f) {
drawCircle(...)
}