Recent

Author Topic: Best way to exchange data between a form and a unit  (Read 49631 times)

Hansvb

  • Hero Member
  • *****
  • Posts: 690
Re: Best way to exchange data between a form and a unit
« Reply #150 on: March 30, 2024, 08:16:25 pm »
Thanks, I will look at it.

Don't spend to much time on changing the presenter. I am all most at the point of giving it up.

cdbc

  • Hero Member
  • *****
  • Posts: 1499
    • http://www.cdbc.dk
Re: Best way to exchange data between a form and a unit
« Reply #151 on: March 30, 2024, 08:50:24 pm »
Hi
Here's the presenter changes:
Code: Pascal  [Select][+][-]
  1.   { IPresenter }
  2.   IPresenter = interface['{48C70B90-A10A-4691-B8E8-JL6JFF35B7DD}']
  3.     function get_AppLocation: String;
  4.     function get_Provider: IobsProvider;
  5.     function get_TrxMan: ITransactionManager; // <--- HERE
  6.     function Obj: TObject;
  7.     procedure GetStaticTexts(aRegion: word);
  8.     procedure SetStatusbartext(aText: string; panel: word);
  9.     procedure set_AppLocation(AValue: String);
  10.     function GetSettingsrecord: TSettingsRec;
  11.     procedure ReadSettings;
  12.  
  13.     property Provider: IobsProvider read get_Provider;
  14.     property TrxMan: ITransactionManager read get_TrxMan; // <--- HERE
  15.     property ApplicationLocation: String read get_AppLocation write set_AppLocation;
  16.   end;
And:
Code: Pascal  [Select][+][-]
  1. unit vwmain.presenter;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils,
  9.   obs_prosu, vwmain.intf, vwmain.decl, vwmain.model, vwmain.trxman;//,
  10.   //Settings;
  11.  
  12. type
  13.   { alias from vwmain.trxman }
  14.   TTransactionManager = vwmain.trxman.TTransactionManager;
  15.   TDirTransaction = vwmain.trxman.TDirTransaction;
  16.   TSettingsTransaction  = vwmain.trxman.TSettingsTransaction;
  17.   //TCreateDbFileTransaction = vwmain.trxman.TCreateDbFileTransaction;
  18.  
  19.   { TPresenter }
  20.  
  21.   TPresenter = class(TObject, IPresenter)
  22.     private
  23.       FProvider: TobsProvider;
  24.       FModel: TModel;
  25.       FTrxMan: TTransactionManager;
  26.       FAppLocation : String;
  27.  
  28.       function get_AppLocation: String;
  29.       function get_Provider: IobsProvider;
  30.       function get_TrxMan: ITransactionManager; // <--- HERE
  31.       function Obj: TObject;
  32.       procedure set_AppLocation(AValue: String);
  33.     protected
  34.  
  35.     public
  36.  
  37.       constructor Create;
  38.       destructor Destroy; override;
  39.  
  40.       procedure GetStaticTexts(aRegion: word);
  41.       procedure SetStatusbartext(aText: string; panel: word);
  42.       function GetSettingsrecord: TSettingsRec;
  43.       procedure ReadSettings;
  44.  
  45.       //procedure StartLogging(logFile: string);
  46.  
  47.       property Provider: IobsProvider read get_Provider;
  48.       property TrxMan: ITransactionManager read get_TrxMan; // <--- HERE
  49.       property ApplicationLocation: String read get_AppLocation write set_AppLocation;
  50.  
  51.   end;
  52.  
  53.  
  54. implementation
  55.  
  56. { TPresenter }
  57.  
  58. {%region Prpoerties}
  59. function TPresenter.get_Provider: IobsProvider;
  60. begin
  61.   Result := FProvider;
  62. end;
  63.  
  64. function TPresenter.get_TrxMan: ITransactionManager; // <--- HERE
  65. begin
  66.   Result := FTrxMan;
  67. end;
  68.  
  69. function TPresenter.get_AppLocation: String;
  70. begin
  71.   Result := FAppLocation;
  72. end;
  73.  
  74. procedure TPresenter.set_AppLocation(AValue: String);
  75. begin
  76.   FAppLocation := AValue;
  77. end;
  78.  
  79.  
  80. {%endregion Prpoerties}
  81.  
  82. function TPresenter.Obj: TObject;
  83. begin
  84.   Result := Self;
  85. end;
  86.  
  87.  
  88. procedure TPresenter.ReadSettings;
  89. var
  90.   lStrx : TSettingsTransaction;
  91.   SettingsFile : String;
  92. begin
  93.   SettingsFile := FAppLocation + adSettings + PathDelim + afSettingsFile;
  94.  
  95.   lStrx := TrxMan.StartTransaction(msReadSettings) as TSettingsTransaction;
  96.   try
  97.     lStrx.SettingsFile := SettingsFile;
  98.     TrxMan.CommitTransaction;
  99.   except
  100.     TrxMan.RollbackTransaction;
  101.   end; // mandatory, does NOTHING and _frees_ transaction
  102. end;
  103.  
  104. constructor TPresenter.Create;
  105. begin
  106.   inherited Create;
  107.   FProvider := CreateObsProvider;
  108.   FModel := TModel.Create(Self);  // Presenter owns the model.
  109.   FTrxMan := TTransactionManager.Create(Self, FModel);
  110. end;
  111.  
  112. destructor TPresenter.Destroy;
  113. begin
  114.   FTrxMan.Free;
  115.   FModel.Free;
  116.   FProvider.Free;
  117.   inherited Destroy;
  118. end;
  119.  
  120. procedure TPresenter.GetStaticTexts(aRegion: word);
  121. var
  122.   lstaRec: TStaticTextsAll;
  123. begin
  124.   case aRegion of
  125.     gstAll: if Assigned(fModel) then begin
  126.               lstaRec:= fModel.FetchViewTextsFull;
  127.               fProvider.NotifySubscribers(prStaticTexts,Self,@lstaRec);
  128.             end;
  129.     end;
  130. end;
  131.  
  132. procedure TPresenter.SetStatusbartext(aText: string; panel: word);
  133. var
  134.   lsbpRec : TStatusbarPanelText;
  135. begin
  136.   lsbpRec := fModel.SetStatusbartext(aText, panel);
  137.   fProvider.NotifySubscribers(prStatusBarPanelText,Self,@lsbpRec);
  138. end;
  139.  
  140. function TPresenter.GetSettingsrecord: TSettingsRec;
  141. begin
  142.   Result := FModel.SettingsRec;
  143. end;
  144.  
  145. end.
  146.  

