main
 1using System;
 2using gorilla.commons.utility;
 3using MoMoney.Presentation.Core;
 4using momoney.presentation.model.menu.file;
 5using MoMoney.Presentation.Model.Projects;
 6using momoney.presentation.views;
 7
 8namespace MoMoney.Presentation.Model.Menu.File
 9{
10    public interface ISaveChangesCommand : ParameterizedCommand<ISaveChangesCallback> {}
11
12    public interface ISaveChangesPresenter : IPresenter
13    {
14        void save();
15        void dont_save();
16        void cancel();
17    }
18
19    public interface ISaveChangesCallback
20    {
21        void saved();
22        void not_saved();
23        void cancelled();
24    }
25
26    public class SaveChangesCommand : ISaveChangesCommand, ISaveChangesPresenter
27    {
28        readonly IProjectController current_project;
29        readonly ISaveChangesView view;
30        readonly ISaveAsCommand save_as_command;
31        ISaveChangesCallback callback;
32
33        public SaveChangesCommand(IProjectController current_project, ISaveChangesView view, ISaveAsCommand save_as_command)
34        {
35            this.current_project = current_project;
36            this.save_as_command = save_as_command;
37            this.view = view;
38        }
39
40        public void run()
41        {
42            throw new NotImplementedException();
43        }
44
45        public void run(ISaveChangesCallback item)
46        {
47            callback = item;
48            if (current_project.has_unsaved_changes())
49            {
50                view.attach_to(this);
51                view.prompt_user_to_save();
52            }
53            else
54            {
55                item.not_saved();
56            }
57        }
58
59        public void save()
60        {
61            if (current_project.has_been_saved_at_least_once())
62            {
63                current_project.save_changes();
64            }
65            else
66            {
67                save_as_command.run();
68            }
69            callback.saved();
70        }
71
72        public void dont_save()
73        {
74            callback.not_saved();
75        }
76
77        public void cancel()
78        {
79            callback.cancelled();
80        }
81    }
82}