@@ -2,6 +2,9 @@ import 'dart:async';
22
33import 'package:bloc/bloc.dart' ;
44import 'package:dslideshow_backend/command.dart' ;
5+ import 'package:dslideshow_backend/config.dart' ;
6+ import 'package:dslideshow_flutter/features/slideshow/presentation/bloc/status/slideshow_status_bloc.dart' ;
7+ import 'package:dslideshow_flutter/features/theme/presentation/src/theme_colors.dart' ;
58import 'package:dslideshow_flutter/src/service/frontend.dart' ;
69import 'package:flutter/material.dart' ;
710import 'package:freezed_annotation/freezed_annotation.dart' ;
@@ -12,30 +15,72 @@ part 'buttons_hint_bloc.freezed.dart';
1215
1316class ButtonsHintBloc extends Bloc <ButtonsHintEvent , ButtonsHintState > {
1417 final FrontendService frontendService;
18+ final SlideshowStatusBloc statusBloc;
19+ final AppConfig appConfig;
1520
1621 late StreamSubscription <ButtonEvent > _onButtonEventSubscription;
22+ late StreamSubscription _onStatusSubscription;
23+ late SlideshowStatusState _currentStatus;
24+ late Timer _hideHintTimer;
25+ DateTime _lastButtonActive = DateTime .now ();
1726
18- ButtonsHintBloc ({required this .frontendService})
19- : super (
20- ButtonsHintDisplayState (
21- isShow: false ,
22- normalColor: Colors .white,
23- pushColor: Colors .blue.shade500,
24- button0Icon: Icons .arrow_back,
25- button0isPush: false ,
26- button1Icon: Icons .arrow_circle_down,
27- button1isPush: false ,
28- button2Icon: Icons .arrow_circle_up,
29- button2isPush: false ,
30- button3Icon: Icons .check_circle_outline_outlined,
31- button3isPush: false ,
32- ),
33- ) {
27+ ButtonsHintBloc ({
28+ required this .frontendService,
29+ required this .statusBloc,
30+ required this .appConfig,
31+ }) : super (
32+ ButtonsHintDisplayState (
33+ isShow: false ,
34+ normalColor: ThemeColors .buttonsHintColor,
35+ pushColor: ThemeColors .buttonsHintPressedColor,
36+ button0Icon: Icons .arrow_back,
37+ button0isPush: false ,
38+ button1Icon: Icons .arrow_circle_down,
39+ button1isPush: false ,
40+ button2Icon: Icons .arrow_circle_up,
41+ button2isPush: false ,
42+ button3Icon: Icons .check_circle_outline_outlined,
43+ button3isPush: false ,
44+ ),
45+ ) {
3446 on < ButtonsHintPushButtonEvent > (_onPushButtonEvent);
3547 on < ButtonsHintShowEvent > (_onShowEvent);
3648 _onButtonEventSubscription = frontendService.onButtonEvent.listen (
3749 _onButtonEvent,
3850 );
51+ on < ButtonsHintUpdateButtonsEvent > (_onUpdateButtons);
52+ _hideHintTimer = Timer (Duration (seconds: 5 ), _hideHint);
53+ _startAutoHideHint ();
54+ _currentStatus = statusBloc.state;
55+ _onStatusChanged (_currentStatus, true );
56+ _onStatusSubscription = statusBloc.stream.listen (_onStatusChanged);
57+ }
58+
59+ void _startAutoHideHint ([bool isForce = false ]) {
60+ if (isForce || ! _hideHintTimer.isActive) {
61+ _hideHintTimer = Timer (Duration (seconds: 5 ), _hideHint);
62+ }
63+ }
64+
65+ void _hideHint () {
66+ if (! _currentStatus.isMenu) {
67+ if ((DateTime .now ().difference (_lastButtonActive).inSeconds > 5 )) {
68+ add (ButtonsHintShowEvent (isShow: false ));
69+ } else {
70+ _startAutoHideHint (true );
71+ }
72+ }
73+ }
74+
75+ FutureOr <void > _onStatusChanged (
76+ SlideshowStatusState state, [
77+ bool isForceUpdate = false ,
78+ ]) {
79+ if (isForceUpdate || _currentStatus.isMenu != state.isMenu) {
80+ _currentStatus = state;
81+ add (ButtonsHintUpdateButtonsEvent ());
82+ }
83+ _currentStatus = state;
3984 }
4085
4186 FutureOr <void > _onPushButtonEvent (
@@ -67,15 +112,19 @@ class ButtonsHintBloc extends Bloc<ButtonsHintEvent, ButtonsHintState> {
67112 }
68113
69114 void _onButtonEvent (ButtonEvent event) {
115+ _lastButtonActive = DateTime .now ();
70116 if (event.event == ButtonEventType .released) {
71117 add (ButtonsHintPushButtonEvent (button: event.button));
72118 }
119+ if (! _currentStatus.isMenu && ! state.isShow){
120+ _onStatusChanged (_currentStatus, true );
121+ }
73122 }
74123
75124 @override
76125 Future <void > close () {
77126 _onButtonEventSubscription.cancel ();
78-
127+ _onStatusSubscription. cancel ();
79128 return super .close ();
80129 }
81130
@@ -85,4 +134,55 @@ class ButtonsHintBloc extends Bloc<ButtonsHintEvent, ButtonsHintState> {
85134 ) {
86135 emit (state.copyWith (isShow: event.isShow));
87136 }
137+
138+ FutureOr <void > _onUpdateButtons (
139+ ButtonsHintUpdateButtonsEvent event,
140+ Emitter <ButtonsHintState > emit,
141+ ) {
142+ if (_currentStatus.isMenu) {
143+ emit (
144+ state.copyWith (
145+ isShow: true ,
146+ button0Icon: Icons .arrow_back,
147+ button1Icon: Icons .arrow_circle_down,
148+ button2Icon: Icons .arrow_circle_up,
149+ button3Icon: Icons .check_circle_outline_outlined,
150+ ),
151+ );
152+ } else {
153+ emit (
154+ state.copyWith (
155+ isShow: true ,
156+ button0Icon: getIconDataBySlideshowAction (
157+ appConfig.slideshow.buttons.button0.action,
158+ ),
159+ button1Icon: getIconDataBySlideshowAction (
160+ appConfig.slideshow.buttons.button1.action,
161+ ),
162+ button2Icon: getIconDataBySlideshowAction (
163+ appConfig.slideshow.buttons.button2.action,
164+ ),
165+ button3Icon: getIconDataBySlideshowAction (
166+ appConfig.slideshow.buttons.button3.action,
167+ ),
168+ ),
169+ );
170+ _startAutoHideHint (true );
171+ }
172+ }
173+
174+ IconData getIconDataBySlideshowAction (SlideshowAction action) {
175+ switch (action) {
176+ case SlideshowAction .none:
177+ return Icons .cancel;
178+ case SlideshowAction .pause:
179+ return Icons .pause;
180+ case SlideshowAction .showInfo:
181+ return Icons .info;
182+ case SlideshowAction .showMenu:
183+ return Icons .menu;
184+ case SlideshowAction .toggleScreen:
185+ return Icons .power_off;
186+ }
187+ }
88188}
0 commit comments