Recent

Author Topic: How to Notify Controller of View Changes  (Read 1496 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 516
  • Weitental is a very beautiful garbage depot.
How to Notify Controller of View Changes
« on: November 16, 2021, 04:13:51 pm »
Hello :),

Here My Problem:

I Have a ComboBox and implemented its OnChange Event:

Code: Pascal  [Select][+][-]
  1. procedure TFormSW.CmbOpeningChange(Sender: TObject);
  2. begin
  3.    case CmbOpening.ItemIndex of
  4.       0: begin // volle Öffnung
  5.          Label46.Visible := True;
  6.          Label19.Visible := True;
  7.          Label20.Visible := True;
  8.          Label50.Visible := True;
  9.          Label51.Visible := True;
  10.          Command5.Visible := False;
  11.          Command6.Visible := False;
  12.       end;
  13.       1: begin // halbe Öffnung oben
  14.          Label46.Visible := True;
  15.          Label19.Visible := True;
  16.          Label20.Visible := True;
  17.          Label50.Visible := True;
  18.          Label51.Visible := True;
  19.          Command5.Visible := True;
  20.          Command6.Visible := True;
  21.       end;
  22.       2: begin // halbe Öffnung unten
  23.          Label46.Visible := True;
  24.          Label19.Visible := True;
  25.          Label20.Visible := True;
  26.          Label50.Visible := True;
  27.          Label51.Visible := True;
  28.          Command5.Visible := True;
  29.          Command6.Visible := True;
  30.       end;
  31.       3: begin // keine Öffnung
  32.          TxBtDp.Text:= '0';
  33.          Label46.Visible := False;
  34.          Label19.Visible := False;
  35.          Label20.Visible := False;
  36.          Label50.Visible := False;
  37.          Label51.Visible := False;
  38.          Command5.Visible := False;
  39.          Command6.Visible := False;
  40.       end;
  41.    end;
  42. end;
  43.  

and i Have another Class which is My Controller

Code: Pascal  [Select][+][-]
  1. procedure OeffnungChanged();
  2. begin
  3.       Case OeffIND Of
  4.          0: CalcOpening0(KlapIND, AnordIND);
  5.          1: CalcOpening1(KlapIND, AnordIND);
  6.          2: CalcOpening2(KlapIND, AnordIND);
  7.          3: begin // keine Öffnung
  8.             KpX1:= 0;
  9.             KpX2:= 0;
  10.             KpY1:= 0;
  11.             KpY2:= 0;
  12.             KpZ1:= 0;
  13.             KpZ2:= 0;
  14.          end;
  15.       End;
  16. end;
  17.  

How do i Notify the Controller about  the Changes ?

i currently do it like this:
Cntlr.OeffnungChanged; //Cntlr = Controller

Anyway this seems not that effective.

i tried to use Observers but thats still a bit too complex for me. Then i was trying to do it via TNotifyEvent, but i was also to stupid to understand how this will work.

Could Someone provide a Sollution how to hande this interface between this classes ?

I would like to achieve something similar to MVC or MVVM to make my code more readable and easier to use. Got a Model, View and Controller, but i don't realy know how to handle the Connections between them. I ask myself a lot how to pass Data, how to Notify any of those classes or how to access those classes. i always end up with the same messy Code.

cdbc

  • Hero Member
  • *****
  • Posts: 1090
    • http://www.cdbc.dk
Re: How to Notify Controller of View Changes
« Reply #1 on: November 17, 2021, 03:51:04 pm »
Hi
The "TObserved" is already implemented in TPersistent.
All you need to do is implement the observer and connect it, maybe 15 lines of code...
Code: Pascal  [Select][+][-]
  1.   THKObserver = class(TInterfacedObject,IFPObserver)
  2.   private
  3.     fDs: THKCollection;
  4.     fGrid: TStringGrid;
  5.   public
  6.     constructor Create(aGrid: TStringGrid);
  7.     procedure ClearGrid;
  8.     procedure CreateGridHeaders;
  9.     procedure PopulateGrid;
  10.     Procedure FPOObservedChanged(ASender : TObject; Operation : TFPObservedOperation; Data : Pointer);
  11.   end;
  12.  
  13. procedure THKObserver.FPOObservedChanged(ASender: TObject;Operation: TFPObservedOperation; Data: Pointer);
  14. var
  15.   HkCount: ptrint;
  16. begin
  17. //  if Data <> nil then HkItem:= THKCollectionItem(Data);
  18.   case Operation of
  19.     ooChange: ; //todo
  20.     ooFree: ; //todo
  21.     ooAddItem: {showmessage('Observer notified: Item added...')}; // todo
  22.     ooDeleteItem: ; // todo
  23.     ooCustom: begin // triggered after EndUpdate...
  24.                 HkCount:= ptrint(Data);
  25.                 fmMain.Caption:= MainTitle+' - [Kontakte in Database: '+inttostr(HkCount)+']';
  26.                 fDs:= THKCollection(ASender);
  27.                 PopulateGrid;
  28.               end;
  29.   end;
  30.  
  31. end;
  32.  
  33.  
Something like this.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Weitentaaal

  • Hero Member
  • *****
  • Posts: 516
  • Weitental is a very beautiful garbage depot.
Re: How to Notify Controller of View Changes
« Reply #2 on: November 18, 2021, 10:49:30 am »
So im going to Notify all my Observers in the ViewModel from my Form when Something changed, and with the Sender i cant identify what component changed ?

Data is obviously gonna be the Changed Value i guess.

So its gonna be Something like this: (i don't know how to accept Data and Send it. How do i know what Changed)?

 
Code: Pascal  [Select][+][-]
  1. //In my View
  2. procedure Combo1Changed(Sender: tObject);
  3. begin
  4.   //How do i notify my Observers Here ?
  5.   TObservable.NotifyObservers;
  6. end;
  7.  
  8. //In my ModelView
  9. procedure THKObserver.FPOObservedChanged(ASender: TObject;Operation: TFPObservedOperation; Data: Pointer); //ASender will be Combo1 //Data will be Combo1.Text
  10. var
  11.   Newtext: String;
  12. begin
  13.    //  if Data <> nil then HkItem:= THKCollectionItem(Data);
  14.   case Operation of
  15.     ooChange: ; //todo
  16.     ooFree: ; //todo
  17.     ooAddItem: {showmessage('Observer notified: Item added...')}; // todo
  18.     ooDeleteItem: ; // todo
  19.     ooCustom: begin // triggered after EndUpdate...
  20.                 NewText:= Data;
  21.                 //Do Something with New Data and then Send Changes back
  22.                 fDs:= THKCollection(ASender);
  23.               end;
  24.   end;
  25.  
  26. end;
  27.  

cdbc

  • Hero Member
  • *****
  • Posts: 1090
    • http://www.cdbc.dk
Re: How to Notify Controller of View Changes
« Reply #3 on: November 18, 2021, 03:23:48 pm »
Hi
Easiest way, define a record and send it along in the data-pointer, @DataRec.... You can put all sorts of stuff in there...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

 

TinyPortal © 2005-2018