Skip to content

Instantly share code, notes, and snippets.

View niwatly's full-sized avatar

Ryohe Takase niwatly

View GitHub Profile
@niwatly
niwatly / GsonUtils.kt
Last active June 26, 2023 19:34
Gson Utils by Kotlin
//convert gson String to Map<K, V>. Crash when invalid structure found
inline fun <reified K, reified V> String.toMapByGson(): Map<K, V> = if (isNotEmpty()) {
Gson().fromJson<HashMap<K, V>>(this, TypeToken.getParameterized(HashMap::class.java, K::class.java, V::class.java).type)
} else {
mapOf<K, V>()
}
//convert gson String to List<T>. Crash when invalid structure found
inline fun <reified T> String.toListByGson(): List<T> = if (isNotEmpty()) {
Gson().fromJson<List<T>>(this, TypeToken.getParameterized(ArrayList::class.java, T::class.java).type)
@niwatly
niwatly / FragmentUtils.kt
Created October 21, 2018 08:01
Fragment Utils by Kotlin
/**
* persistence property that implements Parcelable on Fragment.
* The set value also write to Fragment.arguments, and always read from Fragment.arguments if need.
*/
class ParcelableProperty<T : Any>(private val defaultValue: T? = null) : kotlin.properties.ReadWriteProperty<Fragment, T> {
var value: T? = null
override operator fun getValue(thisRef: android.support.v4.app.Fragment, property: kotlin.reflect.KProperty<*>): T {
if (value == null) {
@niwatly
niwatly / PulseLiveData.kt
Last active October 21, 2018 08:23
pseudo-stateless LiveData
/**
* LiveData that behaviors like as stateless. the set value will be override by null.
* Obervers have soem limit not to detect null-value-to-override as new value.
*
* cited:
* https://github.com/googlesamples/android-architecture/blob/dev-todo-mvvm-live/todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/SingleLiveEvent.java
* https://android.jlelse.eu/android-arch-handling-clicks-and-single-actions-in-your-view-model-with-livedata-ab93d54bc9dc
*/
@niwatly
niwatly / ActionLiveData.kt
Last active October 21, 2018 08:38
Convenient LiveData for observing click event.
/**
* LiveData that ignore current value broadcast when observe.
* Like PulseLiveData(https://gist.github.com/niwatly/3b1ee17f25a63276e36194cd86f9b223), but ActionLiveData holds last value. so, result is defferent when getValue.
*
* cited:
* https://github.com/googlesamples/android-architecture/blob/dev-todo-mvvm-live/todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/SingleLiveEvent.java
*  https://android.jlelse.eu/android-arch-handling-clicks-and-single-actions-in-your-view-model-with-livedata-ab93d54bc9dc
*
* see also:
* https://qiita.com/KazaKago/items/acce0c1a970441b44f39
@niwatly
niwatly / DynamicScreenView.cs
Created June 24, 2019 15:13
WorldSpaceでもマルチ解像度に対応したいスクリプトです
using System.Linq;
using UnityEngine;
namespace Btf.View
{
public class DynamicScreenView : MonoBehaviour
{
[SerializeField]
[Tooltip("端末サイズに合わせて拡大/縮小したいRectを設定します")]
copyfluttericons() {
if test $# != 1; then
echo "フォルダを指定してください"
else
flutterIconsFolder=$1
\mv -f $flutterIconsFolder/*.dart ./lib/resource/icon_resource.dart
\mv -f $flutterIconsFolder/fonts/*.ttf ./fonts/
\mv -f $flutterIconsFolder/config.json ./fonts/
@niwatly
niwatly / test_screen.dart
Created December 23, 2019 09:22
ChangeNotifierProvider
import 'package:collabo_base_app/helper/route_helper.dart';
import 'package:collabo_base_app/view/space_box.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../app.dart';
class _TestNotifier = ValueNotifier<int> with Type;
@niwatly
niwatly / app_state_notifier.dart
Last active March 19, 2023 09:33
アプリのforeground状態を通知してくれるStream example
import 'package:collabo_base_app/common.dart';
import 'package:flutter/widgets.dart';
import 'package:is_lock_screen/is_lock_screen.dart';
import 'package:rxdart/rxdart.dart';
class AppStateNotifier with WidgetsBindingObserver {
final Subject<AppLifecycleState> _appStateChanged = PublishSubject();
late Stream<AppLifecycleState> appStateStream;
final CompositeSubscription _compositeSubscription = CompositeSubscription();