Skip to content

Commit

Permalink
Merge pull request #40 from avuenja/31-adicionar-interação-com-tabcash
Browse files Browse the repository at this point in the history
31 adicionar interação com tabcash
  • Loading branch information
avuenja committed Dec 10, 2022
2 parents 8a29578 + 7a3b8aa commit 1e74d59
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 16 deletions.
10 changes: 10 additions & 0 deletions lib/src/controllers/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import 'package:flutter/material.dart';
import 'package:tabnews/src/constants.dart';
import 'package:tabnews/src/models/user.dart';
import 'package:tabnews/src/preferences.dart';
import 'package:tabnews/src/services/auth.dart';

class AppController {
static final AppController _singleton = AppController._internal();

static final AuthService _authService = AuthService();

factory AppController() {
return _singleton;
}
Expand Down Expand Up @@ -39,4 +42,11 @@ class AppController {

return User.fromJson({});
}

static void updateUser() async {
var userRefresh = await _authService.fetchUser(auth.value);

Preferences.setString(_userKey, jsonEncode(userRefresh.data));
user.value = _getLoggedUser();
}
}
17 changes: 17 additions & 0 deletions lib/src/services/content.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -118,4 +119,20 @@ class ContentService {

return HttpResponse<Content>(response.statusCode, response.body);
}

Future<HttpResponse> 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);
}
}
22 changes: 21 additions & 1 deletion lib/src/ui/layouts/tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TabLayout extends StatefulWidget {
State<TabLayout> createState() => _TabLayoutState();
}

class _TabLayoutState extends State<TabLayout> {
class _TabLayoutState extends State<TabLayout> with WidgetsBindingObserver {
List<ScrollController> pagesScrollController = [
ScrollController(),
ScrollController(),
Expand Down Expand Up @@ -51,6 +51,26 @@ class _TabLayoutState extends State<TabLayout> {
});
}

@override
void initState() {
WidgetsBinding.instance.addObserver(this);
super.initState();
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);

super.dispose();
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (AppController.isLoggedIn.value && state == AppLifecycleState.resumed) {
AppController.updateUser();
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down
48 changes: 41 additions & 7 deletions lib/src/ui/pages/content.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -51,6 +52,29 @@ class _ContentPageState extends State<ContentPage> {
});
}

_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());
Expand Down Expand Up @@ -89,13 +113,23 @@ class _ContentPageState extends State<ContentPage> {
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(
Expand Down
41 changes: 34 additions & 7 deletions lib/src/ui/pages/profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,31 @@ class _ProfilePageState extends State<ProfilePage> implements ViewAction {
ValueListenableBuilder(
valueListenable: AppController.user,
builder: (context, user, child) {
return Text(
'${user.username}',
style: const TextStyle().copyWith(
fontSize: 28.0,
fontWeight: FontWeight.w700,
),
return Column(
children: [
Text(
'${user.username}',
style: const TextStyle().copyWith(
fontSize: 28.0,
fontWeight: FontWeight.w700,
),
),
const SizedBox(height: 15.0),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_buildColorContainer(Colors.blue),
Text('${user.tabcoins ?? 0}'),
const SizedBox(width: 10.0),
_buildColorContainer(Colors.green),
Text('${user.tabcash ?? 0}'),
],
),
],
);
},
),
const SizedBox(height: 80.0),
const SizedBox(height: 60.0),
ListTile(
onTap: () => Navigation.push(context, const MyContentsPage()),
title: const Text('Meu conteúdo'),
Expand Down Expand Up @@ -87,4 +102,16 @@ class _ProfilePageState extends State<ProfilePage> implements ViewAction {
),
);
}

Widget _buildColorContainer(Color color) {
return Container(
margin: const EdgeInsets.all(5.0),
decoration: BoxDecoration(
color: color,
borderRadius: BorderRadius.circular(2.5),
),
width: 12.0,
height: 12.0,
);
}
}
43 changes: 42 additions & 1 deletion lib/src/ui/widgets/item_comment.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
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;

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;

Expand All @@ -18,6 +20,39 @@ class ItemComment extends StatelessWidget {
required this.controller,
});

@override
State<ItemComment> createState() => _ItemCommentState();
}

class _ItemCommentState extends State<ItemComment> {
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());
Expand Down Expand Up @@ -45,6 +80,12 @@ class ItemComment extends StatelessWidget {
: Colors.grey.shade700,
),
),
const Spacer(),
Tabcoins(
upvote: () => _tabcoins('upvote'),
tabcoins: '${comment.tabcoins}',
downvote: () => _tabcoins('downvote'),
),
],
),
MarkedownReader(
Expand Down
36 changes: 36 additions & 0 deletions lib/src/ui/widgets/tabcoins.dart
Original file line number Diff line number Diff line change
@@ -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<Tabcoins> createState() => _TabcoinsState();
}

class _TabcoinsState extends State<Tabcoins> {
@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),
),
],
);
}
}

0 comments on commit 1e74d59

Please sign in to comment.