Skip to content

Instantly share code, notes, and snippets.

View passsy's full-sized avatar

Pascal Welsch passsy

View GitHub Profile
@passsy
passsy / KIntent.kt
Last active March 28, 2023 06:51
Kotlin extension functions to start a generic Activity
package com.pascalwelsch.extensions
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
/**
* Extensions for simpler launching of Activities
@passsy
passsy / TestParcelable.java
Last active April 3, 2017 17:02
How to test a parcelable in androidTest
public class ParcelableTest extends AndroidTestCase {
/**
* Parcels the given Parcelable, unparcels it and returns the unparceled value
*
* @param value the Parcelable to operate on
*/
public static Parcelable parcelAndUnparcel(Parcelable value) {
Parcel parcel = Parcel.obtain();
value.writeToParcel(parcel, 0);
@passsy
passsy / make_archive.sh
Created April 13, 2017 14:51
Create an archive of our source code without git history (including all submodules)
#!/bin/sh
# Check if 7zip is installed and install it if not
hash 7z 2>/dev/null || {
echo >&2 "7z is required to be installed";
echo "I will install it for you";
brew install p7zip;
}
# Read the commit sha
@passsy
passsy / Locale.kt
Last active February 9, 2022 13:05
Allows easy iteration about all available locales. Kotlin Collection implementation for LocaleList which doesn't implement Collection<Locale> itself.
package com.pascalwelsch.android.util
import android.content.Context
import androidx.core.os.ConfigurationCompat
import androidx.core.os.LocaleListCompat
import java.util.Locale
/**
* Returns an [Iterable] for the languages of the user, sorted by priority. First choice first.
*/
@passsy
passsy / RxExt.kt
Created June 6, 2018 16:01
Allow Rx Single to map to nullable values by converting to Maybe. #kotlin
/**
* Maps the [Single] value [T] to a nullable type [R?] which will become [Maybe.empty] when `R == null`,
* otherwise [Maybe.just]
*/
inline fun <T, R> Single<T>.mapNullable(crossinline mapper: (T) -> R?): Maybe<R> = this.flatMapMaybe {
val result = mapper(it)
if (result == null) Maybe.empty() else Maybe.just(result)
}
object Rx {
fun <T> delayFlowable(delay: Long, timeUnit: TimeUnit, scheduler: Scheduler = Schedulers.computation(),
block: () -> Flowable<T>): Flowable<T> {
return Completable.timer(delay, timeUnit, scheduler).andThen(Flowable.defer { block() })
}
fun <T> delayObservable(delay: Long, timeUnit: TimeUnit, scheduler: Scheduler = Schedulers.computation(),
block: () -> Observable<T>): Observable<T> {
return Completable.timer(delay, timeUnit, scheduler).andThen(Observable.defer { block() })
}
@passsy
passsy / main.dart
Last active May 13, 2019 15:23
Missing compile time check
class Apple {}
class Banana {}
List<Apple> bag([List<Apple> apples]) => apples ?? <Banana>[];
void main() {
// ok
final bag1 = bag([Apple()]);
print(bag1);
@passsy
passsy / main.dart
Created July 20, 2019 13:49
Flutter ListView with fixed header expanding below system navigation
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
@passsy
passsy / deep_pick.dart
Created September 27, 2019 15:03
Sample how to use deep_pick to parse json in dart
import 'dart:convert';
import 'package:deep_pick/deep_pick.dart';
import 'package:http/http.dart' as http;
Future<void> main() async {
/// Request data from a json API
final response = await http.get("https://pokeapi.co/api/v2/pokemon/1");
final json = jsonDecode(response.body);
@passsy
passsy / lcd.md
Created December 12, 2019 12:32
LCD Flutter Dojo

Number to LCD Kata

Kotlin coding dojo no. 8: convert a given integer number to a LCD style number, see Part 1 of NumberToLCDKata

    0123456789
         
         
         |
         V