Merge origin/dev into cursor/routing-send-settings-ef15
This commit is contained in:
commit
4065d60a16
84 files changed
+7843
-1144
No files matched your search
@@ -1,6 +1,8 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../models/app_settings.dart';
|
||||
import '../models/custom_theme.dart';
|
||||
import '../models/send_attempt_plan.dart';
|
||||
@@ -136,6 +138,10 @@ class AppSettingsService extends ChangeNotifier {
|
||||
await updateSettings(_settings.copyWith(regionOnHopsLine: value));
|
||||
}
|
||||
|
||||
Future<void> setShowCompressionIndicator(bool value) async {
|
||||
await updateSettings(_settings.copyWith(showCompressionIndicator: value));
|
||||
}
|
||||
|
||||
Future<void> setMapCacheBounds(Map<String, double>? value) async {
|
||||
await updateSettings(_settings.copyWith(mapCacheBounds: value));
|
||||
}
|
||||
|
||||
@@ -6,6 +6,27 @@ import '../l10n/locale_tag.dart';
|
||||
import '../utils/platform_info.dart';
|
||||
|
||||
class BackgroundService {
|
||||
/// Options for the service that keeps the radio link alive in the
|
||||
/// background.
|
||||
///
|
||||
/// The radio link lives in the app process; once the user swipes the app
|
||||
/// away it is gone, so a lingering "connected" notification (or an
|
||||
/// automatic restart) would only be misleading. Swiping away stops the
|
||||
/// service through `android:stopWithTask="true"` in AndroidManifest.xml.
|
||||
///
|
||||
/// `stopWithTask` must stay unset here: in flutter_foreground_task (9.2 to
|
||||
/// 11.0 at least) the option does not watch the task but stops the service
|
||||
/// as soon as no activity is resumed, which is every time the app goes to
|
||||
/// the background or the screen turns off. Android then caches and freezes
|
||||
/// the process, the Bluetooth link drops and the low-memory killer ends it.
|
||||
@visibleForTesting
|
||||
static final ForegroundTaskOptions taskOptions = ForegroundTaskOptions(
|
||||
eventAction: ForegroundTaskEventAction.repeat(5000),
|
||||
autoRunOnBoot: false,
|
||||
allowWifiLock: false,
|
||||
allowAutoRestart: false,
|
||||
);
|
||||
|
||||
bool _initialized = false;
|
||||
String? Function()? _languageOverrideProvider;
|
||||
|
||||
@@ -30,16 +51,7 @@ class BackgroundService {
|
||||
showNotification: false,
|
||||
playSound: false,
|
||||
),
|
||||
foregroundTaskOptions: ForegroundTaskOptions(
|
||||
eventAction: ForegroundTaskEventAction.repeat(5000),
|
||||
autoRunOnBoot: false,
|
||||
allowWifiLock: false,
|
||||
// The radio link lives in the app process; once the user swipes the
|
||||
// app away it is gone, so a lingering "connected" notification (or
|
||||
// an automatic restart) would only be misleading.
|
||||
allowAutoRestart: false,
|
||||
stopWithTask: true,
|
||||
),
|
||||
foregroundTaskOptions: taskOptions,
|
||||
);
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:crypto/crypto.dart';
|
||||
|
||||
import '../connector/meshcore_protocol.dart';
|
||||
import '../models/contact.dart';
|
||||
import '../models/message.dart';
|
||||
@@ -202,6 +204,7 @@ class MessageRetryService extends ChangeNotifier {
|
||||
String? translationModelId,
|
||||
Uint8List? pathBytes,
|
||||
int? pathLength,
|
||||
bool isSmazCompressed = false,
|
||||
}) async {
|
||||
final messageId = const Uuid().v4();
|
||||
final resolved = resolvePathSelection(contact);
|
||||
@@ -220,6 +223,7 @@ class MessageRetryService extends ChangeNotifier {
|
||||
translationModelId: translationModelId,
|
||||
timestamp: DateTime.now(),
|
||||
isOutgoing: true,
|
||||
isSmazCompressed: isSmazCompressed,
|
||||
status: MessageStatus.pending,
|
||||
messageId: messageId,
|
||||
retryCount: 0,
|
||||
|
||||
@@ -39,11 +39,14 @@ class TranslationDownloadCancelled implements Exception {
|
||||
class TranslationService extends ChangeNotifier {
|
||||
final AppSettingsService _appSettingsService;
|
||||
final TranslationFileStore _fileStore;
|
||||
final http.Client Function() _newClient;
|
||||
|
||||
TranslationService(
|
||||
this._appSettingsService, {
|
||||
TranslationFileStore? fileStore,
|
||||
}) : _fileStore = fileStore ?? TranslationFileStore() {
|
||||
http.Client Function()? clientFactory,
|
||||
}) : _fileStore = fileStore ?? TranslationFileStore(),
|
||||
_newClient = clientFactory ?? http.Client.new {
|
||||
// Initialize langdetect once at service construction.
|
||||
_langDetectInit = initLangDetect();
|
||||
}
|
||||
@@ -61,6 +64,13 @@ class TranslationService extends ChangeNotifier {
|
||||
int _downloadedBytes = 0;
|
||||
int? _downloadTotalBytes;
|
||||
String? _downloadFileName;
|
||||
String? _downloadTargetFileName;
|
||||
String? _downloadSourceUrl;
|
||||
String? _downloadModelId;
|
||||
String? _failedDownloadError;
|
||||
String? _failedDownloadSourceUrl;
|
||||
String? _failedDownloadFileName;
|
||||
String? _failedDownloadModelId;
|
||||
|
||||
bool get isBusy => _isBusy;
|
||||
bool get isDownloading => _isDownloading;
|
||||
@@ -68,6 +78,29 @@ class TranslationService extends ChangeNotifier {
|
||||
int get downloadedBytes => _downloadedBytes;
|
||||
int? get downloadTotalBytes => _downloadTotalBytes;
|
||||
String? get downloadFileName => _downloadFileName;
|
||||
|
||||
/// File being fetched. Stays set while chunks are merged, when
|
||||
/// [downloadFileName] is replaced with the merge status.
|
||||
String? get downloadTargetFileName => _downloadTargetFileName;
|
||||
|
||||
/// URL of the download in progress. Settings can rebuild the row after
|
||||
/// the page that started the download has been disposed.
|
||||
String? get downloadSourceUrl => _downloadSourceUrl;
|
||||
|
||||
/// Preset id when the download was started as a preset, otherwise null.
|
||||
String? get downloadModelId => _downloadModelId;
|
||||
|
||||
/// Error message from the most recent failed download, if any.
|
||||
String? get failedDownloadError => _failedDownloadError;
|
||||
|
||||
/// URL of the most recent failed download, if any.
|
||||
String? get failedDownloadSourceUrl => _failedDownloadSourceUrl;
|
||||
|
||||
/// Resolved filename of the most recent failed download, if any.
|
||||
String? get failedDownloadFileName => _failedDownloadFileName;
|
||||
|
||||
/// Preset id of the most recent failed download, if it was a preset.
|
||||
String? get failedDownloadModelId => _failedDownloadModelId;
|
||||
double? get downloadProgress {
|
||||
final total = _downloadTotalBytes;
|
||||
if (!_isDownloading || total == null || total <= 0) {
|
||||
@@ -195,22 +228,26 @@ class TranslationService extends ChangeNotifier {
|
||||
}
|
||||
return _runExclusive(() async {
|
||||
_setBusy(true);
|
||||
_setDownloading(true);
|
||||
_lastError = null;
|
||||
_clearFailedDownload();
|
||||
final resolvedFileName =
|
||||
fileName ??
|
||||
_sanitizeFileName(
|
||||
uri.pathSegments.isNotEmpty
|
||||
? uri.pathSegments.last
|
||||
: 'translation-model.gguf',
|
||||
);
|
||||
_downloadSourceUrl = sourceUrl;
|
||||
_downloadModelId = id;
|
||||
_downloadTargetFileName = resolvedFileName;
|
||||
_downloadFileName = resolvedFileName;
|
||||
_downloadedBytes = 0;
|
||||
_downloadTotalBytes = null;
|
||||
_cancelDownloadRequested = false;
|
||||
_setDownloading(true);
|
||||
try {
|
||||
final resolvedFileName =
|
||||
fileName ??
|
||||
_sanitizeFileName(
|
||||
uri.pathSegments.isNotEmpty
|
||||
? uri.pathSegments.last
|
||||
: 'translation-model.gguf',
|
||||
);
|
||||
_downloadFileName = resolvedFileName;
|
||||
_downloadedBytes = 0;
|
||||
_cancelDownloadRequested = false;
|
||||
|
||||
// HEAD request to check size and range support.
|
||||
final headClient = http.Client();
|
||||
final headClient = _newClient();
|
||||
int? totalSize;
|
||||
bool supportsRange = false;
|
||||
try {
|
||||
@@ -223,6 +260,10 @@ class TranslationService extends ChangeNotifier {
|
||||
headClient.close();
|
||||
}
|
||||
|
||||
if (_cancelDownloadRequested) {
|
||||
throw const TranslationDownloadCancelled();
|
||||
}
|
||||
|
||||
_downloadTotalBytes = totalSize;
|
||||
_notify();
|
||||
|
||||
@@ -260,6 +301,15 @@ class TranslationService extends ChangeNotifier {
|
||||
await _appSettingsService.setTranslationModelSourceUrl(sourceUrl);
|
||||
_failedModelPath = null;
|
||||
return record;
|
||||
} on TranslationDownloadCancelled {
|
||||
rethrow;
|
||||
} catch (error) {
|
||||
_lastError = '$error';
|
||||
_failedDownloadError = '$error';
|
||||
_failedDownloadSourceUrl = sourceUrl;
|
||||
_failedDownloadFileName = resolvedFileName;
|
||||
_failedDownloadModelId = id;
|
||||
rethrow;
|
||||
} finally {
|
||||
_setDownloading(false);
|
||||
}
|
||||
@@ -270,7 +320,7 @@ class TranslationService extends ChangeNotifier {
|
||||
required Uri uri,
|
||||
required String fileName,
|
||||
}) async {
|
||||
final client = http.Client();
|
||||
final client = _newClient();
|
||||
try {
|
||||
final response = await client.send(http.Request('GET', uri));
|
||||
if (response.statusCode < 200 || response.statusCode >= 300) {
|
||||
@@ -314,7 +364,7 @@ class TranslationService extends ChangeNotifier {
|
||||
if (start >= totalSize) break;
|
||||
final chunkPath = await _fileStore.chunkFilePath(fileName, i);
|
||||
chunkPaths.add(chunkPath);
|
||||
final client = http.Client();
|
||||
final client = _newClient();
|
||||
clients.add(client);
|
||||
futures.add(
|
||||
_downloadRange(
|
||||
@@ -701,6 +751,37 @@ class TranslationService extends ChangeNotifier {
|
||||
_notify();
|
||||
}
|
||||
|
||||
void _clearFailedDownload() {
|
||||
_failedDownloadError = null;
|
||||
_failedDownloadSourceUrl = null;
|
||||
_failedDownloadFileName = null;
|
||||
_failedDownloadModelId = null;
|
||||
}
|
||||
|
||||
/// Puts the service in the same state [downloadModel] publishes while a
|
||||
/// fetch is running, so settings can be rebuilt without a network call.
|
||||
@visibleForTesting
|
||||
void debugSetActiveDownload({
|
||||
required String sourceUrl,
|
||||
String? fileName,
|
||||
String? modelId,
|
||||
String? displayedFileName,
|
||||
int downloadedBytes = 0,
|
||||
int? totalBytes,
|
||||
}) {
|
||||
_lastError = null;
|
||||
_clearFailedDownload();
|
||||
_downloadSourceUrl = sourceUrl;
|
||||
_downloadModelId = modelId;
|
||||
_downloadTargetFileName = fileName;
|
||||
_downloadFileName = displayedFileName ?? fileName;
|
||||
_downloadedBytes = downloadedBytes;
|
||||
_downloadTotalBytes = totalBytes;
|
||||
_isBusy = true;
|
||||
_isDownloading = true;
|
||||
_notify();
|
||||
}
|
||||
|
||||
void _setDownloading(bool value) {
|
||||
_isDownloading = value;
|
||||
if (!value) {
|
||||
@@ -708,6 +789,9 @@ class TranslationService extends ChangeNotifier {
|
||||
_downloadedBytes = 0;
|
||||
_downloadTotalBytes = null;
|
||||
_downloadFileName = null;
|
||||
_downloadTargetFileName = null;
|
||||
_downloadSourceUrl = null;
|
||||
_downloadModelId = null;
|
||||
}
|
||||
_notify();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user