Add persistent storage
This commit is contained in:
parent
fef9733ad7
commit
d6fb9caa63
|
@ -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,25 +7,24 @@ 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') ??
|
||||||
? Brightness.dark
|
false)
|
||||||
: Brightness.light),
|
? Brightness.dark
|
||||||
|
: Brightness.light),
|
||||||
child: BlocBuilder<ThemeBloc, Brightness>(
|
child: BlocBuilder<ThemeBloc, Brightness>(
|
||||||
builder: (context, state) => MaterialApp(
|
builder: (context, state) => MaterialApp(
|
||||||
title: 'Tangocard Reader',
|
title: 'Tangocard Reader',
|
||||||
|
|
|
@ -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(
|
||||||
|
|
|
@ -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,
|
||||||
|
@ -116,14 +134,22 @@ class _PractiseViewState extends State<PractiseView> {
|
||||||
icon: const Icon(Icons.repeat),
|
icon: const Icon(Icons.repeat),
|
||||||
),
|
),
|
||||||
ToggleButtons(
|
ToggleButtons(
|
||||||
selectedColor: Colors.white,
|
selectedColor: Colors.white,
|
||||||
children: const [
|
children: const [
|
||||||
Icon(Icons.edit),
|
Icon(Icons.edit),
|
||||||
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,
|
||||||
|
|
|
@ -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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -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:
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue