From 2df0d1f655b4838aa6482ee7696f639b8ed2ae22 Mon Sep 17 00:00:00 2001 From: dstark5 <136567021+dstark5@users.noreply.github.com> Date: Wed, 30 Oct 2024 21:36:27 +0530 Subject: [PATCH] Fixed grey screen on search and changed share button position --- lib/main.dart | 2 +- lib/services/share_book.dart | 23 ++++++++++ lib/ui/about_page.dart | 2 +- lib/ui/book_info_page.dart | 82 ++++++++++++------------------------ lib/ui/categories_page.dart | 2 +- lib/ui/home_page.dart | 2 +- lib/ui/mybook_page.dart | 33 ++++++++++++--- lib/ui/results_page.dart | 6 +-- 8 files changed, 84 insertions(+), 68 deletions(-) create mode 100644 lib/services/share_book.dart diff --git a/lib/main.dart b/lib/main.dart index f476506..fd9491c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -128,7 +128,7 @@ class _MainScreenState extends ConsumerState { return Scaffold( appBar: AppBar( - backgroundColor: Theme.of(context).colorScheme.background, + backgroundColor: Theme.of(context).colorScheme.surface, title: const Text("Openlib"), titleTextStyle: Theme.of(context).textTheme.displayLarge, ), diff --git a/lib/services/share_book.dart b/lib/services/share_book.dart new file mode 100644 index 0000000..9d7edd3 --- /dev/null +++ b/lib/services/share_book.dart @@ -0,0 +1,23 @@ +import 'dart:io'; +import 'package:flutter/material.dart'; +import 'package:path_provider/path_provider.dart'; +import 'package:share_plus/share_plus.dart'; +import 'package:http/http.dart' as http; + +Future shareBook(String title, String link, String path) async { + try { + final url = Uri.parse(path); + final response = await http.get(url); + final bytes = response.bodyBytes; + + //temp + final temp = await getTemporaryDirectory(); + final dest = '${temp.path}/image.jpg'; + File(dest).writeAsBytes(bytes); + + String message = 'Discover this amazing book: "$title"\nRead more : $link'; + await Share.shareXFiles([XFile(dest)], text: message); + } catch (e) { + debugPrint('Error sharing the book: $e'); + } +} diff --git a/lib/ui/about_page.dart b/lib/ui/about_page.dart index 8ac7bec..f9faeaf 100644 --- a/lib/ui/about_page.dart +++ b/lib/ui/about_page.dart @@ -17,7 +17,7 @@ class AboutPage extends StatelessWidget { return Scaffold( appBar: AppBar( - backgroundColor: Theme.of(context).colorScheme.background, + backgroundColor: Theme.of(context).colorScheme.surface, title: const Text("Openlib"), titleTextStyle: Theme.of(context).textTheme.displayLarge, ), diff --git a/lib/ui/book_info_page.dart b/lib/ui/book_info_page.dart index 96922d1..1401f3e 100644 --- a/lib/ui/book_info_page.dart +++ b/lib/ui/book_info_page.dart @@ -2,18 +2,14 @@ // import 'dart:convert'; // Flutter imports: -import 'dart:io'; import 'package:flutter/material.dart'; // import 'package:flutter/scheduler.dart'; // Package imports: import 'package:dio/dio.dart' show CancelToken; import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:path_provider/path_provider.dart'; -import 'package:path/path.dart' as path; -import 'package:http/http.dart' as http; +import 'package:openlib/services/share_book.dart'; // import 'package:flutter_svg/svg.dart'; -import 'package:share_plus/share_plus.dart'; // Project imports: import 'package:openlib/services/annas_archieve.dart' show BookInfoData; @@ -25,8 +21,6 @@ import 'package:openlib/ui/components/file_buttons_widget.dart'; import 'package:openlib/ui/components/snack_bar_widget.dart'; import 'package:openlib/ui/webview_page.dart'; - - import 'package:openlib/state/state.dart' show bookInfoProvider, @@ -43,7 +37,6 @@ import 'package:openlib/state/state.dart' checkSumState, checkIdExists, myLibraryProvider; -import 'package:path_provider/path_provider.dart'; class BookInfoPage extends ConsumerWidget { const BookInfoPage({super.key, required this.url}); @@ -55,9 +48,25 @@ class BookInfoPage extends ConsumerWidget { final bookInfo = ref.watch(bookInfoProvider(url)); return Scaffold( appBar: AppBar( - backgroundColor: Theme.of(context).colorScheme.background, + backgroundColor: Theme.of(context).colorScheme.surface, title: const Text("Openlib"), titleTextStyle: Theme.of(context).textTheme.displayLarge, + actions: [ + bookInfo.maybeWhen(data: (data) { + return IconButton( + icon: Icon( + Icons.share_sharp, + color: Theme.of(context).colorScheme.tertiary, + ), + iconSize: 19.0, + onPressed: () async { + await shareBook(data.title, data.link, data.thumbnail ?? ''); + }, + ); + }, orElse: () { + return const SizedBox.shrink(); + }) + ], ), body: bookInfo.when( skipLoadingOnRefresh: false, @@ -198,13 +207,15 @@ class _ActionButtonWidgetState extends ConsumerState { return Padding( padding: const EdgeInsets.only(top: 21, bottom: 21), child: Row( - mainAxisAlignment: MainAxisAlignment.start, // Aligns buttons properly + mainAxisAlignment: + MainAxisAlignment.start, // Aligns buttons properly children: [ // Button for "Add To My Library" TextButton( style: TextButton.styleFrom( backgroundColor: Theme.of(context).colorScheme.secondary, - padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), + padding: + const EdgeInsets.symmetric(horizontal: 16, vertical: 8), textStyle: const TextStyle( fontSize: 13, fontWeight: FontWeight.w900, @@ -214,8 +225,8 @@ class _ActionButtonWidgetState extends ConsumerState { onPressed: () async { final result = await Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) { - return Webview(url: widget.data.mirror ?? ''); - })); + return Webview(url: widget.data.mirror ?? ''); + })); if (result != null) { widget.data.mirror = result; @@ -223,30 +234,9 @@ class _ActionButtonWidgetState extends ConsumerState { } }, child: const Text('Add To My Library'), - ), - - // Spacing between the buttons - const SizedBox(width: 12), - - // Button for "Share" - TextButton( - style: TextButton.styleFrom( - backgroundColor: Theme.of(context).colorScheme.secondary, - padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), - textStyle: const TextStyle( - fontSize: 13, - fontWeight: FontWeight.w900, - color: Colors.white, - ), - ), - onPressed: () async { - await shareBook(widget.data.title, widget.data.link,widget.data.thumbnail ?? ''); - }, - child: const Text('Share'), - ), + ) ], ), - ); } }, @@ -273,7 +263,7 @@ Future downloadFileWidget( }); List mirrors = [data.mirror!]; - print(mirrors); + // print(mirrors); downloadFile( mirrors: mirrors, md5: data.md5, @@ -672,23 +662,3 @@ Future _showWarningFileDialog(BuildContext context) async { }, ); } - - -Future shareBook(String title, String link,String path) async { - try { - final url = Uri.parse(path); - final response = await http.get(url); - final bytes = response.bodyBytes; - - //temp - final temp = await getTemporaryDirectory(); - final dest = '${temp.path}/image.jpg'; - File(dest).writeAsBytes(bytes); - - String message = 'Discover this amazing book: "$title"\nRead more : $link'; - await Share.shareXFiles([XFile(dest)], text: message); - } catch (e) { - debugPrint('Error sharing the book: $e'); - } -} - diff --git a/lib/ui/categories_page.dart b/lib/ui/categories_page.dart index bd8f7ef..f8e56ba 100644 --- a/lib/ui/categories_page.dart +++ b/lib/ui/categories_page.dart @@ -387,7 +387,7 @@ class CategoryListingPage extends ConsumerWidget { final booksBasedOnGenre = ref.watch(getSubCategoryTypeList(url)); return Scaffold( appBar: AppBar( - backgroundColor: Theme.of(context).colorScheme.background, + backgroundColor: Theme.of(context).colorScheme.surface, title: const Text("Openlib"), titleTextStyle: Theme.of(context).textTheme.displayLarge, ), diff --git a/lib/ui/home_page.dart b/lib/ui/home_page.dart index b16ef9e..1c6f30c 100644 --- a/lib/ui/home_page.dart +++ b/lib/ui/home_page.dart @@ -65,7 +65,7 @@ class PelletContainer extends StatelessWidget { Widget build(BuildContext context) { return Container( height: 30, - width: 125, + width: 115, decoration: BoxDecoration( borderRadius: BorderRadius.circular(30), color: Theme.of(context).colorScheme.secondary, diff --git a/lib/ui/mybook_page.dart b/lib/ui/mybook_page.dart index 8ff236a..b86538c 100644 --- a/lib/ui/mybook_page.dart +++ b/lib/ui/mybook_page.dart @@ -6,6 +6,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; // Project imports: import 'package:openlib/services/database.dart'; +import 'package:openlib/services/share_book.dart'; import 'package:openlib/ui/components/book_info_widget.dart'; import 'package:openlib/ui/components/file_buttons_widget.dart'; @@ -16,18 +17,40 @@ class BookPage extends StatelessWidget { @override Widget build(BuildContext context) { + MyLibraryDb dataBase = MyLibraryDb.instance; + final bookInfo = dataBase.getId(id); + return Scaffold( appBar: AppBar( - backgroundColor: Theme.of(context).colorScheme.background, + backgroundColor: Theme.of(context).colorScheme.surface, title: const Text("Openlib"), titleTextStyle: Theme.of(context).textTheme.displayLarge, + actions: [ + FutureBuilder( + future: bookInfo, + builder: (BuildContext context, AsyncSnapshot snapshot) { + if (snapshot.hasData && + snapshot.data?.title != null && + snapshot.data?.link != null) { + return IconButton( + icon: Icon( + Icons.share_sharp, + color: Theme.of(context).colorScheme.tertiary, + ), + iconSize: 19.0, + onPressed: () async { + await shareBook(snapshot.data!.title, snapshot.data!.link, + snapshot.data?.thumbnail ?? ''); + }, + ); + } else { + return const SizedBox.shrink(); + } + }) + ], ), body: Consumer( builder: (BuildContext context, WidgetRef ref, _) { - MyLibraryDb dataBase = MyLibraryDb.instance; - - final bookInfo = dataBase.getId(id); - return FutureBuilder( future: bookInfo, builder: (BuildContext context, AsyncSnapshot snapshot) { diff --git a/lib/ui/results_page.dart b/lib/ui/results_page.dart index 7d5bd18..b6ba183 100644 --- a/lib/ui/results_page.dart +++ b/lib/ui/results_page.dart @@ -24,7 +24,7 @@ class ResultPage extends ConsumerWidget { return Scaffold( appBar: AppBar( - backgroundColor: Theme.of(context).colorScheme.background, + backgroundColor: Theme.of(context).colorScheme.surface, title: const Text("Openlib"), titleTextStyle: Theme.of(context).textTheme.displayLarge, ), @@ -46,8 +46,8 @@ class ResultPage extends ConsumerWidget { title: i.title, author: i.author ?? "unknown", publisher: i.publisher ?? "unknown", - thumbnail: i.thumbnail!, - info: i.info, + thumbnail: i.thumbnail ?? '', + info: i.info ?? '', link: i.link, onClick: () { Navigator.push(context, MaterialPageRoute(