LCOV - code coverage report
Current view: | top level - src/providers - device_provider.dart (source / functions) |  |  | Hit | Total | Coverage
Test: | lcov.info |  | Lines: | 44 | 44 | 100.0 %
Date: | 2021-10-11 06:57:03 |  | Functions: | 0 | 0 | -

              Line data    Source code
           1             : import 'package:flutter/material.dart';
           2             : import 'package:widgetbook/src/models/device.dart';
           3             : import 'package:widgetbook/src/providers/device_state.dart';
           4             : import 'package:widgetbook/src/providers/provider.dart';
           5             :
           6             : class DeviceBuilder extends StatefulWidget {
           7           1 :   const DeviceBuilder({
           8             :     Key? key,
           9             :     required this.child,
          10             :     required this.availableDevices,
          11             :     required this.currentDevice,
          12           1 :   }) : super(key: key);
          13             :
          14             :   final Widget child;
          15             :   final List<Device> availableDevices;
          16             :   final Device currentDevice;
          17             :
          18           1 :   @override
          19           1 :   _DeviceBuilderState createState() => _DeviceBuilderState();
          20             : }
          21             :
          22             : class _DeviceBuilderState extends State<DeviceBuilder> {
          23             :   late DeviceState state;
          24             :
          25           1 :   @override
          26             :   void initState() {
          27           2 :     state = DeviceState(
          28           2 :       availableDevices: widget.availableDevices,
          29           2 :       currentDevice: widget.currentDevice,
          30             :     );
          31           1 :     super.initState();
          32             :   }
          33             :
          34           1 :   @override
          35             :   Widget build(BuildContext context) {
          36           1 :     return DeviceProvider(
          37           1 :       state: state,
          38           1 :       onStateChanged: (DeviceState state) {
          39           2 :         setState(() {
          40           1 :           this.state = state;
          41             :         });
          42             :       },
          43           2 :       child: widget.child,
          44             :     );
          45             :   }
          46             : }
          47             :
          48             : class DeviceProvider extends Provider<DeviceState> {
          49           1 :   const DeviceProvider({
          50             :     required DeviceState state,
          51             :     required ValueChanged<DeviceState> onStateChanged,
          52             :     required Widget child,
          53             :     Key? key,
          54           1 :   }) : super(
          55             :           state: state,
          56             :           onStateChanged: onStateChanged,
          57             :           child: child,
          58             :           key: key,
          59             :         );
          60             :
          61           1 :   static DeviceProvider? of(BuildContext context) {
          62           1 :     return context.dependOnInheritedWidgetOfExactType<DeviceProvider>();
          63             :   }
          64             :
          65           1 :   void update(List<Device> devices) {
          66           1 :     if (devices.isEmpty) {
          67           1 :       throw ArgumentError.value(
          68             :         devices,
          69             :         'update',
          70             :         'Cannot be empty',
          71             :       );
          72             :     }
          73             :
          74           1 :     emit(
          75           1 :       DeviceState(
          76             :         availableDevices: devices,
          77           1 :         currentDevice: devices.first,
          78             :       ),
          79             :     );
          80             :   }
          81             :
          82           1 :   void selectDevice(Device device) {
          83           1 :     emit(
          84           1 :       DeviceState(
          85           2 :         availableDevices: state.availableDevices,
          86             :         currentDevice: device,
          87             :       ),
          88             :     );
          89             :   }
          90             :
          91           1 :   void nextDevice() {
          92           5 :     final index = state.availableDevices.indexOf(state.currentDevice);
          93           5 :     final nextIndex = (index + 1) % state.availableDevices.length;
          94           3 :     final nextDevice = state.availableDevices[nextIndex];
          95           1 :     emit(
          96           1 :       DeviceState(
          97           2 :         availableDevices: state.availableDevices,
          98             :         currentDevice: nextDevice,
          99             :       ),
         100             :     );
         101             :   }
         102             :
         103           1 :   void previousDevice() {
         104           5 :     final index = state.availableDevices.indexOf(state.currentDevice);
         105             :     final previousIndex =
         106           6 :         index == 0 ? state.availableDevices.length - 1 : index - 1;
         107           3 :     final previousDevice = state.availableDevices[previousIndex];
         108           1 :     emit(
         109           1 :       DeviceState(
         110           2 :         availableDevices: state.availableDevices,
         111             :         currentDevice: previousDevice,
         112             :       ),
         113             :     );
         114             :   }
         115             : }

Generated by: LCOV version 1.15