Skip to content

Offline gym log for Android. No account, no ads, no internet.

Cross-platform file sharing and images

InlitXeb1d4b9784d4
diff --git a/lib/services/backup_zip.dart b/lib/services/backup_zip.dart
index cbe0603..407e459 100644
--- a/lib/services/backup_zip.dart
+++ b/lib/services/backup_zip.dart
@@ -3,6 +3,7 @@ import 'dart:io';
import 'dart:typed_data';
import 'package:archive/archive.dart';
+import 'package:gymmane_merge/gymmane_merge.dart';
import '../models/exercise.dart';
import '../models/nutrition.dart';
@@ -89,6 +90,7 @@ Future<Uint8List> buildBackupZip() async {
for (final (photo, label) in [
for (final f in fit.foods) (f.photo, f.name),
for (final s in fit.supplements) (s.photo, s.name),
+ for (final r in fit.recipes) (r.photo, r.name),
]) {
if (photo.isEmpty || isAssetPhoto(photo) || foodMedia.containsKey(photo)) continue;
final bytes = await MediaStore.readBytes(photo);
@@ -270,6 +272,70 @@ Future<bool> restoreBackupZip(Uint8List zipBytes) async {
return true;
}
+Future<bool?> mergeBackupZip(Uint8List zipBytes, {required bool incomingNewer}) async {
+ final Archive archive;
+ final Map<String, dynamic> incoming;
+ try {
+ archive = ZipDecoder().decodeBytes(zipBytes);
+ final jsonFile = _findJson(archive);
+ if (jsonFile == null) return null;
+ final decoded = jsonDecode(utf8.decode(jsonFile.readBytes() ?? const []));
+ if (decoded is! Map<String, dynamic>) return null;
+ incoming = decoded;
+ } catch (_) {
+ return null;
+ }
+ fit.persistNow();
+ final local = jsonDecode(jsonEncode(Store.finite(fit.toJson()))) as Map<String, dynamic>;
+ final have = MediaStore.names();
+ final added = <String>[];
+
+ final localEx = (local['media'] as Map?) ?? const {};
+ final exMedia = <String, String>{};
+ for (final e in ((incoming['media'] as Map?) ?? const {}).entries) {
+ final id = e.key;
+ final path = e.value;
+ if (id is! String || path is! String || localEx.containsKey(id)) continue;
+ final bytes = archive.findFile(path)?.readBytes();
+ if (bytes == null) continue;
+ final base = await MediaStore.saveBytes(id, MediaStore.extOf(path), bytes);
+ if (base == null) continue;
+ exMedia[id] = base;
+ added.add(base);
+ }
+ incoming['media'] = exMedia;
+
+ final files = <String, String>{};
+ for (final g in mediaGroups) {
+ final map = incoming[g];
+ if (map is! Map) continue;
+ map.forEach((k, v) {
+ if (k is String && v is String && v.isNotEmpty) files[k] = v;
+ });
+ }
+
+ final merged = mergeStates(local, incoming, incomingNewer: incomingNewer);
+ if (sameState(local, merged) && added.isEmpty) return false;
+
+ final used = stringsIn(merged);
+ final renames = <String, String>{};
+ for (final e in files.entries) {
+ if (have.contains(e.key) || !used.contains(e.key)) continue;
+ final bytes = archive.findFile(e.value)?.readBytes();
+ if (bytes == null) continue;
+ final base = await MediaStore.saveBytes('sync', MediaStore.extOf(e.value), bytes);
+ if (base == null) continue;
+ renames[e.key] = base;
+ added.add(base);
+ }
+ final out = renameStrings(merged, renames) as Map<String, dynamic>;
+ if (fit.tryApplyBackup(out)) return true;
+ for (final name in added) {
+ await MediaStore.delete(name);
+ }
+ return null;
+}
+
ArchiveFile? _findJson(Archive a) {
ArchiveFile? fallback;
for (final f in a.files) {
diff --git a/lib/services/file_share.dart b/lib/services/file_share.dart
new file mode 100644
index 0000000..756acd4
--- /dev/null
+++ b/lib/services/file_share.dart
@@ -0,0 +1,39 @@
+import 'dart:convert';
+import 'dart:io';
+
+import 'package:flutter/foundation.dart';
+import 'package:path_provider/path_provider.dart';
+import 'package:share_plus/share_plus.dart';
+
+import 'server/transport_stub.dart' if (dart.library.js_interop) 'server/transport_web.dart' as net;
+
+class ShareItem {
+ const ShareItem(this.name, this.bytes, {this.mime});
+
+ final String name;
+ final Uint8List bytes;
+ final String? mime;
+}
+
+Future<void> shareFiles(List<ShareItem> items, {String? subject, String? text}) async {
+ if (items.isEmpty) return;
+ if (kIsWeb) {
+ for (final f in items) {
+ net.download(f.name, f.bytes, f.mime ?? 'application/octet-stream');
+ }
+ return;
+ }
+ final dir = await getTemporaryDirectory();
+ final files = <XFile>[];
+ for (final f in items) {
+ final file = File('${dir.path}/${f.name}');
+ await file.writeAsBytes(f.bytes, flush: true);
+ files.add(XFile(file.path, mimeType: f.mime));
+ }
+ await SharePlus.instance.share(ShareParams(files: files, subject: subject, text: text));
+}
+
+Future<void> shareFile(String name, Uint8List bytes, {String? mime, String? subject, String? text}) =>
+ shareFiles([ShareItem(name, bytes, mime: mime)], subject: subject, text: text);
+
+Uint8List utf8Bytes(String text) => Uint8List.fromList(utf8.encode(text));
diff --git a/lib/services/media_store.dart b/lib/services/media_store.dart
index a026873..7c3f450 100644
--- a/lib/services/media_store.dart
+++ b/lib/services/media_store.dart
@@ -3,6 +3,8 @@ import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
+import 'server/server_link.dart';
+
class MediaStore {
MediaStore._();
@@ -19,15 +21,24 @@ class MediaStore {
}
}
- static bool get ready => _dir != null;
+ static bool get remote => kIsWeb && ServerLink.instance.available;
+
+ static bool get ready => _dir != null || remote;
@visibleForTesting
static set directory(String? path) => _dir = path;
- static String? pathFor(String basename) =>
- (_dir == null || basename.isEmpty) ? null : '$_dir/$basename';
+ static String? pathFor(String basename) {
+ if (basename.isEmpty) return null;
+ if (remote) return ServerLink.instance.mediaUrl(basename);
+ return _dir == null ? null : '$_dir/$basename';
+ }
static Future<String?> importFor(String exerciseId, String srcPath) async {
+ if (remote) {
+ final bytes = await ServerLink.instance.fetchBytes(srcPath);
+ return bytes == null ? null : saveBytes(exerciseId, extOf(srcPath), bytes);
+ }
if (_dir == null) return null;
try {
final base = _newName(exerciseId, extOf(srcPath));
@@ -39,6 +50,10 @@ class MediaStore {
}
static Future<String?> saveBytes(String exerciseId, String ext, Uint8List bytes) async {
+ if (remote) {
+ final base = _newName(exerciseId, ext);
+ return await ServerLink.instance.putMedia(base, bytes) ? base : null;
+ }
if (_dir == null) return null;
try {
final base = _newName(exerciseId, ext);
@@ -50,6 +65,7 @@ class MediaStore {
}
static Future<Uint8List?> readBytes(String basename) async {
+ if (remote) return basename.isEmpty ? null : ServerLink.instance.getMedia(basename);
final p = pathFor(basename);
if (p == null) return null;
try {
@@ -61,6 +77,7 @@ class MediaStore {
}
static Future<void> delete(String basename) async {
+ if (remote && basename.isNotEmpty) return ServerLink.instance.deleteMedia(basename);
if (_dir == null || basename.isEmpty) return;
try {
final f = File('$_dir/$basename');
diff --git a/lib/widgets/file_thumb.dart b/lib/widgets/file_thumb.dart
index 4400a18..978daac 100644
--- a/lib/widgets/file_thumb.dart
+++ b/lib/widgets/file_thumb.dart
@@ -1,7 +1,7 @@
-import 'dart:io';
import 'dart:math' as math;
import 'package:flutter/widgets.dart';
+import 'local_image.dart';
class FileThumb extends StatelessWidget {
const FileThumb(
@@ -33,7 +33,7 @@ class FileThumb extends StatelessWidget {
final h = height != null && height!.isFinite ? height! : (box.maxHeight.isFinite ? box.maxHeight : screen.height);
final side = _side(context, w, h);
return Image(
- image: ResizeImage(FileImage(File(path)), width: side, height: side, policy: ResizeImagePolicy.fit),
+ image: ResizeImage(localImage(path), width: side, height: side, policy: ResizeImagePolicy.fit),
fit: fit,
alignment: alignment,
width: width,
diff --git a/lib/widgets/local_image.dart b/lib/widgets/local_image.dart
new file mode 100644
index 0000000..3e74402
--- /dev/null
+++ b/lib/widgets/local_image.dart
@@ -0,0 +1,11 @@
+import 'dart:io';
+
+import 'package:flutter/foundation.dart';
+import 'package:flutter/widgets.dart';
+import 'package:video_player/video_player.dart';
+
+ImageProvider localImage(String path) => kIsWeb ? NetworkImage(path) : FileImage(File(path));
+
+VideoPlayerController localVideo(String path, {VideoPlayerOptions? options}) => kIsWeb
+ ? VideoPlayerController.networkUrl(Uri.parse(path), videoPlayerOptions: options)
+ : VideoPlayerController.file(File(path), videoPlayerOptions: options);