Skip to content

Commit

Permalink
capacity card
Browse files Browse the repository at this point in the history
  • Loading branch information
Huber1 committed Oct 23, 2024
1 parent 89d99af commit 02f9a2b
Showing 1 changed file with 99 additions and 22 deletions.
121 changes: 99 additions & 22 deletions lib/home/meals/components/capacity_card.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:better_hm/home/meals/models/canteen.dart';
import 'package:better_hm/home/meals/service/canteen_service.dart';
import 'package:better_hm/home/meals/service/selected_canteen_wrapper.dart';
Expand All @@ -13,36 +15,91 @@ class CapacityCard extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final canteen = ref.watch(selectedCanteenProvider).value;

if (![
"STUCAFE_KARLSTR",
"MENSA_LOTHSTR",
"STUCAFE_LOTHSTR",
"MENSA_PASING",
"STUCAFE_PASING",
].contains(canteen?.canteen?.enumName) ||
canteen?.canteen == null) return const SizedBox.shrink();

return Card(
color: context.theme.colorScheme.surfaceContainer,
elevation: 0,
child: Padding(
padding: const EdgeInsets.fromLTRB(16.0, 8.0, 8.0, 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("${t.mealplan.capacity.label}:"),
const SizedBox(width: 16),
if (canteen?.canteen != null)
Expanded(
child: FutureBuilder(
future: capacity(canteen!.canteen!),
builder: (context, snapshot) {
double progress = snapshot.data ?? 0;

return _ProgressBar(value: progress);
},
),
),
const SizedBox(width: 16),
CapacityFeedbackButton(canteen: canteen!.canteen!),
],
),
child: _CapacityContent(canteen!.canteen!),
),
);
}
}

class _CapacityContent extends StatefulWidget {
const _CapacityContent(this.canteen);

final Canteen canteen;

@override
State<_CapacityContent> createState() => _CapacityContentState();
}

class _CapacityContentState extends State<_CapacityContent> {
double? capacityValue;
late Timer timer;

@override
void initState() {
super.initState();
timer = Timer.periodic(const Duration(minutes: 1), getCapacity);
getCapacity();
}

@override
void didUpdateWidget(covariant _CapacityContent oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.canteen != oldWidget.canteen) getCapacity();
}

getCapacity([Timer? timer]) async {
final c = await capacity(widget.canteen);
setState(() {
capacityValue = c;
});
}

@override
void setState(VoidCallback fn) {
if (mounted) super.setState(fn);
}

@override
void dispose() {
timer.cancel();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("${t.mealplan.capacity.label}:"),
const SizedBox(width: 16),
Expanded(
child: _ProgressBar(value: capacityValue ?? 0),
),
const SizedBox(width: 16),
CapacityFeedbackButton(
canteen: widget.canteen,
isActive: (capacityValue ?? 0) <= 0.5,
),
],
);
}
}

class _ProgressBar extends StatelessWidget {
const _ProgressBar({required this.value});

Expand Down Expand Up @@ -72,16 +129,36 @@ class _ProgressBar extends StatelessWidget {
}

class CapacityFeedbackButton extends StatefulWidget {
const CapacityFeedbackButton({super.key, required this.canteen});
const CapacityFeedbackButton({
super.key,
required this.canteen,
required this.isActive,
});

final Canteen canteen;
final bool isActive;

@override
State<CapacityFeedbackButton> createState() => _CapacityFeedbackButtonState();
}

class _CapacityFeedbackButtonState extends State<CapacityFeedbackButton> {
bool isActive = true;
late bool isActive;

@override
void initState() {
super.initState();
isActive = widget.isActive;
}

@override
void didUpdateWidget(covariant CapacityFeedbackButton oldWidget) {
super.didUpdateWidget(oldWidget);
if (isActive != widget.isActive)
setState(() {
isActive = widget.isActive;
});
}

void onTap() {
showDialog(
Expand Down

0 comments on commit 02f9a2b

Please sign in to comment.