Menu

[r2742]: / trunk / Src / Favourites.UManager.pas  Maximize  Restore  History

Download this file

125 lines (107 with data), 3.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Defines a class used to manage interaction with and updating of the list of
* user favourites.
}
unit Favourites.UManager;
interface
uses
Favourites.UFavourites, USnippetIDs, IntfNotifier, UView;
type
TFavouritesManager = class(TObject)
strict private
var
fFavourites: TFavourites;
fChanging: Boolean;
fChangingSnippetID: TSnippetID;
fNotifier: INotifier;
/// <summary>Handles database change events by updating the favorites list
/// necessary.</summary>
/// <param name="Sender">TObject [in] Object that triggered event. Not
/// used.</param>
/// <param name="EvtInfo">IInterface [in] Object that carries information
/// about the database change event.</param>
procedure DBChangeEventHandler(Sender: TObject; const EvtInfo: IInterface);
public
constructor Create(Notifier: INotifier);
destructor Destroy; override;
procedure ShowDialogue;
function CanAddFavourite(View: IView): Boolean;
procedure AddFavourite(View: IView);
end;
implementation
uses
SysUtils,
DB.UMain, DB.USnippet, Favourites.UPersist, FmFavouritesDlg;
{ TFavouritesManager }
procedure TFavouritesManager.AddFavourite(View: IView);
begin
Assert(CanAddFavourite(View),
ClassName + '.AddFavourite: invalid view or already favourite');
fFavourites.Add((View as ISnippetView).Snippet.ID, Now);
end;
function TFavouritesManager.CanAddFavourite(View: IView): Boolean;
var
SnippetView: ISnippetView;
begin
Result := Supports(View, ISnippetView, SnippetView)
and not fFavourites.IsFavourite(SnippetView.Snippet.ID);
end;
constructor TFavouritesManager.Create(Notifier: INotifier);
begin
inherited Create;
fFavourites := TFavourites.Create;
fNotifier := Notifier;
TFavouritesPersist.Load(fFavourites);
Database.AddChangeEventHandler(DBChangeEventHandler);
end;
procedure TFavouritesManager.DBChangeEventHandler(Sender: TObject;
const EvtInfo: IInterface);
var
EventInfo: IDatabaseChangeEventInfo; // information about the event
function EvtInfoToSnippetID: TSnippetID;
begin
Result := (EventInfo.Info as TSnippet).ID;
end;
begin
EventInfo := EvtInfo as IDatabaseChangeEventInfo;
case EventInfo.Kind of
evBeforeSnippetDelete:
begin
if fFavourites.IsFavourite(EvtInfoToSnippetID) then
fFavourites.Remove(EvtInfoToSnippetID);
end;
evBeforeSnippetChange:
begin
fChangingSnippetID := EvtInfoToSnippetID;
fChanging := fFavourites.IsFavourite(EvtInfoToSnippetID);
end;
evSnippetChanged:
begin
// update snippet id if original was in favourites list and has changed
if fChanging and (fChangingSnippetID <> EvtInfoToSnippetID) then
fFavourites.Replace(fChangingSnippetID, EvtInfoToSnippetID);
fChanging := False;
end;
end;
end;
destructor TFavouritesManager.Destroy;
begin
TFavouritesDlg.Close;
TFavouritesPersist.Save(fFavourites);
fFavourites.Free;
inherited;
end;
procedure TFavouritesManager.ShowDialogue;
begin
TFavouritesDlg.Display(nil, fFavourites, fNotifier);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.