💄 Optimized notification page

⬇️ Remove firebase deps
This commit is contained in:
2024-03-23 20:56:32 +08:00
parent 5da657a73c
commit 8bb9c960c1
16 changed files with 148 additions and 489 deletions

View File

@ -18,8 +18,8 @@ class AboutScreen extends StatelessWidget {
children: [
Text('SolarAgent',
style: Theme.of(context).textTheme.headlineMedium),
Text('Solarworks Official Mobile Helper',
style: Theme.of(context).textTheme.bodyLarge),
Text('Solar Networks Official Mobile Application',
style: Theme.of(context).textTheme.bodyMedium),
const SizedBox(height: 20),
FutureBuilder(
future: PackageInfo.fromPlatform(),

View File

@ -35,7 +35,7 @@ class _AccountScreenState extends State<AccountScreen> {
padding: const EdgeInsets.only(top: 20),
child: NameCard(
onLogin: () async {
await authClient.login(context);
await authClient.signin(context);
var authorized = await authClient.isAuthorized();
setState(() {
isAuthorized = authorized;
@ -58,7 +58,7 @@ class _AccountScreenState extends State<AccountScreen> {
const BorderRadius.all(Radius.circular(40)),
splashColor: Colors.indigo.withAlpha(30),
onTap: () async {
authClient.logout();
authClient.signoff();
var authorized =
await authClient.isAuthorized();
setState(() {

View File

@ -1,6 +1,8 @@
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:solaragent/auth.dart';
class NotificationScreen extends StatefulWidget {
@ -11,32 +13,32 @@ class NotificationScreen extends StatefulWidget {
}
class _NotificationScreenState extends State<NotificationScreen> {
final notificationEndpoint = Uri.parse(
'https://id.solsynth.dev/api/notifications?skip=0&take=25');
final notificationEndpoint =
Uri.parse('https://id.solsynth.dev/api/notifications?skip=0&take=25');
List<dynamic> notifications = List.empty();
@override
void initState() {
super.initState();
_pullNotifications();
pullNotifications();
}
Future<void> _pullNotifications() async {
Future<void> pullNotifications() async {
if (await authClient.isAuthorized()) {
await authClient.pullProfiles();
var profiles = await authClient.readProfiles();
setState(() {
notifications = profiles['notifications'];
});
var res = await authClient.client!.get(notificationEndpoint);
if (res.statusCode == 200) {
setState(() {
notifications = jsonDecode(utf8.decode(res.bodyBytes))["data"];
});
}
}
}
Future<void> _markAsRead(element) async {
Future<void> markAsRead(element) async {
if (authClient.client != null) {
var id = element['id'];
var uri =
Uri.parse('https://id.solsynth.dev/api/notifications/$id/read');
var uri = Uri.parse('https://id.solsynth.dev/api/notifications/$id/read');
await authClient.client!.put(uri);
}
}
@ -45,49 +47,92 @@ class _NotificationScreenState extends State<NotificationScreen> {
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.only(left: 20, right: 20, top: 30),
child: RefreshIndicator(
onRefresh: _pullNotifications,
child: CustomScrollView(
slivers: [
notifications.isEmpty
? const SliverToBoxAdapter(
child: Card(
child: Padding(
padding: EdgeInsets.all(10),
child: ListTile(
leading: Icon(Icons.check),
title: Text('You\'re done!'),
subtitle: Text(
'There are no notifications unread for you.'),
),
child: RefreshIndicator(
onRefresh: pullNotifications,
child: CustomScrollView(
slivers: [
// Title
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.only(left: 10, right: 10, top: 20),
child: ListTile(
title: Text(
'Notifications',
style: Theme.of(context).textTheme.headlineSmall,
),
),
),
),
// Content
notifications.isEmpty
? SliverToBoxAdapter(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 10),
color: Colors.grey[300],
child: const ListTile(
leading: Icon(Icons.check),
title: Text('You\'re done!'),
subtitle: Text(
'There are no notifications unread for you.',
),
),
)
: SliverList.builder(
itemCount: notifications.length,
itemBuilder: (BuildContext context, int index) {
var element = notifications[index];
return Dismissible(
key: Key('notification-$index'),
onDismissed: (direction) {
var subject = element["subject"];
_markAsRead(element).then((value) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content:
Text('$subject」 mark as read')));
});
},
child: ListTile(
title: Text(element["subject"]),
subtitle: Text(element["content"]),
));
},
),
],
),
)
: SliverList.builder(
itemCount: notifications.length,
itemBuilder: (BuildContext context, int index) {
var element = notifications[index];
return Dismissible(
key: Key('notification-$index'),
onDismissed: (direction) {
var subject = element["subject"];
markAsRead(element).then((value) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: RichText(
text: TextSpan(children: [
TextSpan(
text: subject,
style: const TextStyle(
fontWeight: FontWeight.bold),
),
const TextSpan(
text: " is marked as read",
)
]),
),
),
);
});
setState(() {
notifications.removeAt(index);
});
},
background: Container(
color: Colors.green,
),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: ListTile(
title: Text(element["subject"]),
subtitle: Text(element["content"]),
),
),
);
},
),
// Tips
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.only(top: 10),
child: Text(
"Pull to refresh, swipe to dismiss",
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.bodySmall,
),
),
),
],
),
),
),