/
reddit.cpp
172 lines (129 loc) · 4.61 KB
/
reddit.cpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <bdn/foundation.h>
#include <bdn/log.h>
#include <bdn/net.h>
#include <bdn/ui.h>
#include <nlohmann/json.hpp>
#include <bdn/AppRunnerBase.h>
#include <iostream>
using namespace bdn;
using namespace nlohmann;
class RedditPost
{
public:
RedditPost() = default;
RedditPost(String title_, String url_)
{
title = title_;
url = url_;
}
Property<String> title;
Property<String> url;
};
class RedditStore
{
public:
void fetchPosts(std::function<void()> doneHandler)
{
auto response = net::http::request(
{bdn::net::http::Method::GET, "https://www.reddit.com/hot.json?limit=100", [this, doneHandler](auto r) {
json j = json::parse(r->data);
for (auto child : j["data"]["children"]) {
auto post = std::make_shared<RedditPost>();
post->title = child["data"]["title"];
post->url = child["data"]["url"];
posts.push_back(post);
}
doneHandler();
}});
}
std::vector<std::shared_ptr<RedditPost>> posts;
};
class RedditListViewDataSource : public ListViewDataSource
{
public:
RedditListViewDataSource(std::shared_ptr<RedditStore> store) { _store = store; }
size_t numberOfRows() override { return _store->posts.size(); }
String labelTextForRowIndex(size_t rowIndex) override { return _store->posts.at(rowIndex)->title; }
private:
std::shared_ptr<RedditStore> _store;
};
class PostListViewController : public Base
{
public:
using clickNotifier_t = SimpleNotifier<String, String>;
PostListViewController() : _listView(std::make_shared<ListView>())
{
auto store = std::make_shared<RedditStore>();
_dataSource = std::make_shared<RedditListViewDataSource>(store);
_listView->dataSource = _dataSource;
store->fetchPosts([this]() { _listView->reloadData(); });
_listView->horizontalAlignment = View::HorizontalAlignment::expand;
_listView->verticalAlignment = View::VerticalAlignment::expand;
_listView->preferredSizeMinimum = Size(100, 200);
_listView->margin = UIMargin(15, 15, 15, 15);
_listView->selectedRowIndex.onChange() += [store, this](auto indexAccessor) {
auto post = store->posts.at(*indexAccessor->get());
_onClicked.notify(post->title, post->url);
};
}
std::shared_ptr<View> view() { return _listView; }
clickNotifier_t &onClicked() { return _onClicked; }
private:
std::shared_ptr<RedditListViewDataSource> _dataSource;
std::shared_ptr<ListView> _listView;
clickNotifier_t _onClicked;
};
class PostDetailController : public Base
{
public:
PostDetailController(String title, String url) : _mainColumn(std::make_shared<ColumnView>())
{
auto titleField = std::make_shared<TextView>();
auto urlField = std::make_shared<TextView>();
auto openButton = std::make_shared<Button>();
titleField->text = "Title: " + title;
urlField->text = "URL: " + url;
openButton->label = "Open in Browser";
openButton->onClick() += [url](auto) { getAppRunner()->openURL(url); };
_mainColumn->addChildView(titleField);
_mainColumn->addChildView(urlField);
_mainColumn->addChildView(openButton);
}
std::shared_ptr<View> view() { return _mainColumn; }
private:
std::shared_ptr<ColumnView> _mainColumn;
};
class MainViewController : public Base
{
public:
MainViewController() : _listViewController(std::make_shared<PostListViewController>())
{
_window = std::make_shared<Window>();
_window->title = "UI Demo";
auto stack = std::make_shared<Stack>();
stack->preferredSizeMinimum = Size(250, 0);
stack->pushView(_listViewController->view(), "Reddit");
_listViewController->onClicked() += [stack](auto title, auto url) {
auto post = std::make_shared<PostDetailController>(title, url);
stack->pushView(post->view(), "Details");
};
_window->setContentView(stack);
_window->requestAutoSize();
_window->requestCenter();
_window->visible = true;
}
protected:
std::shared_ptr<Window> _window;
std::shared_ptr<PostListViewController> _listViewController;
};
class AppController : public UIAppControllerBase
{
public:
void beginLaunch(const AppLaunchInfo &launchInfo) override
{
_mainViewController = std::make_shared<MainViewController>();
}
protected:
std::shared_ptr<MainViewController> _mainViewController;
};
BDN_APP_INIT(AppController)