2020-09-17 22:50:18 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
|
|
import 'package:lemmy_api_client/lemmy_api_client.dart';
|
|
|
|
|
|
|
|
import '../hooks/delayed_loading.dart';
|
2020-09-18 15:55:23 +02:00
|
|
|
import '../hooks/logged_in_action.dart';
|
2020-09-19 01:06:35 +02:00
|
|
|
import '../util/extensions/api.dart';
|
2020-09-17 22:50:18 +02:00
|
|
|
|
2020-09-18 15:49:36 +02:00
|
|
|
// TODO: sync this button between post and fullpost. the same with voting
|
|
|
|
|
2020-09-17 22:50:18 +02:00
|
|
|
class SavePostButton extends HookWidget {
|
|
|
|
final PostView post;
|
|
|
|
|
|
|
|
SavePostButton(this.post);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final isSaved = useState(post.saved ?? false);
|
|
|
|
final savedIcon = isSaved.value ? Icons.bookmark : Icons.bookmark_border;
|
|
|
|
final loading = useDelayedLoading(Duration(milliseconds: 500));
|
2020-09-18 15:49:36 +02:00
|
|
|
final loggedInAction = useLoggedInAction(post.instanceUrl);
|
2020-09-17 22:50:18 +02:00
|
|
|
|
2020-09-18 15:49:36 +02:00
|
|
|
savePost(Jwt token) async {
|
2020-09-17 22:50:18 +02:00
|
|
|
final api = LemmyApi(post.instanceUrl).v1;
|
|
|
|
|
|
|
|
loading.start();
|
|
|
|
try {
|
|
|
|
final res = await api.savePost(
|
|
|
|
postId: post.id, save: !isSaved.value, auth: token.raw);
|
|
|
|
isSaved.value = res.saved;
|
2020-09-18 15:55:39 +02:00
|
|
|
// ignore: avoid_catches_without_on_clauses
|
2020-09-17 22:50:18 +02:00
|
|
|
} catch (e) {
|
|
|
|
Scaffold.of(context)
|
|
|
|
.showSnackBar(SnackBar(content: Text('saving failed :(')));
|
|
|
|
}
|
|
|
|
loading.cancel();
|
|
|
|
}
|
|
|
|
|
2020-09-18 01:29:17 +02:00
|
|
|
if (loading.loading) {
|
|
|
|
return Padding(
|
2020-09-18 15:49:36 +02:00
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 5),
|
2020-09-18 01:29:17 +02:00
|
|
|
child: SizedBox(
|
|
|
|
width: 30,
|
|
|
|
height: 30,
|
|
|
|
child: CircularProgressIndicator(
|
|
|
|
backgroundColor: Theme.of(context).iconTheme.color,
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
}
|
2020-09-17 22:50:18 +02:00
|
|
|
|
2020-09-18 01:29:17 +02:00
|
|
|
return IconButton(
|
2020-09-18 12:48:32 +02:00
|
|
|
tooltip: 'Save post',
|
|
|
|
icon: Icon(savedIcon),
|
2020-09-18 15:49:36 +02:00
|
|
|
onPressed: loggedInAction(loading.pending ? (_) {} : savePost),
|
2020-09-18 12:48:32 +02:00
|
|
|
);
|
2020-09-17 22:50:18 +02:00
|
|
|
}
|
|
|
|
}
|