Skip to content

Commit

Permalink
fix(loading): fix dead path loading
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrFLEURY committed Mar 17, 2024
1 parent b0c0bba commit 961afb4
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 158 deletions.
150 changes: 7 additions & 143 deletions lib/presentation/library/library_view.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import 'package:epubx/epubx.dart' show EpubBook;
import 'package:file_selector/file_selector.dart';
import 'package:flipub/data/book_library.dart';
import 'package:flipub/data/constants.dart';
import 'package:flipub/providers/epub/book_provider.dart';
import 'package:flipub/presentation/library/views/book_grid_view.dart';
import 'package:flipub/presentation/library/views/empty_library.dart';
import 'package:flipub/providers/epub/library_provider.dart';
import 'package:flipub/providers/preferences/preferences_provider.dart';
import 'package:flipub/providers/theme/theme_provider.dart';
import 'package:flipub/presentation/book/book_cover.dart';
import 'package:flipub/presentation/book/book_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';

class LibrayView extends ConsumerWidget {
const LibrayView({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
final library = ref.watch(bookLibraryProvider);
final library = ref.watch(bookLibraryNotifierProvider);
return library.when(
data: (library) {
return _LibraryViewContent(library: library);
Expand Down Expand Up @@ -66,17 +61,9 @@ class _LibraryViewContent extends ConsumerWidget {
),
body: Padding(
padding: const EdgeInsets.all(48.0),
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: MediaQuery.of(context).isPortrait ? 2 : 4,
),
itemCount: library.bookPathList.length,
itemBuilder: (context, index) {
return LibraryTile(
bookPath: library.bookPathList[index],
);
},
),
child: library.bookPathList.isEmpty
? const EmptyLibraryView()
: BookGridView(library: library),
),
);
}
Expand All @@ -93,130 +80,7 @@ class _LibraryViewContent extends ConsumerWidget {
);
if (files.isNotEmpty) {
final path = files.single.path;
final SharedPreferences preferences =
await ref.read(preferencesProvider.future);
final library =
preferences.getStringList(sharedPreferencesLibraryKey) ?? [];
if (!library.contains(path)) {
library.add(path);
preferences.setStringList(sharedPreferencesLibraryKey, library);
ref.invalidate(bookLibraryProvider);
}
ref.read(bookLibraryNotifierProvider.notifier).addBook(path);
}
}
}

class LibraryTile extends ConsumerWidget {
const LibraryTile({super.key, required this.bookPath});

final String bookPath;

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

class _LibraryTileContent extends ConsumerStatefulWidget {
final String bookPath;
final EpubBook book;

const _LibraryTileContent({
required this.bookPath,
required this.book,
});

@override
_LibraryTileContentState createState() => _LibraryTileContentState();
}

class _LibraryTileContentState extends ConsumerState<_LibraryTileContent> {
bool _isHovering = false;

void _onMouseEnter() {
setState(() {
_isHovering = true;
});
}

void _onMouseExit() {
setState(() {
_isHovering = false;
});
}

@override
Widget build(BuildContext context) {
return MouseRegion(
onEnter: (_) => _onMouseEnter(),
onExit: (_) => _onMouseExit(),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BookView(
bookPath: widget.bookPath,
),
),
);
},
child: FittedBox(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
_isHovering || isMobile
? InkWell(
onTap: () => _removeBook(ref, widget.bookPath),
child: CircleAvatar(
radius: 16,
backgroundColor:
Theme.of(context).brightness == Brightness.dark
? Colors.grey[800]
: Colors.grey[400],
child: const Icon(Icons.close),
),
)
: const SizedBox(
height: 32,
),
BookCover(book: widget.book),
Text(
widget.book.Title ?? 'Unknown title',
),
],
),
),
),
),
);
}

