Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 29 additions & 69 deletions open_wearable/lib/apps/heart_tracker/widgets/rowling_chart.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:community_charts_flutter/community_charts_flutter.dart'
as charts;
import 'package:community_charts_flutter/community_charts_flutter.dart' as charts;

class RollingChart extends StatefulWidget {
final Stream<(int, double)> dataSteam;
final int timestampExponent; // e.g., 6 for microseconds to milliseconds
final int timeWindow; // in seconds
final int timeWindow; // in milliseconds

const RollingChart({
super.key,
Expand All @@ -21,9 +20,8 @@ class RollingChart extends StatefulWidget {
}

class _RollingChartState extends State<RollingChart> {
List<charts.Series<_ChartPoint, num>> _seriesList = [];
final List<_RawChartPoint> _rawData = [];
List<_ChartPoint> _normalizedData = [];
List<charts.Series<_ChartPoint, int>> _seriesList = [];
final List<_ChartPoint> _data = [];
StreamSubscription? _subscription;

@override
Expand All @@ -44,87 +42,56 @@ class _RollingChartState extends State<RollingChart> {
void _listenToStream() {
_subscription = widget.dataSteam.listen((event) {
final (timestamp, value) = event;

setState(() {
_rawData.add(_RawChartPoint(timestamp, value));

_data.add(_ChartPoint(timestamp, value));
// Remove old data outside time window
final ticksPerSecond = pow(10, -widget.timestampExponent).toDouble();
final cutoffTime =
timestamp - (widget.timeWindow * ticksPerSecond).round();
_rawData.removeWhere((data) => data.timestamp < cutoffTime);

int cutoffTime = timestamp - (widget.timeWindow * pow(10, -widget.timestampExponent) as int);
_data.removeWhere((data) => data.time < cutoffTime);

_updateSeries();
});
});
}

void _updateSeries() {
if (_rawData.isEmpty) {
_normalizedData = [];
_seriesList = [];
return;
}

final firstTimestamp = _rawData.first.timestamp;
final secondsPerTick = pow(10, widget.timestampExponent).toDouble();

_normalizedData = _rawData
.map(
(point) => _ChartPoint(
(point.timestamp - firstTimestamp) * secondsPerTick,
point.value,
),
)
.toList(growable: false);

_seriesList = [
charts.Series<_ChartPoint, num>(
id: 'Live Data',
colorFn: (_, __) => charts.MaterialPalette.red.shadeDefault,
domainFn: (_ChartPoint point, _) => point.timeSeconds,
measureFn: (_ChartPoint point, _) => point.value,
data: _normalizedData,
charts.Series<_ChartPoint, int>(
id: 'Live Data',
colorFn: (_, __) => charts.MaterialPalette.red.shadeDefault,
domainFn: (_ChartPoint point, _) => point.time,
measureFn: (_ChartPoint point, _) => point.value,
data: List.of(_data),
),
];
}

@override
Widget build(BuildContext context) {
final filteredPoints = _normalizedData;
final filteredPoints = _data;

final xValues = filteredPoints.map((e) => e.timeSeconds).toList();
final xValues = filteredPoints.map((e) => e.time).toList();
final yValues = filteredPoints.map((e) => e.value).toList();

final double xMin = 0;
final double xMax = max(
widget.timeWindow.toDouble(),
xValues.isNotEmpty ? xValues.reduce((a, b) => a > b ? a : b) : 0,
);
final int? xMin = xValues.isNotEmpty ? xValues.reduce((a, b) => a < b ? a : b) : null;
final int? xMax = xValues.isNotEmpty ? xValues.reduce((a, b) => a > b ? a : b) : null;

final double? yMin =
yValues.isNotEmpty ? yValues.reduce((a, b) => a < b ? a : b) : null;
final double? yMax =
yValues.isNotEmpty ? yValues.reduce((a, b) => a > b ? a : b) : null;
final double? yMin = yValues.isNotEmpty ? yValues.reduce((a, b) => a < b ? a : b) : null;
final double? yMax = yValues.isNotEmpty ? yValues.reduce((a, b) => a > b ? a : b) : null;

return charts.LineChart(
_seriesList,
animate: false,
domainAxis: charts.NumericAxisSpec(
viewport: charts.NumericExtents(xMin, xMax),
tickFormatterSpec: charts.BasicNumericTickFormatterSpec((num? value) {
if (value == null) return '';
final rounded = value.roundToDouble();
if ((value - rounded).abs() < 0.05) {
return '${rounded.toInt()}s';
}
return '${value.toStringAsFixed(1)}s';
}),
viewport: xMin != null && xMax != null
? charts.NumericExtents(xMin, xMax)
: null,
),
primaryMeasureAxis: charts.NumericAxisSpec(
viewport: yMin != null && yMax != null
? charts.NumericExtents(yMin, yMax)
: null,
? charts.NumericExtents(yMin, yMax)
: null,
),
);
}
Expand All @@ -136,16 +103,9 @@ class _RollingChartState extends State<RollingChart> {
}
}

class _RawChartPoint {
final int timestamp;
final double value;

_RawChartPoint(this.timestamp, this.value);
}

class _ChartPoint {
final double timeSeconds;
final int time;
final double value;

_ChartPoint(this.timeSeconds, this.value);
_ChartPoint(this.time, this.value);
}
25 changes: 0 additions & 25 deletions open_wearable/lib/apps/widgets/app_compatibility.dart

This file was deleted.

192 changes: 16 additions & 176 deletions open_wearable/lib/apps/widgets/app_tile.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:open_wearable/apps/widgets/app_compatibility.dart';
import 'package:open_wearable/apps/widgets/apps_page.dart';
import 'package:open_wearable/view_models/wearables_provider.dart';
import 'package:provider/provider.dart';

class AppTile extends StatelessWidget {
final AppInfo app;
Expand All @@ -12,183 +9,26 @@ class AppTile extends StatelessWidget {

@override
Widget build(BuildContext context) {
final connectedWearableNames = context
.watch<WearablesProvider>()
.wearables
.map((wearable) => wearable.name)
.toList(growable: false);
final titleStyle = Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w700,
);

return Card(
margin: const EdgeInsets.only(bottom: 10),
clipBehavior: Clip.antiAlias,
child: InkWell(
return PlatformListTile(
title: PlatformText(app.title),
subtitle: PlatformText(app.description),
leading: SizedBox(
height: 50.0,
width: 50.0,
child: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Image.asset(
app.logoPath,
fit: BoxFit.cover,
),
),
),
onTap: () {
Navigator.push(
context,
platformPageRoute(
context: context,
builder: (context) => app.widget,
),
platformPageRoute(context: context, builder: (context) => app.widget),
);
},
child: Padding(
padding: const EdgeInsets.all(12),
child: Row(
children: [
Container(
height: 62.0,
width: 62.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(14),
border: Border.all(
color: app.accentColor.withValues(alpha: 0.28),
),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(13.0),
child: Image.asset(
app.logoPath,
fit: BoxFit.cover,
),
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(
child: Text(
app.title,
style: titleStyle,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
_LaunchAffordance(accentColor: app.accentColor),
],
),
const SizedBox(height: 3),
Text(
app.description,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: Theme.of(context).textTheme.bodyMedium,
),
const SizedBox(height: 8),
Text(
'Supported devices',
style: Theme.of(context).textTheme.labelSmall?.copyWith(
color: Theme.of(context)
.textTheme
.bodySmall
?.color
?.withValues(alpha: 0.72),
fontWeight: FontWeight.w700,
),
),
const SizedBox(height: 6),
Wrap(
spacing: 6,
runSpacing: 6,
children: app.supportedDevices
.map(
(device) => _SupportedDeviceChip(
text: device,
accentColor: app.accentColor,
isConnected: hasConnectedWearableForPrefix(
devicePrefix: device,
connectedWearableNames: connectedWearableNames,
),
),
)
.toList(),
),
],
),
),
],
),
),
),
);
}
}

class _LaunchAffordance extends StatelessWidget {
final Color accentColor;

const _LaunchAffordance({
required this.accentColor,
});

@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.only(left: 8),
height: 30,
width: 30,
decoration: BoxDecoration(
color: accentColor.withValues(alpha: 0.12),
borderRadius: BorderRadius.circular(999),
),
child: Icon(
Icons.arrow_forward_rounded,
size: 18,
color: accentColor.withValues(alpha: 0.9),
),
);
}
}

class _SupportedDeviceChip extends StatelessWidget {
final String text;
final Color accentColor;
final bool isConnected;

const _SupportedDeviceChip({
required this.text,
required this.accentColor,
required this.isConnected,
});

@override
Widget build(BuildContext context) {
const connectedDotColor = Color(0xFF2F8F5B);
return Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
decoration: BoxDecoration(
color: accentColor.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(999),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
text,
style: Theme.of(context).textTheme.labelSmall?.copyWith(
color: accentColor.withValues(alpha: 0.92),
fontWeight: FontWeight.w600,
),
),
if (isConnected) ...[
const SizedBox(width: 6),
Container(
width: 7,
height: 7,
decoration: const BoxDecoration(
color: connectedDotColor,
shape: BoxShape.circle,
),
),
],
],
),
);
);
}
}
Loading