Skip to content

Commit

Permalink
Merge pull request #251 from DattatreyaReddy:downloads-performance
Browse files Browse the repository at this point in the history
Downloads-performance
  • Loading branch information
DattatreyaReddy authored Nov 11, 2023
2 parents 6700068 + 6e7ae98 commit db34644
Show file tree
Hide file tree
Showing 24 changed files with 254 additions and 152 deletions.
1 change: 1 addition & 0 deletions lib/src/constants/app_sizes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ enum KSizedBox {
w64(SizedBox(width: 64)),
h96(SizedBox(height: 96)),
w96(SizedBox(width: 96)),
h104(SizedBox(height: 104)),
;

static SizedBox scale(
Expand Down
3 changes: 3 additions & 0 deletions lib/src/constants/reader_keyboard_shortcuts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class HideQuickOpenIntent extends Intent {}

ShortcutManager readerShortcutManager(Axis scrollDirection) => ShortcutManager(
shortcuts: {
const SingleActivator(LogicalKeyboardKey.space): NextScrollIntent(),
const SingleActivator(LogicalKeyboardKey.space, shift: true):
PreviousScrollIntent(),
const SingleActivator(LogicalKeyboardKey.arrowLeft):
scrollDirection == Axis.horizontal
? PreviousScrollIntent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class AboutScreen extends HookConsumerWidget {
useEffect(() {
aboutAsync.showToastOnError(toast, withMicrotask: true);
return;
}, [aboutAsync.value]);
}, [aboutAsync.valueOrNull]);

return Scaffold(
appBar: AppBar(title: Text(context.l10n!.about)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class ExtensionScreen extends HookConsumerWidget {
withMicrotask: true,
);
return;
}, [extensionMapData.value]);
}, [extensionMapData.valueOrNull]);

return extensionMapData.showUiWhenData(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,29 @@ AsyncValue<Map<String, List<Source>>> sourceMap(SourceMapRef ref) {
}

@riverpod
List<String> sourceFilterLangList(SourceFilterLangListRef ref) {
final sourceMap = {...?ref.watch(sourceMapProvider).valueOrNull};
sourceMap.remove("lastUsed");
sourceMap.remove("localsourcelang");
return [...sourceMap.keys]..sort();
class SourceFilterLangMap extends _$SourceFilterLangMap {
@override
Map<String, bool> build() {
final sourceMap = {...?ref.watch(sourceMapProvider).valueOrNull};
final enabledLanguages = ref.watch(sourceLanguageFilterProvider);
sourceMap.remove("lastUsed");
sourceMap.remove("localsourcelang");
return Map.fromIterable(
[...sourceMap.keys],
value: (element) => (enabledLanguages?.contains(element)).ifNull(),
);
}

void toggleLang(String langCode, bool value) {
if (!value) {
ref.read(sourceLanguageFilterProvider.notifier).updateWithPreviousState(
(enabledLanguages) => [...?enabledLanguages]..remove(langCode));
} else {
ref.read(sourceLanguageFilterProvider.notifier).updateWithPreviousState(
(enabledLanguages) => {...?enabledLanguages, langCode}.toList(),
);
}
}
}

@riverpod
Expand All @@ -56,7 +74,7 @@ AsyncValue<Map<String, List<Source>>?> sourceMapFiltered(
final sourceMapFiltered = <String, List<Source>>{};
final sourceMapData = ref.watch(sourceMapProvider);
final sourceMap = {...?sourceMapData.valueOrNull};
final enabledLangList = [...?ref.watch(sourceLanguageFilterProvider)];
final enabledLangList = [...?ref.watch(sourceLanguageFilterProvider)]..sort();
for (final e in enabledLangList) {
if (sourceMap.containsKey(e)) sourceMapFiltered[e] = sourceMap[e]!;
}
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SourceScreen extends HookConsumerWidget {
withMicrotask: true,
);
return;
}, [sourceMapData.value]);
}, [sourceMapData.valueOrNull]);

