Skip to content

Commit

Permalink
Fix favorites page crash on book not found
Browse files Browse the repository at this point in the history
  • Loading branch information
WeebNetsu committed Feb 11, 2024
1 parent 81fba38 commit 0f7b9d3
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 47 deletions.
15 changes: 1 addition & 14 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
{
"cSpell.words": [
"ahegao",
"autofocus",
"doujin",
"doujins",
"gomen",
"KHTML",
"lolicon",
"LTRB",
"nclientv",
"nhentai",
"scrollview",
"uoooh"
]
"cSpell.words": ["gomen", "nclientv", "nhentai", "scrollview", "wolt"]
}
136 changes: 105 additions & 31 deletions lib/views/favorites/favorites_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:nclientv3/constants/constants.dart';
import 'package:nclientv3/models/models.dart';
import 'package:nclientv3/theme/theme.dart';
import 'package:nclientv3/widgets/widgets.dart';
import 'package:nhentai/nhentai.dart' as nh;
import 'package:wolt_modal_sheet/wolt_modal_sheet.dart';

class FavoritesView extends StatefulWidget {
const FavoritesView({super.key});
Expand All @@ -20,20 +22,24 @@ class FavoritesView extends StatefulWidget {
}

class _FavoritesViewState extends State<FavoritesView> {
final pageIndexNotifier = ValueNotifier(0);
final _userPreferences = UserPreferencesModel();
// final ScrollController _scrollController = ScrollController();

Map<String, dynamic>? arguments;
String? _errorMessage;
nh.API? _api;
int _currentPage = 1;
// int _currentPage = 1;

/// if searching for a specific tag
final List<nh.Book> _searchedBooks = [];

/// list of books that were not found on fetch (most likely deleted by nhentai)
final List<int> _notFoundBooks = [];

/// Similar to _loadingBooks, but does not store a function
bool _loading = false;
// bool _loadingNextPage = false;
// bool _loadingNextPage = false;

late Future<void> _loadingBooks = _searchBooks();

Expand All @@ -51,11 +57,17 @@ class _FavoritesViewState extends State<FavoritesView> {
_searchedBooks.add(book);
});
} on nh.ApiException catch (error) {
setState(() {
_errorMessage = "Oh no, the API said '${error.message}'!";
});
} on nh.ApiClientException catch (e) {
if (e.response?.reasonPhrase == "Too Many Requests") {
if (error.message == "does not exist") {
setState(() {
_notFoundBooks.add(bookId);
});
} else {
setState(() {
_errorMessage = "Oh no, the API said '${error.message}'!";
});
}
} on nh.ApiClientException catch (error) {
if (error.response?.reasonPhrase == "Too Many Requests") {
// just retry the fetch
await _fetchBook(bookId);
} else {
Expand Down Expand Up @@ -83,13 +95,13 @@ class _FavoritesViewState extends State<FavoritesView> {

try {
setState(() {
if (nextPage) {
// _loadingNextPage = true;
_currentPage += 1;
} else {
_currentPage = 1;
_loading = true;
}
// if (nextPage) {
// // _loadingNextPage = true;
// _currentPage += 1;
// } else {
// _currentPage = 1;
_loading = true;
// }
});

if (selectedBookId != null) {
Expand All @@ -104,9 +116,35 @@ class _FavoritesViewState extends State<FavoritesView> {
await Future.wait(_userPreferences.favoriteBooks.map((book) => _fetchBook(book)));

setState(() {
// if (!nextPage) _searchedBooks.clear();
if (_notFoundBooks.isNotEmpty) {
WoltModalSheet.show<void>(
pageIndexNotifier: pageIndexNotifier,
context: context,
pageListBuilder: (modalSheetContext) {
return [
removedBooksPopup(modalSheetContext),
];
},
modalTypeBuilder: (context) {
final size = MediaQuery.of(context).size.width;
if (size < 768) {
return WoltModalType.bottomSheet;
} else {
return WoltModalType.dialog;
}
},
// onModalDismissedWithBarrierTap: () {
// debugPrint('Closed modal sheet with barrier tap');
// pageIndexNotifier.value = 0;
// },
maxDialogWidth: 560,
minDialogWidth: 400,
minPageHeight: 0.1,
maxPageHeight: 0.9,
);
}

_loading = false;
// _loadingNextPage = false;
});
} catch (error) {
setState(() {
Expand All @@ -122,21 +160,6 @@ class _FavoritesViewState extends State<FavoritesView> {
}
}

// void _scrollListener() {
// if (_userPreferences.slowInternetMode) return;

// if (_scrollController.position.atEdge) {
// if (_scrollController.position.pixels == 0) {
// // Reached the top of the scroll view
// } else {
// // Reached the bottom of the scroll view
// setState(() {
// _searchNextPage();
// });
// }
// }
// }

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -165,6 +188,57 @@ class _FavoritesViewState extends State<FavoritesView> {
super.dispose();
}

WoltModalSheetPage removedBooksPopup(BuildContext modalSheetContext) {
return WoltModalSheetPage.withSingleChild(
backgroundColor: Palette.backgroundColor,
child: Column(
children: [
const Text("These following books could not be found."),
const Text("They may have been removed by nHentai."),
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: _notFoundBooks.map((book) {
return Text("${book.toString()} ");
}).toList(),
),
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
MaterialButton(
onPressed: () {
for (int book in _notFoundBooks) {
if (_userPreferences.favoriteBooks.contains(book)) {
final bookIndex = _userPreferences.favoriteBooks.indexOf(book);
_userPreferences.favoriteBooks.removeAt(bookIndex);
}
}

setState(() {
_userPreferences.saveToFileData();
});

Navigator.of(modalSheetContext).pop();
pageIndexNotifier.value = 0;
},
child: const Text("Remove From Favorites"),
),
MaterialButton(
onPressed: () {
Navigator.of(modalSheetContext).pop();
pageIndexNotifier.value = 0;
},
child: const Text("Close"),
),
],
),
const SizedBox(height: 10),
],
),
);
}

@override
Widget build(BuildContext context) {
if (_api == null) {
Expand Down
2 changes: 0 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ dependencies:
# visibility_detector: ^0.4.0+2
widget_visibility_detector: ^1.0.1
image_size_getter: ^2.1.2
# kumi_popup_window: ^2.0.1
# flutter_local_notifications: ^15.1.0+1
# * do not use below, it webview returns a different agent
# fk_user_agent: ^2.1.0

Expand Down

0 comments on commit 0f7b9d3

Please sign in to comment.