Skip to content

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

Let any exercise use your own photo, GIF or video

InlitXa616fd590a1e
diff --git a/lib/models/exercise.dart b/lib/models/exercise.dart
index 4ca5503..8ca68da 100644
--- a/lib/models/exercise.dart
+++ b/lib/models/exercise.dart
@@ -17,7 +17,6 @@ class Exercise {
required this.difficulty,
required this.art,
required this.steps,
- this.media = '',
});
final String id;
final String name;
@@ -27,19 +26,6 @@ class Exercise {
final String difficulty;
final String art;
final List<String> steps;
- final String media;
-
- Exercise copyWith({String? media}) => Exercise(
- id: id,
- name: name,
- primary: primary,
- secondary: secondary,
- equipment: equipment,
- difficulty: difficulty,
- art: art,
- steps: steps,
- media: media ?? this.media,
- );
Map<String, dynamic> toJson() => {
'id': id,
@@ -47,7 +33,6 @@ class Exercise {
'p': primary,
'e': equipment,
'd': difficulty,
- if (media.isNotEmpty) 'm': media,
};
factory Exercise.fromJson(Map<String, dynamic> j) => Exercise(
id: j['id'] as String,
@@ -58,7 +43,6 @@ class Exercise {
difficulty: (j['d'] as String?) ?? 'Beginner',
art: '',
steps: const [],
- media: (j['m'] as String?) ?? '',
);
}
diff --git a/lib/services/media_store.dart b/lib/services/media_store.dart
index e5d42c1..00becef 100644
--- a/lib/services/media_store.dart
+++ b/lib/services/media_store.dart
@@ -1,5 +1,6 @@
import 'dart:io';
+import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
class MediaStore {
@@ -20,23 +21,45 @@ class MediaStore {
static bool get ready => _dir != null;
+ @visibleForTesting
+ static set directory(String? path) => _dir = path;
+
static String? pathFor(String basename) =>
(_dir == null || basename.isEmpty) ? null : '$_dir/$basename';
static Future<String?> importFor(String exerciseId, String srcPath) async {
if (_dir == null) return null;
try {
- final ext = _ext(srcPath);
+ final base = _newName(exerciseId, extOf(srcPath));
+ await File(srcPath).copy('$_dir/$base');
+ return base;
+ } catch (_) {
+ return null;
+ }
+ }
- final base = '$exerciseId-${DateTime.now().millisecondsSinceEpoch}.$ext';
- final dst = File('$_dir/$base');
- await File(srcPath).copy(dst.path);
+ static Future<String?> saveBytes(String exerciseId, String ext, Uint8List bytes) async {
+ if (_dir == null) return null;
+ try {
+ final base = _newName(exerciseId, ext);
+ await File('$_dir/$base').writeAsBytes(bytes, flush: true);
return base;
} catch (_) {
return null;
}
}
+ static Future<Uint8List?> readBytes(String basename) async {
+ final p = pathFor(basename);
+ if (p == null) return null;
+ try {
+ final f = File(p);
+ return await f.exists() ? await f.readAsBytes() : null;
+ } catch (_) {
+ return null;
+ }
+ }
+
static Future<void> delete(String basename) async {
if (_dir == null || basename.isEmpty) return;
try {
@@ -45,14 +68,27 @@ class MediaStore {
} catch (_) {}
}
- static String _ext(String path) {
+ static Future<void> clearAll() async {
+ if (_dir == null) return;
+ try {
+ for (final f in Directory(_dir!).listSync()) {
+ if (f is File) await f.delete();
+ }
+ } catch (_) {}
+ }
+
+ static int _seq = 0;
+
+ static String _newName(String exerciseId, String ext) =>
+ '$exerciseId-${DateTime.now().microsecondsSinceEpoch}-${_seq++}.$ext';
+
+ static String extOf(String path) {
final dot = path.lastIndexOf('.');
if (dot < 0 || dot == path.length - 1) return 'dat';
return path.substring(dot + 1).toLowerCase();
}
- static bool isVideo(String basename) {
- const vids = {'mp4', 'mov', 'm4v', 'webm', 'mkv', '3gp', 'avi'};
- return vids.contains(_ext(basename));
- }
+ static const videoExtensions = {'mp4', 'mov', 'm4v', 'webm', 'mkv', '3gp', 'avi'};
+
+ static bool isVideo(String basename) => videoExtensions.contains(extOf(basename));
}
diff --git a/lib/widgets/exercise_media.dart b/lib/widgets/exercise_media.dart
index a4224af..ea33150 100644
--- a/lib/widgets/exercise_media.dart
+++ b/lib/widgets/exercise_media.dart
@@ -6,6 +6,7 @@ import 'package:video_player/video_player.dart';
import '../models/exercise.dart';
import '../services/media_store.dart';
+import '../state/fit_state.dart';
import '../theme/app_colors.dart';
import 'exercise_art.dart';
@@ -25,11 +26,12 @@ class ExerciseMedia extends StatelessWidget {
@override
Widget build(BuildContext context) {
- final path = ex.media.isEmpty ? null : MediaStore.pathFor(ex.media);
+ final media = fit.mediaFor(ex.id);
+ final path = media.isEmpty ? null : MediaStore.pathFor(media);
if (path == null) {
return ExerciseArt(slug: ex.art, height: height, radius: radius, live: live);
}
- final isVideo = MediaStore.isVideo(ex.media);
+ final isVideo = MediaStore.isVideo(media);
if (isVideo && live) {
return _VideoTile(key: ValueKey(path), path: path, height: height, radius: radius);
}
@@ -41,7 +43,7 @@ class ExerciseMedia extends StatelessWidget {
: Center(
child: Image.file(
File(path),
- key: ValueKey(ex.media),
+ key: ValueKey(media),
fit: BoxFit.contain,
alignment: Alignment.center,
gaplessPlayback: true,