-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(power_trait_provider.dart): introduce PowerTraitProvider
- Loading branch information
Showing
3 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:yonomi_device_widgets/devices/trait_based_device_screen.dart'; | ||
import 'package:yonomi_platform_sdk/yonomi-sdk.dart'; | ||
|
||
typedef GetDeviceDetailsMethod = Future<Device> Function( | ||
Request request, String id); | ||
|
||
typedef SendPowerMethod = Future<void> Function( | ||
Request request, String id, bool onOff); | ||
|
||
class PowerTraitProvider extends ChangeNotifier { | ||
final int MAX_RETRIES = 10; | ||
|
||
bool _isLoading = false; | ||
|
||
bool _isPerformingAction = false; | ||
|
||
late String _deviceId; | ||
|
||
late Request _request; | ||
|
||
Device? _deviceDetail; | ||
|
||
Device? get deviceDetail => _deviceDetail; | ||
|
||
set deviceDetail(Device? detail) { | ||
_deviceDetail = detail; | ||
|
||
setLoading = false; | ||
} | ||
|
||
PowerTraitProvider(Request request, String deviceId, | ||
{GetDeviceDetailsMethod getDetails = | ||
DevicesRepository.getDeviceDetails}) { | ||
_request = request; | ||
_deviceId = deviceId; | ||
fetchData(getDeviceDetails: getDetails); | ||
} | ||
|
||
Future<Device?> fetchData( | ||
{GetDeviceDetailsMethod getDeviceDetails = | ||
DevicesRepository.getDeviceDetails}) async { | ||
setLoading = true; | ||
|
||
this.deviceDetail = await getDeviceDetails(_request, _deviceId); | ||
|
||
return _deviceDetail; | ||
} | ||
|
||
Future<void> sendPowerOnOffAction(bool setOnOff, | ||
{GetDeviceDetailsMethod getDetails = DevicesRepository.getDeviceDetails, | ||
SendPowerMethod sendPowerMethod = | ||
PowerRepository.sendPowerAction}) async { | ||
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++; | ||
} | ||
|
||
setPerformingAction = false; | ||
} | ||
} | ||
|
||
PowerTrait? getPowerTrait() { | ||
return _deviceDetail?.traits.first as PowerTrait?; | ||
} | ||
|
||
set setLoading(bool newIsLoading) { | ||
_isLoading = newIsLoading; | ||
notifyListeners(); | ||
} | ||
|
||
bool get isLoading => _isLoading; | ||
|
||
set setPerformingAction(bool newIsPerformingAction) { | ||
_isPerformingAction = newIsPerformingAction; | ||
notifyListeners(); | ||
} | ||
|
||
bool get isPerformingAction => _isPerformingAction; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/annotations.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
import 'package:yonomi_device_widgets/devices/trait_based_device_screen.dart'; | ||
import 'package:yonomi_device_widgets/providers/power_trait_provider.dart'; | ||
import 'package:yonomi_platform_sdk/third_party/yonomi_graphql_schema/schema.docs.schema.gql.dart'; | ||
import 'package:yonomi_platform_sdk/yonomi-sdk.dart'; | ||
|
||
import 'power_trait_provider_test.mocks.dart'; | ||
|
||
class GetDeviceDetailsMethod extends Mock { | ||
Future<Device> call(Request request, String id); | ||
} | ||
|
||
class SendPowerMethod extends Mock { | ||
Future<void> call(Request request, String id, bool onOff); | ||
} | ||
|
||
@GenerateMocks([GetDeviceDetailsMethod, SendPowerMethod]) | ||
void main() { | ||
test('Calling sendOnOff calls repository method', () async { | ||
String deviceId = 'aDeviceId'; | ||
Request request = Request("", {}); | ||
final mockDeviceDetailsMethod = MockGetDeviceDetailsMethod(); | ||
final device = Device( | ||
deviceId, | ||
'name', | ||
'description', | ||
'manufacturerName', | ||
'model', | ||
null, | ||
GDateTime('value'), | ||
GDateTime('value'), | ||
[ | ||
PowerTrait('name', UnknownState()), | ||
], | ||
); | ||
when(mockDeviceDetailsMethod.call(request, deviceId)) | ||
.thenAnswer((_) => Future.value(device)); | ||
final mockSendPowerMethod = MockSendPowerMethod(); | ||
PowerTraitProvider powerProvider = await PowerTraitProvider( | ||
request, deviceId, | ||
getDetails: mockDeviceDetailsMethod); | ||
|
||
await powerProvider.sendPowerOnOffAction(true, | ||
getDetails: mockDeviceDetailsMethod, | ||
sendPowerMethod: mockSendPowerMethod); | ||
|
||
verify(mockDeviceDetailsMethod(request, deviceId)).called(greaterThan(0)); | ||
|
||
verify(mockSendPowerMethod(request, deviceId, true)).called(greaterThan(0)); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Mocks generated by Mockito 5.0.15 from annotations | ||
// in yonomi_device_widgets/test/providers/power_provider_test.dart. | ||
// Do not manually edit this file. | ||
|
||
import 'dart:async' as _i4; | ||
|
||
import 'package:mockito/mockito.dart' as _i1; | ||
import 'package:yonomi_platform_sdk/yonomi-sdk.dart' as _i2; | ||
|
||
import 'power_trait_provider_test.dart' as _i3; | ||
|
||
// ignore_for_file: avoid_redundant_argument_values | ||
// ignore_for_file: avoid_setters_without_getters | ||
// ignore_for_file: comment_references | ||
// ignore_for_file: implementation_imports | ||
// ignore_for_file: invalid_use_of_visible_for_testing_member | ||
// ignore_for_file: prefer_const_constructors | ||
// ignore_for_file: unnecessary_parenthesis | ||
|
||
class _FakeDevice_0 extends _i1.Fake implements _i2.Device {} | ||
|
||
/// A class which mocks [GetDeviceDetailsMethod]. | ||
/// | ||
/// See the documentation for Mockito's code generation for more information. | ||
class MockGetDeviceDetailsMethod extends _i1.Mock | ||
implements _i3.GetDeviceDetailsMethod { | ||
MockGetDeviceDetailsMethod() { | ||
_i1.throwOnMissingStub(this); | ||
} | ||
|
||
@override | ||
_i4.Future<_i2.Device> call(_i2.Request? request, String? id) => | ||
(super.noSuchMethod(Invocation.method(#call, [request, id]), | ||
returnValue: Future<_i2.Device>.value(_FakeDevice_0())) | ||
as _i4.Future<_i2.Device>); | ||
@override | ||
String toString() => super.toString(); | ||
} | ||
|
||
/// A class which mocks [SendPowerMethod]. | ||
/// | ||
/// See the documentation for Mockito's code generation for more information. | ||
class MockSendPowerMethod extends _i1.Mock implements _i3.SendPowerMethod { | ||
MockSendPowerMethod() { | ||
_i1.throwOnMissingStub(this); | ||
} | ||
|
||
@override | ||
_i4.Future<void> call(_i2.Request? request, String? id, bool? onOff) => | ||
(super.noSuchMethod(Invocation.method(#call, [request, id, onOff]), | ||
returnValue: Future<void>.value(), | ||
returnValueForMissingStub: Future<void>.value()) as _i4.Future<void>); | ||
@override | ||
String toString() => super.toString(); | ||
} |