Skip to content

Instantly share code, notes, and snippets.

View hleinone's full-sized avatar

Hannu Leinonen hleinone

View GitHub Profile
@hleinone
hleinone / string_localized_case.dart
Last active October 27, 2022 09:39
Dart localized String to upper & lower case, supporting Turkish & Azerbaijani dotted 'i' & dotless 'ı' capitalization & decapitalization.
import 'dart:ui';
extension StringLocalizedCase on String {
String toLocalizedUpperCase(Locale locale) {
if (locale.languageCode == 'tr' || locale.languageCode == 'az') {
return replaceAll('i', 'İ').replaceAll('ı', 'I').toUpperCase();
}
return toUpperCase();
}
@hleinone
hleinone / polyline_decode.dart
Created June 29, 2022 21:25
Google Maps for Flutter Polyline Decoding
import 'package:google_maps_flutter/google_maps_flutter.dart';
List<LatLng> polylineDecode(String polyline) {
final codeUnits = polyline.codeUnits;
final len = codeUnits.length;
// For speed we preallocate to an upper bound on the final length, then
// truncate the array before returning.
final path = <LatLng>[];
var index = 0;
@hleinone
hleinone / compass.dart
Last active June 29, 2022 12:43
Google Maps for Flutter Customizable Buttons
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
/// A button resembling the built-in 'compass' indicator from Google Maps SDK.
/// This supports both light and dark themes. Requires material,
/// google_maps_flutter and flutter_svg.
@hleinone
hleinone / GoogleMaps+Rx.kt
Created February 6, 2019 09:15
Google Maps SDK for Android Rx extension
import android.content.Context
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.SupportMapFragment
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.disposables.Disposables
@androidx.annotation.CheckResult
fun SupportMapFragment.map(): Single<GoogleMap> =
Single.create { emitter ->
@hleinone
hleinone / SocketIO+Rx.kt
Created February 6, 2019 09:11
Android SocketIO Rx extension
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.disposables.Disposables
import io.socket.client.IO
import io.socket.client.Socket
import io.socket.emitter.Emitter
class RxIO(uri: String) {
private val socket = IO.socket(uri)
@hleinone
hleinone / SharedPreferences+Rx.kt
Created February 6, 2019 09:10
Android SharedPreferences Rx extension
import android.content.SharedPreferences
import io.reactivex.Maybe
import io.reactivex.Single
@androidx.annotation.CheckResult
fun SharedPreferences.boolean(key: String, defValue: Boolean): Single<Boolean> = Single.just(getBoolean(key, defValue))
@androidx.annotation.CheckResult
fun SharedPreferences.boolean(key: String): Maybe<Boolean> = Maybe.create { maybeEmitter ->
if (contains(key)) {
@hleinone
hleinone / AlertDialog+Rx.kt
Created February 6, 2019 09:09
Android AlertDialog Rx extension
import android.content.DialogInterface
import androidx.appcompat.app.AlertDialog
import com.jakewharton.rxbinding2.view.clicks
import io.reactivex.Maybe
import io.reactivex.Single
/**
* Sets the dialog non-cancelable. Emits either {@link DialogInterface#BUTTON_POSITIVE},
* {@link DialogInterface#BUTTON_NEGATIVE} or {@link DialogInterface#BUTTON_NEUTRAL} and dismisses
* the dialog.
@hleinone
hleinone / OneDp.kt
Created February 4, 2019 14:12
Density pixel calculation helper
import android.content.res.Resources
import android.util.TypedValue
object OneDp {
fun init(resources: Resources) {
oneDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1f, resources.displayMetrics)
}
}
private var oneDp = 0f
@hleinone
hleinone / rating_bar.dart
Created August 20, 2018 19:12
Flutter rating bar
import 'dart:math';
import 'package:flutter/material.dart';
class RatingBar extends StatelessWidget {
RatingBar({this.numStars = 5, this.rating = 0.0, this.onChanged});
final int numStars;
final double rating;
final ValueChanged<double> onChanged;
@hleinone
hleinone / normalize-gpx.js
Created April 25, 2017 06:44
Normalizes GPX file by replacing track points with exactly same coordinate with interpolated values, removing leaps with inhuman speed.
var fs = require("fs");
var xml2js = require("xml2js");
var gpx = process.argv[2];
// https://gist.github.com/wteuber/6241786
Math.fmod = function(a, b) {
return Number((a - (Math.floor(a / b) * b)).toPrecision(8));
};