Basically I've changed "Presenter.TrxMan" to "(I)TransactionManager" to avoid cyclic references...
With these small changes, it runs like before, but _without_ throwing away the model halfway...  :D
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

Hansvb

  • Hero Member
  • *****
  • Posts: 690
Re: Best way to exchange data between a form and a unit
« Reply #152 on: March 30, 2024, 08:57:23 pm »
Thanks, I will look at it tomorrow evening or monday. I gave it up for today.
I ran into a type mismatch i do not understand.

cdbc

  • Hero Member
  • *****
  • Posts: 1499
    • http://www.cdbc.dk
Re: Best way to exchange data between a form and a unit
« Reply #153 on: March 30, 2024, 08:59:15 pm »
Cheer up mate, learning new stuff, can be a bit hard in the beginning, but it's worth it  :)
I've done that a few times in my 40~ years of programming  ::)
Keep at it, suddenly it's all there, until then, keep asking questions  :)
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

Hansvb

  • Hero Member
  • *****
  • Posts: 690
Re: Best way to exchange data between a form and a unit
« Reply #154 on: March 31, 2024, 09:38:05 am »
Hi,

40 years of programming versus some hobbying. That certainly makes a big difference. :)

I still have to look at your code above. I was looking to find out why I get a type of mismatch and the following only now dawned on me. Why are the interface guids mentioned in vwmain.decl? I get the idea that the setup so far also works without putting those guides there. (but i must be wrong about that).

Happy Easter.

cdbc

  • Hero Member
  • *****
  • Posts: 1499
    • http://www.cdbc.dk
Re: Best way to exchange data between a form and a unit
« Reply #155 on: March 31, 2024, 03:48:03 pm »
Hi
Happy Easter to you too  :)
Well. sometimes I use aliases, to avoid having to reference /many/ units in e.g.: the trxman's uses clause, that means I can't /see/ the guids in xxx.intf,
so I define them in the xxx.decl unit, along with the rest of the consts & types.
Also I find it easier to just put 'SGUID' in front of the interface I want to get with e.g.: anObj.GetInterface(SGUIDIbcCorba,lmi) instead of '{xxxxx-xxxx}'
ex:
Code: Pascal  [Select][+][-]
  1.  
  2. ...
  3.   if anObj = nil then begin pnlInfo.Caption:= '(!) Sorry, the item does not exist in datastore!'; exit; end;
  4.   if anObj.GetInterface(SGUIDImemItem,lmi) then begin
  5. ...
  6.   end;
