From 7a3b8aaf11463ead8f13361a62d2ebad4dd81385 Mon Sep 17 00:00:00 2001 From: Marcelo Pecin Date: Sat, 10 Dec 2022 20:37:19 +0100 Subject: [PATCH] =?UTF-8?q?feat(tabmoney):=20Adicionado=20intera=C3=A7?= =?UTF-8?q?=C3=A3o=20de=20tabcoins=20para=20post=20e=20coment=C3=A1rios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/services/content.dart | 17 ++++++++++ lib/src/ui/pages/content.dart | 48 ++++++++++++++++++++++++---- lib/src/ui/widgets/item_comment.dart | 43 ++++++++++++++++++++++++- lib/src/ui/widgets/tabcoins.dart | 36 +++++++++++++++++++++ 4 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 lib/src/ui/widgets/tabcoins.dart diff --git a/lib/src/services/content.dart b/lib/src/services/content.dart index 0917d18..5c1dacc 100644 --- a/lib/src/services/content.dart +++ b/lib/src/services/content.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'package:http/http.dart' as http; +import 'package:tabnews/src/controllers/app.dart'; import 'package:tabnews/src/enviroment_vars.dart'; import 'package:tabnews/src/models/comment.dart'; @@ -118,4 +119,20 @@ class ContentService { return HttpResponse(response.statusCode, response.body); } + + Future postTabcoins(String slug, String type) async { + final response = await http.post( + Uri.parse('$apiUrl/$slug/tabcoins'), + headers: { + 'Set-Cookie': 'session_id=${AppController.auth.value}', + 'Cookie': 'session_id=${AppController.auth.value}', + 'Content-Type': 'application/json; charset=UTF-8', + }, + body: jsonEncode({ + 'transaction_type': type, + }), + ); + + return HttpResponse(response.statusCode, response.body); + } } diff --git a/lib/src/ui/pages/content.dart b/lib/src/ui/pages/content.dart index 6522850..05b8a80 100644 --- a/lib/src/ui/pages/content.dart +++ b/lib/src/ui/pages/content.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:tabnews/src/ui/widgets/tabcoins.dart'; import 'package:timeago/timeago.dart' as timeago; import 'package:tabnews/src/controllers/favorites.dart'; @@ -51,6 +52,29 @@ class _ContentPageState extends State { }); } + _tabcoins(String vote) async { + var tabcoinsResp = await _contentService.postTabcoins( + '${widget.username}/${widget.slug}', + vote == 'upvote' ? 'credit' : 'debit', + ); + + if (tabcoinsResp.ok) { + setState(() { + content.tabcoins = tabcoinsResp.data['tabcoins']; + }); + } else { + _onResponse(tabcoinsResp.message); + } + } + + void _onResponse(String message) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text(message), + ), + ); + } + @override Widget build(BuildContext context) { timeago.setLocaleMessages('pt-BR', timeago.PtBrMessages()); @@ -89,13 +113,23 @@ class _ContentPageState extends State { child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - Text( - '${content.ownerUsername} · ${timeago.format(DateTime.parse(content.publishedAt!), locale: "pt-BR")}', - style: const TextStyle().copyWith( - color: context.isDarkMode - ? Colors.grey.shade400 - : Colors.grey.shade700, - ), + Row( + children: [ + Text( + '${content.ownerUsername} · ${timeago.format(DateTime.parse(content.publishedAt!), locale: "pt-BR")}', + style: const TextStyle().copyWith( + color: context.isDarkMode + ? Colors.grey.shade400 + : Colors.grey.shade700, + ), + ), + const Spacer(), + Tabcoins( + upvote: () => _tabcoins('upvote'), + tabcoins: '${content.tabcoins}', + downvote: () => _tabcoins('downvote'), + ), + ], ), const SizedBox(height: 10.0), Text( diff --git a/lib/src/ui/widgets/item_comment.dart b/lib/src/ui/widgets/item_comment.dart index 19343cd..e4e6917 100644 --- a/lib/src/ui/widgets/item_comment.dart +++ b/lib/src/ui/widgets/item_comment.dart @@ -1,6 +1,8 @@ import 'package:flutter/material.dart'; +import 'package:tabnews/src/services/content.dart'; import 'package:tabnews/src/ui/pages/profile_user.dart'; import 'package:tabnews/src/ui/widgets/comments_children.dart'; +import 'package:tabnews/src/ui/widgets/tabcoins.dart'; import 'package:tabnews/src/utils/navigation.dart'; import 'package:timeago/timeago.dart' as timeago; @@ -8,7 +10,7 @@ import 'package:tabnews/src/extensions/dark_mode.dart'; import 'package:tabnews/src/models/comment.dart'; import 'package:tabnews/src/ui/widgets/markdown.dart'; -class ItemComment extends StatelessWidget { +class ItemComment extends StatefulWidget { final Comment comment; final ScrollController controller; @@ -18,6 +20,39 @@ class ItemComment extends StatelessWidget { required this.controller, }); + @override + State createState() => _ItemCommentState(); +} + +class _ItemCommentState extends State { + Comment get comment => widget.comment; + ScrollController get controller => widget.controller; + + final _contentService = ContentService(); + + _tabcoins(String vote) async { + var tabcoinsResp = await _contentService.postTabcoins( + '${comment.ownerUsername}/${comment.slug}', + vote == 'upvote' ? 'credit' : 'debit', + ); + + if (tabcoinsResp.ok) { + setState(() { + comment.tabcoins = tabcoinsResp.data['tabcoins']; + }); + } else { + _onResponse(tabcoinsResp.message); + } + } + + void _onResponse(String message) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text(message), + ), + ); + } + @override Widget build(BuildContext context) { timeago.setLocaleMessages('pt-BR', timeago.PtBrMessages()); @@ -45,6 +80,12 @@ class ItemComment extends StatelessWidget { : Colors.grey.shade700, ), ), + const Spacer(), + Tabcoins( + upvote: () => _tabcoins('upvote'), + tabcoins: '${comment.tabcoins}', + downvote: () => _tabcoins('downvote'), + ), ], ), MarkedownReader( diff --git a/lib/src/ui/widgets/tabcoins.dart b/lib/src/ui/widgets/tabcoins.dart new file mode 100644 index 0000000..9f6472a --- /dev/null +++ b/lib/src/ui/widgets/tabcoins.dart @@ -0,0 +1,36 @@ +import 'package:flutter/material.dart'; + +class Tabcoins extends StatefulWidget { + final String tabcoins; + final void Function() upvote; + final void Function() downvote; + + const Tabcoins({ + super.key, + required this.tabcoins, + required this.upvote, + required this.downvote, + }); + + @override + State createState() => _TabcoinsState(); +} + +class _TabcoinsState extends State { + @override + Widget build(BuildContext context) { + return Row( + children: [ + IconButton( + onPressed: widget.upvote, + icon: const Icon(Icons.expand_less), + ), + Text(widget.tabcoins), + IconButton( + onPressed: widget.downvote, + icon: const Icon(Icons.expand_more), + ), + ], + ); + } +}