2024-04-20 09:53:55 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/console_log.dart';
|
|
|
|
|
|
|
|
class ConsoleModel extends ChangeNotifier {
|
2024-05-19 23:09:23 +00:00
|
|
|
/// limit for history, so logs won't affect memory and overflow
|
|
|
|
static const logBufferLimit = 500;
|
|
|
|
|
|
|
|
/// differs from log buffer limit so as to not rearrange memory each time
|
|
|
|
/// we add incoming log
|
|
|
|
static const incomingBufferBreakpoint = 750;
|
|
|
|
|
2024-04-20 09:53:55 +00:00
|
|
|
final List<ConsoleLog> _logs = [];
|
2024-05-19 23:09:23 +00:00
|
|
|
final List<ConsoleLog> _incomingQueue = [];
|
|
|
|
|
|
|
|
bool _paused = false;
|
|
|
|
bool get paused => _paused;
|
2024-04-20 09:53:55 +00:00
|
|
|
List<ConsoleLog> get logs => _logs;
|
|
|
|
|
|
|
|
void log(final ConsoleLog newLog) {
|
2024-05-19 23:09:23 +00:00
|
|
|
if (paused) {
|
|
|
|
_incomingQueue.add(newLog);
|
|
|
|
if (_incomingQueue.length > incomingBufferBreakpoint) {
|
|
|
|
logs.removeRange(0, _incomingQueue.length - logBufferLimit);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logs.add(newLog);
|
|
|
|
_updateQueue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void play() {
|
|
|
|
_logs.addAll(_incomingQueue);
|
|
|
|
_paused = false;
|
|
|
|
_updateQueue();
|
|
|
|
_incomingQueue.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void pause() {
|
|
|
|
_paused = true;
|
2024-04-20 09:53:55 +00:00
|
|
|
notifyListeners();
|
2024-05-19 23:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// drop logs over the limit and
|
|
|
|
void _updateQueue() {
|
2024-04-20 09:53:55 +00:00
|
|
|
// Make sure we don't have too many
|
2024-05-19 23:09:23 +00:00
|
|
|
if (logs.length > logBufferLimit) {
|
|
|
|
logs.removeRange(0, logs.length - logBufferLimit);
|
2024-04-20 09:53:55 +00:00
|
|
|
}
|
2024-05-19 23:09:23 +00:00
|
|
|
notifyListeners();
|
2024-04-20 09:53:55 +00:00
|
|
|
}
|
|
|
|
}
|