Fix user status handling
This commit is contained in:
parent
bcc03b4c9a
commit
c1da0da897
7 changed files with 85 additions and 158 deletions
|
@ -1,5 +1,6 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:contacts_plus_plus/hub_manager.dart';
|
import 'package:contacts_plus_plus/hub_manager.dart';
|
||||||
|
import 'package:contacts_plus_plus/models/users/online_status.dart';
|
||||||
import 'package:contacts_plus_plus/models/users/user_status.dart';
|
import 'package:contacts_plus_plus/models/users/user_status.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/widgets.dart';
|
import 'package:flutter/widgets.dart';
|
||||||
|
@ -57,12 +58,15 @@ class MessagingClient extends ChangeNotifier {
|
||||||
final Logger _logger = Logger("Messaging");
|
final Logger _logger = Logger("Messaging");
|
||||||
final NotificationClient _notificationClient;
|
final NotificationClient _notificationClient;
|
||||||
final HubManager _hubManager = HubManager();
|
final HubManager _hubManager = HubManager();
|
||||||
|
|
||||||
Friend? selectedFriend;
|
Friend? selectedFriend;
|
||||||
|
|
||||||
Timer? _notifyOnlineTimer;
|
Timer? _notifyOnlineTimer;
|
||||||
Timer? _autoRefresh;
|
Timer? _autoRefresh;
|
||||||
Timer? _unreadSafeguard;
|
Timer? _unreadSafeguard;
|
||||||
String? _initStatus;
|
String? _initStatus;
|
||||||
|
UserStatus _userStatus = UserStatus.empty().copyWith(onlineStatus: OnlineStatus.online);
|
||||||
|
|
||||||
|
UserStatus get userStatus => _userStatus;
|
||||||
|
|
||||||
MessagingClient({required ApiClient apiClient, required NotificationClient notificationClient})
|
MessagingClient({required ApiClient apiClient, required NotificationClient notificationClient})
|
||||||
: _apiClient = apiClient,
|
: _apiClient = apiClient,
|
||||||
|
@ -72,11 +76,6 @@ class MessagingClient extends ChangeNotifier {
|
||||||
box.delete(_lastUpdateKey);
|
box.delete(_lastUpdateKey);
|
||||||
});
|
});
|
||||||
_setupHub();
|
_setupHub();
|
||||||
_notifyOnlineTimer = Timer.periodic(const Duration(seconds: 60), (timer) async {
|
|
||||||
// We should probably let the MessagingClient handle the entire state of USerStatus instead of mirroring like this
|
|
||||||
// but I don't feel like implementing that right now.
|
|
||||||
setUserStatus(await UserApi.getUserStatus(apiClient, userId: apiClient.userId));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -146,18 +145,23 @@ class MessagingClient extends ChangeNotifier {
|
||||||
Future<void> setUserStatus(UserStatus status) async {
|
Future<void> setUserStatus(UserStatus status) async {
|
||||||
final pkginfo = await PackageInfo.fromPlatform();
|
final pkginfo = await PackageInfo.fromPlatform();
|
||||||
|
|
||||||
status = status.copyWith(
|
_userStatus = status.copyWith(
|
||||||
appVersion: "${pkginfo.version} of ${pkginfo.appName}",
|
appVersion: "${pkginfo.version} of ${pkginfo.appName}",
|
||||||
isMobile: true,
|
isMobile: true,
|
||||||
|
lastStatusChange: DateTime.now(),
|
||||||
);
|
);
|
||||||
|
|
||||||
_hubManager.send("BroadcastStatus", arguments: [
|
_hubManager.send("BroadcastStatus", arguments: [
|
||||||
status.toMap(),
|
_userStatus.toMap(),
|
||||||
{
|
{
|
||||||
"group": 0,
|
"group": 0,
|
||||||
"targetIds": [],
|
"targetIds": [],
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
final self = getAsFriend(_apiClient.userId);
|
||||||
|
await _updateContact(self!.copyWith(userStatus: _userStatus));
|
||||||
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
void addUnread(Message message) {
|
void addUnread(Message message) {
|
||||||
|
|
|
@ -14,6 +14,7 @@ import 'package:contacts_plus_plus/widgets/update_notifier.dart';
|
||||||
import 'package:dynamic_color/dynamic_color.dart';
|
import 'package:dynamic_color/dynamic_color.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
import 'package:flutter_downloader/flutter_downloader.dart';
|
import 'package:flutter_downloader/flutter_downloader.dart';
|
||||||
import 'package:flutter_phoenix/flutter_phoenix.dart';
|
import 'package:flutter_phoenix/flutter_phoenix.dart';
|
||||||
import 'package:hive_flutter/hive_flutter.dart';
|
import 'package:hive_flutter/hive_flutter.dart';
|
||||||
|
@ -132,14 +133,12 @@ class _ContactsPlusPlusState extends State<ContactsPlusPlus> {
|
||||||
theme: ThemeData(
|
theme: ThemeData(
|
||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
textTheme: _typography.black,
|
textTheme: _typography.black,
|
||||||
canvasColor: lightDynamic?.surfaceVariant,
|
|
||||||
colorScheme:
|
colorScheme:
|
||||||
lightDynamic ?? ColorScheme.fromSeed(seedColor: Colors.purple, brightness: Brightness.light),
|
lightDynamic ?? ColorScheme.fromSeed(seedColor: Colors.purple, brightness: Brightness.light),
|
||||||
),
|
),
|
||||||
darkTheme: ThemeData(
|
darkTheme: ThemeData(
|
||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
textTheme: _typography.white,
|
textTheme: _typography.white,
|
||||||
canvasColor: darkDynamic?.surfaceVariant,
|
|
||||||
colorScheme: darkDynamic ?? ColorScheme.fromSeed(seedColor: Colors.purple, brightness: Brightness.dark),
|
colorScheme: darkDynamic ?? ColorScheme.fromSeed(seedColor: Colors.purple, brightness: Brightness.dark),
|
||||||
),
|
),
|
||||||
themeMode: ThemeMode.values[widget.settingsClient.currentSettings.themeMode.valueOrDefault],
|
themeMode: ThemeMode.values[widget.settingsClient.currentSettings.themeMode.valueOrDefault],
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
import 'package:contacts_plus_plus/apis/user_api.dart';
|
|
||||||
import 'package:contacts_plus_plus/client_holder.dart';
|
import 'package:contacts_plus_plus/client_holder.dart';
|
||||||
import 'package:contacts_plus_plus/clients/messaging_client.dart';
|
import 'package:contacts_plus_plus/clients/messaging_client.dart';
|
||||||
import 'package:contacts_plus_plus/models/users/online_status.dart';
|
import 'package:contacts_plus_plus/models/users/online_status.dart';
|
||||||
import 'package:contacts_plus_plus/models/users/user_status.dart';
|
|
||||||
import 'package:contacts_plus_plus/widgets/friends/user_search.dart';
|
import 'package:contacts_plus_plus/widgets/friends/user_search.dart';
|
||||||
import 'package:contacts_plus_plus/widgets/my_profile_dialog.dart';
|
import 'package:contacts_plus_plus/widgets/my_profile_dialog.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
@ -18,33 +16,6 @@ class FriendsListAppBar extends StatefulWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
class _FriendsListAppBarState extends State<FriendsListAppBar> with AutomaticKeepAliveClientMixin {
|
class _FriendsListAppBarState extends State<FriendsListAppBar> with AutomaticKeepAliveClientMixin {
|
||||||
Future<UserStatus>? _userStatusFuture;
|
|
||||||
ClientHolder? _clientHolder;
|
|
||||||
|
|
||||||
@override
|
|
||||||
void didChangeDependencies() async {
|
|
||||||
super.didChangeDependencies();
|
|
||||||
final clientHolder = ClientHolder.of(context);
|
|
||||||
if (_clientHolder != clientHolder) {
|
|
||||||
_clientHolder = clientHolder;
|
|
||||||
_refreshUserStatus();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void _refreshUserStatus() {
|
|
||||||
final apiClient = _clientHolder!.apiClient;
|
|
||||||
final messagingClient = Provider.of<MessagingClient>(context, listen: false);
|
|
||||||
_userStatusFuture ??= UserApi.getUserStatus(apiClient, userId: apiClient.userId).then((value) async {
|
|
||||||
if (value.onlineStatus == OnlineStatus.offline) {
|
|
||||||
final newStatus = value.copyWith(
|
|
||||||
onlineStatus:
|
|
||||||
OnlineStatus.values[_clientHolder!.settingsClient.currentSettings.lastOnlineStatus.valueOrDefault]);
|
|
||||||
await messagingClient.setUserStatus(newStatus);
|
|
||||||
return newStatus;
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
@ -52,106 +23,63 @@ class _FriendsListAppBarState extends State<FriendsListAppBar> with AutomaticKee
|
||||||
return AppBar(
|
return AppBar(
|
||||||
title: const Text("Contacts++"),
|
title: const Text("Contacts++"),
|
||||||
systemOverlayStyle: SystemUiOverlayStyle(
|
systemOverlayStyle: SystemUiOverlayStyle(
|
||||||
systemNavigationBarColor: Theme.of(context).colorScheme.surfaceVariant,
|
systemNavigationBarColor: Theme.of(context).navigationBarTheme.backgroundColor,
|
||||||
),
|
),
|
||||||
actions: [
|
actions: [
|
||||||
FutureBuilder(
|
Consumer<MessagingClient>(builder: (context, client, _) {
|
||||||
future: _userStatusFuture,
|
return PopupMenuButton<OnlineStatus>(
|
||||||
builder: (context, snapshot) {
|
child: Row(
|
||||||
if (snapshot.hasData) {
|
children: [
|
||||||
final userStatus = snapshot.data as UserStatus;
|
Padding(
|
||||||
return PopupMenuButton<OnlineStatus>(
|
padding: const EdgeInsets.only(right: 8.0),
|
||||||
child: Row(
|
child: Icon(
|
||||||
children: [
|
Icons.circle,
|
||||||
Padding(
|
size: 16,
|
||||||
padding: const EdgeInsets.only(right: 8.0),
|
color: client.userStatus.onlineStatus.color(context),
|
||||||
child: Icon(
|
),
|
||||||
|
),
|
||||||
|
Text(toBeginningOfSentenceCase(client.userStatus.onlineStatus.name) ?? "Unknown"),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
onSelected: (OnlineStatus onlineStatus) async {
|
||||||
|
final newStatus = client.userStatus.copyWith(onlineStatus: onlineStatus);
|
||||||
|
final settingsClient = ClientHolder.of(context).settingsClient;
|
||||||
|
try {
|
||||||
|
await client.setUserStatus(newStatus);
|
||||||
|
await settingsClient
|
||||||
|
.changeSettings(settingsClient.currentSettings.copyWith(lastOnlineStatus: onlineStatus.index));
|
||||||
|
} catch (e, s) {
|
||||||
|
FlutterError.reportError(FlutterErrorDetails(exception: e, stack: s));
|
||||||
|
if (context.mounted) {
|
||||||
|
ScaffoldMessenger.of(context)
|
||||||
|
.showSnackBar(const SnackBar(content: Text("Failed to set online-status.")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
itemBuilder: (BuildContext context) => OnlineStatus.values
|
||||||
|
.where((element) => element == OnlineStatus.online || element == OnlineStatus.invisible)
|
||||||
|
.map(
|
||||||
|
(item) => PopupMenuItem<OnlineStatus>(
|
||||||
|
value: item,
|
||||||
|
child: Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Icon(
|
||||||
Icons.circle,
|
Icons.circle,
|
||||||
size: 16,
|
size: 16,
|
||||||
color: userStatus.onlineStatus.color(context),
|
color: item.color(context),
|
||||||
),
|
),
|
||||||
),
|
const SizedBox(
|
||||||
Text(toBeginningOfSentenceCase(userStatus.onlineStatus.name) ?? "Unknown"),
|
width: 8,
|
||||||
],
|
),
|
||||||
),
|
Text(toBeginningOfSentenceCase(item.name)!),
|
||||||
onSelected: (OnlineStatus onlineStatus) async {
|
],
|
||||||
try {
|
|
||||||
final messagingClient = Provider.of<MessagingClient>(context, listen: false);
|
|
||||||
final newStatus = userStatus.copyWith(onlineStatus: onlineStatus);
|
|
||||||
setState(() {
|
|
||||||
_userStatusFuture = Future.value(newStatus.copyWith(lastStatusChange: DateTime.now()));
|
|
||||||
});
|
|
||||||
final settingsClient = _clientHolder!.settingsClient;
|
|
||||||
await messagingClient.setUserStatus(newStatus);
|
|
||||||
await settingsClient.changeSettings(
|
|
||||||
settingsClient.currentSettings.copyWith(lastOnlineStatus: onlineStatus.index));
|
|
||||||
} catch (e, s) {
|
|
||||||
FlutterError.reportError(FlutterErrorDetails(exception: e, stack: s));
|
|
||||||
if (context.mounted) {
|
|
||||||
ScaffoldMessenger.of(context)
|
|
||||||
.showSnackBar(const SnackBar(content: Text("Failed to set online-status.")));
|
|
||||||
}
|
|
||||||
setState(() {
|
|
||||||
_userStatusFuture = Future.value(userStatus);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
itemBuilder: (BuildContext context) => OnlineStatus.values
|
|
||||||
.where((element) => element == OnlineStatus.online || element == OnlineStatus.invisible)
|
|
||||||
.map(
|
|
||||||
(item) => PopupMenuItem<OnlineStatus>(
|
|
||||||
value: item,
|
|
||||||
child: Row(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.start,
|
|
||||||
children: [
|
|
||||||
Icon(
|
|
||||||
Icons.circle,
|
|
||||||
size: 16,
|
|
||||||
color: item.color(context),
|
|
||||||
),
|
|
||||||
const SizedBox(
|
|
||||||
width: 8,
|
|
||||||
),
|
|
||||||
Text(toBeginningOfSentenceCase(item.name)!),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
)
|
|
||||||
.toList());
|
|
||||||
} else if (snapshot.hasError) {
|
|
||||||
return TextButton.icon(
|
|
||||||
style: TextButton.styleFrom(
|
|
||||||
foregroundColor: Theme.of(context).colorScheme.onSurface,
|
|
||||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 2)),
|
|
||||||
onPressed: () {
|
|
||||||
setState(() {
|
|
||||||
_userStatusFuture =
|
|
||||||
UserApi.getUserStatus(_clientHolder!.apiClient, userId: _clientHolder!.apiClient.userId);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
icon: const Icon(Icons.warning),
|
|
||||||
label: const Text("Retry"),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
return TextButton.icon(
|
|
||||||
style: TextButton.styleFrom(
|
|
||||||
disabledForegroundColor: Theme.of(context).colorScheme.onSurface,
|
|
||||||
),
|
|
||||||
onPressed: null,
|
|
||||||
icon: Container(
|
|
||||||
width: 16,
|
|
||||||
height: 16,
|
|
||||||
margin: const EdgeInsets.only(right: 4),
|
|
||||||
child: CircularProgressIndicator(
|
|
||||||
strokeWidth: 2,
|
|
||||||
color: Theme.of(context).colorScheme.onSurface,
|
|
||||||
),
|
),
|
||||||
),
|
)
|
||||||
label: const Text("Loading"),
|
.toList(),
|
||||||
);
|
);
|
||||||
}
|
}),
|
||||||
},
|
|
||||||
),
|
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.only(left: 4, right: 4),
|
padding: const EdgeInsets.only(left: 4, right: 4),
|
||||||
child: PopupMenuButton<MenuItemDefinition>(
|
child: PopupMenuButton<MenuItemDefinition>(
|
||||||
|
@ -190,16 +118,16 @@ class _FriendsListAppBarState extends State<FriendsListAppBar> with AutomaticKee
|
||||||
]
|
]
|
||||||
.map(
|
.map(
|
||||||
(item) => PopupMenuItem<MenuItemDefinition>(
|
(item) => PopupMenuItem<MenuItemDefinition>(
|
||||||
value: item,
|
value: item,
|
||||||
child: Row(
|
child: Row(
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
children: [
|
children: [
|
||||||
Text(item.name),
|
Text(item.name),
|
||||||
Icon(item.icon),
|
Icon(item.icon),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.toList(),
|
.toList(),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
|
@ -47,13 +47,9 @@ class _HomeState extends State<Home> {
|
||||||
SettingsPage(),
|
SettingsPage(),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
bottomNavigationBar: BottomNavigationBar(
|
bottomNavigationBar: NavigationBar(
|
||||||
backgroundColor: Theme.of(context).colorScheme.surfaceVariant,
|
selectedIndex: _selectedPage,
|
||||||
type: BottomNavigationBarType.fixed,
|
onDestinationSelected: (index) {
|
||||||
unselectedItemColor: Theme.of(context).colorScheme.onBackground,
|
|
||||||
selectedItemColor: Theme.of(context).colorScheme.primary,
|
|
||||||
currentIndex: _selectedPage,
|
|
||||||
onTap: (index) {
|
|
||||||
_pageController.animateToPage(
|
_pageController.animateToPage(
|
||||||
index,
|
index,
|
||||||
duration: const Duration(milliseconds: 200),
|
duration: const Duration(milliseconds: 200),
|
||||||
|
@ -63,20 +59,20 @@ class _HomeState extends State<Home> {
|
||||||
_selectedPage = index;
|
_selectedPage = index;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
items: const [
|
destinations: const [
|
||||||
BottomNavigationBarItem(
|
NavigationDestination(
|
||||||
icon: Icon(Icons.message),
|
icon: Icon(Icons.message),
|
||||||
label: "Chat",
|
label: "Chat",
|
||||||
),
|
),
|
||||||
BottomNavigationBarItem(
|
NavigationDestination(
|
||||||
icon: Icon(Icons.public),
|
icon: Icon(Icons.public),
|
||||||
label: "Sessions",
|
label: "Sessions",
|
||||||
),
|
),
|
||||||
BottomNavigationBarItem(
|
NavigationDestination(
|
||||||
icon: Icon(Icons.inventory),
|
icon: Icon(Icons.inventory),
|
||||||
label: "Inventory",
|
label: "Inventory",
|
||||||
),
|
),
|
||||||
BottomNavigationBarItem(
|
NavigationDestination(
|
||||||
icon: Icon(Icons.settings),
|
icon: Icon(Icons.settings),
|
||||||
label: "Settings",
|
label: "Settings",
|
||||||
),
|
),
|
||||||
|
|
|
@ -62,14 +62,14 @@ class _InventoryBrowserAppBarState extends State<InventoryBrowserAppBar> {
|
||||||
key: const ValueKey("default-appbar"),
|
key: const ValueKey("default-appbar"),
|
||||||
title: const Text("Inventory"),
|
title: const Text("Inventory"),
|
||||||
systemOverlayStyle: SystemUiOverlayStyle(
|
systemOverlayStyle: SystemUiOverlayStyle(
|
||||||
systemNavigationBarColor: Theme.of(context).colorScheme.surfaceVariant,
|
systemNavigationBarColor: Theme.of(context).navigationBarTheme.backgroundColor,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
: AppBar(
|
: AppBar(
|
||||||
key: const ValueKey("selection-appbar"),
|
key: const ValueKey("selection-appbar"),
|
||||||
title: Text("${iClient.selectedRecordCount} Selected"),
|
title: Text("${iClient.selectedRecordCount} Selected"),
|
||||||
systemOverlayStyle: SystemUiOverlayStyle(
|
systemOverlayStyle: SystemUiOverlayStyle(
|
||||||
systemNavigationBarColor: Theme.of(context).colorScheme.surfaceVariant,
|
systemNavigationBarColor: Theme.of(context).navigationBarTheme.backgroundColor,
|
||||||
),
|
),
|
||||||
leading: IconButton(
|
leading: IconButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
|
|
|
@ -17,7 +17,7 @@ class _SessionListAppBarState extends State<SessionListAppBar> {
|
||||||
return AppBar(
|
return AppBar(
|
||||||
title: const Text("Sessions"),
|
title: const Text("Sessions"),
|
||||||
systemOverlayStyle: SystemUiOverlayStyle(
|
systemOverlayStyle: SystemUiOverlayStyle(
|
||||||
systemNavigationBarColor: Theme.of(context).colorScheme.surfaceVariant,
|
systemNavigationBarColor: Theme.of(context).navigationBarTheme.backgroundColor,
|
||||||
),
|
),
|
||||||
actions: [
|
actions: [
|
||||||
Padding(
|
Padding(
|
||||||
|
|
|
@ -9,7 +9,7 @@ class SettingsAppBar extends StatelessWidget {
|
||||||
return AppBar(
|
return AppBar(
|
||||||
title: const Text("Settings"),
|
title: const Text("Settings"),
|
||||||
systemOverlayStyle: SystemUiOverlayStyle(
|
systemOverlayStyle: SystemUiOverlayStyle(
|
||||||
systemNavigationBarColor: Theme.of(context).colorScheme.surfaceVariant,
|
systemNavigationBarColor: Theme.of(context).navigationBarTheme.backgroundColor,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue