Skip to content

Commit 1de5a1a

Browse files
committed
use lists for comments.allow http
1 parent 2bd0df4 commit 1de5a1a

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<application
1010
android:name="io.flutter.app.FlutterApplication"
1111
android:label="HNReader"
12+
android:usesCleartextTraffic="true"
1213
android:icon="@mipmap/ic_launcher">
1314
<activity
1415
android:name=".MainActivity"

lib/comment.dart

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,37 @@ class HNComment {
1212
int id;
1313
double left;
1414
Future<Map<String, dynamic>> future;
15-
Widget card;
1615
List<HNComment> children;
1716
}
1817

19-
class CommentPage extends StatelessWidget {
20-
final HNStory story;
21-
List<Widget> cards;
22-
StreamController<Widget> _controller = StreamController<Widget>.broadcast();
18+
class CommentPage extends StatefulWidget {
19+
final HNStory _story;
2320

24-
CommentPage(this.story) {
25-
cards = new List();
21+
CommentPage(this._story);
22+
23+
CommentPageState createState() => CommentPageState(_story);
24+
}
25+
26+
class CommentPageState extends State<CommentPage> {
27+
final HNStory _story;
28+
List<Widget> _cards;
29+
30+
CommentPageState(this._story) {
31+
_cards = new List(_story.comments);
2632
getComments();
2733
}
2834

29-
Widget makeCard(String text, String by, int time, double left) {
35+
void initState() {
36+
super.initState();
37+
var loading = _makeCard("...", "...", 0, 0);
38+
for (int i = 0; i < _story.comments; i++) {
39+
_cards[i] = loading;
40+
}
41+
}
42+
43+
Widget _makeCard(String text, String by, int time, double left) {
3044
var before = DateTime.now()
31-
.difference(DateTime.fromMillisecondsSinceEpoch(story.time * 1000));
45+
.difference(DateTime.fromMillisecondsSinceEpoch(time * 1000));
3246
String timeString = "${before.inHours} hours";
3347
if (before.inHours < 1) {
3448
timeString = "${before.inMinutes} minutes";
@@ -46,16 +60,22 @@ class CommentPage extends StatelessWidget {
4660
}
4761

4862
void getComments() async {
49-
for (var comment in story.children) {
63+
int j = 0;
64+
for (var comment in _story.children) {
5065
comment.future = HNAPI.fetchItem(comment.id);
5166
ListQueue<HNComment> stack = new ListQueue();
5267
stack.addLast(comment);
5368
while (stack.isNotEmpty) {
5469
HNComment top = stack.last;
5570
stack.removeLast();
5671
var res = await top.future;
57-
_controller.add(makeCard(
58-
res["text"] ?? "deleted", res["by"] ?? "?", res['time'], top.left));
72+
setState(() {
73+
if (j < _story.comments) {
74+
_cards[j] = _makeCard(res["text"] ?? "deleted", res["by"] ?? "?",
75+
res['time'], top.left);
76+
j++;
77+
}
78+
});
5979
if (res['kids'] != null) {
6080
top.children = new List(res["kids"].length);
6181
for (int i = 0; i < res["kids"].length; i++) {
@@ -74,7 +94,7 @@ class CommentPage extends StatelessWidget {
7494
length: 2,
7595
child: Scaffold(
7696
appBar: AppBar(
77-
title: Text("${story.headline}"),
97+
title: Text("${_story.headline}"),
7898
backgroundColor: Color(0xffff6600),
7999
bottom: TabBar(tabs: [
80100
Tab(text: "Website"),
@@ -83,30 +103,14 @@ class CommentPage extends StatelessWidget {
83103
),
84104
body: Center(
85105
child: TabBarView(children: [
106+
WebViewScreen(_story.url),
86107
Container(
87108
decoration: BoxDecoration(color: Colors.grey[200]),
88-
child: StreamBuilder(
89-
stream: _controller.stream,
90-
builder: (context, snapshot) {
91-
if (snapshot.hasData) {
92-
cards.add(snapshot.data);
93-
return ListView.builder(
94-
itemBuilder: (context, index) {
95-
return cards[index];
96-
},
97-
itemCount: cards.length);
98-
} else if (snapshot.connectionState ==
99-
ConnectionState.done ||
100-
snapshot.connectionState == ConnectionState.waiting) {
101-
return ListView.builder(
102-
itemBuilder: (context, index) {
103-
return cards[index];
104-
},
105-
itemCount: cards.length);
106-
}
107-
return Text("...");
108-
})),
109-
WebViewScreen(story.url),
109+
child: ListView.builder(
110+
itemBuilder: (context, index) {
111+
return _cards[index];
112+
},
113+
itemCount: _cards.length)),
110114
])),
111115
));
112116
}

0 commit comments

Comments
 (0)