Skip to content

Commit

Permalink
feat(power_trait_provider): reflect error state in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
ses110 committed Dec 10, 2021
1 parent 84a96ac commit cd3d154
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
41 changes: 29 additions & 12 deletions lib/providers/power_trait_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class PowerTraitProvider extends ChangeNotifier {
final int MAX_RETRIES = 10;

bool _isLoading = false;

bool _isPerformingAction = false;
bool _isInErrorState = false;

late String _deviceId;

Expand Down Expand Up @@ -54,18 +54,22 @@ class PowerTraitProvider extends ChangeNotifier {
if (!isPerformingAction) {
setPerformingAction = true;

await sendPowerMethod(_request, this._deviceId, setOnOff);

int numRetries = 0;
while (getPowerTrait()?.state.value != setOnOff &&
numRetries < MAX_RETRIES) {
_deviceDetail = await getDetails(_request, _deviceId);

await Future.delayed(Duration(milliseconds: 750));
numRetries++;
try {
await sendPowerMethod(_request, this._deviceId, setOnOff);

int numRetries = 0;
while (getPowerTrait()?.state.value != setOnOff &&
numRetries < MAX_RETRIES) {
_deviceDetail = await getDetails(_request, _deviceId);

await Future.delayed(Duration(milliseconds: 750));
numRetries++;
}
setPerformingAction = false;
} catch (error) {
setErrorState = true;
Future.delayed(Duration(seconds: 1)).then((_) => setErrorState = false);
}

setPerformingAction = false;
}
}

Expand All @@ -74,16 +78,29 @@ class PowerTraitProvider extends ChangeNotifier {
}

set setLoading(bool newIsLoading) {
_isPerformingAction = false;
_isLoading = newIsLoading;
_isInErrorState = false;
notifyListeners();
}

bool get isLoading => _isLoading;

set setPerformingAction(bool newIsPerformingAction) {
_isPerformingAction = newIsPerformingAction;
_isLoading = false;
_isInErrorState = false;
notifyListeners();
}

bool get isPerformingAction => _isPerformingAction;

set setErrorState(bool newIsError) {
_isInErrorState = newIsError;
_isLoading = false;
_isPerformingAction = false;
notifyListeners();
}

bool get isInErrorState => _isInErrorState;
}
8 changes: 5 additions & 3 deletions lib/traits/trait_based_device_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,14 @@ class PowerTraitWidget extends StatelessWidget {
if (powerDeviceNotifier.isLoading ||
powerDeviceNotifier.isPerformingAction) {
return CircularProgressIndicator();
} else if (powerDeviceNotifier.isInErrorState) {
return Icon(Icons.error);
} else {
return Switch(
value: powerDeviceNotifier.getPowerTrait()?.state.value ?? false,
onChanged: (bool newValue) {
print("Power Switch value set to: ${newValue}");
powerDeviceNotifier.sendPowerOnOffAction(newValue);
onChanged: (bool onOff) {
print("Power switch value set to: ${onOff}");
powerDeviceNotifier.sendPowerOnOffAction(onOff);
},
);
}
Expand Down

0 comments on commit cd3d154

Please sign in to comment.