Skip to content

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

5.0 KB
Raw Blame
import 'workout.dart';

class SessionSet {
  SessionSet(this.reps, this.weight, this.done,
      {this.kind = SetKind.normal, this.rpe, this.sec, this.km, this.rest, this.note = '', this.repsR, this.goal});
  int reps;
  int? repsR;
  String? goal;
  String note;
  int? rest;
  double weight;
  bool done;
  bool half = false;
  SetKind kind;
  double? rpe;
  int? sec;
  double? km;

  bool get counts => kind != SetKind.warmup;

  int get weakReps {
    final right = repsR;
    return right == null || right > reps ? reps : right;
  }

  LoggedSet get logged => LoggedSet(weakReps, weight,
      kind: kind, rpe: rpe, sec: sec, km: km, note: note.trim(), left: repsR == null ? null : reps, right: repsR);

  SessionSet copy({bool done = false}) =>
      SessionSet(reps, weight, done, kind: kind, rpe: rpe, sec: sec, km: km, repsR: repsR, goal: goal);

  Map<String, dynamic> toJson() => {
        'r': reps,
        if (repsR != null) 'rr': repsR,
        if (goal != null) 'g': goal,
        'w': weight,
        'd': done,
        if (half && !done) 'h': true,
        if (kind != SetKind.normal) 'k': kind.index,
        if (rpe != null) 'e': rpe,
        if (sec != null) 't': sec,
        if (km != null) 'km': km,
        if (rest != null) 'rs': rest,
        if (note.isNotEmpty) 'n': note,
      };
  factory SessionSet.fromJson(Map<String, dynamic> j) => SessionSet(
        (j['r'] as num).toInt(),
        (j['w'] as num).toDouble(),
        j['d'] as bool? ?? false,
        kind: setKindFrom(j['k']),
        rpe: (j['e'] as num?)?.toDouble(),
        sec: (j['t'] as num?)?.toInt(),
        km: (j['km'] as num?)?.toDouble(),
        rest: (j['rs'] as num?)?.toInt().clamp(0, 3600),
        note: j['n'] as String? ?? '',
        repsR: (j['rr'] as num?)?.toInt(),
        goal: j['g'] as String?,
      )..half = j['h'] as bool? ?? false;
}

class SessionExercise {
  SessionExercise(this.id, this.name, this.primary, this.sets, {this.linkedNext = false, this.swappedFrom});
  final String id;
  final String name;
  final String primary;
  final List<SessionSet> sets;
  bool linkedNext;
  final String? swappedFrom;

  bool get hasUndone => sets.any((s) => !s.done);

  Map<String, dynamic> toJson() => {
        'id': id,
        'n': name,
        'p': primary,
        's': sets.map((s) => s.toJson()).toList(),
        if (linkedNext) 'l': true,
        if (swappedFrom != null) 'sw': swappedFrom,
      };
  factory SessionExercise.fromJson(Map<String, dynamic> j) => SessionExercise(
        j['id'] as String,
        j['n'] as String,
        j['p'] as String,
        (j['s'] as List).map((e) => SessionSet.fromJson((e as Map).cast<String, dynamic>())).toList(),
        linkedNext: j['l'] as bool? ?? false,
        swappedFrom: j['sw'] as String?,
      );
}

class WorkoutSession {
  WorkoutSession();

  List<SessionExercise> exercises = [];
  int currentIndex = 0;
  bool complete = false;
  bool manual = false;
  bool deload = false;
  bool lightened = false;
  String? routineId;
  String? name;
  DateTime? loggedAt;
  DateTime? restEndsAt;
  int? restFrozen;
  bool restSwitch = false;
  int? summaryVolume;
  int? summarySets;
  int? summaryDuration;

  int? get restRemaining {
    final frozen = restFrozen;
    if (frozen != null) return frozen;
    final end = restEndsAt;
    if (end == null) return null;
    final left = end.difference(DateTime.now()).inMilliseconds;
    return left <= 0 ? null : (left / 1000).ceil();
  }

  void clearRest() {
    restEndsAt = null;
    restFrozen = null;
    restSwitch = false;
  }

  Map<String, dynamic> toJson() => {
        'ex': exercises.map((e) => e.toJson()).toList(),
        'i': currentIndex,
        'c': complete,
        if (manual) 'm': true,
        if (deload) 'dl': true,
        if (lightened) 'lt': true,
        if (routineId != null) 'r': routineId,
        if (name != null) 'n': name,
        'at': loggedAt?.toIso8601String(),
        if (restEndsAt != null) 're': restEndsAt!.toIso8601String(),
        if (restFrozen != null) 'rf': restFrozen,
        if (restSwitch && restEndsAt != null) 'rsw': true,
        'sv': summaryVolume,
        'ss': summarySets,
        'sd': summaryDuration,
      };

  factory WorkoutSession.fromJson(Map<String, dynamic> j) => WorkoutSession()
    ..exercises =
        (j['ex'] as List).map((e) => SessionExercise.fromJson((e as Map).cast<String, dynamic>())).toList()
    ..currentIndex = (j['i'] as num?)?.toInt() ?? 0
    ..complete = j['c'] as bool? ?? false
    ..manual = j['m'] as bool? ?? false
    ..deload = j['dl'] as bool? ?? false
    ..lightened = j['lt'] as bool? ?? false
    ..routineId = j['r'] as String?
    ..name = j['n'] as String?
    ..loggedAt = DateTime.tryParse((j['at'] as String?) ?? '')
    ..restEndsAt = DateTime.tryParse((j['re'] as String?) ?? '')
    ..restFrozen = (j['rf'] as num?)?.toInt()
    ..restSwitch = j['rsw'] as bool? ?? false
    ..summaryVolume = (j['sv'] as num?)?.toInt()
    ..summarySets = (j['ss'] as num?)?.toInt()
    ..summaryDuration = (j['sd'] as num?)?.toInt();
}