Add platform checks and add auto refresh with rate limiting

This commit is contained in:
Nutcake 2023-05-04 14:44:40 +02:00
parent 28fe3fc3c0
commit 2a4a23f6aa
5 changed files with 45 additions and 11 deletions

View file

@ -1,4 +1,5 @@
import 'dart:developer'; import 'dart:developer';
import 'dart:io' show Platform;
import 'package:contacts_plus_plus/clients/neos_hub.dart'; import 'package:contacts_plus_plus/clients/neos_hub.dart';
import 'package:contacts_plus_plus/clients/settings_client.dart'; import 'package:contacts_plus_plus/clients/settings_client.dart';
@ -12,12 +13,15 @@ import 'clients/api_client.dart';
import 'models/authentication_data.dart'; import 'models/authentication_data.dart';
void main() async { void main() async {
await Workmanager().initialize(
callbackDispatcher, // The top level function, aka callbackDispatcher
isInDebugMode: true // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
);
Logger.root.onRecord.listen((event) => log(event.message, name: event.loggerName));
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
if (Platform.isAndroid) {
await Workmanager().initialize(
callbackDispatcher, // The top level function, aka callbackDispatcher
isInDebugMode: true // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
);
}
Logger.root.onRecord.listen((event) => log(event.message, name: event.loggerName));
final settingsClient = SettingsClient(); final settingsClient = SettingsClient();
await settingsClient.loadSettings(); await settingsClient.loadSettings();
runApp(Phoenix(child: ContactsPlusPlus(settingsClient: settingsClient,))); runApp(Phoenix(child: ContactsPlusPlus(settingsClient: settingsClient,)));

View file

@ -19,15 +19,19 @@ class FriendsList extends StatefulWidget {
} }
class _FriendsListState extends State<FriendsList> { class _FriendsListState extends State<FriendsList> {
static const Duration _autoRefreshDuration = Duration(seconds: 90);
static const Duration _refreshTimeoutDuration = Duration(seconds: 30);
final _unreads = <String, List<Message>>{};
Future<List<Friend>>? _friendsFuture; Future<List<Friend>>? _friendsFuture;
ClientHolder? _clientHolder; ClientHolder? _clientHolder;
Timer? _debouncer; Timer? _autoRefresh;
Timer? _refreshTimeout;
String _searchFilter = ""; String _searchFilter = "";
final _unreads = <String, List<Message>>{};
@override @override
void dispose() { void dispose() {
_debouncer?.cancel(); _autoRefresh?.cancel();
_refreshTimeout?.cancel();
super.dispose(); super.dispose();
} }
@ -42,6 +46,7 @@ class _FriendsListState extends State<FriendsList> {
} }
void _refreshFriendsList() { void _refreshFriendsList() {
if (_refreshTimeout?.isActive == true) return;
_friendsFuture = FriendApi.getFriendsList(_clientHolder!.apiClient).then((Iterable<Friend> value) async { _friendsFuture = FriendApi.getFriendsList(_clientHolder!.apiClient).then((Iterable<Friend> value) async {
final unreadMessages = await MessageApi.getUserMessages(_clientHolder!.apiClient, unreadOnly: true); final unreadMessages = await MessageApi.getUserMessages(_clientHolder!.apiClient, unreadOnly: true);
_unreads.clear(); _unreads.clear();
@ -66,6 +71,10 @@ class _FriendsListState extends State<FriendsList> {
aVal += a.userStatus.onlineStatus.compareTo(b.userStatus.onlineStatus) * 2; aVal += a.userStatus.onlineStatus.compareTo(b.userStatus.onlineStatus) * 2;
return aVal.compareTo(bVal); return aVal.compareTo(bVal);
}); });
_autoRefresh?.cancel();
_autoRefresh = Timer(_autoRefreshDuration, () => setState(() => _refreshFriendsList()));
_refreshTimeout?.cancel();
_refreshTimeout = Timer(_refreshTimeoutDuration, () {});
return friends; return friends;
}); });
} }
@ -147,7 +156,6 @@ class _FriendsListState extends State<FriendsList> {
}); });
}, },
onExpansionChanged: (expanded) { onExpansionChanged: (expanded) {
if (_debouncer?.isActive ?? false) _debouncer?.cancel();
if (!expanded) { if (!expanded) {
setState(() { setState(() {
_searchFilter = ""; _searchFilter = "";

View file

@ -104,7 +104,7 @@ class _LoginScreenState extends State<LoginScreen> {
final requestResult = await notificationManager.resolvePlatformSpecificImplementation< final requestResult = await notificationManager.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>() AndroidFlutterLocalNotificationsPlugin>()
?.requestPermission(); ?.requestPermission();
await settingsClient.changeSettings(settingsClient.currentSettings.copyWith(notificationsDenied: requestResult == null ? null : !requestResult)); await settingsClient.changeSettings(settingsClient.currentSettings.copyWith(notificationsDenied: requestResult == null ? false : !requestResult));
}, },
child: const Text("Yes"), child: const Text("Yes"),
) )

View file

@ -1,4 +1,5 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:io' show Platform;
import 'package:contacts_plus_plus/clients/api_client.dart'; import 'package:contacts_plus_plus/clients/api_client.dart';
import 'package:contacts_plus_plus/auxiliary.dart'; import 'package:contacts_plus_plus/auxiliary.dart';
@ -25,13 +26,32 @@ class _MessageAudioPlayerState extends State<MessageAudioPlayer> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_audioPlayer.setAudioSource(AudioSource.uri(Uri.parse( if (Platform.isAndroid) {
_audioPlayer.setAudioSource(AudioSource.uri(Uri.parse(
Aux.neosDbToHttp(AudioClipContent.fromMap(jsonDecode(widget.message.content)).assetUri) Aux.neosDbToHttp(AudioClipContent.fromMap(jsonDecode(widget.message.content)).assetUri)
))).whenComplete(() => _audioPlayer.setLoopMode(LoopMode.off)); ))).whenComplete(() => _audioPlayer.setLoopMode(LoopMode.off));
}
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (!Platform.isAndroid) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(Icons.error_outline, color: Theme.of(context).colorScheme.error,),
const SizedBox(height: 4,),
Text("Sorry, audio-messages are not\n supported on this platform.", textAlign: TextAlign.center,
softWrap: true,
maxLines: 3,
style: Theme.of(context).textTheme.bodySmall?.copyWith(color: Theme.of(context).colorScheme.error),
),
],
),
);
}
return IntrinsicWidth( return IntrinsicWidth(
child: StreamBuilder<PlayerState>( child: StreamBuilder<PlayerState>(
stream: _audioPlayer.playerStateStream, stream: _audioPlayer.playerStateStream,

View file

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:cached_network_image/cached_network_image.dart'; import 'package:cached_network_image/cached_network_image.dart';
import 'package:contacts_plus_plus/clients/api_client.dart'; import 'package:contacts_plus_plus/clients/api_client.dart';
import 'package:contacts_plus_plus/auxiliary.dart'; import 'package:contacts_plus_plus/auxiliary.dart';