-
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(unknown_widget.dart,device_provider.dart,unknown_provider.dart):…
… Add a provider for the unknown trait devices and a base provider
- Loading branch information
Showing
3 changed files
with
118 additions
and
2 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 |
---|---|---|
@@ -1,9 +1,51 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'package:yonomi_device_widgets/assets/traits/unknown_item_icon.dart'; | ||
import 'package:yonomi_device_widgets/components/arc.dart'; | ||
import 'package:yonomi_device_widgets/providers/unknown_provider.dart'; | ||
import 'package:yonomi_device_widgets/ui/widget_style_constants.dart'; | ||
|
||
class UnknownWidget extends StatelessWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
// TODO: implement build | ||
throw UnimplementedError(); | ||
final provider = Provider.of<UnknownProvider>(context, listen: true); | ||
|
||
return provider.loadingDetail | ||
? Center(child: CircularProgressIndicator()) | ||
: Column(mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[ | ||
Row(children: <Widget>[ | ||
Text( | ||
provider.deviceDetails.displayName, | ||
style: Theme.of(context).textTheme.headline6, | ||
), | ||
]), | ||
SizedBox( | ||
height: 80, | ||
), | ||
Center( | ||
child: Arc( | ||
showThumb: false, | ||
centerWidget: SizedBox( | ||
width: WidgetStyleConstants.defaultDeviceWidgetSize, | ||
height: WidgetStyleConstants.defaultDeviceWidgetSize, | ||
child: GestureDetector( | ||
child: getUnknownStateIcon(provider), | ||
onTap: () { | ||
provider.performAction(provider.deviceDetails.id); | ||
}, | ||
)), | ||
initialValue: 0, | ||
maxValue: 1.0, | ||
onFinalSetPoint: (double value) {})) | ||
]); | ||
} | ||
|
||
Widget getUnknownStateIcon(UnknownProvider provider) { | ||
return (provider.loadingDetail || provider.loadingAction) | ||
? Center(child: CircularProgressIndicator()) | ||
: UnknownItemIcon( | ||
size: WidgetStyleConstants.defaultDeviceWidgetSize, | ||
color: WidgetStyleConstants.deviceDetailIconColorActive); | ||
} | ||
} |
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,34 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:yonomi_platform_sdk/yonomi-sdk.dart'; | ||
|
||
typedef GetDeviceDetails = Future<Device> Function( | ||
Request request, String deviceId); | ||
|
||
abstract class DeviceProvider extends ChangeNotifier { | ||
bool loadingDetail = false; | ||
bool loadingAction = false; | ||
|
||
late Request _request; | ||
late Device _deviceDetails; | ||
|
||
Device get deviceDetails => _deviceDetails; | ||
Request get request => _request; | ||
|
||
DeviceProvider(Request request, String deviceId, | ||
{GetDeviceDetails getDetails = DevicesRepository.getDeviceDetails}) { | ||
this._request = request; | ||
getDeviceDetails(deviceId, getDetails: getDetails); | ||
} | ||
|
||
Future<void> getDeviceDetails(String deviceId, | ||
{GetDeviceDetails getDetails = | ||
DevicesRepository.getDeviceDetails}) async { | ||
loadingDetail = true; | ||
notifyListeners(); | ||
|
||
_deviceDetails = await getDetails(_request, deviceId); | ||
|
||
loadingDetail = false; | ||
notifyListeners(); | ||
} | ||
} |
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,40 @@ | ||
import 'package:yonomi_platform_sdk/yonomi-sdk.dart'; | ||
|
||
import 'device_provider.dart'; | ||
|
||
typedef DoAction = Future<void> Function(Request request, String deviceId); | ||
typedef GetDeviceDetails = Future<Device> Function( | ||
Request request, String deviceId); | ||
|
||
class UnknownProvider extends DeviceProvider { | ||
UnknownProvider(Request request, String deviceId) : super(request, deviceId); | ||
|
||
Trait getTrait() { | ||
return deviceDetails.traits | ||
.skipWhile((trait) => !(trait is UnknownTrait)) | ||
.first; | ||
} | ||
|
||
Future<void> performAction(String deviceId, | ||
{DoAction doAction = doAction}) async { | ||
loadingAction = true; | ||
notifyListeners(); | ||
|
||
final traitValue = getTrait().state.value; | ||
await doAction(request, deviceId); | ||
|
||
var retries = 0; | ||
while (traitValue == getTrait().state.value && retries++ < 10) { | ||
getDeviceDetails(deviceId).timeout(Duration(milliseconds: 750)); | ||
await Future.delayed(Duration(milliseconds: 750)); | ||
} | ||
|
||
loadingAction = false; | ||
notifyListeners(); | ||
} | ||
|
||
// todo(Delete after repository function exists to assign to doAction) | ||
static Future<void> doAction(Request request, String deviceId) async { | ||
throw UnimplementedError(); | ||
} | ||
} |