Skip to content

Commit

Permalink
feat(power_trait_provider.dart): expose an error message along with e…
Browse files Browse the repository at this point in the history
…rror state
  • Loading branch information
ses110 committed Dec 10, 2021
1 parent e940029 commit e0a9260
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
42 changes: 30 additions & 12 deletions lib/providers/power_trait_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class PowerTraitProvider extends ChangeNotifier {
bool _isLoading = false;
bool _isPerformingAction = false;
bool _isInErrorState = false;
String _latestErrorMsg = "An error occurred.";

late String _deviceId;

Expand Down Expand Up @@ -44,63 +45,80 @@ class PowerTraitProvider extends ChangeNotifier {

this.deviceDetail = await getDeviceDetails(_request, _deviceId);

return _deviceDetail;
return deviceDetail;
}

Future<void> sendPowerOnOffAction(bool setOnOff,
Future<void> sendPowerOnOffAction(bool desiredOnOffState,
{GetDeviceDetailsMethod getDetails = DevicesRepository.getDeviceDetails,
SendPowerMethod sendPowerMethod =
PowerRepository.sendPowerAction}) async {
if (!isPerformingAction) {
setPerformingAction = true;

try {
await sendPowerMethod(_request, this._deviceId, setOnOff);
await sendPowerMethod(_request, this._deviceId, desiredOnOffState);

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

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

PowerTrait? getPowerTrait() {
return _deviceDetail?.traits.first as PowerTrait?;
try {
return _deviceDetail?.traits.first as PowerTrait?;
} catch (error) {
return null;
}
}

bool get getOnOffState {
return getPowerTrait()?.state.value ?? false;
}

set setLoading(bool newIsLoading) {
bool get isBusy => isLoading || isPerformingAction;

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

bool get isLoading => _isLoading;

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

bool get isPerformingAction => _isPerformingAction;

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

bool get isInErrorState => _isInErrorState;

set setErrorMessage(String errorMsg) {
if (errorMsg.isEmpty) errorMsg = "An error occurred.";
_latestErrorMsg = errorMsg;
}

String get getErrorMessage => _latestErrorMsg;
}
7 changes: 4 additions & 3 deletions lib/traits/trait_based_device_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@ class PowerTraitWidget extends StatelessWidget {
create: (_) => PowerTraitProvider(request, deviceId),
child: Consumer<PowerTraitProvider>(
builder: (_, powerDeviceNotifier, child) {
if (powerDeviceNotifier.isLoading ||
powerDeviceNotifier.isPerformingAction) {
if (powerDeviceNotifier.isBusy) {
return CircularProgressIndicator();
} else if (powerDeviceNotifier.isInErrorState) {
print(
"PowerDeviceTrait Error: ${powerDeviceNotifier.getErrorMessage}");
return Icon(Icons.error);
} else {
return Switch(
value: powerDeviceNotifier.getPowerTrait()?.state.value ?? false,
value: powerDeviceNotifier.getOnOffState,
onChanged: (bool onOff) {
print("Power switch value set to: ${onOff}");
powerDeviceNotifier.sendPowerOnOffAction(onOff);
Expand Down

0 comments on commit e0a9260

Please sign in to comment.