From 38e63028ca41d858b0d77701fd8c6568b0af9ec1 Mon Sep 17 00:00:00 2001 From: Godsend Joseph Date: Sun, 28 Jul 2024 00:47:13 +0100 Subject: [PATCH] changed the hide play button to using cubit --- .../cloud_notes/views/cloud_read_note.dart | 4 +- .../views/local_notes/read_notes_screens.dart | 4 +- .../settings/controller/settings_screen.dart | 8 +-- .../play_button_cubit/play_button_cubit.dart | 42 +++++++++++ .../play_button_cubit/play_button_state.dart | 25 +++++++ lib/main.dart | 7 +- lib/providers/hide_play_button_provider.dart | 72 +++++++++---------- 7 files changed, 115 insertions(+), 47 deletions(-) create mode 100644 lib/cubits/play_button_cubit/play_button_cubit.dart create mode 100644 lib/cubits/play_button_cubit/play_button_state.dart diff --git a/lib/app/resources/home/views/cloud_notes/views/cloud_read_note.dart b/lib/app/resources/home/views/cloud_notes/views/cloud_read_note.dart index 8270950..25d0504 100644 --- a/lib/app/resources/home/views/cloud_notes/views/cloud_read_note.dart +++ b/lib/app/resources/home/views/cloud_notes/views/cloud_read_note.dart @@ -5,6 +5,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_tts/flutter_tts.dart'; import 'package:note_app/app/resources/home/views/cloud_notes/models/cloud_note_model.dart'; import 'package:note_app/app/resources/home/views/cloud_notes/views/cloud_edit_note.dart'; +import 'package:note_app/cubits/play_button_cubit/play_button_cubit.dart'; import 'package:note_app/m_functions/navigate_to.dart'; import 'package:note_app/providers/hide_play_button_provider.dart'; import 'package:note_app/providers/theme_provider.dart'; @@ -182,7 +183,6 @@ class _CloudReadNoteState extends State { @override Widget build(BuildContext context) { - final checkButtonState = Provider.of(context); return Scaffold( appBar: AppBar( title: const Text( @@ -233,7 +233,7 @@ class _CloudReadNoteState extends State { ), ), floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, - floatingActionButton: checkButtonState.mPlayButton == false + floatingActionButton: context.watch().state.canPlay ? FloatingActionButton( backgroundColor: Colors.white60, onPressed: () { diff --git a/lib/app/resources/home/views/local_notes/read_notes_screens.dart b/lib/app/resources/home/views/local_notes/read_notes_screens.dart index cd9baf9..a937959 100644 --- a/lib/app/resources/home/views/local_notes/read_notes_screens.dart +++ b/lib/app/resources/home/views/local_notes/read_notes_screens.dart @@ -5,6 +5,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:note_app/app/helpers/hive_manager.dart'; import 'package:note_app/app/resources/home/views/local_notes/edit_note_screen.dart'; import 'package:note_app/app/resources/home/views/local_notes/models/note_model.dart'; +import 'package:note_app/cubits/play_button_cubit/play_button_cubit.dart'; import 'package:note_app/cubits/theme_cubit/theme_cubit.dart'; import 'package:note_app/m_functions/navigate_to.dart'; import 'package:note_app/providers/theme_provider.dart'; @@ -239,7 +240,6 @@ class _ReadNotesScreenState extends State { @override Widget build(BuildContext context) { - final checkButtonState = Provider.of(context); final hiveData = HiveManager().userModelBox; return Scaffold( appBar: AppBar( @@ -309,7 +309,7 @@ class _ReadNotesScreenState extends State { ), ), floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, - floatingActionButton: checkButtonState.mPlayButton == false + floatingActionButton: context.watch().state.canPlay == false ? FloatingActionButton( backgroundColor: Colors.white60, onPressed: () { diff --git a/lib/app/resources/settings/controller/settings_screen.dart b/lib/app/resources/settings/controller/settings_screen.dart index 842bcbd..f599a7d 100644 --- a/lib/app/resources/settings/controller/settings_screen.dart +++ b/lib/app/resources/settings/controller/settings_screen.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:note_app/app/helpers/hive_manager.dart'; import 'package:note_app/app/resources/trash/controller/trashed_notes.dart'; +import 'package:note_app/cubits/play_button_cubit/play_button_cubit.dart'; import 'package:note_app/cubits/theme_cubit/theme_cubit.dart'; import 'package:note_app/providers/hide_play_button_provider.dart'; import 'package:note_app/providers/theme_provider.dart'; @@ -83,7 +84,6 @@ class _SettingsScreenState extends State { @override Widget build(BuildContext context) { - final checkButtonState = Provider.of(context); final userModel = HiveManager().userModelBox; return Scaffold( appBar: AppBar( @@ -134,7 +134,7 @@ class _SettingsScreenState extends State { style: TextStyle(), ), trailing: Switch( - value: context.read().state.isDarkTheme, + value: context.watch().state.isDarkTheme, onChanged: (val) { context.read().toggleTheme(); }, @@ -154,9 +154,9 @@ class _SettingsScreenState extends State { 'in the read note screen', ), trailing: Switch( - value: checkButtonState.mPlayButton, + value: context.watch().state.canPlay, onChanged: (val) { - checkButtonState.checkButtonState(); + context.read().togglePlayButton(); }, ), ), diff --git a/lib/cubits/play_button_cubit/play_button_cubit.dart b/lib/cubits/play_button_cubit/play_button_cubit.dart new file mode 100644 index 0000000..b28d85b --- /dev/null +++ b/lib/cubits/play_button_cubit/play_button_cubit.dart @@ -0,0 +1,42 @@ +import 'package:bloc/bloc.dart'; +import 'package:equatable/equatable.dart'; +import 'package:meta/meta.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +part 'play_button_state.dart'; + +class PlayButtonCubit extends Cubit { + String key = 'playButton'; + SharedPreferences? _pref; + + + PlayButtonCubit() : super(PlayButtonState.initial()) { + _loadFromPref(); + } + + // initialize the shared Preference Instance + Future _initPrefs() async { + _pref ??= await SharedPreferences.getInstance(); + } + + // Load the data from it and check the current value + Future _loadFromPref() async { + await _initPrefs(); + final bool canPlay = _pref!.getBool(key) ?? false; + emit(state.copyWith(canPlay: canPlay)); + } + + // save the new value to the key + Future _saveToPref(bool value) async { + await _initPrefs(); + await _pref!.setBool(key, value); + } + + // toggle between light or dark mode + void togglePlayButton() { + final newCanPlay = !state.canPlay; + _saveToPref(newCanPlay); + emit(state.copyWith(canPlay: newCanPlay)); + } + +} diff --git a/lib/cubits/play_button_cubit/play_button_state.dart b/lib/cubits/play_button_cubit/play_button_state.dart new file mode 100644 index 0000000..86b4ed5 --- /dev/null +++ b/lib/cubits/play_button_cubit/play_button_state.dart @@ -0,0 +1,25 @@ +part of 'play_button_cubit.dart'; + +@immutable +class PlayButtonState extends Equatable { + final bool canPlay; + + const PlayButtonState({ + this.canPlay = false, + }); + + factory PlayButtonState.initial() { + return const PlayButtonState(); + } + + @override + List get props => [canPlay]; + + PlayButtonState copyWith({ + bool? canPlay, + }) { + return PlayButtonState( + canPlay: canPlay ?? this.canPlay, + ); + } +} diff --git a/lib/main.dart b/lib/main.dart index f06cccb..73a3687 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -4,6 +4,7 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:note_app/app/api/api_constant.dart'; import 'package:note_app/app/helpers/hive_manager.dart'; import 'package:note_app/app/src/app.dart'; +import 'package:note_app/cubits/play_button_cubit/play_button_cubit.dart'; import 'package:note_app/cubits/theme_cubit/theme_cubit.dart'; import 'package:note_app/providers/change_view_style_provider.dart'; import 'package:note_app/providers/hide_play_button_provider.dart'; @@ -29,12 +30,12 @@ void main() async { ChangeNotifierProvider.value( value: ChangeViewStyleProvider(), ), - ChangeNotifierProvider.value( - value: HidePlayButtonProvider(), - ), BlocProvider( create: (context) => ThemeCubit(), ), + BlocProvider( + create: (context) => PlayButtonCubit(), + ), ], child: const App(), ), diff --git a/lib/providers/hide_play_button_provider.dart b/lib/providers/hide_play_button_provider.dart index 4aa895b..db8504a 100644 --- a/lib/providers/hide_play_button_provider.dart +++ b/lib/providers/hide_play_button_provider.dart @@ -1,36 +1,36 @@ -import 'package:flutter/material.dart'; -import 'package:shared_preferences/shared_preferences.dart'; - -class HidePlayButtonProvider with ChangeNotifier { - final String? key = 'playButton'; - SharedPreferences? _pref; - bool? _mPlayButton; - - bool get mPlayButton => _mPlayButton!; - - HidePlayButtonProvider() { - _mPlayButton = false; - _loadFromPref(); - } - - Future _initPrefs() async { - _pref == null ? _pref = await SharedPreferences.getInstance() : null; - } - - void _loadFromPref() async { - await _initPrefs(); - _mPlayButton = _pref!.getBool(key!) ?? false; - notifyListeners(); - } - - void _saveToPref() async { - await _initPrefs(); - await _pref!.setBool(key!, _mPlayButton!); - } - - void checkButtonState() { - _mPlayButton = !_mPlayButton!; - _saveToPref(); - notifyListeners(); - } -} +// import 'package:flutter/material.dart'; +// import 'package:shared_preferences/shared_preferences.dart'; +// +// class HidePlayButtonProvider with ChangeNotifier { +// final String? key = 'playButton'; +// SharedPreferences? _pref; +// bool? _mPlayButton; +// +// bool get mPlayButton => _mPlayButton!; +// +// HidePlayButtonProvider() { +// _mPlayButton = false; +// _loadFromPref(); +// } +// +// Future _initPrefs() async { +// _pref == null ? _pref = await SharedPreferences.getInstance() : null; +// } +// +// void _loadFromPref() async { +// await _initPrefs(); +// _mPlayButton = _pref!.getBool(key!) ?? false; +// notifyListeners(); +// } +// +// void _saveToPref() async { +// await _initPrefs(); +// await _pref!.setBool(key!, _mPlayButton!); +// } +// +// void checkButtonState() { +// _mPlayButton = !_mPlayButton!; +// _saveToPref(); +// notifyListeners(); +// } +// }