Skip to content

Commit

Permalink
feat(library): add library view)
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrFLEURY committed Mar 3, 2024
1 parent 890cd6d commit 89724f7
Show file tree
Hide file tree
Showing 13 changed files with 418 additions and 41 deletions.
23 changes: 2 additions & 21 deletions lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
import 'package:epubx/epubx.dart';
import 'package:flipub/book_service.dart';
import 'package:flipub/views/book_view.dart';
import 'package:flipub/views/library_view.dart';
import 'package:flutter/material.dart';

const sharedPreferencesFontSizeKey = 'fontSize';

class Flipub extends StatelessWidget {
const Flipub({
super.key,
required this.bookService,
});

final BookService bookService;

@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData.dark(),
home: FutureBuilder<EpubBook>(
future: bookService.parseBook(
'book_1.epub',
),
builder: (context, snapshot) {
if (snapshot.hasData) {
return BookView(
book: snapshot.data!,
);
}
return const Center(
child: CircularProgressIndicator(),
);
},
),
home: const LibrayView(),
);
}
}
11 changes: 0 additions & 11 deletions lib/book_service.dart

This file was deleted.

7 changes: 7 additions & 0 deletions lib/data/book_library.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class BookLibrary {
const BookLibrary({
required this.bookFileNames,
});

final List<String> bookFileNames;
}
8 changes: 2 additions & 6 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import 'package:flipub/book_service.dart';
import 'package:flutter/material.dart';
import 'package:flipub/app.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

Future<void> main() async {
final BookService bookService = BookService();
runApp(
ProviderScope(
child: Flipub(
bookService: bookService,
),
const ProviderScope(
child: Flipub(),
),
);
}
15 changes: 15 additions & 0 deletions lib/providers/book_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'dart:async';

import 'package:epubx/epubx.dart';
import 'package:flutter/services.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'book_provider.g.dart';

@riverpod
Future<EpubBook> book(BookRef ref, String fileName) async {
String fullPath = 'assets/books/$fileName';
ByteData byteData = await rootBundle.load(fullPath);
List<int> bytes = byteData.buffer.asUint8List();
return EpubReader.readBook(bytes);
}
155 changes: 155 additions & 0 deletions lib/providers/book_provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions lib/providers/library_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'dart:async';

import 'package:flipub/data/book_library.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'library_provider.g.dart';

@riverpod
Future<BookLibrary> bookLibrary(BookLibraryRef ref) async {
return const BookLibrary(
bookFileNames: [
'book_1.epub',
'book_2.epub',
'book_3.epub',
],
);
}
24 changes: 24 additions & 0 deletions lib/providers/library_provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions lib/views/book_cover.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'dart:typed_data';

import 'package:epubx/epubx.dart' show EpubBook;
import 'package:flutter/material.dart';
import 'package:image/image.dart' as image;

class BookCover extends StatelessWidget {
const BookCover({
super.key,
required this.book,
});

final EpubBook book;

@override
Widget build(BuildContext context) {
return book.CoverImage != null
? Image.memory(
Uint8List.fromList(
image.encodePng(book.CoverImage!),
),
width: 200,
height: 200,
)
: const SizedBox(
height: 200,
width: 200,
child: Center(
child: Text('No cover'),
),
);
}
}
33 changes: 31 additions & 2 deletions lib/views/book_view.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
import 'package:epubx/epubx.dart';
import 'package:flipub/providers/book_provider.dart';
import 'package:flipub/views/chapter_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class BookView extends ConsumerWidget {
final EpubBook book;
final String bookFileName;

const BookView({
super.key,
required this.book,
required this.bookFileName,
});

@override
Widget build(BuildContext context, WidgetRef ref) {
final AsyncValue<EpubBook> book = ref.watch(bookProvider(bookFileName));
return book.when(
data: (EpubBook book) {
return _BookViewContent(
book: book,
);
},
loading: () => const Center(
child: CircularProgressIndicator(),
),
error: (error, stackTrace) {
return Center(
child: Text('Error: $error'),
);
},
);
}
}

class _BookViewContent extends StatelessWidget {
final EpubBook book;

const _BookViewContent({
required this.book,
});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(book.Title ?? 'Unknown title'),
Expand Down
Loading

0 comments on commit 89724f7

Please sign in to comment.