Skip to content

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

Import from openGym and from any CSV with date, exercise, reps and weight (#20, #35)

InlitX6d2cadff75fa
diff --git a/lib/catalog/opengym_ids.dart b/lib/catalog/opengym_ids.dart
new file mode 100644
index 0000000..eaaceb8
--- /dev/null
+++ b/lib/catalog/opengym_ids.dart
@@ -0,0 +1,373 @@
+const Map<String, String> kOpenGymIds = {
+ '0001': '2gPfomN',
+ '0002': 'Hy9D21L',
+ '0003': 'assault-bike',
+ '0006': 'qaZVsGk',
+ '0007': '4IKbhHV',
+ '3293': '72BC5Za',
+ '3294': 'A9qxk2F',
+ '2355': 'uWpxD4v',
+ '2333': 'PXTIwgu',
+ '0009': 'PAgTVaK',
+ '0015': 'vrhHa6D',
+ '0017': 'kiJ4Z2K',
+ '1431': '7OeHptV',
+ '1432': 'f4xtKBj',
+ '0019': 'J60bN17',
+ '3297': 'GaSzzuh',
+ '1473': 'SaDOwk7',
+ '0023': 'Yza7XrQ',
+ '0024': 'Y7YcmIJ',
+ '0025': 'EIeI8Vf',
+ '0026': 'W9pFVv1',
+ '1316': 'cA9FuWG',
+ '0027': 'eZyBC3j',
+ '2407': 'aee2Fcj',
+ '0028': 'SGY8Zui',
+ '0029': 'qi996YS',
+ '0030': 'J6Dx1Mu',
+ '0031': '25GPyDY',
+ '0032': 'ila4NZS',
+ '0033': 'GrO65fd',
+ '0034': 'hMEptv0',
+ '0035': 'LMGXZn8',
+ '1255': '9sgNE2O',
+ '0036': 'hl8DUh8',
+ '0037': 'Hj4FOCd',
+ '0038': 'IENzBdA',
+ '1370': '2IHEa2T',
+ '0039': 'IeTIEqg',
+ '0041': 'b2Uoz54',
+ '0040': '33AzZeV',
+ '0042': 'zG0zs85',
+ '0043': 'qXTaZnJ',
+ '1461': 'DhMl549',
+ '1545': 'vR1vold',
+ '1409': 'qKBpF7I',
+ '0044': 'XlZ4lAC',
+ '0045': 'GXoaSgn',
+ '0046': '5VCj6iH',
+ '0047': '3TZduzM',
+ '1719': 'gx7s7uF',
+ '0048': '641mIfk',
+ '0049': 'dmgMp3n',
+ '0050': 'xi0yckC',
+ '0052': 'ZsiqXYa',
+ '1720': 'yg8Totb',
+ '0059': 'SYJ4Bkt',
+ '0061': 'iZop9xO',
+ '0067': 'xHKN2s8',
+ '0068': 'uKyN64F',
+ '0069': 'gfk9kD4',
+ '1411': 'yzYH9pI',
+ '1412': 'SJAA2IQ',
+ '3017': 'r0z6xzQ',
+ '0070': 'qOgPVf6',
+ '0071': 'wnEscH8',
+ '0072': 'WLvTAv5',
+ '0073': 'i6LWjok',
+ '0022': 'znLogoF',
+ '0075': 'Ln9iTbU',
+ '0076': 'S9zHIvU',
+ '0079': 'qDnGfDb',
+ '0080': 'xNrS20v',
+ '0081': '4LIG9xr',
+ '0082': 'LsZkfU6',
+ '0084': '7M66AVi',
+ '0083': 'Gxg9lDc',
+ '0085': 'wQ2c4XD',
+ '0086': 'ngPpyRS',
+ '0087': '0dCyly0',
+ '0088': 'ktsFQAZ',
+ '1371': 'ktsFQAZ',
+ '0089': '1V1gj1u',
+ '0091': 'kTbSH9h',
+ '0094': 'dFSNDOA',
+ '0095': 'dG7tG5y',
+ '0096': 'i4JkUaL',
+ '0098': 'W31mMjd',
+ '0099': 'gGNQmVt',
+ '2799': 'G7xoEzr',
+ '2810': 'HBYyX94',
+ '0102': 'oR7O9LW',
+ '2798': 'RYcV1kH',
+ '0103': 'xnInPfE',
+ '0104': '2qTvJAZ',
+ '1372': '8ozhUIZ',
+ '0106': '4dUn2iv',
+ '0108': 'rGwhJ5o',
+ '0111': '6HiHHe0',
+ '1457': 'Kyd9Rz5',
+ '0116': 'hrVQWvE',
+ '0117': 'KgI0tqW',
+ '0122': 'JsKq9so',
+ '0124': 's7HX1BY',
+ '0126': '82LxxkW',
+ '3360': 'bear-crawl',
+ '0129': 'RrLske5',
+ '1399': '9RT8oQW',
+ '0130': 'u27Kcdz',
+ '3019': 'mExgrF9',
+ '1770': 'sJFIDIp',
+ '0139': '50BETrz',
+ '0140': 'guT8YnS',
+ '0137': 'U6G2gk9',
+ '3543': 'wfotm7S',
+ '3544': '5VXmnV5',
+ '1771': 's0HKO2I',
+ '1769': 'gscGLOU',
+ '3168': '3xK09Sk',
+ '1373': 'bJYHBIN',
+ '3156': 'v2DfH14',
+ '3158': 'tig3PXb',
+ '3162': 'xbkPfaw',
+ '1374': 'iPm26QU',
+ '1160': 'burpee',
+ '0148': 'KHPZL0b',
+ '0149': 'Gchi5Tr',
+ '3235': 'zHEpuuc',
+ '0150': 'eYnzaCm',
+ '0151': '7xI5MXA',
+ '1631': 'NvfE43H',
+ '0152': 'Db7eEgw',
+ '0153': 'OQ1otBN',
+ '0154': 'aqvSOQE',
+ '0155': '0CXGHya',
+ '0868': 'G08RZcQ',
+ '0157': 'eGDudUV',
+ '0158': '7saC5zz',
+ '1261': '2Pya1cP',
+ '0159': 'kesXOpB',
+ '1632': 'dXz8zjF',
+ '0160': 'veXwo0D',
+ '0161': 'hvHhCv8',
+ '0162': 'u2X71Np',
+ '0164': 'mTT3KLn',
+ '0165': 'HPlPoQA',
+ '1722': '1xHyxys',
+ '0167': 'ZSJNetl',
+ '0168': 'hBGWILP',
+ '0169': 'Vh0GsK4',
+ '1318': 'yaMIo4D',
+ '0171': 'tBWXbIT',
+ '0173': 'Hx1WC8I',
+ '0174': 'MvQPqVW',
+ '0860': 'HEJ6DIX',
+ '0175': 'WW95auq',
+ '0178': 'goJ6ezq',
+ '1634': 'otqIxU4',
+ '0182': '61GrD55',
+ '0194': '2IxROQ1',
+ '0196': 'OM46QHm',
+ '0198': 'RVwzP10',
+ '0201': '3ZflifB',
+ '0202': 'yUdIGNs',
+ '0873': 'RqOtqD7',
+ '0210': 'eYmsEPR',
+ '0211': 'd9Xaxq6',
+ '0212': '8xUv4J7',
+ '0861': 'fUBheHs',
+ '2399': 'UEjSrKI',
+ '0220': 'Eg98Ft9',
+ '0222': 'wPypxFY',
+ '0224': 'VhX2JdE',
+ '1375': 'yl2IYyy',
+ '0226': 'jpgqxiS',
+ '0228': 'Kpajagk',
+ '0247': 'LrV4s90',
+ '0251': '9WTm7dq',
+ '1430': 'XgWyAiA',
+ '2462': 'LQFOrMn',
+ '1326': 'T2mxWqc',
+ '1273': 'wigSg76',
+ '0258': 'CMAxnsG',
+ '0259': 'x6KpKpq',
+ '0274': 'TFqbd8t',
+ '0276': 'dead-bug',
+ '0279': 'decline-push-up',
+ '1274': 'vptOQ4N',
+ '0284': 'u5ESqzH',
+ '1646': 'fy7Tgy4',
+ '1647': 'NlfIbzq',
+ '1648': '6em2Dxj',
+ '0286': 'izMnLqz',
+ '1649': 'Zwiw7XR',
+ '1650': 'J74XlNf',
+ '2137': 'Xy4jlWA',
+ '0287': 'eOrFCnx',
+ '0288': 'vi8EhoE',
+ '0289': 'SpYC0Kp',
+ '0290': '3d7wHyd',
+ '0291': 'mnzcrIB',
+ '0293': 'BJ0Hz5L',
+ '1651': '1VpF8db',
+ '1652': '2NImIAG',
+ '0294': 'NbVPDMW',
+ '0295': '7Hg55JG',
+ '1731': '7jGOBF3',
+ '0296': '7jGOBF3',
+ '0297': 'gvsWLQw',
+ '3635': 'ecl28tP',
+ '0299': 'QfAKy1G',
+ '0300': 'nUwVh7b',
+ '0301': 'DwhEmmE',
+ '0302': 'xXm4nYq',
+ '0303': '1qrWgZ2',
+ '1617': 'SHUMp5H',
+ '0305': 'cwsAI4G',
+ '0306': 'OTgkHwR',
+ '0307': 'reFHapa',
+ '1437': 'mtXengz',
+ '0308': 'yz9nUhF',
+ '1732': 'XalXcvM',
+ '0310': '3eGE2JC',
+ '0309': 'Rr7S3yg',
+ '0311': 'AQ0mC4Y',
+ '1760': 'yn8yg1r',
+ '0313': 'slDvUAU',
+ '0314': 'ns0SIbU',
+ '1618': 'wkgnGfb',
+ '0323': 'aTNKZiC',
+ '0325': 'nxW6BkN',
+ '0327': '7vG5o25',
+ '0328': '6e2DcYX',
+ '0329': 'JymLInS',
+ '3541': 'PbzNu7c',
+ '0334': 'DsgkuIt',
+ '0336': 'RRWFUcw',
+ '0339': 'FkBIE6a',
+ '0347': 'mym4hJo',
+ '2705': '7RWNjiB',
+ '1328': 'XUUD0Fs',
+ '0349': 'M2Pm3zj',
+ '2706': 'rEhi2o5',
+ '3888': '6pTkI99',
+ '1329': 'wt6rwjk',
+ '0371': 'S4pwGlc',
+ '0375': '9XjtHvS',
+ '0380': 'v1qBec9',
+ '0383': 'EAs3xL9',
+ '1330': 'ZIViNh1',
+ '1331': '9pQSkH8',
+ '1379': 'r29jP7S',
+ '1380': 'FxhcxUW',
+ '1381': 'VW88JNd',
+ '0406': 'NJzBsGJ',
+ '0407': 'IpONWYv',
+ '0409': '1kB3Wmk',
+ '0410': 'qx4fgX7',
+ '0417': 'dPmaUaU',
+ '0426': 'A6wtbuL',
+ '0430': 'PdmaD0N',
+ '2796': 'gFyFj9z',
+ '2812': 'QjE2DcA',
+ '2803': 'r5DgrW9',
+ '2133': 'kettlebell-farmer-carry',
+ '3561': 'glute-bridge-march',
+ '1511': 'hamstring-stretch',
+ '0471': 'handstand-push-up',
+ '0472': 'QOA0FD0',
+ '1471': 'inchworm',
+ '0493': 'incline-push-up',
+ '2612': 'jump-rope',
+ '0533': 'kettlebell-front-squat',
+ '0534': 'kettlebell-goblet-squat',
+ '0549': 'kettlebell-swing',
+ '0578': 'UfePqpx',
+ '0584': 'machine-lateral-raise',
+ '0602': 'myfUsKf',
+ '1350': '7I6LNUG',
+ '1385': 'IeDEXTe',
+ '0603': '67n3r98',
+ '0604': 'ZZKbeMw',
+ '0605': 'ykUOVze',
+ '3758': 'WbNq5Xu',
+ '0606': 'aaXr7ld',
+ '0607': 'Ser9eQp',
+ '1301': 'wDN97Ca',
+ '1421': 'arvaszz',
+ '0630': 'mountain-climber',
+ '1387': '0jp9Rlz',
+ '0648': 'SiWCcTN',
+ '0652': 'lBDjFxJ',
+ '0662': 'I4hDWkc',
+ '3552': 'mweqJin',
+ '3007': 'Y1MsI1l',
+ '0687': 'XVDdcoj',
+ '3012': '7xeukSt',
+ '0688': 'uTBt1HV',
+ '0697': 'GwYwElT',
+ '1766': 'GwYwElT',
+ '1775': 'VO2qeJg',
+ '0730': 'LNE3wfo',
+ '1489': 'xdYPUtE',
+ '0746': 'MzNnwx9',
+ '0747': 'Gpn4ADc',
+ '0748': 'trqKQv2',
+ '0749': '1bQkKZK',
+ '0750': 'Gu2rNJd',
+ '0751': 'WcHl7ru',
+ '0752': 'UfePqpx',
+ '0753': 'ETZfAbZ',
+ '0754': 'MY9P1WA',
+ '1433': 'lFhb2Rw',
+ '3281': 'NNoHCEA',
+ '0755': 'ZuPXtCK',
+ '0756': 'CqhoytW',
+ '0759': 'ayAHcEm',
+ '0760': '7zdxRTl',
+ '1683': 'zILLZ98',
+ '1625': 'yB9SvIF',
+ '1752': 'o8aOcrz',
+ '1393': '9GXrTE6',
+ '0762': 'nFUwqG6',
+ '0763': 'ywaNfuh',
+ '1394': 'ywaNfuh',
+ '1395': '0S75mYG',
+ '0765': 'xUwnBMT',
+ '1426': 'B6dAO1t',
+ '0766': '67n3r98',
+ '0767': 'ZZKbeMw',
+ '0768': 'wWFspEi',
+ '0771': 'hfmQ0Tz',
+ '0772': 'ht8xDrP',
+ '0773': '6MaEjVA',
+ '0776': 'dG5Smob',
+ '2368': '9E25EOx',
+ '0786': 'IMRsOCn',
+ '1685': 'QChZi3x',
+ '1686': '5BZHW9s',
+ '0795': 'C5jncD2',
+ '0809': 'QpXqiq8',
+ '0811': 'trap-bar-deadlift',
+ '0814': 'X6C6i5Y',
+ '0830': 'MU9HnE7',
+ '2987': 'Gk1r408',
+ '0832': 's8nrDXF',
+ '3670': 'EZeDVzO',
+ '0833': 'LmaFNZS',
+ '1310': 'PSlvNMs',
+ '2135': 'VBAWRPG',
+ '0834': 'e4aFmFY',
+ '0866': 'QOA0FD0',
+ '0835': '8urJS9b',
+ '3641': '7uFJuXp',
+ '3644': '5WiFcYk',
+ '3286': 'JsOV1SU',
+ '3290': 'fXfqg1E',
+ '0840': 'xmM75XG',
+ '0841': 'HMzLjXx',
+ '0844': 'VLYXo8S',
+ '0846': 'fZFZ704',
+ '0849': 's34Y4LR',
+ '0850': '6bOA1Oi',
+ '0851': '0lQnxMZ',
+ '0852': 'JZuApnB',
+ '0853': 'M5Y7GPg',
+ '0854': 'bjqbauy',
+ '3313': 'Ff18ItA',
+ '1754': 'gtO1ErP',
+ '1755': 'bZq4bwK',
+ '1767': 'K1vlode',
+ '0859': 'bd5b860',
+};
diff --git a/lib/services/workout_import.dart b/lib/services/workout_import.dart
index 044b384..106d73a 100644
--- a/lib/services/workout_import.dart
+++ b/lib/services/workout_import.dart
@@ -1,8 +1,23 @@
+import 'dart:convert';
+
import 'package:archive/archive.dart';
+import '../catalog/opengym_ids.dart';
+
const double _lbPerKg = 2.2046226218;
-enum ImportFormat { hevy, strong, fitnotes, gymmane, hevyWeights, strongWeights, weights, unknown }
+enum ImportFormat {
+ hevy,
+ strong,
+ fitnotes,
+ gymmane,
+ openGym,
+ generic,
+ hevyWeights,
+ strongWeights,
+ weights,
+ unknown,
+}
extension ImportFormatX on ImportFormat {
bool get isWeights =>
@@ -24,9 +39,10 @@ class ParsedWeight {
}
class ParsedExercise {
- ParsedExercise(this.name, this.muscle);
+ ParsedExercise(this.name, this.muscle, {this.id});
final String name;
final String? muscle;
+ final String? id;
final List<ParsedSet> sets = [];
}
@@ -115,14 +131,38 @@ const _formats = <ImportFormat, _Fmt>{
weightLb: [],
muscle: ['muscle'],
),
+ ImportFormat.generic: _Fmt(
+ date: _genericDateCols,
+ exercise: _genericExerciseCols,
+ reps: _genericRepCols,
+ weightKg: _weightKgCols,
+ weightLb: _weightLbCols,
+ weightPlain: _genericWeightCols,
+ group: ['workout', 'routine', 'rutina', 'plan', 'entrenamiento', 'training'],
+ muscle: ['category', 'muscle', 'categoria', 'categoría', 'musculo', 'músculo'],
+ ),
};
+const _genericDateCols = [
+ 'date', 'fecha', 'datum', 'data', 'day', 'dia', 'día', 'timestamp', 'start_time', 'time',
+];
+const _genericExerciseCols = [
+ 'exercise', 'exercise name', 'exercise_name', 'ejercicio', 'exercicio', 'exercício',
+ 'übung', 'ubung', 'uebung', 'esercizio', 'exercice', 'name', 'nombre',
+];
+const _genericRepCols = [
+ 'reps', 'rep', 'repetitions', 'repeticiones', 'repetições', 'repeticoes',
+ 'wiederholungen', 'ripetizioni', 'répétitions', 'repetitions_count',
+];
+const _genericWeightCols = ['weight', 'peso', 'gewicht', 'poids', 'carga'];
+
const _anyWeight = ['weight (kg)', 'weight (lbs)', 'weight (lb)', 'weight', 'weight_kg'];
const _weightKgCols = ['weight_kg', 'weight (kg)', 'weight kg', 'peso (kg)'];
const _weightLbCols = ['weight_lbs', 'weight_lb', 'weight (lbs)', 'weight (lb)', 'weight lbs'];
const _dateCols = ['date', 'fecha', 'start_time', 'time'];
ImportFormat detectFormat(String csv) {
+ if (_backupOf(csv) != null) return ImportFormat.openGym;
final cols = _header(csv);
if (cols.isEmpty) return ImportFormat.unknown;
if (cols.contains('exercise_title') && cols.contains('start_time')) return ImportFormat.hevy;
@@ -133,6 +173,10 @@ ImportFormat detectFormat(String csv) {
if (cols.contains('exercise') && cols.contains('category') && _pick(cols, _anyWeight) != null) {
return ImportFormat.fitnotes;
}
+ final exerciseCol = _pick(cols, _genericExerciseCols);
+ final repsCol = _pick(cols, _genericRepCols);
+ final genericDate = _pick(cols, _genericDateCols);
+ if (exerciseCol != null && repsCol != null && genericDate != null) return ImportFormat.generic;
final hasExercise = cols.any((c) => c.contains('exercise') || c.contains('ejercicio'));
final dateCol = _pick(cols, _dateCols);
final weightCol = _pick(cols, [..._weightKgCols, ..._weightLbCols, 'weight', 'peso']);
@@ -225,6 +269,7 @@ List<ParsedWeight> parseWeights(String csv, {bool isLb = false}) {
ImportResult parseImport(String csv, {bool isLb = false}) {
final format = detectFormat(csv);
+ if (format == ImportFormat.openGym) return _parseOpenGym(_backupOf(csv)!);
if (format.isWeights) {
return ImportResult(format, const [], weights: parseWeights(csv, isLb: isLb));
}
@@ -293,6 +338,89 @@ ImportResult parseImport(String csv, {bool isLb = false}) {
return ImportResult(format, sessions.values.toList());
}
+Map<String, dynamic>? _backupOf(String text) {
+ final head = text.trimLeft();
+ if (!head.startsWith('{')) return null;
+ Object? decoded;
+ try {
+ decoded = jsonDecode(head);
+ } catch (_) {
+ return null;
+ }
+ if (decoded is! Map) return null;
+ final map = decoded.cast<String, dynamic>();
+ return map['workouts'] is List && map['routines'] is List ? map : null;
+}
+
+ImportResult _parseOpenGym(Map<String, dynamic> backup) {
+ final isLb = _norm((backup['unit'] as String?) ?? 'kg').startsWith('lb');
+ final custom = <String, String>{};
+ for (final entry in (backup['customEx'] as List?) ?? const []) {
+ if (entry is! Map) continue;
+ final id = entry['id'];
+ final name = entry['n'];
+ if (id is String && name is String && name.trim().isNotEmpty) custom[id] = name.trim();
+ }
+
+ final sessions = <ParsedSession>[];
+ for (final raw in (backup['workouts'] as List?) ?? const []) {
+ if (raw is! Map) continue;
+ final workout = raw.cast<String, dynamic>();
+ final date = _openGymDate(workout);
+ if (date == null) continue;
+ final session = ParsedSession(date, _openGymDuration(workout));
+ for (final rawEntry in (workout['entries'] as List?) ?? const []) {
+ if (rawEntry is! Map) continue;
+ final sourceId = rawEntry['id'];
+ if (sourceId is! String) continue;
+ final localId = kOpenGymIds[sourceId];
+ final name = custom[sourceId] ?? '';
+ if (localId == null && name.isEmpty) continue;
+ final exercise = ParsedExercise(name, null, id: localId);
+ for (final rawSet in (rawEntry['sets'] as List?) ?? const []) {
+ if (rawSet is! Map) continue;
+ if (rawSet['done'] != true) continue;
+ if (rawSet['phase'] == 'warmup' || rawSet['warmup'] == true) continue;
+ final reps = (rawSet['r'] as num?)?.toInt() ?? 0;
+ if (reps <= 0) continue;
+ final weight = (rawSet['w'] as num?)?.toDouble() ?? 0;
+ exercise.sets.add(ParsedSet(reps, isLb ? weight / _lbPerKg : weight));
+ }
+ if (exercise.sets.isNotEmpty) session.exercises.add(exercise);
+ }
+ if (session.exercises.isNotEmpty) sessions.add(session);
+ }
+
+ final weights = <ParsedWeight>[];
+ for (final raw in (backup['bodyweight'] as List?) ?? const []) {
+ if (raw is! Map) continue;
+ final date = _parseDate((raw['d'] as String?) ?? '');
+ final value = (raw['w'] as num?)?.toDouble() ?? 0;
+ if (date == null || value <= 0) continue;
+ weights.add(ParsedWeight(date, isLb ? value / _lbPerKg : value));
+ }
+
+ return ImportResult(ImportFormat.openGym, sessions, weights: weights);
+}
+
+DateTime? _openGymDate(Map<String, dynamic> workout) {
+ final start = (workout['start'] as num?)?.toInt();
+ final day = _parseDate((workout['d'] as String?) ?? '');
+ if (start != null && start > 0) {
+ final stamp = DateTime.fromMillisecondsSinceEpoch(start);
+ if (day == null || _dayId(stamp) == _dayId(day)) return stamp;
+ }
+ return day;
+}
+
+int _openGymDuration(Map<String, dynamic> workout) {
+ final start = (workout['start'] as num?)?.toInt() ?? 0;
+ final end = (workout['end'] as num?)?.toInt() ?? 0;
+ if (start <= 0 || end <= start) return 0;
+ final seconds = (end - start) ~/ 1000;
+ return seconds > 24 * 3600 ? 0 : seconds;
+}
+
double _weightKg({
required String kg,
required String lb,