Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
escamoteur committed Apr 4, 2018
0 parents commit 85f2e49
Show file tree
Hide file tree
Showing 14 changed files with 849 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
.atom/
.dart_tool/
.idea
.packages
.pub/
packages
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
// Verwendet IntelliSense zum Ermitteln möglicher Attribute.
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Flutter",
"request": "launch",
"type": "dart"
}
]
}
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## [0.0.1] - TODO: Add release date.

* TODO: Describe initial release.
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO: Add your license here.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# rx_command

A new flutter package project.

## Getting Started

For help getting started with Flutter, view our online [documentation](https://flutter.io/).

For help on editing package code, view the [documentation](https://flutter.io/developing-packages/).
8 changes: 8 additions & 0 deletions ios/Flutter/Generated.xcconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This is a generated file; do not edit or check into version control.
FLUTTER_ROOT=C:\Entwicklung\Flutter
FLUTTER_APPLICATION_PATH=c:\Entwicklung\FlutterApps\packages\rx_command
FLUTTER_TARGET=lib/main.dart
FLUTTER_BUILD_MODE=debug
FLUTTER_BUILD_DIR=build
SYMROOT=${SOURCE_ROOT}/../build\ios
FLUTTER_FRAMEWORK_DIR=C:\Entwicklung\Flutter\bin\cache\artifacts\engine\ios
14 changes: 14 additions & 0 deletions ios/Runner/GeneratedPluginRegistrant.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Generated file. Do not edit.
//

#ifndef GeneratedPluginRegistrant_h
#define GeneratedPluginRegistrant_h

#import <Flutter/Flutter.h>

@interface GeneratedPluginRegistrant : NSObject
+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry;
@end

#endif /* GeneratedPluginRegistrant_h */
12 changes: 12 additions & 0 deletions ios/Runner/GeneratedPluginRegistrant.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Generated file. Do not edit.
//

#import "GeneratedPluginRegistrant.h"

@implementation GeneratedPluginRegistrant

+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry {
}

@end
220 changes: 220 additions & 0 deletions lib/rx_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
library rx_command;

import 'dart:async';

import 'package:rxdart/rxdart.dart';

/*
typedef TResult Func<TParam, TResult>(TParam param);
abstract class RxCommandFactory
{
static RxCommand<TParam, TResult> createSync<TParam, TResult>(Func<TParam, TResult> func)
{
return new RxCommandSync<TParam,TResult>(func);
}
}
*/

typedef void Action();
typedef void Action1<TParam>(TParam param);


typedef TResult Func<TResult>();
typedef TResult Func1<TParam, TResult>(TParam param);

typedef Future AsyncAction();
typedef Future AsyncAction1<TParam>(TParam param);


typedef Future<TResult> AsyncFunc<TResult>();
typedef Future<TResult> AsyncFunc1<TParam, TResult>(TParam param);


abstract class RxCommandFactory
{

static RxCommand<Unit, Unit> createSync(Action action)
{
return new RxCommandSync<Unit,Unit>((_) {action(); return Unit.Default;});
}

static RxCommand<TParam, Unit> createSync1<TParam>(Action1<TParam> action)
{
return new RxCommandSync<TParam,Unit>((x) {action(x); return Unit.Default;});
}

static RxCommand<Unit, TResult> createSync2<TResult>(Func<TResult> func)
{
return new RxCommandSync<Unit,TResult>((_) => func());
}

static RxCommand<TParam, TResult> createSync4<TParam, TResult>(Func1<TParam,TResult> func)
{
return new RxCommandSync<TParam,TResult>((x) => func(x));
}

}



abstract class RxCommand<TParam, TRESULT>
{

void execute([TParam param]);

Observable<TRESULT> get results => _resultsSubject.observable;
BehaviorSubject<TRESULT> _resultsSubject = new BehaviorSubject<TRESULT>();


Observable<bool> get isExecuting => _isExecutingSubject.observable.startWith(false).distinct();
Observable<bool> canExecute;
Observable<Exception> get thrownExceptions => _thrownExceptionsSubject.observable;

ReplaySubject<bool> _isExecutingSubject = new ReplaySubject<bool>(maxSize: 1);
ReplaySubject<bool> _canExcuteubject = new ReplaySubject<bool>(maxSize: 1);
ReplaySubject<Exception> _thrownExceptionsSubject = new ReplaySubject<Exception>(maxSize: 1);

}


class RxCommandSync<TParam, TResult> extends RxCommand<TParam, TResult>
{


Func1<TParam, TResult> _func;

RxCommandSync(Func1<TParam, TResult> func, [Observable<bool> canExecute] )
{

_func = func;

var canExecuteParam = canExecute == null ? new Observable.just(true)
: canExecute.handleError((error)
{
if (error is Exception)
{
_thrownExceptionsSubject.add(error);
}
return false;
})
.startWith(false);


this.canExecute = new Observable(Observable
.combineLatest2(isExecuting, canExecuteParam, (isEx, canEx) => canEx && !isEx)
.distinct().asBroadcastStream());

}

@override
void execute([TParam param])
{
canExecute
.where( (can) => can == true)
.doOnEach((_){

_isExecutingSubject.add(true);
var result = _func(param);
_isExecutingSubject.add(false);

if (TResult is Unit )
{
_resultsSubject.add(Unit.Default as TResult);
}
_resultsSubject.add(result);

})
.handleError((error)
{
if (error is Exception)
{
_thrownExceptionsSubject.add(error);
}
print(error.toString());

})
.first;

}
}

/*
class RxCommandAsync<TParam, TResult> extends RxCommand<TParam, TResult>
{
AsyncFunc1<TParam, TResult> _func;
RxCommandSync(AsyncFunc1<TParam, TResult> func, [Observable<bool> canExecute] )
{
_func = func;
var canExecuteParam = canExecute == null ? new Observable.just(true)
: canExecute.handleError((error)
{
if (error is Exception)
{
_thrownExceptionsSubject.add(error);
}
return false;
})
.startWith(false);
this.canExecute = new Observable(Observable
.combineLatest2(isExecuting, canExecuteParam, (isEx, canEx) => canEx && !isEx)
.distinct().asBroadcastStream());
}
@override
void execute([TParam param])
{
canExecute
.where( (can) => can == true)
.doOnEach((_){
_isExecutingSubject.add(true);
var result = _func(param);
_isExecutingSubject.add(false);
if (TResult is Unit )
{
_resultsSubject.add(Unit.Default as TResult);
}
_resultsSubject.add(result);
})
.handleError((error)
{
if (error is Exception)
{
_thrownExceptionsSubject.add(error);
}
print(error.toString());
})
.first;
}
}
*/

/// If you don't want to pass one of the generic parameters e.g. if you passed function has no parameter, just use Unit as Type
class Unit
{
static Unit get Default => new Unit();

bool operator == (o) => o is Unit;

}


Loading

0 comments on commit 85f2e49

Please sign in to comment.