Skip to content

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

6.3 KB
Raw Blame
part of 'fit_state.dart';

const List<String> kPlacePresets = ['gym', 'home', 'outdoors'];

const Map<String, List<String>> kPlacePresetGear = {
  'gym': kEquipment,
  'home': ['Bodyweight', 'Dumbbell', 'Kettlebell', 'Band', 'Weighted'],
  'outdoors': ['Bodyweight', 'Band'],
};

mixin PlacesState on FitCore, LibraryState {
  String activePlaceId = '';
  int _placeSeq = 0;

  GymPlace? get activePlace {
    for (final p in places) {
      if (p.id == activePlaceId) return p;
    }
    return null;
  }

  Set<String> get gearHere {
    final place = activePlace;
    return place == null ? kEquipment.toSet() : {...place.equipment, 'Bodyweight'};
  }

  @override
  bool fitsHere(Exercise ex) =>
      activePlace == null || gearHere.contains(ex.equipment) || isCustom(ex.id);

  int placeExerciseCount(GymPlace place) {
    final gear = {...place.equipment, 'Bodyweight'};
    return allExercises.where((e) => gear.contains(e.equipment)).length;
  }

  List<Exercise> alternativesHere(Exercise ex, int n) {
    if (fitsHere(ex)) return const [];
    final pool = allExercises
        .where((e) => e.id != ex.id && e.primary == ex.primary && fitsHere(e))
        .toList();
    pool.sort((a, b) {
      int rank(Exercise e) {
        if (favorites[e.id] == true) return 0;
        return e.equipment == 'Bodyweight' ? 1 : 2;
      }

      return rank(a).compareTo(rank(b));
    });
    return pool.take(n).toList();
  }

  String addPlace(String name, {Set<String> equipment = const {'Bodyweight'}}) {
    final clean = name.trim();
    if (clean.isEmpty) return '';
    final id = 'p${DateTime.now().microsecondsSinceEpoch}-${_placeSeq++}';
    places.add(GymPlace(id: id, name: clean, equipment: {...equipment}));
    _persist();
    notifyListeners();
    return id;
  }

  String addPresetPlace(String preset) => addPlace(
        t.placePresetName(preset),
        equipment: (kPlacePresetGear[preset] ?? const ['Bodyweight']).toSet(),
      );

  void renamePlace(String id, String name) {
    final i = places.indexWhere((p) => p.id == id);
    final clean = name.trim();
    if (i < 0 || clean.isEmpty) return;
    places[i] = places[i].copyWith(name: clean);
    _persist();
    notifyListeners();
  }

  void togglePlaceGear(String id, String equipment) {
    final i = places.indexWhere((p) => p.id == id);
    if (i < 0) return;
    final gear = {...places[i].equipment};
    gear.contains(equipment) ? gear.remove(equipment) : gear.add(equipment);
    places[i] = places[i].copyWith(equipment: gear);
    _persist();
    notifyListeners();
  }

  void deletePlace(String id) {
    places.removeWhere((p) => p.id == id);
    if (activePlaceId == id) activePlaceId = '';
    _persist();
    notifyListeners();
  }

  @override
  Map<double, int>? get plateStockKg {
    final p = activePlace;
    if (p == null || p.plates.isEmpty) return null;
    return p.plates;
  }

  @override
  double? get placeBarKg => activePlace?.bar;

  @override
  List<double>? dumbbellStockFor(String id) {
    final owned = activePlace?.dumbbells;
    if (owned == null || owned.isEmpty || exerciseById(id)?.equipment != 'Dumbbell' || isAssisted(id)) return null;
    return owned;
  }

  List<double> get dumbbellSizes => isLb
      ? const [5, 8, 10, 12, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 90, 100]
      : const [1, 2, 2.5, 3, 4, 5, 6, 7, 7.5, 8, 9, 10, 12, 12.5, 14, 15, 16, 17.5, 18, 20, 22, 22.5, 24, 25, 26,
          27.5, 28, 30, 32, 32.5, 34, 35, 36, 37.5, 40, 42.5, 45, 47.5, 50];

  bool ownsDumbbell(GymPlace place, double kg) => place.dumbbells.any((v) => (v - kg).abs() < 0.05);

  void toggleDumbbell(String placeId, double kg) {
    final i = places.indexWhere((p) => p.id == placeId);
    if (i < 0 || kg <= 0) return;
    final list = [...places[i].dumbbells];
    final at = list.indexWhere((v) => (v - kg).abs() < 0.05);
    at >= 0 ? list.removeAt(at) : list.add(_round3(kg));
    list.sort();
    places[i] = places[i].copyWith(dumbbells: list);
    _persist();
    notifyListeners();
  }

  void clearDumbbells(String placeId) {
    final i = places.indexWhere((p) => p.id == placeId);
    if (i < 0) return;
    places[i] = places[i].copyWith(dumbbells: const []);
    _persist();
    notifyListeners();
  }

  void setPlatePairs(String placeId, double kg, int pairs) {
    final i = places.indexWhere((p) => p.id == placeId);
    if (i < 0) return;
    final plates = {...places[i].plates};
    final key = _plateKey(plates, kg);
    if (pairs <= 0) {
      plates.remove(key);
    } else {
      plates[key] = pairs.clamp(1, 20);
    }
    places[i] = places[i].copyWith(plates: plates);
    _persist();
    notifyListeners();
  }

  void clearPlates(String placeId) {
    final i = places.indexWhere((p) => p.id == placeId);
    if (i < 0) return;
    places[i] = places[i].copyWith(plates: const {}, clearBar: true);
    _persist();
    notifyListeners();
  }

  void setPlaceBar(String placeId, double kg) {
    final i = places.indexWhere((p) => p.id == placeId);
    if (i < 0) return;
    places[i] = places[i].copyWith(bar: _round3(kg.clamp(1, 60)));
    _persist();
    notifyListeners();
  }

  int platePairs(GymPlace place, double kg) {
    for (final e in place.plates.entries) {
      if ((e.key - kg).abs() < 0.05) return e.value;
    }
    return 0;
  }

  double _plateKey(Map<double, int> plates, double kg) {
    for (final k in plates.keys) {
      if ((k - kg).abs() < 0.05) return k;
    }
    return _round3(kg);
  }

  void setActivePlace(String id) {
    activePlaceId = activePlaceId == id ? '' : id;
    _persist();
    notifyListeners();
  }

  bool exNoGearOnly = false;
  bool exMineOnly = false;

  void toggleNoGearFilter() {
    exNoGearOnly = !exNoGearOnly;
    if (exNoGearOnly) exEquipmentFilter = null;
    notifyListeners();
  }

  void toggleMineFilter() {
    exMineOnly = !exMineOnly;
    notifyListeners();
  }

  @override
  void setEquipmentFilter(String e) {
    exNoGearOnly = false;
    super.setEquipmentFilter(e);
  }

  @override
  List<Exercise> exercisesMatching(String query) => super
      .exercisesMatching(query)
      .where((ex) =>
          (!exNoGearOnly || isNoKit(ex)) && (!exMineOnly || isCustom(ex.id)) && fitsHere(ex))
      .toList();

  @override
  void clearExFilters() {
    exNoGearOnly = false;
    exMineOnly = false;
    super.clearExFilters();
  }

  void goPlaces() => pushRoute('places');

  void backFromPlaces() => popRoute(fallback: 'settings');
}