-
Notifications
You must be signed in to change notification settings - Fork 31
Add support for retrieving and displaying Switch labels #411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should have a separate getLabels
method, but instead return the labels with the getNumberOfPositions
/// ``` | ||
/// | ||
/// For more information, see [Switch component](https://docs.viam.com/dev/reference/apis/components/servo/#getnumberofpositions). | ||
Future<List<String>> getLabels({Map<String, dynamic>? extra}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be folded into getNumberOfPositions
because you're likely not displaying labels without getting number of positions first, so it doesn't make sense to have 2 distinct calls for it (IMO)
@@ -30,12 +30,21 @@ abstract class Switch extends Resource { | |||
/// Get the number of available positions (int) of the [Switch]. | |||
/// | |||
/// ``` | |||
/// await myServo.getNumberOfPositions(); | |||
/// await mySwitch.getNumberOfPositions(); | |||
/// ``` | |||
/// | |||
/// For more information, see [Switch component](https://docs.viam.com/dev/reference/apis/components/servo/#getnumberofpositions). | |||
Future<int> getNumberOfPositions({Map<String, dynamic>? extra}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So return both int
and List<String>
here (I don't think you can return tuples in Flutter, so you might want to alias GetNumberOfPositionsResponse
to PositionsAndLabels
or something similar and return that)
test('getLabels should return the expected labels', () async { | ||
final labels = await nswitch.getLabels(); | ||
expect(labels, nswitch.labels); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great testing!
class PositionsInfo { | ||
final int numberOfPositions; | ||
final List<String>? labels; | ||
|
||
PositionsInfo({required this.numberOfPositions, this.labels}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this class mirrors the attributes in GetNumberOfPositionsResponse
, it's probably easier to typealias (see camera example)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok last thing!!!
Future<void> _getLabels() async { | ||
try { | ||
final positionsInfo = await widget.nswitch.getNumberOfPositionsWithLabels(); | ||
if (mounted) { | ||
setState(() { | ||
labels = positionsInfo.labels; | ||
}); | ||
} | ||
} catch (e) { | ||
error = e as Error; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can delete this method, and set the labels state when you get it above in the _getNumberOfPositions
method
thanks for completing this @njooma |
Added getLabels() to Switch interface and SwitchClient
Updated ViamSwitchWidget to display labels if available