Skip to content

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

6.1 KB
Raw Blame
import 'package:flutter/material.dart';
import 'package:phosphoricons_flutter/phosphoricons_flutter.dart';

import '../l10n/l10n.dart';
import '../models/progress_shot.dart';
import '../services/media_store.dart';
import '../state/fit_state.dart';
import '../theme/app_colors.dart';
import '../theme/app_theme.dart';
import '../widgets/file_thumb.dart';
import '../widgets/share_cards.dart';
import '../widgets/ui_kit.dart';
import 'share_sheet.dart';

class CompareScreen extends StatefulWidget {
  const CompareScreen({super.key});

  @override
  State<CompareScreen> createState() => _CompareScreenState();
}

class _CompareScreenState extends State<CompareScreen> {
  final _zoom = TransformationController();
  String? _shown;

  @override
  void dispose() {
    _zoom.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final gc = context.gc;
    final pair = fit.comparePair;
    if (pair == null) return const SizedBox.shrink();

    final poses = fit.posesWithBoth;
    final pose = poses.contains(fit.comparePose) ? fit.comparePose : (poses.isEmpty ? null : poses.first);
    final shown = '${pair.from.id}|${pair.to.id}|$pose';
    if (_shown != shown) {
      _shown = shown;
      _zoom.value = Matrix4.identity();
    }
    final delta = fit.compareWeightDelta;
    final entries = fit.timelineAsc;

    return SafeArea(
      bottom: false,
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.fromLTRB(20, 12, 20, 8),
            child: ScreenHeader(
              title: t.compare,
              onBack: fit.backFromCompare,
              actions: [
                RoundAction(
                  label: t.share,
                  onTap: () => showShareSheet(context, initial: ShareKind.compare),
                  child: Icon(PhosphorIconsRegular.shareNetwork, size: 16, color: gc.text),
                ),
              ],
            ),
          ),
          if (poses.length > 1)
            Padding(
              padding: const EdgeInsets.fromLTRB(20, 6, 20, 0),
              child: SegToggle([
                for (final p in poses)
                  SegOption(t.poseName(p), p == pose, () => fit.setComparePose(p)),
              ]),
            ),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.fromLTRB(20, 16, 20, 0),
              child: Row(
                children: [
                  Expanded(child: _shot(gc, pair.from, pose)),
                  const SizedBox(width: 10),
                  Expanded(child: _shot(gc, pair.to, pose)),
                ],
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.fromLTRB(20, 14, 20, 6),
            child: Row(
              children: [
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(t.daysApart(fit.compareDays),
                          style: AppTheme.d(18, weight: FontWeight.w700, color: gc.text)),
                      if (delta != null) ...[
                        const SizedBox(height: 2),
                        Text('${delta > 0 ? '+' : ''}${fmt(delta)} ${fit.units}',
                            style: AppTheme.s(13,
                                weight: FontWeight.w600,
                                color: delta == 0 ? gc.textSecondary : gc.accent)),
                      ],
                    ],
                  ),
                ),
              ],
            ),
          ),
          _picker(gc, entries, pair.from.id, fit.setCompareFrom, gc.textSecondary),
          _picker(gc, entries, pair.to.id, fit.setCompareTo, gc.accent),
          const SizedBox(height: 16),
        ],
      ),
    );
  }

  Widget _picker(
    GymColors gc,
    List<ProgressEntry> entries,
    String selectedId,
    ValueChanged<String> onPick,
    Color tint,
  ) =>
      SizedBox(
        height: 44,
        child: ListView(
          key: const PageStorageKey('h-compare_screen-127'),
          scrollDirection: Axis.horizontal,
          padding: const EdgeInsets.fromLTRB(20, 4, 20, 4),
          children: [
            for (final e in entries)
              Padding(
                padding: const EdgeInsetsDirectional.only(end: 8),
                child: Pill(
                  label: t.shortDate(e.date),
                  bg: e.id == selectedId ? tint.withValues(alpha: 0.18) : gc.bgRaised,
                  fg: e.id == selectedId ? tint : gc.textTertiary,
                  fontSize: 12,
                  onTap: () => onPick(e.id),
                ),
              ),
          ],
        ),
      );

  Widget _shot(GymColors gc, ProgressEntry entry, String? pose) {
    final name = pose == null ? (entry.media.isEmpty ? null : entry.media.first) : entry.shot(pose);
    final path = name == null ? null : MediaStore.pathFor(name);
    return Column(
      children: [
        Expanded(
          child: Container(
            width: double.infinity,
            decoration: BoxDecoration(
              color: gc.bgRaised2,
              borderRadius: BorderRadius.circular(18),
              border: Border.all(color: gc.border),
            ),
            clipBehavior: Clip.antiAlias,
            child: path == null
                ? Icon(PhosphorIconsRegular.imageSquare, size: 22, color: gc.textTertiary)
                : GestureDetector(
                    onDoubleTap: () => _zoom.value = Matrix4.identity(),
                    child: InteractiveViewer(
                      transformationController: _zoom,
                      minScale: 1,
                      maxScale: 5,
                      child: SizedBox.expand(
                        child: FileThumb(path,
                            errorBuilder: (_, _, _) =>
                                Icon(PhosphorIconsRegular.imageSquare, size: 22, color: gc.textTertiary)),
                      ),
                    ),
                  ),
          ),
        ),
        const SizedBox(height: 8),
        Text(t.shortDateYear(entry.date),
            style: AppTheme.s(11.5, weight: FontWeight.w600, color: gc.textSecondary)),
      ],
    );
  }
}