git-touch-android-ios-app/lib/widgets/notification_item.dart

145 lines
4.0 KiB
Dart
Raw Normal View History

2022-09-24 20:46:37 +02:00
import 'package:antd_mobile/antd_mobile.dart';
2022-09-17 14:35:45 +02:00
import 'package:flutter/widgets.dart';
import 'package:git_touch/models/auth.dart';
2019-12-26 11:00:36 +01:00
import 'package:git_touch/models/github.dart';
import 'package:git_touch/utils/utils.dart';
2022-09-17 14:35:45 +02:00
import 'package:git_touch/widgets/issue_icon.dart';
2019-12-26 11:00:36 +01:00
import 'package:git_touch/widgets/link.dart';
2022-09-17 14:35:45 +02:00
import 'package:provider/provider.dart';
2019-01-30 07:46:18 +01:00
2019-02-06 06:06:11 +01:00
class NotificationItem extends StatefulWidget {
2022-09-06 18:28:12 +02:00
const NotificationItem({
2022-09-25 19:59:30 +02:00
super.key,
2021-05-16 09:16:35 +02:00
required this.payload,
required this.markAsRead,
2022-09-25 19:59:30 +02:00
});
2022-09-21 18:28:21 +02:00
final GithubNotificationItem payload;
final Function markAsRead;
2019-01-30 07:46:18 +01:00
2019-02-06 06:06:11 +01:00
@override
2022-10-03 19:05:29 +02:00
State<NotificationItem> createState() => _NotificationItemState();
2019-02-06 06:06:11 +01:00
}
class _NotificationItemState extends State<NotificationItem> {
2019-12-26 11:00:36 +01:00
GithubNotificationItem get payload => widget.payload;
2019-02-06 06:06:11 +01:00
bool loading = false;
2019-01-30 07:46:18 +01:00
2019-02-06 12:14:11 +01:00
Widget _buildIcon(IconData data, [Color color = Colors.black54]) {
return Icon(data, color: color, size: 20);
}
Widget _buildIconData() {
2021-05-16 09:16:35 +02:00
switch (payload.subject!.type) {
2019-01-30 07:46:18 +01:00
case 'Issue':
2019-02-06 12:14:11 +01:00
switch (payload.state) {
case 'OPEN':
2022-09-06 18:28:12 +02:00
return const IssueIcon(IssueIconState.open, size: 20);
2019-02-06 12:14:11 +01:00
case 'CLOSED':
2022-09-06 18:28:12 +02:00
return const IssueIcon(IssueIconState.closed, size: 20);
2019-02-06 12:14:11 +01:00
default:
return _buildIcon(Octicons.person);
}
2019-01-30 07:46:18 +01:00
case 'PullRequest':
2019-02-06 12:14:11 +01:00
switch (payload.state) {
case 'OPEN':
2022-09-06 18:28:12 +02:00
return const IssueIcon(IssueIconState.prOpen, size: 20);
2019-02-06 12:14:11 +01:00
case 'CLOSED':
2022-09-06 18:28:12 +02:00
return const IssueIcon(IssueIconState.prClosed, size: 20);
2019-02-06 12:14:11 +01:00
case 'MERGED':
2022-09-06 18:28:12 +02:00
return const IssueIcon(IssueIconState.prMerged, size: 20);
2019-02-06 12:14:11 +01:00
default:
return _buildIcon(Octicons.person);
}
2019-01-30 07:46:18 +01:00
// color: Color.fromRGBO(0x6f, 0x42, 0xc1, 1),
2019-02-06 12:14:11 +01:00
case 'Release':
return _buildIcon(Octicons.tag);
case 'Commit':
return _buildIcon(Octicons.git_commit);
2019-12-26 11:06:10 +01:00
case 'CheckSuite':
return _buildIcon(Octicons.x, GithubPalette.closed);
2019-01-30 07:46:18 +01:00
default:
2022-09-17 16:35:45 +02:00
return _buildIcon(Octicons.bell);
2019-01-30 07:46:18 +01:00
}
}
2019-02-06 06:06:11 +01:00
Widget _buildCheckIcon() {
return Icon(
2022-09-17 16:35:45 +02:00
payload.unread! ? Ionicons.checkmark : Octicons.dot_fill,
2022-09-24 20:46:37 +02:00
color: loading
? AntTheme.of(context).colorBox
: AntTheme.of(context).colorWeak,
2019-02-10 12:45:08 +01:00
size: 24,
2019-02-06 06:06:11 +01:00
);
}
2019-02-06 14:35:52 +01:00
void _markAsRead() async {
2021-05-16 09:16:35 +02:00
if (payload.unread! && !loading) {
2019-02-06 14:35:52 +01:00
setState(() {
loading = true;
});
try {
2020-10-04 14:37:23 +02:00
await context
.read<AuthModel>()
2021-06-14 08:56:42 +02:00
.ghClient
2020-02-07 15:05:07 +01:00
.activity
2021-05-16 09:16:35 +02:00
.markThreadRead(payload.id!);
2019-02-06 14:35:52 +01:00
widget.markAsRead();
} finally {
if (mounted) {
setState(() {
loading = false;
});
}
}
}
}
2021-05-16 09:16:35 +02:00
String? get _url {
final fullName = payload.repository!.fullName;
switch (payload.subject!.type) {
2019-12-26 07:10:52 +01:00
case 'Issue':
2021-05-16 09:16:35 +02:00
return '/github/$fullName/issues/${payload.subject!.number}';
2019-12-26 07:10:52 +01:00
case 'PullRequest':
2021-05-16 09:16:35 +02:00
return '/github/$fullName/pull/${payload.subject!.number}';
2019-12-26 07:10:52 +01:00
case 'Release':
2019-12-27 07:08:37 +01:00
return 'https://github.com/$fullName/releases';
2019-12-26 07:10:52 +01:00
case 'Commit':
2019-12-27 07:08:37 +01:00
case 'CheckSuite':
return '/github/$fullName';
2019-12-26 07:10:52 +01:00
default:
return null;
}
}
2019-01-30 07:46:18 +01:00
@override
Widget build(BuildContext context) {
2021-05-16 09:16:35 +02:00
return LinkWidget(
2019-12-26 07:10:52 +01:00
url: _url,
onTap: _markAsRead,
2019-02-06 06:06:11 +01:00
child: Opacity(
2021-05-16 09:16:35 +02:00
opacity: payload.unread! ? 1 : 0.5,
2019-02-06 06:06:11 +01:00
child: Container(
2022-09-06 18:28:12 +02:00
padding: const EdgeInsets.all(8),
2019-02-06 06:06:11 +01:00
child: Row(
children: <Widget>[
Container(
2022-09-06 18:28:12 +02:00
padding: const EdgeInsets.only(right: 8),
2019-02-06 12:14:11 +01:00
child: _buildIconData(),
2019-02-06 06:06:11 +01:00
),
Expanded(
child: Text(
2021-05-16 09:16:35 +02:00
payload.subject!.title!,
2019-02-06 06:06:11 +01:00
overflow: TextOverflow.ellipsis,
2022-09-24 20:46:37 +02:00
style: TextStyle(
fontSize: 15, color: AntTheme.of(context).colorText),
),
2019-02-06 06:06:11 +01:00
),
2022-09-06 18:28:12 +02:00
LinkWidget(onTap: _markAsRead, child: _buildCheckIcon()),
2019-02-06 06:06:11 +01:00
],
),
2019-02-05 13:57:05 +01:00
),
2019-01-30 07:46:18 +01:00
),
);
}
}