veilidchat/lib/tick.dart

218 lines
7.0 KiB
Dart
Raw Normal View History

2023-08-08 06:03:26 +00:00
import 'dart:async';
2023-10-01 01:22:12 +00:00
import 'package:flutter/foundation.dart';
2023-08-08 06:03:26 +00:00
import 'package:flutter/material.dart';
2024-01-05 03:29:43 +00:00
import 'package:veilid_support/veilid_support.dart';
2023-08-08 06:03:26 +00:00
2023-12-28 03:56:24 +00:00
import 'init.dart';
2024-01-05 03:29:43 +00:00
import 'veilid_processor/veilid_processor.dart';
2023-08-08 06:03:26 +00:00
const int ticksPerContactInvitationCheck = 5;
const int ticksPerNewMessageCheck = 5;
2023-12-27 01:26:54 +00:00
class BackgroundTicker extends StatefulWidget {
2023-08-08 06:03:26 +00:00
const BackgroundTicker({required this.builder, super.key});
final Widget Function(BuildContext) builder;
@override
BackgroundTickerState createState() => BackgroundTickerState();
2023-09-30 02:45:50 +00:00
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
2023-10-01 01:22:12 +00:00
properties.add(ObjectFlagProperty<Widget Function(BuildContext p1)>.has(
'builder', builder));
2023-09-30 02:45:50 +00:00
}
2023-08-08 06:03:26 +00:00
}
2023-12-27 01:26:54 +00:00
class BackgroundTickerState extends State<BackgroundTicker> {
2023-08-08 06:03:26 +00:00
Timer? _tickTimer;
bool _inTick = false;
2024-01-05 03:29:43 +00:00
bool _inDoContactInvitationCheck = false;
bool _inDoNewMessageCheck = false;
2023-08-08 06:03:26 +00:00
int _contactInvitationCheckTick = 0;
int _newMessageCheckTick = 0;
@override
void initState() {
super.initState();
_tickTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
if (!_inTick) {
unawaited(_onTick());
}
});
}
@override
void dispose() {
final tickTimer = _tickTimer;
if (tickTimer != null) {
tickTimer.cancel();
}
super.dispose();
}
@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
return widget.builder(context);
}
Future<void> _onTick() async {
2024-01-05 03:29:43 +00:00
// Don't tick until we are initialized
if (!eventualInitialized.isCompleted) {
2023-09-26 22:46:02 +00:00
return;
}
2024-01-05 03:29:43 +00:00
if (!ProcessorRepository
.instance.processorConnectionState.isPublicInternetReady) {
2023-10-21 23:23:43 +00:00
return;
}
2023-09-26 22:46:02 +00:00
2023-08-08 06:03:26 +00:00
_inTick = true;
try {
2024-01-05 03:29:43 +00:00
// Tick DHT record pool
if (!DHTRecordPool.instance.inTick) {
unawaited(DHTRecordPool.instance.tick());
}
2023-08-08 06:03:26 +00:00
// Check extant contact invitations once every N seconds
_contactInvitationCheckTick += 1;
if (_contactInvitationCheckTick >= ticksPerContactInvitationCheck) {
_contactInvitationCheckTick = 0;
2024-01-05 03:29:43 +00:00
if (!_inDoContactInvitationCheck) {
unawaited(_doContactInvitationCheck());
}
2023-08-08 06:03:26 +00:00
}
// Check new messages once every N seconds
_newMessageCheckTick += 1;
if (_newMessageCheckTick >= ticksPerNewMessageCheck) {
_newMessageCheckTick = 0;
2024-01-05 03:29:43 +00:00
if (!_inDoNewMessageCheck) {
unawaited(_doNewMessageCheck());
}
2023-08-08 06:03:26 +00:00
}
} finally {
_inTick = false;
}
}
Future<void> _doContactInvitationCheck() async {
2024-01-05 03:29:43 +00:00
if (_inDoContactInvitationCheck) {
2023-10-21 23:23:43 +00:00
return;
}
2024-01-05 03:29:43 +00:00
_inDoContactInvitationCheck = true;
if (!ProcessorRepository
.instance.processorConnectionState.isPublicInternetReady) {
2023-08-08 06:03:26 +00:00
return;
}
2024-01-05 03:29:43 +00:00
// final contactInvitationRecords =
// await ref.read(fetchContactInvitationRecordsProvider.future);
// if (contactInvitationRecords == null) {
// return;
// }
try {
// final activeAccountInfo =
// await ref.read(fetchActiveAccountProvider.future);
// if (activeAccountInfo == null) {
// return;
// }
// final allChecks = <Future<void>>[];
// for (final contactInvitationRecord in contactInvitationRecords) {
// allChecks.add(() async {
// final acceptReject = await checkAcceptRejectContact(
// activeAccountInfo: activeAccountInfo,
// contactInvitationRecord: contactInvitationRecord);
// if (acceptReject != null) {
// final acceptedContact = acceptReject.acceptedContact;
// if (acceptedContact != null) {
// // Accept
// await createContact(
// activeAccountInfo: activeAccountInfo,
// profile: acceptedContact.profile,
// remoteIdentity: acceptedContact.remoteIdentity,
// remoteConversationRecordKey:
// acceptedContact.remoteConversationRecordKey,
// localConversationRecordKey:
// acceptedContact.localConversationRecordKey,
// );
// ref
// ..invalidate(fetchContactInvitationRecordsProvider)
// ..invalidate(fetchContactListProvider);
// } else {
// // Reject
// ref.invalidate(fetchContactInvitationRecordsProvider);
// }
// }
// }());
// }
// await Future.wait(allChecks);
} finally {
_inDoContactInvitationCheck = true;
2023-08-08 06:03:26 +00:00
}
}
Future<void> _doNewMessageCheck() async {
2024-01-05 03:29:43 +00:00
if (_inDoNewMessageCheck) {
2023-08-08 06:03:26 +00:00
return;
}
2024-01-05 03:29:43 +00:00
_inDoNewMessageCheck = true;
2023-08-08 06:03:26 +00:00
2024-01-05 03:29:43 +00:00
try {
if (!ProcessorRepository
.instance.processorConnectionState.isPublicInternetReady) {
return;
2023-08-09 06:33:31 +00:00
}
2024-01-05 03:29:43 +00:00
// final activeChat = ref.read(activeChatStateProvider);
// if (activeChat == null) {
// return;
// }
// final activeAccountInfo =
// await ref.read(fetchActiveAccountProvider.future);
// if (activeAccountInfo == null) {
// return;
// }
// final contactList = ref.read(fetchContactListProvider).asData?.value ??
// const IListConst([]);
// final activeChatContactIdx = contactList.indexWhere(
// (c) =>
// proto.TypedKeyProto.fromProto(c.remoteConversationRecordKey) ==
// activeChat,
// );
// if (activeChatContactIdx == -1) {
// return;
// }
// final activeChatContact = contactList[activeChatContactIdx];
// final remoteIdentityPublicKey =
// proto.TypedKeyProto.fromProto(activeChatContact.identityPublicKey);
// final remoteConversationRecordKey = proto.TypedKeyProto.fromProto(
// activeChatContact.remoteConversationRecordKey);
// final localConversationRecordKey = proto.TypedKeyProto.fromProto(
// activeChatContact.localConversationRecordKey);
// final newMessages = await getRemoteConversationMessages(
// activeAccountInfo: activeAccountInfo,
// remoteIdentityPublicKey: remoteIdentityPublicKey,
// remoteConversationRecordKey: remoteConversationRecordKey);
// if (newMessages != null && newMessages.isNotEmpty) {
// final changed = await mergeLocalConversationMessages(
// activeAccountInfo: activeAccountInfo,
// localConversationRecordKey: localConversationRecordKey,
// remoteIdentityPublicKey: remoteIdentityPublicKey,
// newMessages: newMessages);
// if (changed) {
// ref.invalidate(activeConversationMessagesProvider);
// }
// }
} finally {
_inDoNewMessageCheck = false;
2023-08-08 06:03:26 +00:00
}
}
}