Skip to content

Commit

Permalink
fixed page lag
Browse files Browse the repository at this point in the history
  • Loading branch information
clragon committed Sep 15, 2020
1 parent 85c1fc8 commit bc6761f
Show file tree
Hide file tree
Showing 17 changed files with 916 additions and 845 deletions.
2 changes: 1 addition & 1 deletion lib/appInfo.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const String appName = 'e1547';
const String appVersion = '1.6.0';
const String appVersion = '1.6.1';
const String about = 'An app for e621 and e926.';
const String developer = 'binaryfloof';
const String github = 'clragon/e1547';
34 changes: 19 additions & 15 deletions lib/blacklist_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,31 @@ class _BlacklistPageState extends State<BlacklistPage> {
direction: Axis.horizontal,
children: () {
Widget cardWidget(String tag) {
return InkWell(
onTap: () => wikiDialog(context, noDash(tag),
actions: true),
onLongPress: () => wikiDialog(
context, noDash(tag),
actions: true),
child: Card(
clipBehavior: Clip.antiAlias,
return Card(
child: InkWell(
onTap: () => wikiDialog(
context, noDash(tag), actions: true),
onLongPress: () => wikiDialog(
context, noDash(tag),
actions: true),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
height: 24,
width: 5,
color: () {
if ('${tag[0]}' == '-') {
return Colors.green[300];
} else {
return Colors.red[300];
}
}(),
decoration: BoxDecoration(
color: () {
if ('${tag[0]}' == '-') {
return Colors.green[300];
} else {
return Colors.red[300];
}
}(),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(5),
bottomLeft: Radius.circular(5)),
),
),
Padding(
padding: EdgeInsets.only(
Expand Down
37 changes: 20 additions & 17 deletions lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ class Client {
return false;
}

Future<List<Post>> posts(Tagset tags, int page, {bool filter = true}) async {
Future<List<Post>> posts(String tags, int page, {bool filter = true}) async {
try {
String body = await _http.get(await _host, '/posts.json', query: {
'tags': tags,
'page': page + 1,
'tags': sortTags(tags),
'page': page,
'login': await _username,
'api_key': await _apiKey,
}).then((response) => response.body);
Expand Down Expand Up @@ -249,7 +249,7 @@ class Client {
try {
String body = await _http.get(await _host, '/pools.json', query: {
'search[name_matches]': title,
'page': page + 1,
'page': page,
'login': await _username,
'api_key': await _apiKey,
}).then((response) => response.body);
Expand Down Expand Up @@ -289,8 +289,7 @@ class Client {
}

for (List<String> tag in tags) {
posts
.addAll(await client.posts(Tagset.parse('~' + tag.join(' ~')), page));
posts.addAll(await client.posts('~${tag.join(' ~')}', page));
}
return posts;
}
Expand Down Expand Up @@ -416,14 +415,18 @@ class Client {
}

Future<List> wiki(String search, int page) async {
String body = await _http.get(await _host, 'wiki_pages.json', query: {
'search[title]': search,
'page': page + 1,
'login': await _username,
'api_key': await _apiKey,
}).then((response) => response.body);
try {
String body = await _http.get(await _host, 'wiki_pages.json', query: {
'search[title]': search,
'page': page + 1,
'login': await _username,
'api_key': await _apiKey,
}).then((response) => response.body);

return json.decode(body);
return json.decode(body);
} catch (SocketException) {
return null;
}
}

Future<Map> user(String name) async {
Expand Down Expand Up @@ -454,11 +457,11 @@ class Client {
return tags;
}

Future<List<Comment>> comments(int postId, int page) async {
Future<List<Comment>> comments(int postID, int page) async {
String body = await _http.get(await _host, '/comments.json', query: {
'group_by': 'comment',
'search[post_id]': '$postId',
'page': page + 1,
'search[post_id]': '$postID',
'page': page,
'login': await _username,
'api_key': await _apiKey,
}).then((response) => response.body);
Expand All @@ -475,7 +478,7 @@ class Client {
}

Future<Map> postComment(String text, Post post, {Comment comment}) async {
Map<String, dynamic> query = {
Map<String, String> query = {
'login': await _username,
'api_key': await _apiKey,
};
Expand Down
155 changes: 63 additions & 92 deletions lib/comment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import 'package:e1547/interface.dart';
import 'package:e1547/persistence.dart';
import 'package:e1547/post.dart' show Post;
import 'package:e1547/posts_page.dart';
import 'package:e1547/tag.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class Comment {
Map raw;

int id;
int creatorID;
String creator;
String body;
int score;
Expand All @@ -21,6 +21,7 @@ class Comment {

Comment.fromRaw(this.raw) {
id = raw['id'] as int;
creatorID = raw['creator_id'] as int;
creator = raw['creator_name'] as String;
body = raw['body'] as String;
score = raw['score'] as int;
Expand All @@ -31,6 +32,7 @@ class Comment {

class CommentsWidget extends StatefulWidget {
final Post post;

CommentsWidget(this.post);

@override
Expand All @@ -39,38 +41,22 @@ class CommentsWidget extends StatefulWidget {

class _CommentsWidgetState extends State<CommentsWidget> {
bool _loading = true;
ValueNotifier<List<List<Comment>>> _pages = ValueNotifier([]);
List<Comment> get _comments {
return _pages.value
.fold<Iterable<Comment>>(Iterable.empty(), (a, b) => a.followedBy(b))
.toList();
}
CommentProvider provider;
RefreshController _refreshController =
RefreshController(initialRefresh: false);

@override
void initState() {
super.initState();
_loadNextPage();
provider = CommentProvider(postID: widget.post.id);
}

Future<void> _loadNextPage({bool reset = false}) async {
int page = reset ? 0 : _pages.value.length;
List<Comment> nextPage = [];
nextPage.addAll(await client.comments(widget.post.id, page));
if (reset && nextPage.length != 0) {
_pages.value.clear();
}
_pages.value = List.from(_pages.value..add(nextPage));
}

RefreshController _refreshController =
RefreshController(initialRefresh: false);

@override
Widget build(BuildContext context) {
_pages.addListener(() {
provider.pages.addListener(() {
if (this.mounted) {
setState(() {
if (_pages.value.length == 0) {
if (provider.pages.value.length == 0) {
_loading = true;
} else {
_loading = false;
Expand All @@ -80,64 +66,29 @@ class _CommentsWidgetState extends State<CommentsWidget> {
});

Widget body() {
return Stack(
children: <Widget>[
Visibility(
visible: _loading,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 28,
width: 28,
child: CircularProgressIndicator(),
),
Padding(
padding: EdgeInsets.all(20),
child: Text('Loading comments'),
),
],
),
),
return pageLoader(
onLoading: Text('Loading comments'),
onEmpty: Text('No comments'),
isLoading: _loading,
isEmpty: (!_loading && provider.comments.length == 0),
child: SmartRefresher(
controller: _refreshController,
header: ClassicHeader(
refreshingText: 'Refreshing...',
completeText: 'Refreshed comments!',
),
SmartRefresher(
controller: _refreshController,
header: ClassicHeader(
refreshingText: 'Refreshing...',
completeText: 'Refreshed comments!',
),
onRefresh: () async {
await _loadNextPage(reset: true);
_refreshController.refreshCompleted();
},
onRefresh: () async {
await provider.loadNextPage(reset: true);
_refreshController.refreshCompleted();
},
physics: BouncingScrollPhysics(),
child: ListView.builder(
itemBuilder: _itemBuilder,
itemCount: provider.comments.length,
padding: EdgeInsets.all(10.0),
physics: BouncingScrollPhysics(),
child: ListView.builder(
itemBuilder: _itemBuilder,
itemCount: _comments.length,
padding: EdgeInsets.all(10.0),
physics: BouncingScrollPhysics(),
),
),
Visibility(
visible: (!_loading && _comments.length == 0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.error_outline,
size: 32,
),
Padding(
padding: EdgeInsets.all(20),
child: Text('No comments'),
),
],
),
),
),
],
),
);
}

Expand Down Expand Up @@ -244,8 +195,7 @@ class _CommentsWidgetState extends State<CommentsWidget> {
Navigator.of(context).push(
MaterialPageRoute<Null>(builder: (context) {
return SearchPage(
tags: Tagset.parse(
'user:${comment.creator}'));
tags: 'user:${comment.creator}');
}));
},
),
Expand Down Expand Up @@ -315,7 +265,8 @@ class _CommentsWidgetState extends State<CommentsWidget> {
dotAll: true),
(match) => '')
.trim();
body = '[quote]"${comment.creator}":/users/${comment.creator} said:\n$body[/quote]\n';
body =
'[quote]"${comment.creator}":/users/${comment.creatorID} said:\n$body[/quote]\n';
sendComment(context, widget.post, text: body);
}
},
Expand All @@ -327,23 +278,43 @@ class _CommentsWidgetState extends State<CommentsWidget> {
}

Widget _itemBuilder(BuildContext context, int item) {
for (List<Comment> page in _pages.value) {
if (page.isEmpty) {
return null;
}

if (item == _comments.length) {
_loadNextPage();
}
if (item == provider.comments.length - 1) {
provider.loadNextPage();
}

if (item < _comments.length) {
return commentWidget(page[item]);
}
if (item < provider.comments.length) {
return commentWidget(provider.comments[item]);
}
return null;
}
}

class CommentProvider extends DataProvider<Comment> {
final int postID;
List<Comment> get comments => super.items;

CommentProvider({@required this.postID})
: super(provider: ((search, page) => client.comments(postID, page)));

/*
provider: provider ??
(search, page, {pages}) async {
String cursor;
if (pages.length == 0) {
cursor = 'a0';
} else {
cursor =
'a${pages.last.reduce((value, element) => (value.id > element.id) ? value : element).id.toString()}';
}
List<Comment> comments =
await client.comments(postID, cursor);
comments.sort((one, two) => DateTime.parse(one.creation)
.compareTo(DateTime.parse(two.creation)));
return comments;
}
*/
}

Future<bool> sendComment(BuildContext context, Post post,
{String text, Comment comment}) async {
bool sent = false;
Expand Down
Loading

0 comments on commit bc6761f

Please sign in to comment.