return sourceMapData.showUiWhenData(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../../../../../constants/language_list.dart';
Expand All @@ -14,40 +15,40 @@ import '../../../../../widgets/pop_button.dart';
import '../../../domain/language/language_model.dart';
import '../controller/source_controller.dart';

class SourceLanguageFilter extends ConsumerWidget {
class SourceLanguageFilter extends HookConsumerWidget {
const SourceLanguageFilter({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final languageCodes = ref.watch(sourceFilterLangListProvider);
final enabledLanguages = ref.watch(sourceLanguageFilterProvider);
final languageCodesMap = ref.watch(sourceFilterLangMapProvider);
final languageCodes = useState([]);

useEffect(() {
final languageList = languageCodesMap.keys.toList();
languageList.sort((a, b) {
final aExist = (languageCodesMap[a].ifNull());
final bExist = (languageCodesMap[b].ifNull());
return aExist == bExist ? a.compareTo(b) : bExist.toIntWithNegative;
});
languageCodes.value = languageList;
return null;
}, []);

return AlertDialog(
title: Text(context.l10n!.languages),
content: SizedBox(
height: context.heightScale(scale: .5),
width: context.widthScale(scale: context.isSmallTablet ? .5 : .8),
child: ListView.builder(
itemCount: languageCodes.length,
itemCount: languageCodes.value.length,
itemBuilder: (context, index) {
final String languageCode = (languageCodes[index]).toLowerCase();
final String languageCode = languageCodes.value[index];
final Language? language = languageMap[languageCode];
final enabledLanguagesIndex =
enabledLanguages?.indexOf(languageCode);
return SwitchListTile(
value: enabledLanguagesIndex != -1,
value: languageCodesMap[languageCode].ifNull(),
onChanged: (value) {
if (value) {
ref.read(sourceLanguageFilterProvider.notifier).update(
{...?enabledLanguages, languageCode}.toList(),
);
} else {
if (!((enabledLanguagesIndex?.isNegative).ifNull(true))) {
final updatedEnabledLanguages = [...?enabledLanguages]
..remove(languageCode);
ref
.read(sourceLanguageFilterProvider.notifier)
.update(updatedEnabledLanguages);
}
}
ref
.read(sourceFilterLangMapProvider.notifier)
.toggleLang(languageCode, value);
},
title: Text(
language?.nativeName ?? language?.name ?? languageCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SourceListTile extends ConsumerWidget {
size: const Size.square(48),
),
),
title: Text(source.displayName ?? source.name ?? ""),
title: Text(source.name ?? ""),
subtitle: (source.lang?.displayName).isNotBlank
? Text(source.lang?.displayName ?? "")
: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class EditCategoryScreen extends HookConsumerWidget {
withMicrotask: true,
);
return;
}, [categoryList.value]);
}, [categoryList.valueOrNull]);

return Scaffold(
appBar: AppBar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class CategoryMangaList extends HookConsumerWidget {
}
final Widget mangaList = switch (displayMode) {
DisplayMode.list || null => ListView.builder(
itemExtent: 96,
itemCount: (data?.length).getValueOnNullOrNegative(),
itemBuilder: (context, index) => MangaCoverListTile(
manga: data![index],
Expand Down Expand Up @@ -106,6 +107,7 @@ class CategoryMangaList extends HookConsumerWidget {
),
),
DisplayMode.descriptiveList => ListView.builder(
itemExtent: 176,
itemCount: (data?.length).getValueOnNullOrNegative(),
itemBuilder: (context, index) => MangaCoverDescriptiveListTile(
manga: data![index],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LibraryScreen extends HookConsumerWidget {
useEffect(() {
categoryList.showToastOnError(toast, withMicrotask: true);
return;
}, [categoryList.value]);
}, [categoryList.valueOrNull]);

return categoryList.showUiWhenData(
context,
Expand Down Expand Up @@ -135,7 +135,8 @@ class LibraryScreen extends HookConsumerWidget {
child: TabBarView(
children: data
.map((e) => CategoryMangaList(
categoryId: e.id.getValueOnNullOrNegative()))
categoryId: e.id.getValueOnNullOrNegative(),
))
.toList(),
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,24 @@ Map<int, DownloadsQueue> downloadsMap(DownloadsMapRef ref) {

@riverpod
DownloadsQueue? downloadsFromId(DownloadsFromIdRef ref, int chapterId) =>
ref.watch(downloadsMapProvider.select((value) => value[chapterId]));
ref.watch(downloadsMapProvider.select((map) => map[chapterId]));

@riverpod
List<int> downloadsChapterIds(DownloadsChapterIdsRef ref) {
return ref.watch(downloadsMapProvider).keys.toList();
}

@riverpod
AsyncValue<String?> downloadsStatus(DownloadsStatusRef ref) {
return ref.watch(downloadsSocketProvider
.select((value) => value.copyWithData((data) => data.status)));
}

@riverpod
bool showDownloadsFAB(ShowDownloadsFABRef ref) {
final downloads = ref.watch(downloadsSocketProvider);
return (downloads.valueOrNull?.queue).isNotBlank &&
downloads.valueOrNull!.queue!.any(
(element) => element.state != "Error" || element.tries != 3,
);
}

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

Loading

0 comments on commit db34644

Please sign in to comment.