Add persistent storage

This commit is contained in:
Oystein Kristoffer Tveit 2022-02-04 15:13:51 +01:00
parent fef9733ad7
commit d6fb9caa63
6 changed files with 76 additions and 29 deletions

View File

@ -1,3 +1,4 @@
import 'package:get_it/get_it.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:tangocard_reader/router.dart'; import 'package:tangocard_reader/router.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -6,23 +7,22 @@ import 'service/theme_bloc.dart';
void main() async { void main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
final prefs = await SharedPreferences.getInstance(); GetIt.instance.registerSingleton<SharedPreferences>(
runApp(MyApp(prefs: prefs)); await SharedPreferences.getInstance());
runApp(const MyApp());
} }
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
final SharedPreferences prefs;
const MyApp({ const MyApp({
Key? key, Key? key,
required this.prefs,
}) : super(key: key); }) : super(key: key);
@override @override
Widget build(BuildContext context) => BlocProvider( Widget build(BuildContext context) => BlocProvider(
create: (context) => ThemeBloc( create: (context) => ThemeBloc(
prefs: prefs, init:
init: (prefs.getBool('darkTheme') ?? false) (GetIt.instance.get<SharedPreferences>().getBool('darkTheme') ??
false)
? Brightness.dark ? Brightness.dark
: Brightness.light), : Brightness.light),
child: BlocBuilder<ThemeBloc, Brightness>( child: BlocBuilder<ThemeBloc, Brightness>(

View File

@ -76,6 +76,19 @@ class _KanjiPageState extends State<KanjiPage> {
), ),
const SizedBox(width: 20), const SizedBox(width: 20),
const Divider(thickness: 5), 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( Positioned(

View File

@ -2,6 +2,8 @@ import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.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/models/data_entry.dart';
import 'package:tangocard_reader/screens/practise/kanji.dart'; import 'package:tangocard_reader/screens/practise/kanji.dart';
@ -32,13 +34,22 @@ const encouragingWords = [
class _PractiseViewState extends State<PractiseView> { class _PractiseViewState extends State<PractiseView> {
late int currentCard; late int currentCard;
final List<bool> _flashcardToggles = [false, false];
final List<bool> _kanjiToggles = [false, false];
bool get isShuffleMode => _flashcardToggles[0]; SharedPreferences get prefs => GetIt.instance.get<SharedPreferences>();
bool get isLanguageSwitchedMode => _flashcardToggles[1];
bool get isKanjiDrawingMode => _kanjiToggles[0]; set isShuffleMode(b) => prefs.setBool('shuffleMode', b);
bool get isKanjiAnimationMode => _kanjiToggles[1]; 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<bool> get _flashcardToggles => [isShuffleMode, isLanguageSwitchedMode];
List<bool> get _kanjiToggles => [isKanjiDrawingMode, isKanjiAnimationMode];
int get randomCard => Random().nextInt(widget.entries.length); int get randomCard => Random().nextInt(widget.entries.length);
String get randomEncouragingWord => String get randomEncouragingWord =>
@ -98,8 +109,15 @@ class _PractiseViewState extends State<PractiseView> {
Icon(Icons.translate), Icon(Icons.translate),
], ],
isSelected: _flashcardToggles, isSelected: _flashcardToggles,
onPressed: (int index) => onPressed: (int index) => setState(
setState(() => _flashcardToggles[index] = !_flashcardToggles[index])), () {
if (index == 0) {
isShuffleMode = !_flashcardToggles[index];
} else if (index == 1) {
isLanguageSwitchedMode = !_flashcardToggles[index];
}
},
)),
], ],
), ),
centerTitle: true, centerTitle: true,
@ -122,8 +140,16 @@ class _PractiseViewState extends State<PractiseView> {
Icon(Icons.animation), Icon(Icons.animation),
], ],
isSelected: _kanjiToggles, isSelected: _kanjiToggles,
onPressed: (int index) => onPressed: (int index) => setState(
setState(() => _kanjiToggles[index] = !_kanjiToggles[index])), () {
if (index == 0) {
isKanjiDrawingMode = !_flashcardToggles[index];
} else if (index == 1) {
isKanjiAnimationMode = !_flashcardToggles[index];
}
},
),
),
], ],
), ),
centerTitle: true, centerTitle: true,

View File

@ -1,5 +1,6 @@
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:get_it/get_it.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
@ -12,14 +13,13 @@ class SetTheme {
} }
class ThemeBloc extends Bloc<SetTheme, Brightness> { class ThemeBloc extends Bloc<SetTheme, Brightness> {
final SharedPreferences prefs;
ThemeBloc({ ThemeBloc({
required this.prefs,
required Brightness init, required Brightness init,
}) : super(init) { }) : super(init) {
on<SetTheme>((event, emit) { on<SetTheme>((event, emit) {
prefs.setBool('darkTheme', event.isDark); GetIt.instance
.get<SharedPreferences>()
.setBool('darkTheme', event.isDark);
emit(event.isDark ? Brightness.dark : Brightness.light); emit(event.isDark ? Brightness.dark : Brightness.light);
}); });
} }

View File

@ -114,6 +114,13 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" 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: image:
dependency: transitive dependency: transitive
description: description:

View File

@ -30,6 +30,7 @@ dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
flutter_bloc: ^8.0.1 flutter_bloc: ^8.0.1
get_it: ^7.2.0
shared_preferences: ^2.0.12 shared_preferences: ^2.0.12
signature: ^5.0.0 signature: ^5.0.0