Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unread badges to profile switcher #1523

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions lib/account/widgets/profile_modal_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:swipeable_page_route/swipeable_page_route.dart';

import 'package:thunder/account/models/account.dart';
import 'package:thunder/account/pages/login_page.dart';
import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/core/theme/bloc/theme_bloc.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
import 'package:thunder/user/utils/logout_dialog.dart';
Expand Down Expand Up @@ -300,13 +302,29 @@ class _ProfileSelectState extends State<ProfileSelect> {
),
],
),
title: Text(
accounts![index].account.username ?? 'N/A',
style: theme.textTheme.titleMedium?.copyWith(),
title: Row(
children: [
Text(
accounts![index].account.username ?? 'N/A',
style: theme.textTheme.titleMedium?.copyWith(),
),
Row(
children: [
const SizedBox(width: 7),
AnimatedOpacity(
duration: const Duration(milliseconds: 250),
opacity: accounts![index].totalUnreadCount == null ? 0 : 1,
child: Badge(
label: Text(accounts![index].totalUnreadCount.toString()),
),
),
],
),
],
),
subtitle: Wrap(
children: [
Text(accounts![index].account.instance.replaceAll('https://', '') ?? 'N/A'),
Text(accounts![index].account.instance.replaceAll('https://', '')),
AnimatedSize(
duration: const Duration(milliseconds: 250),
child: accounts![index].version == null
Expand Down Expand Up @@ -713,6 +731,7 @@ class _ProfileSelectState extends State<ProfileSelect> {
// Intentionally don't await these here
fetchInstanceInfo(accountsExtended);
pingInstances(accountsExtended);
getUnreadCounts(accountsExtended);

setState(() => this.accounts = accountsExtended);
}
Expand Down Expand Up @@ -744,6 +763,19 @@ class _ProfileSelectState extends State<ProfileSelect> {
});
}

Future<void> getUnreadCounts(List<AccountExtended> accountsExtended) async {
for (final AccountExtended account in accountsExtended) {
try {
final GetUnreadCountResponse getUnreadCountResponse = (await (LemmyClient()..changeBaseUrl(account.instance!)).lemmyApiV3.run(GetUnreadCount(auth: account.account.jwt)));
int? totalUnreadCount = getUnreadCountResponse.replies + getUnreadCountResponse.mentions + getUnreadCountResponse.privateMessages;
if (totalUnreadCount == 0) totalUnreadCount = null;
setState(() => account.totalUnreadCount = totalUnreadCount);
} catch (e) {
// If we can't do this, for any reason, it's not a big deal. Just move on to the next one
}
}
}

Future<void> fetchAnonymousInstances() async {
final List<AnonymousInstanceExtended> anonymousInstances = (await Account.anonymousInstances()).map((anonymousInstance) => AnonymousInstanceExtended(anonymousInstance: anonymousInstance)).toList()
..sort((a, b) => a.anonymousInstance.index.compareTo(b.anonymousInstance.index));
Expand Down Expand Up @@ -801,6 +833,7 @@ class AccountExtended {
String? version;
Duration? latency;
bool? alive;
int? totalUnreadCount;

AccountExtended({required this.account, this.instance, this.instanceIcon});
}
Expand Down
Loading