Skip to content

Commit c41c80f

Browse files
committed
Fix mvvm
1 parent 3973eed commit c41c80f

11 files changed

Lines changed: 41 additions & 33 deletions

File tree

src/ap/mvc/model/SharedData.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ void SharedData::setData(const std::string& data) {
99
}
1010

1111
void SharedData::notifyObservers() {
12-
for (auto *o : observers_) {
12+
for (auto* o : observers_) {
1313
o->onDataChanged(this->data_);
1414
}
1515
}

src/ap/mvc/mvc_ap.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "view/DisplayWidget.h"
66
#include "view/EditorWidget.h"
77

8+
namespace mvc {
89
class ContainerWindow : public Gtk::Window {
910
public:
1011
ContainerWindow();
@@ -67,8 +68,9 @@ ContainerWindow::ContainerWindow()
6768
mainLayout_.append(*editorView_); // Add bottom row
6869
set_child(mainLayout_);
6970
}
71+
} // namespace mvc
7072

7173
int main(int argc, char* argv[]) {
7274
auto app = Gtk::Application::create("org.gtkmm.example.singlemvc");
73-
return app->make_window_and_run<ContainerWindow>(argc, argv);
75+
return app->make_window_and_run<mvc::ContainerWindow>(argc, argv);
7476
}

src/ap/mvvm/model/SharedData.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
#include "SharedData.h"
22

3-
SharedData::SharedData() : data_{"Initial Data"} {}
3+
mvvm::SharedData::SharedData() : data_{"Initial Data"} {}
44

5-
void SharedData::setData(const std::string& data) {
5+
void mvvm::SharedData::setData(const std::string& data) {
66
this->data_ = data;
77
notifyObservers();
88
}
99

10-
void SharedData::notifyObservers() {
10+
void mvvm::SharedData::notifyObservers() {
1111
for (auto* o : observers_) {
1212
o->onDataChanged(this->data_);
1313
}
1414
}
15-
void SharedData::addObserver(IObserver* obs) {
15+
void mvvm::SharedData::addObserver(IObserver* obs) {
1616
if (obs != nullptr)
1717
observers_.push_back(obs);
1818
}
1919

20-
std::string SharedData::getData() const {
20+
std::string mvvm::SharedData::getData() const {
2121
return data_;
2222
}

src/ap/mvvm/model/SharedData.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <vector>
44
#include "../IObserver.h"
55

6+
namespace mvvm {
67
class SharedData {
78
public:
89
SharedData();
@@ -17,4 +18,5 @@ class SharedData {
1718

1819
std::string data_;
1920
std::vector<IObserver*> observers_;
20-
};
21+
};
22+
} // namespace mvvm

src/ap/mvvm/mvvm_ap.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
#include "view/EditorWidget.h"
66
#include "viewmodel/SharedDataVM.h"
77

8+
namespace mvvm {
89
/**
910
* @brief ContainerWindow wires up the MVVM triad:
1011
* Key differences from MVC's ContainerWindow:
1112
* 1. No Controller is created.
1213
* 2. No manual addObserver() calls, each View self-registers with the ViewModel during construction.
13-
* 3. Views receive a shared_ptr<SharedDataViewModel>, never a Model pointer.
14+
* 3. Views receive a shared_ptr<SharedDataVM>, never a Model pointer.
1415
*/
1516
class ContainerWindow : public Gtk::Window {
1617
public:
@@ -35,7 +36,7 @@ class ContainerWindow : public Gtk::Window {
3536
ContainerWindow::ContainerWindow()
3637
: mainLayout_(Gtk::Orientation::VERTICAL),
3738
topRowLayout_(Gtk::Orientation::HORIZONTAL) {
38-
set_title("MVC Integrated Demo");
39+
set_title("MVVM Integrated Demo");
3940
set_default_size(600, 400);
4041

4142
// Step 1 – construct the Model.
@@ -51,10 +52,6 @@ ContainerWindow::ContainerWindow()
5152
displayViewRight_ = std::make_unique<DisplayWidget>("ZONE 3: MONITOR B (Red)",
5253
"red", viewModel_);
5354

54-
dataModel_->addObserver(editorView_.get());
55-
dataModel_->addObserver(displayViewLeft_.get());
56-
dataModel_->addObserver(displayViewRight_.get());
57-
5855
// Layout, unchanged from MVC
5956
displayViewLeft_->set_hexpand(true);
6057
displayViewRight_->set_hexpand(true);
@@ -66,8 +63,9 @@ ContainerWindow::ContainerWindow()
6663
mainLayout_.append(*editorView_);
6764
set_child(mainLayout_);
6865
}
66+
} // namespace mvvm
6967

7068
int main(int argc, char* argv[]) {
7169
auto app = Gtk::Application::create("org.gtkmm.example.singlemvvm");
72-
return app->make_window_and_run<ContainerWindow>(argc, argv);
70+
return app->make_window_and_run<mvvm::ContainerWindow>(argc, argv);
7371
}

src/ap/mvvm/view/DisplayWidget.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "DisplayWidget.h"
22

3-
DisplayWidget::DisplayWidget(const std::string& title, const std::string& color,
4-
std::shared_ptr<SharedDataVM> vm)
3+
mvvm::DisplayWidget::DisplayWidget(const std::string& title, std::string color,
4+
std::shared_ptr<SharedDataVM> vm)
55
: Gtk::Box(Gtk::Orientation::VERTICAL),
6-
color_(color),
6+
color_(std::move(color)),
77
innerBox_(Gtk::Orientation::VERTICAL),
88
view_model_(std::move(vm)) {
99
frame_.set_label(title);
@@ -22,12 +22,12 @@ DisplayWidget::DisplayWidget(const std::string& title, const std::string& color,
2222
view_model_->addObserver(this);
2323
}
2424

25-
void DisplayWidget::updateLabel(const std::string& text) {
25+
void mvvm::DisplayWidget::updateLabel(const std::string& text) {
2626
std::string markup = "<span foreground='" + color_ +
2727
"' size='x-large' weight='bold'>" + text + "</span>";
2828
labelData_.set_markup(markup);
2929
}
3030

31-
void DisplayWidget::onDataChanged(const std::string& newData) {
31+
void mvvm::DisplayWidget::onDataChanged(const std::string& newData) {
3232
updateLabel(newData);
3333
}

src/ap/mvvm/view/DisplayWidget.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
#include "../IObserver.h"
44
#include "../viewmodel/SharedDataVM.h"
55

6+
namespace mvvm {
67
class DisplayWidget : public Gtk::Box, public IObserver {
78
public:
8-
DisplayWidget(const std::string& title, const std::string& color,
9+
DisplayWidget(const std::string& title, std::string color,
910
std::shared_ptr<SharedDataVM> vm);
1011

1112
void onDataChanged(const std::string& newData) override;
@@ -19,4 +20,5 @@ class DisplayWidget : public Gtk::Box, public IObserver {
1920
Gtk::Label labelData_;
2021

2122
std::shared_ptr<SharedDataVM> view_model_;
22-
};
23+
};
24+
} // namespace mvvm

src/ap/mvvm/view/EditorWidget.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <utility>
44

5-
EditorWidget::EditorWidget(std::shared_ptr<SharedDataVM> vm)
5+
mvvm::EditorWidget::EditorWidget(std::shared_ptr<SharedDataVM> vm)
66
: Gtk::Box(Gtk::Orientation::VERTICAL),
77
innerBox_(Gtk::Orientation::VERTICAL),
88
view_model_(std::move(vm)) {
@@ -30,7 +30,7 @@ EditorWidget::EditorWidget(std::shared_ptr<SharedDataVM> vm)
3030
view_model_->addObserver(this);
3131
}
3232

33-
void EditorWidget::onDataChanged(const std::string& newData) {
33+
void mvvm::EditorWidget::onDataChanged(const std::string& newData) {
3434
if (entry_.get_text() != Glib::ustring(newData)) {
3535
entry_.set_text(newData);
3636
}

src/ap/mvvm/view/EditorWidget.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "../IObserver.h"
55
#include "../viewmodel/SharedDataVM.h"
66

7+
namespace mvvm {
78
class EditorWidget : public Gtk::Box, public IObserver {
89
public:
910
explicit EditorWidget(std::shared_ptr<SharedDataVM> vm);
@@ -18,4 +19,5 @@ class EditorWidget : public Gtk::Box, public IObserver {
1819
Gtk::Button button_;
1920

2021
std::shared_ptr<SharedDataVM> view_model_;
21-
};
22+
};
23+
} // namespace mvvm

src/ap/mvvm/viewmodel/SharedDataVM.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
#include "SharedDataVM.h"
22

3-
SharedDataVM::SharedDataVM(std::shared_ptr<SharedData> model)
3+
mvvm::SharedDataVM::SharedDataVM(std::shared_ptr<SharedData> model)
44
: model_(std::move(model)) {
55
model_->addObserver(this);
66
}
77

8-
void SharedDataVM::submitText(const std::string& text) {
8+
void mvvm::SharedDataVM::submitText(const std::string& text) {
99
if (text.empty())
1010
return;
1111

1212
model_->setData(text);
1313
}
1414

15-
std::string SharedDataVM::getCurrentText() const {
15+
std::string mvvm::SharedDataVM::getCurrentText() const {
1616
return model_->getData();
1717
}
1818

19-
void SharedDataVM::addObserver(IObserver* obs) {
19+
void mvvm::SharedDataVM::addObserver(IObserver* obs) {
2020
if (obs != nullptr)
2121
view_observers_.push_back(obs);
2222
}
2323

24-
void SharedDataVM::onDataChanged(const std::string& newData) {
24+
void mvvm::SharedDataVM::onDataChanged(const std::string& newData) {
2525
notifyObservers(newData);
2626
}
2727

28-
void SharedDataVM::notifyObservers(const std::string& data) {
28+
void mvvm::SharedDataVM::notifyObservers(const std::string& data) {
2929
for (auto* obs : view_observers_) {
3030
obs->onDataChanged(data);
3131
}

0 commit comments

Comments
 (0)