Skip to content

Commit

Permalink
Merge pull request #231 from DattatreyaReddy:revert-use-state-record
Browse files Browse the repository at this point in the history
reverted useStateRecord
  • Loading branch information
DattatreyaReddy authored Oct 4, 2023
2 parents 2f2fd8b + 2dd231e commit 619b717
Show file tree
Hide file tree
Showing 27 changed files with 186 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import '../../../../constants/app_sizes.dart';

import '../../../../routes/router_config.dart';
import '../../../../utils/extensions/custom_extensions.dart';
import '../../../../utils/hooks/hook_primitives_wrapper.dart';
import '../../../../widgets/search_field.dart';
import '../extension/controller/extension_controller.dart';
import '../extension/extension_screen.dart';
Expand All @@ -29,14 +28,14 @@ class BrowseScreen extends HookConsumerWidget {
final tabController = useTabController(initialLength: 2);
useListenable(tabController);
final key = useMemoized(() => GlobalKey());
final (showSearch, setShowSearch) = useStateRecord(false);
final showSearch = useState(false);
return Scaffold(
appBar: AppBar(
title: Text(context.l10n!.browse),
centerTitle: true,
actions: [
IconButton(
onPressed: () => setShowSearch(true),
onPressed: () => showSearch.value = (true),
icon: Icon(
tabController.index == 0
? Icons.travel_explore_rounded
Expand All @@ -57,7 +56,7 @@ class BrowseScreen extends HookConsumerWidget {
),
],
bottom: PreferredSize(
preferredSize: kCalculateAppBarBottomSize([true, showSearch]),
preferredSize: kCalculateAppBarBottomSize([true, showSearch.value]),
child: Column(
children: [
TabBar(
Expand All @@ -69,7 +68,7 @@ class BrowseScreen extends HookConsumerWidget {
Tab(text: context.l10n!.extensions),
],
),
if (showSearch)
if (showSearch.value)
Align(
alignment: Alignment.centerRight,
child: tabController.index == 0
Expand All @@ -80,15 +79,15 @@ class BrowseScreen extends HookConsumerWidget {
GlobalSearchRoute(query: value).push(context);
}
},
onClose: () => setShowSearch(false),
onClose: () => showSearch.value = (false),
)
: SearchField(
key: const ValueKey(1),
initialText: ref.read(extensionQueryProvider),
onChanged: (val) => ref
.read(extensionQueryProvider.notifier)
.update(val),
onClose: () => setShowSearch(false),
onClose: () => showSearch.value = (false),
),
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

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

import '../../../../../constants/app_sizes.dart';
import '../../../../../utils/extensions/custom_extensions.dart';
import '../../../../../utils/hooks/hook_primitives_wrapper.dart';
import '../../../../../utils/misc/toast/toast.dart';
import '../../../../../widgets/server_image.dart';
import '../../../data/extension_repository/extension_repository.dart';
Expand All @@ -30,7 +30,7 @@ class ExtensionListTile extends HookConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final repository = ref.watch(extensionRepositoryProvider);
final (isLoading, setIsLoading) = useStateRecord(false);
final isLoading = useState(false);
return ListTile(
key: key,
leading: ClipRRect(
Expand All @@ -39,7 +39,7 @@ class ExtensionListTile extends HookConsumerWidget {
url: extension.iconUrl ?? "",
outerSize: const Size.square(48),
innerSize: const Size.square(24),
isLoading: isLoading,
isLoading: isLoading.value,
),
),
title: Text(
Expand Down Expand Up @@ -73,7 +73,6 @@ class ExtensionListTile extends HookConsumerWidget {
extension: extension,
repository: repository,
isLoading: isLoading,
setIsLoading: setIsLoading,
ref: ref,
refresh: refresh,
),
Expand All @@ -87,15 +86,13 @@ class ExtensionListTileTailing extends StatelessWidget {
required this.extension,
required this.repository,
required this.isLoading,
required this.setIsLoading,
required this.ref,
required this.refresh,
}) : super(key: key);

final Extension extension;
final ExtensionRepository repository;
final bool isLoading;
final ValueSetter<bool> setIsLoading;
final ValueNotifier<bool> isLoading;
final WidgetRef ref;
final AsyncCallback refresh;

Expand All @@ -114,10 +111,10 @@ class ExtensionListTileTailing extends StatelessWidget {
} else {
if (extension.installed.ifNull()) {
return TextButton(
onPressed: (!isLoading)
onPressed: (!isLoading.value)
? () async {
try {
setIsLoading(true);
isLoading.value = (true);
final result = (await AsyncValue.guard(
() async {
if (extension.pkgName.isBlank) {
Expand All @@ -136,28 +133,28 @@ class ExtensionListTileTailing extends StatelessWidget {
if (context.mounted) {
result.showToastOnError(ref.read(toastProvider(context)));
}
setIsLoading(false);
isLoading.value = (false);
} catch (e) {
//
}
}
: null,
child: Text(
extension.hasUpdate.ifNull()
? isLoading
? isLoading.value
? context.l10n!.updating
: context.l10n!.update
: isLoading
: isLoading.value
? context.l10n!.uninstalling
: context.l10n!.uninstall,
),
);
} else {
return TextButton(
onPressed: !isLoading
onPressed: !isLoading.value
? () async {
try {
setIsLoading(true);
isLoading.value = (true);
final result = await AsyncValue.guard(() async {
if (extension.pkgName.isBlank) {
throw context.l10n!.errorExtension;
Expand All @@ -183,14 +180,14 @@ class ExtensionListTileTailing extends StatelessWidget {
ref.read(toastProvider(context)),
);
}
setIsLoading(false);
isLoading.value = (false);
} catch (e) {
//
}
}
: null,
child: Text(
isLoading ? context.l10n!.installing : context.l10n!.install,
isLoading.value ? context.l10n!.installing : context.l10n!.install,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// 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/app_sizes.dart';
import '../../../../utils/extensions/custom_extensions.dart';
import '../../../../utils/hooks/hook_primitives_wrapper.dart';
import '../../../../widgets/emoticons.dart';
import '../../../../widgets/search_field.dart';
import '../source/controller/source_controller.dart';
Expand All @@ -21,9 +21,9 @@ class GlobalSearchScreen extends HookConsumerWidget {
final String? initialQuery;
@override
Widget build(BuildContext context, WidgetRef ref) {
final (query, setQuery) = useStateRecord(initialQuery);
final query = useState(initialQuery);
final quickSearchResult =
ref.watch(quickSearchResultsProvider(query: query));
ref.watch(quickSearchResultsProvider(query: query.value));
return Scaffold(
appBar: AppBar(
title: Text(context.l10n!.globalSearch),
Expand All @@ -34,8 +34,8 @@ class GlobalSearchScreen extends HookConsumerWidget {
Align(
alignment: Alignment.centerRight,
child: SearchField(
initialText: query,
onSubmitted: setQuery,
initialText: query.value,
onSubmitted: (value) => query.value = value,
),
),
],
Expand All @@ -60,7 +60,7 @@ class GlobalSearchScreen extends HookConsumerWidget {
return SourceShortSearch(
source: data[index].source,
mangaList: data[index].mangaList,
query: query,
query: query.value,
);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import '../../../../constants/enum.dart';

import '../../../../routes/router_config.dart';
import '../../../../utils/extensions/custom_extensions.dart';
import '../../../../utils/hooks/hook_primitives_wrapper.dart';
import '../../../../utils/hooks/paging_controller_hook.dart';
import '../../../../widgets/search_field.dart';
import '../../../manga_book/domain/manga/manga_model.dart';
Expand Down Expand Up @@ -44,15 +43,15 @@ class SourceMangaListScreen extends HookConsumerWidget {
SourceRepository repository,
PagingController<int, Manga> controller,
int pageKey, {
String? query,
ValueNotifier<String?>? query,
List<Map<String, dynamic>>? filter,
}) {
AsyncValue.guard(
() => repository.getMangaList(
sourceId: sourceId,
sourceType: sourceType,
pageNum: pageKey,
query: query,
query: query?.value,
filter: filter,
),
).then(
Expand Down Expand Up @@ -116,8 +115,8 @@ class SourceMangaListScreen extends HookConsumerWidget {
final filterList = ref.watch(filtersProvider);
final source = ref.watch(sourceProvider(sourceId));

final (query, setQuery) = useStateRecord(initialQuery);
final (showSearch, setShowSearch) = useStateRecord(initialQuery.isNotBlank);
final query = useState(initialQuery);
final showSearch = useState(initialQuery.isNotBlank);
final controller = usePagingController<int, Manga>(firstPageKey: 1);

useEffect(() {
Expand All @@ -139,7 +138,7 @@ class SourceMangaListScreen extends HookConsumerWidget {
title: Text(data?.displayName ?? context.l10n!.source),
actions: [
IconButton(
onPressed: () => setShowSearch(true),
onPressed: () => showSearch.value = true,
icon: const Icon(Icons.search_rounded),
),
const SourceMangaDisplayIconPopup(),
Expand All @@ -151,7 +150,7 @@ class SourceMangaListScreen extends HookConsumerWidget {
),
],
bottom: PreferredSize(
preferredSize: kCalculateAppBarBottomSize([true, showSearch]),
preferredSize: kCalculateAppBarBottomSize([true, showSearch.value]),
child: Column(
children: [
Row(
Expand Down Expand Up @@ -202,15 +201,15 @@ class SourceMangaListScreen extends HookConsumerWidget {
],
),
const Divider(height: 0),
if (showSearch)
if (showSearch.value)
Align(
alignment: Alignment.centerRight,
child: SearchField(
initialText: query,
onClose: () => setShowSearch(false),
initialText: query.value,
onClose: () => showSearch.value = (false),
onSubmitted: (val) {
if (sourceType == SourceType.filter) {
setQuery(val);
query.value = (val);
controller.refresh();
} else {
if (val == null) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'package:flutter_hooks/flutter_hooks.dart';
import '../../../../../constants/app_sizes.dart';

import '../../../../../utils/extensions/custom_extensions.dart';
import '../../../../../utils/hooks/hook_primitives_wrapper.dart';
import '../../../domain/filter/filter_model.dart';
import 'filter_to_widget.dart';

Expand All @@ -28,7 +27,7 @@ class SourceMangaFilter extends HookWidget {
final VoidCallback onReset;
@override
Widget build(BuildContext context) {
final (filters, setFilters) = useStateRecord(initialFilters);
final filters = useState(initialFilters);
return Scaffold(
appBar: PreferredSize(
preferredSize: const Size.fromHeight(kAppBarBottomHeight),
Expand All @@ -39,13 +38,13 @@ class SourceMangaFilter extends HookWidget {
TextButton(
onPressed: () {
onReset();
setFilters(initialFilters);
filters.value = (initialFilters);
},
child: Text(context.l10n!.reset),
),
const Spacer(),
FilledButton(
onPressed: () => onSubmitted(filters),
onPressed: () => onSubmitted(filters.value),
child: Text(context.l10n!.filter),
),
],
Expand All @@ -54,20 +53,20 @@ class SourceMangaFilter extends HookWidget {
),
body: ListView.builder(
itemBuilder: (context, index) {
final filter = filters[index];
final filter = filters.value[index];
return FilterToWidget(
key: ValueKey("Filter-${filter.filterState?.name}"),
filter: filter,
onChanged: (value) {
setFilters([...initialFilters]..replaceRange(
filters.value = ([...initialFilters]..replaceRange(
index,
index + 1,
[value],
));
},
);
},
itemCount: filters.length,
itemCount: filters.value.length,
),
);
}
Expand Down
Loading

0 comments on commit 619b717

Please sign in to comment.