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:yokutango_mobile_reader/models/data_entry.dart'; import 'package:yokutango_mobile_reader/screens/practise/kanji.dart'; import 'flashcard.dart'; class PractiseView extends StatefulWidget { final List entries; final bool isKanji; final int index; const PractiseView({ super.key, required this.entries, required this.isKanji, this.index = 0, }); @override State createState() => _PractiseViewState(); } const encouragingWords = [ '頑張れ〜!', 'できるぞ!', 'ヨッシャー!', 'いけいけー!', ]; class _PractiseViewState extends State { late int currentCard; 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 => encouragingWords[Random().nextInt(encouragingWords.length)]; bool get isPhone => MediaQueryData.fromWindow(WidgetsBinding.instance.window) .size .shortestSide < 600; @override void initState() { currentCard = widget.index; if (isPhone) { if (widget.isKanji) { SystemChrome.setPreferredOrientations([ DeviceOrientation.portraitUp, DeviceOrientation.portraitDown, ]); } else { SystemChrome.setPreferredOrientations([ DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight, ]); } } super.initState(); } @override void dispose() { SystemChrome.setPreferredOrientations([ DeviceOrientation.landscapeRight, DeviceOrientation.landscapeLeft, DeviceOrientation.portraitUp, DeviceOrientation.portraitDown, ]); super.dispose(); } AppBar get flashcardAppBar => AppBar( title: Row( children: [ Expanded(child: Container()), Text(randomEncouragingWord), Expanded(child: Container()), IconButton( onPressed: () => setState(() => currentCard = 0), icon: const Icon(Icons.repeat), ), ToggleButtons( selectedColor: Colors.white, isSelected: _flashcardToggles, onPressed: (int index) => setState( () { if (index == 0) { isShuffleMode = !_flashcardToggles[index]; } else if (index == 1) { isLanguageSwitchedMode = !_flashcardToggles[index]; } }, ), children: const [ Icon(Icons.shuffle), Icon(Icons.translate), ], ), ], ), centerTitle: true, ); AppBar get kanjiAppBar => AppBar( title: Row( children: [ Expanded(child: Container()), Text(randomEncouragingWord), Expanded(child: Container()), IconButton( onPressed: () => setState(() => currentCard = 0), icon: const Icon(Icons.repeat), ), ToggleButtons( selectedColor: Colors.white, isSelected: _kanjiToggles, onPressed: (int index) => setState( () { if (index == 0) { isKanjiDrawingMode = !_kanjiToggles[index]; } else if (index == 1) { isKanjiAnimationMode = !_kanjiToggles[index]; } }, ), children: const [ Icon(Icons.edit), Icon(Icons.animation), ], ), ], ), centerTitle: true, ); @override Widget build(BuildContext context) => Scaffold( appBar: widget.isKanji ? kanjiAppBar : flashcardAppBar, body: widget.isKanji ? KanjiPage( entry: widget.entries[currentCard] as KanjiEntry, index: currentCard, showDrawingPanel: isKanjiDrawingMode, showStrokeOrder: isKanjiAnimationMode, onNextCard: () => setState(() { currentCard = isShuffleMode ? randomCard : currentCard + 1; if (currentCard == widget.entries.length) currentCard = 0; }), onPreviousCard: () => setState(() { currentCard = isShuffleMode ? randomCard : currentCard - 1; if (currentCard == -1) { currentCard = widget.entries.length - 1; } }), ) : FlashcardPage( card: widget.entries[currentCard] as YokutangoEntry, index: currentCard, languageFlipped: isLanguageSwitchedMode, onNextCard: () => setState(() { currentCard = isShuffleMode ? randomCard : currentCard + 1; if (currentCard == widget.entries.length) currentCard = 0; }), onPreviousCard: () => setState(() { currentCard = isShuffleMode ? randomCard : currentCard - 1; if (currentCard == -1) { currentCard = widget.entries.length - 1; } }), ), ); }