Skip to content

Commit

Permalink
changed the hide play button to using cubit
Browse files Browse the repository at this point in the history
  • Loading branch information
sudo-which-qp committed Jul 27, 2024
1 parent 41c28ca commit 38e6302
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -182,7 +183,6 @@ class _CloudReadNoteState extends State<CloudReadNote> {

@override
Widget build(BuildContext context) {
final checkButtonState = Provider.of<HidePlayButtonProvider>(context);
return Scaffold(
appBar: AppBar(
title: const Text(
Expand Down Expand Up @@ -233,7 +233,7 @@ class _CloudReadNoteState extends State<CloudReadNote> {
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: checkButtonState.mPlayButton == false
floatingActionButton: context.watch<PlayButtonCubit>().state.canPlay
? FloatingActionButton(
backgroundColor: Colors.white60,
onPressed: () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -239,7 +240,6 @@ class _ReadNotesScreenState extends State<ReadNotesScreen> {

@override
Widget build(BuildContext context) {
final checkButtonState = Provider.of<HidePlayButtonProvider>(context);
final hiveData = HiveManager().userModelBox;
return Scaffold(
appBar: AppBar(
Expand Down Expand Up @@ -309,7 +309,7 @@ class _ReadNotesScreenState extends State<ReadNotesScreen> {
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: checkButtonState.mPlayButton == false
floatingActionButton: context.watch<PlayButtonCubit>().state.canPlay == false
? FloatingActionButton(
backgroundColor: Colors.white60,
onPressed: () {
Expand Down
8 changes: 4 additions & 4 deletions lib/app/resources/settings/controller/settings_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -83,7 +84,6 @@ class _SettingsScreenState extends State<SettingsScreen> {

@override
Widget build(BuildContext context) {
final checkButtonState = Provider.of<HidePlayButtonProvider>(context);
final userModel = HiveManager().userModelBox;
return Scaffold(
appBar: AppBar(
Expand Down Expand Up @@ -134,7 +134,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
style: TextStyle(),
),
trailing: Switch(
value: context.read<ThemeCubit>().state.isDarkTheme,
value: context.watch<ThemeCubit>().state.isDarkTheme,
onChanged: (val) {
context.read<ThemeCubit>().toggleTheme();
},
Expand All @@ -154,9 +154,9 @@ class _SettingsScreenState extends State<SettingsScreen> {
'in the read note screen',
),
trailing: Switch(
value: checkButtonState.mPlayButton,
value: context.watch<PlayButtonCubit>().state.canPlay,
onChanged: (val) {
checkButtonState.checkButtonState();
context.read<PlayButtonCubit>().togglePlayButton();
},
),
),
Expand Down
42 changes: 42 additions & 0 deletions lib/cubits/play_button_cubit/play_button_cubit.dart
Original file line number Diff line number Diff line change
@@ -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<PlayButtonState> {
String key = 'playButton';
SharedPreferences? _pref;


PlayButtonCubit() : super(PlayButtonState.initial()) {
_loadFromPref();
}

// initialize the shared Preference Instance
Future<void> _initPrefs() async {
_pref ??= await SharedPreferences.getInstance();
}

// Load the data from it and check the current value
Future<void> _loadFromPref() async {
await _initPrefs();
final bool canPlay = _pref!.getBool(key) ?? false;
emit(state.copyWith(canPlay: canPlay));
}

// save the new value to the key
Future<void> _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));
}

}
25 changes: 25 additions & 0 deletions lib/cubits/play_button_cubit/play_button_state.dart
Original file line number Diff line number Diff line change
@@ -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<Object> get props => [canPlay];

PlayButtonState copyWith({
bool? canPlay,
}) {
return PlayButtonState(
canPlay: canPlay ?? this.canPlay,
);
}
}
7 changes: 4 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -29,12 +30,12 @@ void main() async {
ChangeNotifierProvider.value(
value: ChangeViewStyleProvider(),
),
ChangeNotifierProvider.value(
value: HidePlayButtonProvider(),
),
BlocProvider<ThemeCubit>(
create: (context) => ThemeCubit(),
),
BlocProvider<PlayButtonCubit>(
create: (context) => PlayButtonCubit(),
),
],
child: const App(),
),
Expand Down
72 changes: 36 additions & 36 deletions lib/providers/hide_play_button_provider.dart
Original file line number Diff line number Diff line change
@@ -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();
// }
// }

0 comments on commit 38e6302

Please sign in to comment.