Skip to content

Commit

Permalink
fix: Fix the lock icons not updating on actions
Browse files Browse the repository at this point in the history
  • Loading branch information
rlperez committed Jan 18, 2022
1 parent cf17d1d commit 1da3ac3
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 28 deletions.
13 changes: 8 additions & 5 deletions lib/providers/device_provider.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:yonomi_device_widgets/providers/widget_state.dart';
import 'package:yonomi_platform_sdk/yonomi-sdk.dart';
Expand Down Expand Up @@ -53,19 +55,20 @@ abstract class DeviceProvider extends ChangeNotifier {
_latestErrorMsg = errorMsg;
}

Future<void> performAction<T>(T state, T desiredState, Function action,
Future<void> performAction<T>(
T desiredState, Function getState, Function action,
{GetDeviceDetailsMethod getDetails =
DevicesRepository.getDeviceDetails}) async {
if (!isPerformingAction) {
setState = WidgetState.performingAction;

try {
await action.call();

await action();
int numRetries = 0;
T state = getState();
while (state != desiredState && numRetries < MAX_RETRIES) {
await getDetails(_request, _deviceId);

_deviceDetail = await getDetails(_request, _deviceId);
state = getState();
await Future.delayed(Duration(milliseconds: RETRY_DELAY_MS));
numRetries++;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/providers/lock_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class LockProvider extends DeviceProvider {
SendLockUnlockFunction sendLockUnlock =
LockRepository.sendLockUnlockAction}) async {
return performAction<bool>(
isLocked, setLock, () => sendLockUnlock(_request, deviceId, setLock),
setLock, () => isLocked,
() => sendLockUnlock(_request, deviceId, setLock),
getDetails: getDetails);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/providers/power_trait_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PowerTraitProvider extends DeviceProvider {
{GetDeviceDetailsMethod getDetails = DevicesRepository.getDeviceDetails,
SendPowerMethod sendPowerMethod =
PowerRepository.sendPowerAction}) async {
return performAction<bool>(getOnOffState, desiredOnOffState,
return performAction<bool>(desiredOnOffState, () => getOnOffState,
() => sendPowerMethod(_request, this._deviceId, desiredOnOffState),
getDetails: getDetails);
}
Expand Down
8 changes: 5 additions & 3 deletions lib/providers/thermostat_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ class ThermostatProvider extends DeviceProvider {
Future<void> setPointAction(String deviceId, double temperature,
{SetPointActionFunction setPoint =
ThermostatRepository.setPointThermostat}) async {
return performAction<double>(thermostatTargetTemperature, temperature,
return performAction<double>(temperature, () => thermostatTargetTemperature,
() => setPoint(_request, deviceId, temperature));
}

Future<void> setThermostatMode(String deviceId, GThermostatMode mode,
{SetModeFunction setMode = ThermostatRepository.setMode}) async {
return performAction<GThermostatMode?>(getThermostatTrait()?.state.value,
mode, () => setMode(_request, deviceId, mode));
return performAction<GThermostatMode?>(
mode,
() => getThermostatTrait()?.state.value,
() => setMode(_request, deviceId, mode));
}

double get thermostatTargetTemperature =>
Expand Down
5 changes: 3 additions & 2 deletions test/components/modes_toolbar_test.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ class MockThermostatProvider extends _i1.Mock
super.noSuchMethod(Invocation.method(#setErrorState, [errorMsg]),
returnValueForMissingStub: null);
@override
_i4.Future<void> performAction<T>(T? state, T? desiredState, Function? action,
_i4.Future<void> performAction<T>(
T? desiredState, Function? getState, Function? action,
{_i7.GetDeviceDetailsMethod? getDetails =
_i8.DevicesRepository.getDeviceDetails}) =>
(super.noSuchMethod(
Invocation.method(#performAction, [state, desiredState, action],
Invocation.method(#performAction, [desiredState, getState, action],
{#getDetails: getDetails}),
returnValue: Future<void>.value(),
returnValueForMissingStub: Future<void>.value()) as _i4.Future<void>);
Expand Down
20 changes: 12 additions & 8 deletions test/traits/detail_screen_test.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ class MockTraitDetailProvider extends _i1.Mock
super.noSuchMethod(Invocation.method(#setErrorState, [errorMsg]),
returnValueForMissingStub: null);
@override
_i6.Future<void> performAction<T>(T? state, T? desiredState, Function? action,
_i6.Future<void> performAction<T>(
T? desiredState, Function? getState, Function? action,
{_i7.GetDeviceDetailsMethod? getDetails =
_i8.DevicesRepository.getDeviceDetails}) =>
(super.noSuchMethod(
Invocation.method(#performAction, [state, desiredState, action],
Invocation.method(#performAction, [desiredState, getState, action],
{#getDetails: getDetails}),
returnValue: Future<void>.value(),
returnValueForMissingStub: Future<void>.value()) as _i6.Future<void>);
Expand Down Expand Up @@ -201,11 +202,12 @@ class MockLockProvider extends _i1.Mock implements _i10.LockProvider {
super.noSuchMethod(Invocation.method(#setErrorState, [errorMsg]),
returnValueForMissingStub: null);
@override
_i6.Future<void> performAction<T>(T? state, T? desiredState, Function? action,
_i6.Future<void> performAction<T>(
T? desiredState, Function? getState, Function? action,
{_i7.GetDeviceDetailsMethod? getDetails =
_i8.DevicesRepository.getDeviceDetails}) =>
(super.noSuchMethod(
Invocation.method(#performAction, [state, desiredState, action],
Invocation.method(#performAction, [desiredState, getState, action],
{#getDetails: getDetails}),
returnValue: Future<void>.value(),
returnValueForMissingStub: Future<void>.value()) as _i6.Future<void>);
Expand Down Expand Up @@ -297,11 +299,12 @@ class MockPowerTraitProvider extends _i1.Mock
super.noSuchMethod(Invocation.method(#setErrorState, [errorMsg]),
returnValueForMissingStub: null);
@override
_i6.Future<void> performAction<T>(T? state, T? desiredState, Function? action,
_i6.Future<void> performAction<T>(
T? desiredState, Function? getState, Function? action,
{_i7.GetDeviceDetailsMethod? getDetails =
_i8.DevicesRepository.getDeviceDetails}) =>
(super.noSuchMethod(
Invocation.method(#performAction, [state, desiredState, action],
Invocation.method(#performAction, [desiredState, getState, action],
{#getDetails: getDetails}),
returnValue: Future<void>.value(),
returnValueForMissingStub: Future<void>.value()) as _i6.Future<void>);
Expand Down Expand Up @@ -382,11 +385,12 @@ class MockBatteryLevelProvider extends _i1.Mock
super.noSuchMethod(Invocation.method(#setErrorState, [errorMsg]),
returnValueForMissingStub: null);
@override
_i6.Future<void> performAction<T>(T? state, T? desiredState, Function? action,
_i6.Future<void> performAction<T>(
T? desiredState, Function? getState, Function? action,
{_i7.GetDeviceDetailsMethod? getDetails =
_i8.DevicesRepository.getDeviceDetails}) =>
(super.noSuchMethod(
Invocation.method(#performAction, [state, desiredState, action],
Invocation.method(#performAction, [desiredState, getState, action],
{#getDetails: getDetails}),
returnValue: Future<void>.value(),
returnValueForMissingStub: Future<void>.value()) as _i6.Future<void>);
Expand Down
5 changes: 3 additions & 2 deletions test/traits/mixins/battery_widget_testing.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ class MockBatteryLevelProvider extends _i1.Mock
super.noSuchMethod(Invocation.method(#setErrorState, [errorMsg]),
returnValueForMissingStub: null);
@override
_i4.Future<void> performAction<T>(T? state, T? desiredState, Function? action,
_i4.Future<void> performAction<T>(
T? desiredState, Function? getState, Function? action,
{_i5.GetDeviceDetailsMethod? getDetails =
_i6.DevicesRepository.getDeviceDetails}) =>
(super.noSuchMethod(
Invocation.method(#performAction, [state, desiredState, action],
Invocation.method(#performAction, [desiredState, getState, action],
{#getDetails: getDetails}),
returnValue: Future<void>.value(),
returnValueForMissingStub: Future<void>.value()) as _i4.Future<void>);
Expand Down
5 changes: 3 additions & 2 deletions test/traits/mixins/lock_widget_testing.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ class MockLockProvider extends _i1.Mock implements _i2.LockProvider {
super.noSuchMethod(Invocation.method(#setErrorState, [errorMsg]),
returnValueForMissingStub: null);
@override
_i4.Future<void> performAction<T>(T? state, T? desiredState, Function? action,
_i4.Future<void> performAction<T>(
T? desiredState, Function? getState, Function? action,
{_i5.GetDeviceDetailsMethod? getDetails =
_i6.DevicesRepository.getDeviceDetails}) =>
(super.noSuchMethod(
Invocation.method(#performAction, [state, desiredState, action],
Invocation.method(#performAction, [desiredState, getState, action],
{#getDetails: getDetails}),
returnValue: Future<void>.value(),
returnValueForMissingStub: Future<void>.value()) as _i4.Future<void>);
Expand Down
5 changes: 3 additions & 2 deletions test/traits/mixins/power_widget_testing.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ class MockPowerTraitProvider extends _i1.Mock
super.noSuchMethod(Invocation.method(#setErrorState, [errorMsg]),
returnValueForMissingStub: null);
@override
_i4.Future<void> performAction<T>(T? state, T? desiredState, Function? action,
_i4.Future<void> performAction<T>(
T? desiredState, Function? getState, Function? action,
{_i5.GetDeviceDetailsMethod? getDetails =
_i6.DevicesRepository.getDeviceDetails}) =>
(super.noSuchMethod(
Invocation.method(#performAction, [state, desiredState, action],
Invocation.method(#performAction, [desiredState, getState, action],
{#getDetails: getDetails}),
returnValue: Future<void>.value(),
returnValueForMissingStub: Future<void>.value()) as _i4.Future<void>);
Expand Down
5 changes: 3 additions & 2 deletions test/widgets/components/modes_toolbar_test.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ class MockThermostatProvider extends _i1.Mock
super.noSuchMethod(Invocation.method(#setErrorState, [errorMsg]),
returnValueForMissingStub: null);
@override
_i4.Future<void> performAction<T>(T? state, T? desiredState, Function? action,
_i4.Future<void> performAction<T>(
T? desiredState, Function? getState, Function? action,
{_i7.GetDeviceDetailsMethod? getDetails =
_i8.DevicesRepository.getDeviceDetails}) =>
(super.noSuchMethod(
Invocation.method(#performAction, [state, desiredState, action],
Invocation.method(#performAction, [desiredState, getState, action],
{#getDetails: getDetails}),
returnValue: Future<void>.value(),
returnValueForMissingStub: Future<void>.value()) as _i4.Future<void>);
Expand Down

0 comments on commit 1da3ac3

Please sign in to comment.