Skip to content

Instantly share code, notes, and snippets.

@fuxingloh
fuxingloh / HorizontalSnapWidget.dart
Created January 16, 2020 07:18
Flutter horizontal snap widget with sampleBuilder to estimate the height of widget because widget height is dynamically generated.
import 'package:flutter/widgets.dart';
class HorizontalSnapWidget extends StatelessWidget {
final WidgetBuilder sampleBuilder;
final IndexedWidgetBuilder itemBuilder;
final int itemCount;
final double itemWidth;
final int initialPage;
final double spacing;
export const Day = {
mon: {
text: "Monday", isToday() {
return Day.isToday(Day.mon)
}
},
tue: {
text: "Tuesday", isToday() {
return Day.isToday(Day.tue)
}
const R = 6371; // Radius of the earth in km
function deg2rad(deg) {
return deg * (Math.PI / 180)
}
/**
*
* @param latLng1
* @param latLng2
@fuxingloh
fuxingloh / UICollectionViewFlowLayoutSnapping.swift
Last active January 14, 2020 14:00
UI collection view flow layout snapping.
//
// Created by Fuxing Loh on 2019-03-07.
// Copyright (c) 2019 Munch Technologies. All rights reserved.
//
import Foundation
import UIKit
class UICollectionViewFlowLayoutSnapping: UICollectionViewFlowLayout {
override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
// Page width used for estimating and calculating paging.
@fuxingloh
fuxingloh / example.vue
Created January 13, 2020 16:00
A plugin for nuxtjs, changing browser url without overwriting query string or path.
<script>
export default {
mounted() {
// Replace place without changing querystring
this.$path.replace({path: '/path-name'})
// Replace query string without changing path
this.$path.replace({query: {'foo': 'bar'}})
}
}
@fuxingloh
fuxingloh / EntityPatcher.java
Last active December 25, 2019 17:58
How to patch entity in JPA Persistence. Json & RESTful API.
import com.fasterxml.jackson.databind.JsonNode;
import dev.fuxing.utils.JsonUtils;
import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import java.util.function.Consumer;
/**
* Created by: Fuxing
*/
@fuxingloh
fuxingloh / EnumValidator.java
Created June 25, 2019 19:13
Java Enum, @ValidEnum with Constraint that supports JsonIgnoreProperties if not registered. Basically: how to handle unknown enum values in swift & java.
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
* Created by: Fuxing
*/
public class EnumValidator implements ConstraintValidator<ValidEnum, Enum> {
@Override
public boolean isValid(Enum value, ConstraintValidatorContext context) {
if (value == null) return false;
@fuxingloh
fuxingloh / PostPatch.java
Created April 22, 2019 09:24
Don't build: Use Intellij
@Test
void patch() {
AirtableRecord record = new AirtableRecord();
record.putField("Name", TEST_NAME);
record = table.post(record);
record = new AirtableRecord();
record.setId(record.getId());
record.putField("Integer", "42");
record = table.patch(record, true);
@fuxingloh
fuxingloh / FacebookAppEventsPlugin.java
Created February 13, 2019 06:35
Facebook App Event plugin on Flutter. (Android only)
package app.munch.facebookappevents;
import android.os.Bundle;
import com.facebook.appevents.AppEventsLogger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
@fuxingloh
fuxingloh / ecs-clean.sh
Created February 5, 2019 09:28
A simple bash script to remove all untagged ECS images.
aws ecr describe-repositories --output text | awk '{print $5}' | while read line; do aws ecr list-images --repository-name $line --filter tagStatus=UNTAGGED --query 'imageIds[*]' --output text | while read imageId; do aws ecr batch-delete-image --repository-name $line --image-ids imageDigest=$imageId; done; done