Skip to content

Commit

Permalink
implemented basic value conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
sglienke committed Jun 15, 2015
1 parent bba2cae commit adade4c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion SimpleMVVM.Binding.Components.pas
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ function TEditBinding.Initialize(const observable: IObservable): TFunc<TValue>;
Result :=
function: TValue
begin
fComponent.Text := observable.Value.AsString;
fComponent.Text := observable.Value.ToString;
end;
end;

Expand Down
6 changes: 5 additions & 1 deletion SimpleMVVM.Binding.pas
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ procedure ApplyBindings(const view: TComponent; const viewModel: TObject);
viewModel);
end;

function CreateObservable(const instance: TObject;
function CreateObservable(instance: TObject;
const expression: string): IObservable;

function CreateRootProp(const prop: TRttiProperty; const instance: TObject): IObservable;
Expand Down Expand Up @@ -134,6 +134,7 @@ function CreateObservable(const instance: TObject;
begin
prop := typ.GetProperty(expressions[i]);
if Assigned(prop) then
begin
if StartsText('IObservable<', prop.PropertyType.Name) then
begin
Result := prop.GetValue(instance).AsInterface as IObservable;
Expand All @@ -147,6 +148,9 @@ function CreateObservable(const instance: TObject;
Result := CreateSubProp(prop, Result);
typ := prop.PropertyType;
end;
if i < High(expressions) then
instance := prop.GetValue(instance).AsObject;
end;
end;
end;

Expand Down
34 changes: 30 additions & 4 deletions SimpleMVVM.Observable.pas
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,29 @@ TDependentObservable<T> = class(TObservableBase, IObservable<T>)
constructor Create(const getter: TFunc<T>; const setter: TAction<T>); overload;
end;

type
TValueHelper = record helper for TValue
function ToType<T>: T;
end;

implementation

uses
TypInfo;
// Spring;

function TValueHelper.ToType<T>: T;
begin
// hardcode some simple conversions for demo purpose - use Spring4D value converter later
case Kind of
tkUString:
case PTypeInfo(System.TypeInfo(T)).Kind of
tkInteger: PInteger(@Result)^ := StrToInt(AsString);
end;
end;
end;



{$REGION 'TObservableBase'}

Expand Down Expand Up @@ -200,7 +221,9 @@ function TDependentObservable.GetValueNonGeneric: TValue;

procedure TDependentObservable.SetValueNonGeneric(const value: TValue);
begin
fSetter(value);
if fIsNotifying then Exit;
if Assigned(fSetter) then
fSetter(value);
inherited Changed;
end;

Expand Down Expand Up @@ -247,8 +270,9 @@ procedure TObservable<T>.SetValue(const value: T);

procedure TObservable<T>.SetValueNonGeneric(const value: TValue);
begin
SetValue(value.AsType<T>);
SetValue(value.ToType<T>);
end;

{$ENDREGION}


Expand Down Expand Up @@ -305,13 +329,15 @@ function TDependentObservable<T>.GetValueNonGeneric: TValue;

procedure TDependentObservable<T>.SetValue(const value: T);
begin
fSetter(value);
if fIsNotifying then Exit;
if Assigned(fSetter) then
fSetter(value);
inherited Changed;
end;

procedure TDependentObservable<T>.SetValueNonGeneric(const value: TValue);
begin
SetValue(value.AsType<T>);
SetValue(value.ToType<T>);
end;

{$ENDREGION}
Expand Down

0 comments on commit adade4c

Please sign in to comment.