added db to persist theme modes

This commit is contained in:
dstark5
2023-08-24 23:12:30 -07:00
parent 162ea41196
commit 27061a1922
4 changed files with 54 additions and 12 deletions

View File

@ -21,10 +21,16 @@ void main() async {
databaseFactory = databaseFactoryFfi;
}
Database db = await Sqlite.initDb();
Database initDb = await Sqlite.initDb();
MyLibraryDb dataBase = MyLibraryDb(dbInstance: initDb);
bool isDarkMode = await dataBase.getPreference('darkMode');
runApp(
ProviderScope(
overrides: [dbProvider.overrideWithValue(MyLibraryDb(dbInstance: db))],
overrides: [
dbProvider.overrideWithValue(dataBase),
themeModeProvider.overrideWith(
(ref) => isDarkMode ? ThemeMode.dark : ThemeMode.light)
],
child: const MyApp(),
),
);
@ -89,12 +95,12 @@ class _HomePageState extends ConsumerState<HomePage> {
color: Theme.of(context).colorScheme.secondary,
),
tabMargin: const EdgeInsets.fromLTRB(13, 6, 13, 2.5),
curve: Curves.easeInOut, // tab animation curves
duration: const Duration(milliseconds: 150),
curve: Curves.easeInOut,
duration: const Duration(milliseconds: 50),
gap: 5,
color: const Color.fromARGB(255, 255, 255, 255),
activeColor: const Color.fromARGB(255, 255, 255, 255),
iconSize: 21, // tab button icon size
iconSize: 19, // tab button icon size
tabBackgroundColor: Theme.of(context).colorScheme.secondary,
padding: const EdgeInsets.symmetric(horizontal: 13, vertical: 6.5),
tabs: const [
@ -105,7 +111,7 @@ class _HomePageState extends ConsumerState<HomePage> {
textStyle: TextStyle(
fontWeight: FontWeight.w900,
color: Colors.white,
fontSize: 13,
fontSize: 11,
),
),
GButton(
@ -115,7 +121,7 @@ class _HomePageState extends ConsumerState<HomePage> {
textStyle: TextStyle(
fontWeight: FontWeight.w900,
color: Colors.white,
fontSize: 13,
fontSize: 11,
),
),
GButton(
@ -125,7 +131,7 @@ class _HomePageState extends ConsumerState<HomePage> {
textStyle: TextStyle(
fontWeight: FontWeight.w900,
color: Colors.white,
fontSize: 13,
fontSize: 11,
),
),
GButton(
@ -135,7 +141,7 @@ class _HomePageState extends ConsumerState<HomePage> {
textStyle: TextStyle(
fontWeight: FontWeight.w900,
color: Colors.white,
fontSize: 13,
fontSize: 11,
),
),
],

View File

@ -9,10 +9,12 @@ class Sqlite {
Database dbInstance = await openDatabase(
path,
version: 2,
version: 3,
onCreate: (Database db, int version) async {
await db.execute(
'CREATE TABLE mybooks (id TEXT PRIMARY KEY, title TEXT,author TEXT,thumbnail TEXT,link TEXT,publisher TEXT,info TEXT,format TEXT,description TEXT)');
await db.execute(
'CREATE TABLE preferences (name TEXT PRIMARY KEY,value BOOLEAN)');
if (isMobile) {
await db.execute(
'CREATE TABLE bookposition (fileName TEXT PRIMARY KEY,position TEXT)');
@ -21,6 +23,12 @@ class Sqlite {
onUpgrade: (db, oldVersion, newVersion) async {
List<dynamic> isTableExist = await db.query('sqlite_master',
where: 'name = ?', whereArgs: ['bookposition']);
List<dynamic> isPreferenceTableExist = await db.query('sqlite_master',
where: 'name = ?', whereArgs: ['preferences']);
if (isPreferenceTableExist.isEmpty) {
await db.execute(
'CREATE TABLE preferences (name TEXT PRIMARY KEY,value BOOLEAN)');
}
if (isMobile && isTableExist.isEmpty) {
await db.execute(
'CREATE TABLE bookposition (fileName TEXT PRIMARY KEY,position TEXT)');
@ -164,4 +172,26 @@ class MyLibraryDb {
return null;
}
}
Future<void> savePreference(String name, bool value) async {
int boolInt = value ? 1 : 0;
await dbInstance.insert(
'preferences',
{'name': name, 'value': boolInt},
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
Future<bool> getPreference(String name) async {
List<Map<String, dynamic>> data = await dbInstance
.query('preferences', where: 'name = ?', whereArgs: [name]);
List<dynamic> dataList = List.generate(data.length, (i) {
return {'name': data[i]['name'], 'value': data[i]['value']};
});
if (dataList.isNotEmpty) {
return dataList[0]['value'] == 0 ? false : true;
} else {
return false;
}
}
}

View File

@ -10,6 +10,9 @@ class BookInfoWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
String description = data.description.toString().length < 3
? "No Description available"
: data.description.toString();
return SingleChildScrollView(
physics: const BouncingScrollPhysics(),
scrollDirection: Axis.vertical,
@ -111,7 +114,7 @@ class BookInfoWidget extends StatelessWidget {
Padding(
padding: const EdgeInsets.only(top: 7, bottom: 10),
child: Text(
data.description ?? "",
description,
style: TextStyle(
fontSize: 11,
fontWeight: FontWeight.bold,

View File

@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:openlib/ui/components/page_title_widget.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:openlib/state/state.dart' show themeModeProvider;
import 'package:openlib/state/state.dart' show themeModeProvider, dbProvider;
class SettingsPage extends ConsumerWidget {
const SettingsPage({Key? key}) : super(key: key);
@ -45,6 +45,9 @@ class SettingsPage extends ConsumerWidget {
onChanged: (bool value) {
ref.read(themeModeProvider.notifier).state =
value == true ? ThemeMode.dark : ThemeMode.light;
ref
.read(dbProvider)
.savePreference('darkMode', value);
},
)
],