screen-locker/stronglift_replacement/workout_app/lib/services/sync_service.dart
Krzysztof kuhy Rudnicki 8e52688138
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (flutter, flutter-analyze, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,flutter-analyze) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / lock-popup-safety (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (0) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (1) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (2) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (3) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (4) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (5) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (6) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (7) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (8) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (9) (push) Waiting to run
Tests / flutter-coverage (push) Blocked by required conditions
Tests / web-coverage (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Key workout_app /sdcard paths on package name
A slot build (com.kuhy.slotN) shared /sdcard/WorkoutTracker and
workout_result.json with the daily build. MainActivity now reports the
package name; any package but com.kuhy.workout_app gets sibling paths
and never reads RunnerUp's exports. The daily build's paths are
unchanged, so the PC side needs nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:47:49 +01:00

87 lines
3.1 KiB
Dart

/// Writes workout result JSON to external storage (ADB) and the HTTP server.
library;
import 'dart:io';
import 'package:flutter/foundation.dart' show debugPrint;
import 'package:path_provider/path_provider.dart';
import 'package:workout_app/models/workout_session.dart';
import 'package:workout_app/services/http_server_service.dart';
/// Path on the phone's external storage where the PC reads workout data.
const String kSyncFilePath = '/sdcard/workout_result.json';
/// Handles writing completed workout sessions to disk and the HTTP server.
class SyncService {
/// Where the result file is written; the sandbox flavor points this at
/// its own file so the PC's ADB pull never sees a sandbox workout, and
/// `main` keys it on packageName for any other install (shared_storage.dart).
static String filePath = kSyncFilePath;
/// Writes [session] as JSON to external storage and updates the HTTP server.
///
/// Falls back to app-external directory if /sdcard/ is not writable.
Future<SyncResult> writeWorkoutResult(WorkoutSession session) async {
final json = session.toJsonString();
// Always update the in-app HTTP server so the PC can read via WiFi.
HttpServerService.instance.latestWorkout = json;
// Try the primary path first (/sdcard/ — ADB-accessible without root).
try {
final file = File(filePath);
await file.writeAsString(json);
return SyncResult(success: true, path: filePath);
} on Exception catch (error) {
// Fallback: app-specific external directory (still ADB accessible).
debugPrint(
'WorkoutApp: could not write $filePath ($error) — trying the '
'app-specific external directory next.',
);
}
// External storage is Android-only; getExternalStorageDirectory() throws
// an UnsupportedError (not an Exception) elsewhere, so guard the call.
// coverage:ignore-start
// Android-only: getExternalStorageDirectory is unavailable on the Linux
// test host, so this block can't be exercised in CI.
if (Platform.isAndroid) {
try {
final dir = await getExternalStorageDirectory();
if (dir != null) {
final file = File('${dir.path}/workout_result.json');
await file.writeAsString(json);
return SyncResult(success: true, path: file.path);
}
} on Exception catch (error) {
// Fallback failed.
debugPrint(
'WorkoutApp: the external-storage fallback failed too ($error) — '
'this workout will NOT be readable over ADB/HTTP.',
);
}
}
// coverage:ignore-end
return const SyncResult(
success: false,
path: null,
error: 'No writable external path',
);
}
}
/// Result of a [SyncService.writeWorkoutResult] call.
class SyncResult {
/// Creates a sync result.
const SyncResult({required this.success, required this.path, this.error});
/// Whether the write succeeded.
final bool success;
/// Absolute path where the file was written, or null on failure.
final String? path;
/// Human-readable error message on failure.
final String? error;
}