Skip to content

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

Add a third home-screen widget with the muscle map

InlitX2dc4039a5968
diff --git a/android/app/src/main/kotlin/com/gymmane/app/BodyWidgetProvider.kt b/android/app/src/main/kotlin/com/gymmane/app/BodyWidgetProvider.kt
new file mode 100644
index 0000000..fe36f41
--- /dev/null
+++ b/android/app/src/main/kotlin/com/gymmane/app/BodyWidgetProvider.kt
@@ -0,0 +1,39 @@
+package com.gymmane.app
+
+import android.app.PendingIntent
+import android.appwidget.AppWidgetManager
+import android.content.Context
+import android.content.Intent
+import android.content.SharedPreferences
+import android.graphics.BitmapFactory
+import android.widget.RemoteViews
+import es.antonborri.home_widget.HomeWidgetProvider
+
+/// Widget del mapa muscular: solo muestra el PNG que renderiza Flutter
+/// (WidgetBridge -> home_widget). Al tocarlo abre la app.
+class BodyWidgetProvider : HomeWidgetProvider() {
+ override fun onUpdate(
+ context: Context,
+ appWidgetManager: AppWidgetManager,
+ appWidgetIds: IntArray,
+ widgetData: SharedPreferences
+ ) {
+ appWidgetIds.forEach { widgetId ->
+ val views = RemoteViews(context.packageName, R.layout.widget_body).apply {
+ val path = widgetData.getString("body_img", null)
+ if (path != null) {
+ val bmp = BitmapFactory.decodeFile(path)
+ if (bmp != null) setImageViewBitmap(R.id.widget_image, bmp)
+ }
+ val open = PendingIntent.getActivity(
+ context,
+ 0,
+ Intent(context, MainActivity::class.java),
+ PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
+ )
+ setOnClickPendingIntent(R.id.widget_image, open)
+ }
+ appWidgetManager.updateAppWidget(widgetId, views)
+ }
+ }
+}
diff --git a/android/app/src/main/res/drawable/preview_body.png b/android/app/src/main/res/drawable/preview_body.png
new file mode 100644
index 0000000..566adb2
Binary files /dev/null and b/android/app/src/main/res/drawable/preview_body.png differ
diff --git a/android/app/src/main/res/layout/widget_body.xml b/android/app/src/main/res/layout/widget_body.xml
new file mode 100644
index 0000000..815119c
--- /dev/null
+++ b/android/app/src/main/res/layout/widget_body.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+
+ <ImageView
+ android:id="@+id/widget_image"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:adjustViewBounds="true"
+ android:scaleType="fitCenter"
+ android:contentDescription="GymMane" />
+</FrameLayout>
diff --git a/android/app/src/main/res/xml/body_widget_info.xml b/android/app/src/main/res/xml/body_widget_info.xml
new file mode 100644
index 0000000..7132a9d
--- /dev/null
+++ b/android/app/src/main/res/xml/body_widget_info.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
+ android:minWidth="250dp"
+ android:minHeight="170dp"
+ android:targetCellWidth="4"
+ android:targetCellHeight="3"
+ android:updatePeriodMillis="0"
+ android:initialLayout="@layout/widget_body"
+ android:resizeMode="horizontal|vertical"
+ android:widgetCategory="home_screen"
+ android:previewImage="@drawable/preview_body" />
diff --git a/lib/services/home_widget_bridge.dart b/lib/services/home_widget_bridge.dart
index 429085b..461ba09 100644
--- a/lib/services/home_widget_bridge.dart
+++ b/lib/services/home_widget_bridge.dart
@@ -14,6 +14,8 @@ class HomeWidgetBridge {
static const _pkg = 'com.gymmane.app';
static const heatmapKey = 'heatmap_img';
static const statsKey = 'stats_img';
+ static const bodyKey = 'body_img';
+ static const bodyDays = 7;
static bool get _supported => !kIsWeb && Platform.isAndroid;
static Future<void> update() async {
@@ -43,8 +45,21 @@ class HomeWidgetBridge {
logicalSize: const Size(155, 155),
pixelRatio: 3,
);
+ await HomeWidget.renderFlutterWidget(
+ BodyWidgetView(
+ gc: gc,
+ intensity: fit.muscleHeatOver(bodyDays),
+ days: bodyDays,
+ size: const Size(320, 220),
+ ),
+ key: bodyKey,
+ logicalSize: const Size(320, 220),
+ pixelRatio: 3,
+ );
await HomeWidget.updateWidget(
qualifiedAndroidName: '$_pkg.HeatmapWidgetProvider');
+ await HomeWidget.updateWidget(
+ qualifiedAndroidName: '$_pkg.BodyWidgetProvider');
await HomeWidget.updateWidget(
qualifiedAndroidName: '$_pkg.StatsWidgetProvider');
} catch (e) {
diff --git a/lib/widgets/home_widget_views.dart b/lib/widgets/home_widget_views.dart
index 39f7333..c0a0431 100644
--- a/lib/widgets/home_widget_views.dart
+++ b/lib/widgets/home_widget_views.dart
@@ -3,23 +3,16 @@ import 'dart:math' as math;
import 'package:flutter/material.dart';
import '../l10n/l10n.dart';
+import '../state/fit_state.dart';
import '../theme/app_colors.dart';
+import 'body_map.dart';
const _kDisplay = 'Oswald';
const _kBody = 'IBM Plex Sans';
+const _kBorder = 1.0;
-Color _heat(int level, GymColors gc) {
- switch (level) {
- case 3:
- return gc.accent;
- case 2:
- return gc.brass;
- case 1:
- return gc.mutedFill;
- default:
- return gc.heatEmpty;
- }
-}
+Color _heat(int level, GymColors gc) =>
+ level <= 0 ? gc.heatEmpty : heatLevelColor(gc, level);
class HeatmapWidgetView extends StatelessWidget {
const HeatmapWidgetView({
@@ -41,8 +34,8 @@ class HeatmapWidgetView extends StatelessWidget {
const vgap = 3.0;
const pad = 16.0;
const headerH = 30.0;
- final gridW = size.width - pad * 2;
- final gridH = size.height - pad * 2 - headerH;
+ final gridW = size.width - pad * 2 - _kBorder * 2;
+ final gridH = size.height - pad * 2 - headerH - _kBorder * 2;
final cell = ((gridH - (rows - 1) * vgap) / rows).clamp(4.0, 40.0);
final cols = math.max(1, ((gridW + vgap) / (cell + vgap)).floor());
@@ -65,20 +58,25 @@ class HeatmapWidgetView extends StatelessWidget {
decoration: BoxDecoration(
color: gc.bgRaised,
borderRadius: BorderRadius.circular(24),
- border: Border.all(color: gc.border),
+ border: Border.all(color: gc.border, width: _kBorder),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
- Text('GYMMANE',
- style: TextStyle(
- fontFamily: _kDisplay,
- fontSize: 13,
- fontWeight: FontWeight.w600,
- letterSpacing: 2,
- color: gc.text)),
+ Flexible(
+ child: Text('GYMMANE',
+ maxLines: 1,
+ overflow: TextOverflow.clip,
+ softWrap: false,
+ style: TextStyle(
+ fontFamily: _kDisplay,
+ fontSize: 13,
+ fontWeight: FontWeight.w600,
+ letterSpacing: 2,
+ color: gc.text)),
+ ),
const Spacer(),
Icon(Icons.local_fire_department_rounded, size: 15, color: gc.accent),
const SizedBox(width: 3),
@@ -147,7 +145,7 @@ class StatsWidgetView extends StatelessWidget {
decoration: BoxDecoration(
color: gc.bgRaised,
borderRadius: BorderRadius.circular(24),
- border: Border.all(color: gc.border),
+ border: Border.all(color: gc.border, width: _kBorder),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
@@ -235,3 +233,84 @@ class StatsWidgetView extends StatelessWidget {
);
}
}
+
+class BodyWidgetView extends StatelessWidget {
+ const BodyWidgetView({
+ super.key,
+ required this.gc,
+ required this.intensity,
+ required this.days,
+ this.size = const Size(320, 220),
+ });
+
+ final GymColors gc;
+ final Map<String, double> intensity;
+ final int days;
+ final Size size;
+
+ @override
+ Widget build(BuildContext context) {
+ const pad = 16.0;
+ const headerH = 28.0;
+ final artW = math.min(
+ size.width - pad * 2 - _kBorder * 2,
+ (size.height - pad * 2 - headerH - _kBorder * 2) / kBodyAspect,
+ );
+
+ return Container(
+ width: size.width,
+ height: size.height,
+ padding: const EdgeInsets.all(pad),
+ decoration: BoxDecoration(
+ color: gc.bgRaised,
+ borderRadius: BorderRadius.circular(24),
+ border: Border.all(color: gc.border, width: _kBorder),
+ ),
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ SizedBox(
+ height: headerH - 8,
+ child: Row(
+ children: [
+ Flexible(
+ child: Text(t.muscleMap,
+ maxLines: 1,
+ overflow: TextOverflow.clip,
+ softWrap: false,
+ style: TextStyle(
+ fontFamily: _kDisplay,
+ fontSize: 12,
+ fontWeight: FontWeight.w600,
+ letterSpacing: 2,
+ color: gc.text)),
+ ),
+ const Spacer(),
+ for (int i = 1; i <= heatLevels; i++) ...[
+ if (i > 1) const SizedBox(width: 3),
+ Container(
+ width: 12,
+ height: 6,
+ decoration: BoxDecoration(
+ color: heatLevelColor(gc, i),
+ borderRadius: BorderRadius.circular(2),
+ ),
+ ),
+ ],
+ const SizedBox(width: 8),
+ Text(t.daysShort(days),
+ style: TextStyle(fontFamily: _kBody, fontSize: 10.5, color: gc.textSecondary)),
+ ],
+ ),
+ ),
+ const SizedBox(height: 8),
+ Expanded(
+ child: Center(
+ child: BodyHeatArt(gc: gc, intensity: intensity, width: artW),
+ ),
+ ),
+ ],
+ ),
+ );
+ }
+}
diff --git a/test/home_widgets_test.dart b/test/home_widgets_test.dart
new file mode 100644
index 0000000..f2fa6eb
--- /dev/null
+++ b/test/home_widgets_test.dart
@@ -0,0 +1,35 @@
+import 'package:flutter/material.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:gymmane/theme/app_colors.dart';
+import 'package:gymmane/widgets/home_widget_views.dart';
+
+void main() {
+ Future<void> draw(WidgetTester tester, Widget view) async {
+ await tester.pumpWidget(Directionality(
+ textDirection: TextDirection.ltr,
+ child: Center(child: view),
+ ));
+ await tester.pump();
+ expect(tester.takeException(), isNull);
+ }
+
+ for (final (name, gc) in [('dark', GymColors.dark), ('light', GymColors.light)]) {
+ testWidgets('the three home widgets draw in $name without overflowing', (tester) async {
+ await draw(tester, HeatmapWidgetView(gc: gc, levels: List.filled(182, 2), streak: 12));
+ await draw(tester,
+ StatsWidgetView(gc: gc, streak: 12, sessionsThisWeek: 3, goalPct: 75));
+ await draw(
+ tester,
+ BodyWidgetView(
+ gc: gc,
+ days: 7,
+ intensity: const {'chest': 1.0, 'back': 0.5, 'quads': 0.1},
+ ));
+ });
+ }
+
+ testWidgets('the muscle map widget survives an empty history', (tester) async {
+ await draw(tester,
+ const BodyWidgetView(gc: GymColors.dark, days: 7, intensity: {}));
+ });
+}