diff --git a/lib/database.dart b/lib/database.dart index f24005b..7c724c0 100644 --- a/lib/database.dart +++ b/lib/database.dart @@ -34,11 +34,12 @@ CREATE TABLE IF NOT EXISTS $_bookmarksTable( void _createFictionCacheTable() { if (_database.isOpen) _database.execute(""" CREATE TABLE IF NOT EXISTS $_fictionCacheTable( - fiction_id TEXT NOT NULL PRIMARY KEY, + fiction_id TEXT NOT NULL, chapter_id TEXT NOT NULL, fiction_title TEXT NOT NULL, chapter_title TEXT NOT NULL, - content TEXT + content TEXT, + PRIMARY KEY(fiction_id, chapter_id) ) """); } @@ -126,7 +127,7 @@ CREATE TABLE IF NOT EXISTS $_historyTable ( } remember(String fictionId, String lastRead) async { - if (!await historyExists(fictionId)) { + if (!(await historyExists(fictionId))) { _database.insert(_historyTable, { "fiction_id": fictionId, "last_read": lastRead, diff --git a/lib/pages/fictionreader.dart b/lib/pages/fictionreader.dart index a5ddb16..f039871 100644 --- a/lib/pages/fictionreader.dart +++ b/lib/pages/fictionreader.dart @@ -5,8 +5,10 @@ import 'package:fiction_reader/api/detail.dart'; import 'package:fiction_reader/database.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:provider/provider.dart'; +import 'package:shared_preferences/shared_preferences.dart'; class FictionReaderPage extends StatefulWidget { final String _novelID; @@ -21,18 +23,64 @@ class FictionReaderPage extends StatefulWidget { class _FictionReaderPageState extends State { final StreamController> _fictionStreamController = StreamController(); + final Future _prefs = SharedPreferences.getInstance(); + PageController _pageViewController; Database _database; + bool _showMenu = false; + double _fontSize = 25; + String _fontFamily = "Noto Serif"; + Color _background = Colors.white; @override void initState() { super.initState(); final pageIndex = int.parse(widget._chapter.id); _pageViewController = PageController(initialPage: pageIndex); + readFromPref(); + } + + Future readFromPref() async { + final pref = await _prefs; + final fontSize = pref.getDouble("font_size"); + final fontFamily = pref.getString("font_family"); + final background = pref.getString("background_color"); + if (fontSize != null && fontFamily != null && background != null) { + _fontSize = pref.getDouble("font_size"); + _fontFamily = pref.getString("font_family"); + _background = + Color(int.parse(pref.getString("background_color"), radix: 16)); + setState(() {}); + } else { + _initPref(); + } + } + + _initPref() async { + _storeFontSize(25); + _storeFontFamily("Noto Serif"); + _storeBackground(Colors.white); + } + + _storeFontSize(double size) async { + final pref = await _prefs; + await pref.setDouble("font_size", size); + } + + _storeFontFamily(String family) async { + final pref = await _prefs; + await pref.setString("font_family", family); + } + + _storeBackground(Color background) async { + final pref = await _prefs; + await pref.setString("background_color", + background.toString().split('(0x')[1].split(')')[0]); } @override Widget build(BuildContext context) { + SystemChrome.setEnabledSystemUIOverlays([]); return Consumer( builder: (_, database, __) { if (_database == null) { @@ -98,34 +146,179 @@ class _FictionReaderPageState extends State { ], ), ), - body: SafeArea( - child: Container( - decoration: BoxDecoration(color: Colors.green[100]), - child: PageView.builder( - controller: _pageViewController, - onPageChanged: (page) { - _changePage(page); - }, - itemBuilder: (_, index) => SingleChildScrollView( - child: (snapshot.connectionState == - ConnectionState.done) - ? SafeArea( - maintainBottomViewPadding: true, - minimum: EdgeInsets.only(bottom: 70), - child: Text( - snapshot.data.lines - .where((String element) => - element.trim().length > 0) - .map((String e) => - "\t\t\t\t\t\t${e.trim()}") - .join("\n"), - style: - GoogleFonts.maShanZheng(fontSize: 35), - ), - ) - : Container(), + body: GestureDetector( + onTap: () { + // _showMenuTemporally(); + _toggleMenu(); + }, + child: Stack( + children: [ + Container( + decoration: BoxDecoration(color: _background), + child: PageView.builder( + controller: _pageViewController, + onPageChanged: (page) { + _changePage(page); + }, + itemBuilder: (_, index) => SingleChildScrollView( + child: (snapshot.connectionState == + ConnectionState.done) + ? Text( + snapshot.data.lines + .where((String element) => + element.trim().length > 0) + .map((String e) => + "\t\t\t\t\t\t${e.trim()}") + .join("\n"), + style: GoogleFonts.asMap()[_fontFamily]( + fontSize: _fontSize, + color: _background == Colors.black + ? Colors.grey[500] + : Colors.black), + // ( fontSize: _fontSize, + // color: _background == Colors.black + // ? Colors.grey[500] + // : Colors.black),), + ) + : Container(), + ), + ), ), - ), + if (_showMenu) + Positioned( + left: 0, + right: 0, + bottom: 0, + child: Container( + height: 150, + decoration: BoxDecoration(color: Colors.white), + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text("Font size: "), + Slider( + onChanged: (newVal) { + setState(() { + _fontSize = newVal; + _storeFontSize(newVal); + }); + }, + value: _fontSize, + max: 50, + min: 20, + ), + ], + ), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text("Font familly: "), + DropdownButton( + value: _fontFamily, + items: [ + DropdownMenuItem( + value: "Noto Serif", + child: Text( + "NotoSerif(示例字体)", + style: GoogleFonts.notoSerif( + fontSize: 25, + ), + ), + ), + DropdownMenuItem( + value: "ZCOOL KuaiLe", + child: Text( + "ZCOOL KuaiLe(示例字体)", + style: GoogleFonts.zcoolKuaiLe( + fontSize: 25, + ), + ), + ), + DropdownMenuItem( + value: "Ma Shan Zheng", + child: Text( + "MaShanZheng(示例字体)", + style: GoogleFonts.maShanZheng( + fontSize: 25, + ), + ), + ), + DropdownMenuItem( + value: "Liu Jian Mao Cao", + child: Text( + "LiuJianMaoCao(示例字体)", + style: GoogleFonts.liuJianMaoCao( + fontSize: 25, + ), + ), + ), + DropdownMenuItem( + value: "Long Cang", + child: Text( + "LongCang(示例字体)", + style: GoogleFonts.liuJianMaoCao( + fontSize: 25, + ), + ), + ), + ], + onChanged: (String value) { + setState( + () { + _fontFamily = value; + _storeFontFamily(value); + }, + ); + }, + ), + ], + ), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text("Background: "), + OutlineButton( + color: Colors.white, + onPressed: () { + setState(() { + _background = Colors.white; + _storeBackground(Colors.white); + }); + }, + ), + FlatButton( + splashColor: Colors.grey, + color: Colors.black, + onPressed: () { + setState(() { + _background = Colors.black; + _storeBackground(Colors.black); + }); + }, + child: null, + ), + FlatButton( + color: Colors.lightGreen[100], + onPressed: () { + setState(() { + _background = + Colors.lightGreen[100]; + _storeBackground( + Colors.lightGreen[100]); + }); + }, + child: null, + ) + ], + ) + ], + ), + ), + ), + ], ), ), ); @@ -153,4 +346,28 @@ class _FictionReaderPageState extends State { }); _database.remember(fictionId, chapterId); } + + // void _showMenuTemporally() { + // if (!_showMenu) { + // setState(() { + // _showMenu = true; + // Future.delayed(Duration(seconds: 2), () { + // setState(() { + // _showMenu = false; + // }); + // }); + // }); + // } + // } + + void _toggleMenu() { + setState(() { + _showMenu = !_showMenu; + }); + } + + @override + void deactivate() { + SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values); + } } diff --git a/lib/pages/searchpage.dart b/lib/pages/searchpage.dart index bd58365..60ef867 100644 --- a/lib/pages/searchpage.dart +++ b/lib/pages/searchpage.dart @@ -106,7 +106,7 @@ class _SearchPageState extends State { await database.removeBookmark(novel); setState(() {}); } catch (_) { - Scaffold.of(context).showSnackBar( + ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( "The book you want remove doesn't exist in bookmarks!"), @@ -120,7 +120,7 @@ class _SearchPageState extends State { novelListSnapshot.data[offset]); setState(() {}); } catch (e) { - Scaffold.of(context).showSnackBar( + ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text("Already added!"), duration: Duration(seconds: 1), diff --git a/pubspec.lock b/pubspec.lock index 5bece6c..5b66021 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -125,6 +125,11 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" glob: dependency: transitive description: @@ -187,7 +192,7 @@ packages: name: js url: "https://pub.dartlang.org" source: hosted - version: "0.6.2" + version: "0.6.3-nullsafety.1" logging: dependency: transitive description: @@ -342,6 +347,48 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.4.4" + shared_preferences: + dependency: "direct main" + description: + name: shared_preferences + url: "https://pub.dartlang.org" + source: hosted + version: "0.5.12+2" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.2+2" + shared_preferences_macos: + dependency: transitive + description: + name: shared_preferences_macos + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.1+10" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.4" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.2+7" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.1+1" shelf: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index cee6de6..12e9ed6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -24,6 +24,8 @@ dependencies: google_fonts: ^1.1.1 + shared_preferences: ^0.5.12+2 + dev_dependencies: test: ^1.15.4