Skip to content

Commit

Permalink
RSDK-6116: Mobile app should auto refresh board's analog readings (#185)
Browse files Browse the repository at this point in the history
* RSDK-6116: Mobile app should auto refresh board's analog readings

* Cancel timer when widget is destroyed

* Made refreshInterval settable by user

* Added docstring
  • Loading branch information
Ojima Abraham authored Mar 15, 2024
1 parent 40b4a4d commit c65b3e3
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/widgets/resources/board.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:viam_sdk/viam_sdk.dart';
Expand All @@ -12,9 +14,14 @@ class ViamBoardWidget extends StatefulWidget {
/// The [Board]
final Board board;

/// The refresh interval
/// This field determines how frequently the user wants to auto refresh the board's status readings.
final Duration refreshInterval;

const ViamBoardWidget({
super.key,
required this.board,
this.refreshInterval = const Duration(seconds: 1),
});

@override
Expand All @@ -34,6 +41,8 @@ class _ViamBoardWidgetState extends State<ViamBoardWidget> {
final _pwmFreqFormKey = GlobalKey<FormState>();
int pwmFrequency = 0;

Timer? timer;

BoardStatus status = const BoardStatus({}, {});

Future<void> _fetchStatus() async {
Expand All @@ -43,10 +52,27 @@ class _ViamBoardWidgetState extends State<ViamBoardWidget> {
});
}

void refresh() {
_fetchStatus();
}

void _createTimer() {
timer = Timer.periodic(widget.refreshInterval, (_) {
refresh();
});
}

@override
void initState() {
super.initState();
_fetchStatus();
_createTimer();
}

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

void _dismissKeyboard() {
Expand Down

0 comments on commit c65b3e3

Please sign in to comment.