From d6fb9caa63e256a63a5f01d3a1e54a8058a0e395 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Fri, 4 Feb 2022 15:13:51 +0100 Subject: [PATCH] Add persistent storage --- lib/main.dart | 18 +++++----- lib/screens/practise/kanji.dart | 13 +++++++ lib/screens/practise/practise.dart | 58 +++++++++++++++++++++--------- lib/service/theme_bloc.dart | 8 ++--- pubspec.lock | 7 ++++ pubspec.yaml | 1 + 6 files changed, 76 insertions(+), 29 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index a1ac00a..9501661 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,3 +1,4 @@ +import 'package:get_it/get_it.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:tangocard_reader/router.dart'; import 'package:flutter/material.dart'; @@ -6,25 +7,24 @@ import 'service/theme_bloc.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); - final prefs = await SharedPreferences.getInstance(); - runApp(MyApp(prefs: prefs)); + GetIt.instance.registerSingleton( + await SharedPreferences.getInstance()); + runApp(const MyApp()); } class MyApp extends StatelessWidget { - final SharedPreferences prefs; - const MyApp({ Key? key, - required this.prefs, }) : super(key: key); @override Widget build(BuildContext context) => BlocProvider( create: (context) => ThemeBloc( - prefs: prefs, - init: (prefs.getBool('darkTheme') ?? false) - ? Brightness.dark - : Brightness.light), + init: + (GetIt.instance.get().getBool('darkTheme') ?? + false) + ? Brightness.dark + : Brightness.light), child: BlocBuilder( builder: (context, state) => MaterialApp( title: 'Tangocard Reader', diff --git a/lib/screens/practise/kanji.dart b/lib/screens/practise/kanji.dart index c981940..2b1a000 100644 --- a/lib/screens/practise/kanji.dart +++ b/lib/screens/practise/kanji.dart @@ -76,6 +76,19 @@ class _KanjiPageState extends State { ), const SizedBox(width: 20), const Divider(thickness: 5), + if (widget.showDrawingPanel) ...[ + const SizedBox(width: 20), + Row( + children: [ + const Expanded(child: SizedBox()), + IconButton( + iconSize: 40, + onPressed: controller.clear, + icon: const Icon(Icons.delete), + ) + ], + ) + ] ], ), Positioned( diff --git a/lib/screens/practise/practise.dart b/lib/screens/practise/practise.dart index bc6f600..3624947 100644 --- a/lib/screens/practise/practise.dart +++ b/lib/screens/practise/practise.dart @@ -2,6 +2,8 @@ import 'dart:math'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:get_it/get_it.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:tangocard_reader/models/data_entry.dart'; import 'package:tangocard_reader/screens/practise/kanji.dart'; @@ -32,13 +34,22 @@ const encouragingWords = [ class _PractiseViewState extends State { late int currentCard; - final List _flashcardToggles = [false, false]; - final List _kanjiToggles = [false, false]; - bool get isShuffleMode => _flashcardToggles[0]; - bool get isLanguageSwitchedMode => _flashcardToggles[1]; - bool get isKanjiDrawingMode => _kanjiToggles[0]; - bool get isKanjiAnimationMode => _kanjiToggles[1]; + SharedPreferences get prefs => GetIt.instance.get(); + + set isShuffleMode(b) => prefs.setBool('shuffleMode', b); + set isLanguageSwitchedMode(b) => prefs.setBool('languageSwitchedMode', b); + set isKanjiDrawingMode(b) => prefs.setBool('kanjiDrawingMode', b); + set isKanjiAnimationMode(b) => prefs.setBool('kanjiAnimationMode', b); + + bool get isShuffleMode => prefs.getBool('shuffleMode') ?? false; + bool get isLanguageSwitchedMode => + prefs.getBool('languageSwitchedMode') ?? false; + bool get isKanjiDrawingMode => prefs.getBool('kanjiDrawingMode') ?? false; + bool get isKanjiAnimationMode => prefs.getBool('kanjiAnimationMode') ?? false; + + List get _flashcardToggles => [isShuffleMode, isLanguageSwitchedMode]; + List get _kanjiToggles => [isKanjiDrawingMode, isKanjiAnimationMode]; int get randomCard => Random().nextInt(widget.entries.length); String get randomEncouragingWord => @@ -98,8 +109,15 @@ class _PractiseViewState extends State { Icon(Icons.translate), ], isSelected: _flashcardToggles, - onPressed: (int index) => - setState(() => _flashcardToggles[index] = !_flashcardToggles[index])), + onPressed: (int index) => setState( + () { + if (index == 0) { + isShuffleMode = !_flashcardToggles[index]; + } else if (index == 1) { + isLanguageSwitchedMode = !_flashcardToggles[index]; + } + }, + )), ], ), centerTitle: true, @@ -116,14 +134,22 @@ class _PractiseViewState extends State { icon: const Icon(Icons.repeat), ), ToggleButtons( - selectedColor: Colors.white, - children: const [ - Icon(Icons.edit), - Icon(Icons.animation), - ], - isSelected: _kanjiToggles, - onPressed: (int index) => - setState(() => _kanjiToggles[index] = !_kanjiToggles[index])), + selectedColor: Colors.white, + children: const [ + Icon(Icons.edit), + Icon(Icons.animation), + ], + isSelected: _kanjiToggles, + onPressed: (int index) => setState( + () { + if (index == 0) { + isKanjiDrawingMode = !_flashcardToggles[index]; + } else if (index == 1) { + isKanjiAnimationMode = !_flashcardToggles[index]; + } + }, + ), + ), ], ), centerTitle: true, diff --git a/lib/service/theme_bloc.dart b/lib/service/theme_bloc.dart index 8104f20..d16022a 100644 --- a/lib/service/theme_bloc.dart +++ b/lib/service/theme_bloc.dart @@ -1,5 +1,6 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter/services.dart'; +import 'package:get_it/get_it.dart'; import 'package:meta/meta.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -12,14 +13,13 @@ class SetTheme { } class ThemeBloc extends Bloc { - final SharedPreferences prefs; - ThemeBloc({ - required this.prefs, required Brightness init, }) : super(init) { on((event, emit) { - prefs.setBool('darkTheme', event.isDark); + GetIt.instance + .get() + .setBool('darkTheme', event.isDark); emit(event.isDark ? Brightness.dark : Brightness.light); }); } diff --git a/pubspec.lock b/pubspec.lock index d34cd9a..6ac01fc 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -114,6 +114,13 @@ packages: description: flutter source: sdk version: "0.0.0" + get_it: + dependency: "direct main" + description: + name: get_it + url: "https://pub.dartlang.org" + source: hosted + version: "7.2.0" image: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 8f2557e..77107bb 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -30,6 +30,7 @@ dependencies: flutter: sdk: flutter flutter_bloc: ^8.0.1 + get_it: ^7.2.0 shared_preferences: ^2.0.12 signature: ^5.0.0