From fe318cd271688d663c1a3e9ad8b1e188242670ba Mon Sep 17 00:00:00 2001 From: Felipe Bueno Date: Tue, 11 Jun 2024 21:18:31 -0300 Subject: [PATCH] minor --- lib/views/pages/home_page.dart | 95 +------------------ .../widgets/maybe_notifications_button.dart | 95 +++++++++++++++++++ 2 files changed, 96 insertions(+), 94 deletions(-) create mode 100644 lib/views/widgets/maybe_notifications_button.dart diff --git a/lib/views/pages/home_page.dart b/lib/views/pages/home_page.dart index b425c86..71a5b94 100644 --- a/lib/views/pages/home_page.dart +++ b/lib/views/pages/home_page.dart @@ -1,17 +1,9 @@ -import 'dart:async'; - import 'package:flutter/material.dart'; import 'package:stacker_news/data/models/post_type.dart'; -import 'package:stacker_news/data/models/session.dart'; -import 'package:stacker_news/data/sn_api_client.dart'; -import 'package:stacker_news/main.dart'; -import 'package:stacker_news/utils.dart'; -import 'package:stacker_news/views/pages/auth/sign_in_page.dart'; -import 'package:stacker_news/views/pages/notifications/notifications_page.dart'; -import 'package:stacker_news/views/pages/profile/profile_page.dart'; import 'package:stacker_news/views/widgets/base_tab.dart'; import 'package:stacker_news/views/widgets/generic_page_scaffold.dart'; import 'package:stacker_news/views/widgets/maybe_new_post_button.dart'; +import 'package:stacker_news/views/widgets/maybe_notifications_button.dart'; import 'package:stacker_news/views/widgets/sn_logo.dart'; class HomePage extends StatelessWidget { @@ -56,88 +48,3 @@ class HomePage extends StatelessWidget { ); } } - -class MaybeNotificationsButton extends StatefulWidget { - const MaybeNotificationsButton({super.key}); - - @override - State createState() => - _MaybeNotificationsButtonState(); -} - -class _MaybeNotificationsButtonState extends State { - bool _hasNewNotes = false; - Timer? _timer; - - void _startFetchingNewNotes() { - _timer?.cancel(); - - _timer = Timer.periodic(const Duration(seconds: 30), (timer) async { - final ret = await locator().hasNewNotes(); - - if (mounted) { - setState(() { - _hasNewNotes = ret; - }); - } - }); - } - - @override - void dispose() { - _timer?.cancel(); - - super.dispose(); - } - - @override - Widget build(BuildContext context) { - return FutureBuilder( - future: Utils.getSession(), - builder: (context, snapshot) { - if (snapshot.hasData && snapshot.data is Session) { - _startFetchingNewNotes(); - - final session = snapshot.data as Session; - - return Row( - children: [ - IconButton( - onPressed: () { - Navigator.pushNamed( - context, - NotificationsPage.id, - arguments: session.user?.name, - ); - }, - icon: Icon( - _hasNewNotes - ? Icons.notifications_active - : Icons.notifications, - color: _hasNewNotes ? Colors.green : null, - ), - ), - TextButton( - child: Text('@${session.user?.name}'), - onPressed: () async { - Navigator.pushNamed( - context, - ProfilePage.id, - arguments: session.user?.name, - ); - }, - ), - ], - ); - } - - return IconButton( - icon: const Icon(Icons.login), - onPressed: () async { - Navigator.pushNamed(context, SignInPage.id); - }, - ); - }, - ); - } -} diff --git a/lib/views/widgets/maybe_notifications_button.dart b/lib/views/widgets/maybe_notifications_button.dart new file mode 100644 index 0000000..ecb34a4 --- /dev/null +++ b/lib/views/widgets/maybe_notifications_button.dart @@ -0,0 +1,95 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:stacker_news/data/models/session.dart'; +import 'package:stacker_news/data/sn_api_client.dart'; +import 'package:stacker_news/main.dart'; +import 'package:stacker_news/utils.dart'; +import 'package:stacker_news/views/pages/auth/sign_in_page.dart'; +import 'package:stacker_news/views/pages/notifications/notifications_page.dart'; +import 'package:stacker_news/views/pages/profile/profile_page.dart'; + +class MaybeNotificationsButton extends StatefulWidget { + const MaybeNotificationsButton({super.key}); + + @override + State createState() => + _MaybeNotificationsButtonState(); +} + +class _MaybeNotificationsButtonState extends State { + bool _hasNewNotes = false; + Timer? _timer; + + void _startFetchingNewNotes() { + _timer?.cancel(); + + _timer = Timer.periodic(const Duration(seconds: 30), (timer) async { + final ret = await locator().hasNewNotes(); + + if (mounted) { + setState(() { + _hasNewNotes = ret; + }); + } + }); + } + + @override + void dispose() { + _timer?.cancel(); + + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return FutureBuilder( + future: Utils.getSession(), + builder: (context, snapshot) { + if (snapshot.hasData && snapshot.data is Session) { + _startFetchingNewNotes(); + + final session = snapshot.data as Session; + + return Row( + children: [ + IconButton( + onPressed: () { + Navigator.pushNamed( + context, + NotificationsPage.id, + arguments: session.user?.name, + ); + }, + icon: Icon( + _hasNewNotes + ? Icons.notifications_active + : Icons.notifications, + color: _hasNewNotes ? Colors.green : null, + ), + ), + TextButton( + child: Text('@${session.user?.name}'), + onPressed: () async { + Navigator.pushNamed( + context, + ProfilePage.id, + arguments: session.user?.name, + ); + }, + ), + ], + ); + } + + return IconButton( + icon: const Icon(Icons.login), + onPressed: () async { + Navigator.pushNamed(context, SignInPage.id); + }, + ); + }, + ); + } +}