LCOV - code coverage report
Current view: | top level - src/providers - canvas_provider.dart (source / functions) |  |  | Hit | Total | Coverage
Test: | lcov.info |  | Lines: | 40 | 40 | 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/organizers/story.dart';
           3             : import 'package:widgetbook/src/providers/canvas_state.dart';
           4             : import 'package:widgetbook/src/providers/provider.dart';
           5             : import 'package:widgetbook/src/repositories/selected_story_repository.dart';
           6             : import 'package:widgetbook/src/repositories/story_repository.dart';
           7             :
           8             : class CanvasBuilder extends StatefulWidget {
           9           1 :   const CanvasBuilder({
          10             :     Key? key,
          11             :     required this.child,
          12             :     required this.storyRepository,
          13             :     required this.selectedStoryRepository,
          14           1 :   }) : super(key: key);
          15             :
          16             :   final Widget child;
          17             :   final StoryRepository storyRepository;
          18             :   final SelectedStoryRepository selectedStoryRepository;
          19             :
          20           1 :   @override
          21           1 :   _CanvasBuilderState createState() => _CanvasBuilderState();
          22             : }
          23             :
          24             : class _CanvasBuilderState extends State<CanvasBuilder> {
          25             :   CanvasState state = CanvasState.unselected();
          26             :   late CanvasProvider provider;
          27             :
          28           1 :   void setProvider() {
          29           2 :     provider = CanvasProvider(
          30           2 :       storyRepository: widget.storyRepository,
          31           2 :       selectedStoryRepository: widget.selectedStoryRepository,
          32           1 :       state: state,
          33           1 :       onStateChanged: (CanvasState state) {
          34           2 :         setState(() {
          35           1 :           this.state = state;
          36           1 :           setProvider();
          37             :         });
          38             :       },
          39           2 :       child: widget.child,
          40             :     );
          41             :   }
          42             :
          43           1 :   @override
          44             :   void initState() {
          45           1 :     setProvider();
          46           5 :     widget.storyRepository.getStreamOfItems().forEach((_) {
          47           2 :       provider.updateStory();
          48             :     });
          49             :
          50           1 :     super.initState();
          51             :   }
          52             :
          53           1 :   @override
          54             :   Widget build(BuildContext context) {
          55           1 :     return provider;
          56             :   }
          57             : }
          58             :
          59             : class CanvasProvider extends Provider<CanvasState> {
          60           1 :   const CanvasProvider({
          61             :     required this.storyRepository,
          62             :     required this.selectedStoryRepository,
          63             :     required CanvasState state,
          64             :     required ValueChanged<CanvasState> onStateChanged,
          65             :     required Widget child,
          66             :     Key? key,
          67           1 :   }) : super(
          68             :           state: state,
          69             :           onStateChanged: onStateChanged,
          70             :           child: child,
          71             :           key: key,
          72             :         );
          73             :
          74             :   final StoryRepository storyRepository;
          75             :   final SelectedStoryRepository selectedStoryRepository;
          76             :
          77           1 :   static CanvasProvider? of(BuildContext context) {
          78           1 :     return context.dependOnInheritedWidgetOfExactType<CanvasProvider>();
          79             :   }
          80             :
          81           1 :   void updateStory() {
          82           2 :     if (state.isStorySelected) {
          83           3 :       final currentStoryPath = state.selectedStory!.path;
          84           2 :       if (storyRepository.doesItemExist(currentStoryPath)) {
          85           2 :         final story = storyRepository.getItem(currentStoryPath);
          86           1 :         selectStory(story);
          87             :       } else {
          88           1 :         deselectStory();
          89             :       }
          90             :     }
          91             :   }
          92             :
          93           1 :   void selectStory(Story? story) {
          94           2 :     selectedStoryRepository.setItem(story);
          95           1 :     emit(
          96           1 :       CanvasState(
          97             :         selectedStory: story,
          98             :       ),
          99             :     );
         100             :   }
         101             :
         102           1 :   void deselectStory() {
         103           2 :     selectedStoryRepository.deleteItem();
         104           1 :     emit(
         105           1 :       CanvasState.unselected(),
         106             :     );
         107             :   }
         108             : }

Generated by: LCOV version 1.15