void _removeBook(WidgetRef ref, String bookPath) async {
final SharedPreferences preferences =
await ref.read(preferencesProvider.future);
final library =
preferences.getStringList(sharedPreferencesLibraryKey) ?? [];
library.remove(bookPath);
preferences.setStringList(sharedPreferencesLibraryKey, library);
ref.invalidate(bookLibraryProvider);
}
}
28 changes: 28 additions & 0 deletions lib/presentation/library/views/book_grid_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:flipub/data/book_library.dart';
import 'package:flipub/data/constants.dart';
import 'package:flipub/presentation/library/views/library_tile.dart';
import 'package:flutter/material.dart';

class BookGridView extends StatelessWidget {
const BookGridView({
super.key,
required this.library,
});

final BookLibrary library;

@override
Widget build(BuildContext context) {
return GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: MediaQuery.of(context).isPortrait ? 2 : 4,
),
itemCount: library.bookPathList.length,
itemBuilder: (context, index) {
return LibraryTile(
bookPath: library.bookPathList[index],
);
},
);
}
}
35 changes: 35 additions & 0 deletions lib/presentation/library/views/empty_library.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:flutter/material.dart';

class EmptyLibraryView extends StatelessWidget {
const EmptyLibraryView({
super.key,
});

@override
Widget build(BuildContext context) {
return const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.book_outlined,
size: 64,
),
Text(
'No books added yet',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.w400,
),
),
Text(
'Open a new file to add a book',
style: TextStyle(
fontSize: 12,
),
),
],
),
);
}
}
120 changes: 120 additions & 0 deletions lib/presentation/library/views/library_tile.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import 'package:epubx/epubx.dart';
import 'package:flipub/data/constants.dart';
import 'package:flipub/presentation/book/book_cover.dart';
import 'package:flipub/presentation/book/book_view.dart';
import 'package:flipub/providers/epub/book_provider.dart';
import 'package:flipub/providers/epub/library_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class LibraryTile extends ConsumerWidget {
const LibraryTile({super.key, required this.bookPath});

final String bookPath;

@override
Widget build(BuildContext context, WidgetRef ref) {
final book = ref.watch(bookProvider(bookPath));
return book.when(
data: (book) {
return _LibraryTileContent(
bookPath: bookPath,
book: book,
);
},
loading: () => const Center(
child: CircularProgressIndicator(),
),
error: (error, stackTrace) {
return InkWell(
onTap: () {
ref.read(bookLibraryNotifierProvider.notifier).removeBook(bookPath);
},
child: Center(
child: Text('Error: $error'),
),
);
},
);
}
}

class _LibraryTileContent extends ConsumerStatefulWidget {
final String bookPath;
final EpubBook book;

const _LibraryTileContent({
required this.bookPath,
required this.book,
});

@override
_LibraryTileContentState createState() => _LibraryTileContentState();
}

class _LibraryTileContentState extends ConsumerState<_LibraryTileContent> {
bool _isHovering = false;

void _onMouseEnter() {
setState(() {
_isHovering = true;
});
}

void _onMouseExit() {
setState(() {
_isHovering = false;
});
}

@override
Widget build(BuildContext context) {
return MouseRegion(
onEnter: (_) => _onMouseEnter(),
onExit: (_) => _onMouseExit(),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BookView(
bookPath: widget.bookPath,
),
),
);
},
child: FittedBox(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
_isHovering || isMobile
? InkWell(
onTap: () => ref
.read(bookLibraryNotifierProvider.notifier)
.removeBook(widget.bookPath),
child: CircleAvatar(
radius: 16,
backgroundColor:
Theme.of(context).brightness == Brightness.dark
? Colors.grey[800]
: Colors.grey[400],
child: const Icon(Icons.close),
),
)
: const SizedBox(
height: 32,
),
BookCover(book: widget.book),
Text(
widget.book.Title ?? 'Unknown title',
),
],
),
),
),
),
);
}
}
2 changes: 1 addition & 1 deletion lib/providers/epub/cover_provider.g.dart

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

Loading

0 comments on commit 961afb4

Please sign in to comment.