HaHum, I'm getting 'Crispier' NOT 'Older'  :D
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

Hansvb

  • Hero Member
  • *****
  • Posts: 690
Re: Best way to exchange data between a form and a unit
« Reply #156 on: April 01, 2024, 01:14:16 pm »
I woke up early this morning and made some adjustments.
Menu item Create new database file now also works via a transaction and the notifier transfers a text to the status bar in the main view. (sqlite3.dll is not added to the zip).

I am happy with it.

cdbc

  • Hero Member
  • *****
  • Posts: 1499
    • http://www.cdbc.dk
Re: Best way to exchange data between a form and a unit
« Reply #157 on: April 01, 2024, 02:55:12 pm »
Hi Hans
Cool  8-)
Will take a look, I'm glad that you're satisfied.
edit: Had a 'LookSee' and it's really good, Me Likey  8)
Regards Benny
« Last Edit: April 01, 2024, 07:05:38 pm by cdbc »
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

Hansvb

  • Hero Member
  • *****
  • Posts: 690
Re: Best way to exchange data between a form and a unit
« Reply #158 on: April 01, 2024, 07:24:19 pm »
Thanks  ::) 
I'm still having trouble getting my head around it. Maybe this week I'll grab a pen and paper the old-fashioned way and write it down schematically.

I forgot to enter a few lines in a table after the tables were created. I'm looking at that now. This can also be done via the transaction manager and then the model. I keep forgetting that the transaction manager is a powerful mechanism. :-[

cdbc

  • Hero Member
  • *****
  • Posts: 1499
    • http://www.cdbc.dk
Re: Best way to exchange data between a form and a unit
« Reply #159 on: April 01, 2024, 11:36:54 pm »
Hi
Hehehe, yeah, I kind of like the transactionmanager, so simple, yet so powerful...
When I sometimes get that *Head-spinning-thingy* I just go talk to the wife about her day at work, then 10 minutes later, I'm certain that I've got the easier job...  :D  :D  :D
It was her birthday today, that's why I've been a little absent  O:-)
Have fun, 'chugging along' =^
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

Hansvb

  • Hero Member
  • *****
  • Posts: 690
Re: Best way to exchange data between a form and a unit
« Reply #160 on: April 02, 2024, 09:27:45 pm »
Shouldn't you have partied yesterday instead of helping me.

cdbc

  • Hero Member
  • *****
  • Posts: 1499
    • http://www.cdbc.dk
Re: Best way to exchange data between a form and a unit
« Reply #161 on: April 03, 2024, 03:33:16 am »
Hi
Oh, but I did, all day long  :D
...But even I get to have a break, now and again  :-X
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

Hansvb

  • Hero Member
  • *****
  • Posts: 690
Re: Best way to exchange data between a form and a unit
« Reply #162 on: April 07, 2024, 08:00:57 pm »
Hi,
I have adjusted the settings unit. The record did not sit well with me and I deleted it and the settings are now saved/read via properties. That's more like how I normally did it. It's a bit more complicated now, but it works well. I can now save/read settings from the main view and from the config view. In the previous setup (zip file) I got an annoying AV error that I could not find. That is solved now.




cdbc

  • Hero Member
  • *****
  • Posts: 1499
    • http://www.cdbc.dk
Re: Best way to exchange data between a form and a unit
« Reply #163 on: April 07, 2024, 08:18:48 pm »
Hi
Nice to hear, I'm glad you got it to work, the way it should.  :)
Well, I think that, your new solution is better than the one with the record, I thought it best, for you to figure it out and you did so nicely  ;)
Good work mate  8-)
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

Hansvb

  • Hero Member
  • *****
  • Posts: 690
Re: Best way to exchange data between a form and a unit
« Reply #164 on: April 07, 2024, 08:45:43 pm »
I hardly dare say it out loud, but I'm starting to like the MVP design  :-X
Thank you for all the effort you put into it/me.

One thing still bothers me :-[, but that may come later. For now i do not seem to need it.

 

TinyPortal © 2005-2018