After adding the fonctionality for apk publish10

This commit is contained in:
Your Name 2026-10-31 15:21:54 +02:00
parent 2891329e8f
commit 21502e4403
4 changed files with 139 additions and 26 deletions

View File

@ -2,6 +2,7 @@ import 'dart:io';
import 'package:crypto/crypto.dart';
import 'package:dio/dio.dart';
import 'package:native_dio_adapter/native_dio_adapter.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
@ -174,6 +175,33 @@ class AppUpdateRepository {
.toString();
}
String _absoluteDownloadUrl(String target) {
if (target.startsWith('http://') || target.startsWith('https://')) return target;
final base = _api.baseUrl.replaceAll(RegExp(r'/+$'), '');
final path = target.startsWith('/') ? target : '/$target';
return '$base$path';
}
/// Dedicated client: Chromium Cronet (Android) — much closer to browser download speed
/// than Dart's default HttpClient, and without API JSON/auth interceptors.
Dio _createNativeDownloadClient() {
final dio = Dio(
BaseOptions(
connectTimeout: const Duration(seconds: 30),
receiveTimeout: const Duration(minutes: 45),
sendTimeout: const Duration(minutes: 2),
followRedirects: true,
maxRedirects: 5,
headers: {
Headers.acceptHeader: 'application/vnd.android.package-archive,*/*',
},
validateStatus: (s) => s != null && s < 400,
),
);
dio.httpClientAdapter = NativeAdapter();
return dio;
}
/// Downloads APK to temp dir, verifies size + SHA-256, returns local file.
Future<File> downloadApk(
AppUpdateCheckResult update, {
@ -181,43 +209,35 @@ class AppUpdateRepository {
CancelToken? cancelToken,
}) async {
final target = resolveDownloadTarget(update);
final absoluteUrl = _absoluteDownloadUrl(target);
final dir = await getTemporaryDirectory();
final file = File('${dir.path}/tloto-update-${update.versionCode}.apk');
if (await file.exists()) {
await file.delete();
}
final releaseId = _releaseIdFromUrl(target) ?? _releaseIdFromUrl(update.downloadUrl ?? '');
final releaseId = _releaseIdFromUrl(absoluteUrl) ?? _releaseIdFromUrl(update.downloadUrl ?? '');
await reportEvent(eventType: 'download_start', releaseId: releaseId, versionCode: update.versionCode);
final downloadDio = _createNativeDownloadClient();
try {
// Relative paths use the app's API base (same fast path as the website).
await _api.dio.download(
target,
await downloadDio.download(
absoluteUrl,
file.path,
cancelToken: cancelToken,
onReceiveProgress: onProgress,
deleteOnError: true,
lengthHeader: Headers.contentLengthHeader,
options: Options(
// Large APKs on mobile uplinks — keep receiving as long as bytes arrive.
receiveTimeout: const Duration(minutes: 45),
sendTimeout: const Duration(minutes: 2),
connectTimeout: const Duration(seconds: 30),
followRedirects: true,
maxRedirects: 5,
responseType: ResponseType.stream,
headers: {
// Avoid JSON Accept confusing some proxies for binary streams.
Headers.acceptHeader: 'application/vnd.android.package-archive,*/*',
},
extra: {'skipAuthRefresh': true},
validateStatus: (s) => s != null && s < 400,
),
);
} on DioException catch (e) {
await reportEvent(eventType: 'download_fail', releaseId: releaseId, versionCode: update.versionCode);
throw AppException.fromDio(e, fallback: 'Download failed');
} finally {
downloadDio.close(force: true);
}
final bytes = await file.length();

View File

@ -49,6 +49,8 @@ class _AppUpdateDialogState extends State<_AppUpdateDialog> {
double? _progress;
String? _error;
CancelToken? _cancelToken;
DateTime? _lastProgressUiAt;
int _lastProgressPercent = -1;
@override
void dispose() {
@ -56,11 +58,29 @@ class _AppUpdateDialogState extends State<_AppUpdateDialog> {
super.dispose();
}
void _onDownloadProgress(int received, int total) {
if (!mounted || total <= 0) return;
final pct = ((received / total) * 100).floor();
final now = DateTime.now();
// setState on every socket chunk stalls the UI isolate and slows transfers.
if (pct < 100 &&
pct == _lastProgressPercent &&
_lastProgressUiAt != null &&
now.difference(_lastProgressUiAt!) < const Duration(milliseconds: 250)) {
return;
}
_lastProgressPercent = pct;
_lastProgressUiAt = now;
setState(() => _progress = received / total);
}
Future<void> _downloadAndInstall() async {
setState(() {
_downloading = true;
_error = null;
_progress = 0;
_lastProgressPercent = -1;
_lastProgressUiAt = null;
});
_cancelToken = CancelToken();
// Keep the screen on — Android sleep commonly aborts long APK downloads.
@ -69,15 +89,7 @@ class _AppUpdateDialogState extends State<_AppUpdateDialog> {
final file = await widget.repository.downloadApk(
widget.update,
cancelToken: _cancelToken,
onProgress: (received, total) {
if (!mounted) return;
if (total > 0) {
setState(() => _progress = received / total);
} else if (received > 0) {
// Server omitted Content-Length — show indeterminate progress still moving.
setState(() => _progress = null);
}
},
onProgress: _onDownloadProgress,
);
if (!mounted) return;
final open = await OpenFilex.open(file.path, type: 'application/vnd.android.package-archive');

View File

@ -105,6 +105,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.0"
cronet_http:
dependency: transitive
description:
name: cronet_http
sha256: "7f8976ce1531399e42b4c5ad0937bd57bd93fbcff564f7d51c695be19850e977"
url: "https://pub.dev"
source: hosted
version: "1.10.1"
cross_file:
dependency: transitive
description:
@ -129,6 +137,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.2"
cupertino_http:
dependency: transitive
description:
name: cupertino_http
sha256: "82cbec60c90bf785a047a9525688b6dacac444e177e1d5a5876963d3c50369e8"
url: "https://pub.dev"
source: hosted
version: "2.4.0"
cupertino_icons:
dependency: "direct main"
description:
@ -415,6 +431,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.1.2"
http_profile:
dependency: transitive
description:
name: http_profile
sha256: "7e679e355b09aaee2ab5010915c932cce3f2d1c11c3b2dc177891687014ffa78"
url: "https://pub.dev"
source: hosted
version: "0.1.0"
image:
dependency: transitive
description:
@ -500,6 +524,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.20.2"
jni:
dependency: transitive
description:
name: jni
sha256: d8df6dab6847855512fe44dd9032b0bbe34dda7bf7597e41d30750879779813f
url: "https://pub.dev"
source: hosted
version: "1.1.1"
jni_flutter:
dependency: transitive
description:
name: jni_flutter
sha256: "0dd33d67797560f66e961a6053882d4df54bd345bf063b8fe7ea133f7ad8b191"
url: "https://pub.dev"
source: hosted
version: "1.0.4+1"
jni_util:
dependency: transitive
description:
name: jni_util
sha256: "1ba86da04a5f2bf18fde2edb235587e70c5b0fc5bd4ba955f46b00942c3fc35f"
url: "https://pub.dev"
source: hosted
version: "1.0.0"
js:
dependency: transitive
description:
@ -588,6 +636,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.0"
native_dio_adapter:
dependency: "direct main"
description:
name: native_dio_adapter
sha256: "7ca3d04c76095a02c3b0d2e6f3c90e7781373671f7aacad34b81f31074a7369b"
url: "https://pub.dev"
source: hosted
version: "1.8.0"
nested:
dependency: transitive
description:
@ -604,6 +660,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.6.0"
objective_c:
dependency: transitive
description:
name: objective_c
sha256: "1f81ed9e41909d44162d7ec8663b2c647c202317cc0b56d3d56f6a13146a0b64"
url: "https://pub.dev"
source: hosted
version: "9.1.0"
open_filex:
dependency: "direct main"
description:
@ -612,6 +676,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.0.0"
package_config:
dependency: transitive
description:
name: package_config
sha256: f096c55ebb7deb7e384101542bfba8c52696c1b56fca2eb62827989ef2353bbc
url: "https://pub.dev"
source: hosted
version: "2.2.0"
package_info_plus:
dependency: "direct main"
description:
@ -732,6 +804,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "6.1.5+1"
pub_semver:
dependency: transitive
description:
name: pub_semver
sha256: "261236774e8b1d69cfc6b9eabbc96c40f25e7a2d6b171f3385d4f65d5734fb24"
url: "https://pub.dev"
source: hosted
version: "2.2.1"
qr:
dependency: transitive
description:
@ -1051,4 +1131,4 @@ packages:
version: "3.1.3"
sdks:
dart: ">=3.9.2 <4.0.0"
flutter: ">=3.35.0"
flutter: ">=3.35.6"

View File

@ -1,7 +1,7 @@
name: prediqo_mobile
description: "Tloto mobile app - prediction games and crypto wallet."
publish_to: 'none'
version: 2.0.2+6
version: 2.0.2+7
environment:
sdk: ^3.9.2
@ -34,6 +34,7 @@ dependencies:
connectivity_plus: ^7.3.2
crypto: ^3.0.8
open_filex: ^5.0.0
native_dio_adapter: ^1.8.0
dev_dependencies:
flutter_test: