1import 'package:blood_pressure_app/components/confirm_deletion_dialoge.dart';
 2import 'package:blood_pressure_app/data_util/entry_context.dart';
 3import 'package:blood_pressure_app/data_util/repository_builder.dart';
 4import 'package:blood_pressure_app/model/storage/storage.dart';
 5import 'package:blood_pressure_app/model/weight_unit.dart';
 6import 'package:flutter/material.dart';
 7import 'package:health_data_store/health_data_store.dart';
 8import 'package:intl/intl.dart';
 9import 'package:provider/provider.dart';
10
11/// List of weights recorded in the contexts [BodyweightRepository].
12class WeightList extends StatelessWidget {
13  /// Create a list of weights
14  const WeightList({super.key, required this.rangeType});
15
16  /// The location from which the displayed interval is taken.
17  final IntervalStoreManagerLocation rangeType;
18
19  @override
20  Widget build(BuildContext context) {
21    final format = DateFormat(context.select<Settings, String>((s) => s.dateFormatString));
22    final weightUnit = context.select((Settings s) => s.weightUnit);
23    return RepositoryBuilder<BodyweightRecord, BodyweightRepository>(
24      rangeType: rangeType,
25      onData: (context, records) {
26        final manager = context.watch<IntervalStoreManager>();
27        final timeLimitRange = manager.get(rangeType).timeLimitRange;
28        if (timeLimitRange != null) {
29          records = records.where((r) {
30            final time = TimeOfDay.fromDateTime(r.time);
31            return time.isAfter(timeLimitRange.start) && time.isBefore(timeLimitRange.end);
32          }).toList();
33        }
34        records.sort((a, b) => b.time.compareTo(a.time));
35        return ListView.builder(
36          itemCount: records.length,
37          itemBuilder: (context, idx) => ListTile(
38            title: Text(_buildWeightText(weightUnit, records[idx].weight)),
39            subtitle: Text(format.format(records[idx].time)),
40            trailing: Row(
41              mainAxisSize: MainAxisSize.min,
42              children: [
43                IconButton(
44                  icon: Icon(Icons.edit),
45                  onPressed: () => context.createEntry((
46                    timestamp: records[idx].time,
47                    note: null,
48                    record: null,
49                    intake: null,
50                    weight: records[idx],
51                  )),
52                ),
53                IconButton(
54                  icon: Icon(Icons.delete),
55                  onPressed: () async {
56                    final repo = context.read<BodyweightRepository>();
57                    if ((!context.read<Settings>().confirmDeletion) || await showConfirmDeletionDialoge(context)) {
58                      await repo.remove(records[idx]);
59                    }
60                  },
61                ),
62              ],
63            ),
64          ),
65        );
66      },
67    );
68  }
69
70  String _buildWeightText(WeightUnit u, Weight w) {
71    String weightStr = u.extract(w).toStringAsFixed(2);
72    if (weightStr.endsWith('0')) weightStr = weightStr.substring(0, weightStr.length - 1);
73    if (weightStr.endsWith('0')) weightStr = weightStr.substring(0, weightStr.length - 1);
74    if (weightStr.endsWith('.')) weightStr = weightStr.substring(0, weightStr.length - 1);
75
76    return '$weightStr ${u.name}';
77  }
78}