diff --git a/lib/main.dart b/lib/main.dart index b058949d..e581eca6 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -4,10 +4,11 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'package:hive_flutter/hive_flutter.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:package_info_plus/package_info_plus.dart'; +import 'package:path_provider/path_provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'src/features/about/presentation/about/controllers/about_controller.dart'; @@ -18,12 +19,14 @@ Future main() async { WidgetsFlutterBinding.ensureInitialized(); final packageInfo = await PackageInfo.fromPlatform(); final sharedPreferences = await SharedPreferences.getInstance(); - await Hive.initFlutter(); + var appDirectory = (kIsWeb) ? null : await getApplicationDocumentsDirectory(); + runApp( ProviderScope( overrides: [ packageInfoProvider.overrideWithValue(packageInfo), sharedPreferencesProvider.overrideWithValue(sharedPreferences), + appDirectoryProvider.overrideWithValue(appDirectory), ], child: const Sorayomi(), ), diff --git a/lib/src/features/settings/presentation/general/general_screen.dart b/lib/src/features/settings/presentation/general/general_screen.dart index 129271af..25ebe8ce 100644 --- a/lib/src/features/settings/presentation/general/general_screen.dart +++ b/lib/src/features/settings/presentation/general/general_screen.dart @@ -12,6 +12,7 @@ import 'package:hooks_riverpod/hooks_riverpod.dart'; import '../../../../constants/language_list.dart'; import '../../../../global_providers/global_providers.dart'; import '../../../../utils/extensions/custom_extensions.dart'; +import '../../../../utils/misc/toast/toast.dart'; import '../../../../widgets/radio_list_popup.dart'; class GeneralScreen extends ConsumerWidget { @@ -41,6 +42,18 @@ class GeneralScreen extends ConsumerWidget { ), ), ), + ListTile( + leading: const Icon(Icons.cached_rounded), + title: Text(context.l10n!.clearCache), + onTap: () async { + await ref.watch(hiveCacheStoreProvider).clean(); + if (context.mounted) { + ref + .read(toastProvider(context)) + .show(context.l10n!.cacheCleared); + } + }, + ), ], ), ); diff --git a/lib/src/global_providers/global_providers.dart b/lib/src/global_providers/global_providers.dart index d1b8ff01..b5f29de5 100644 --- a/lib/src/global_providers/global_providers.dart +++ b/lib/src/global_providers/global_providers.dart @@ -4,6 +4,9 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +import 'dart:io'; + +import 'package:dio_cache_interceptor_hive_store/dio_cache_interceptor_hive_store.dart'; import 'package:flutter/material.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -20,11 +23,12 @@ import '../utils/storage/dio/network_module.dart'; part 'global_providers.g.dart'; @riverpod -DioClient dioClientKey(ref) => DioClient( +DioClient dioClientKey(DioClientKeyRef ref) => DioClient( dio: ref.watch(networkModuleProvider).provideDio( baseUrl: ref.watch(serverUrlProvider) ?? DBKeys.serverUrl.initial, authType: ref.watch(authTypeKeyProvider) ?? DBKeys.authType.initial, credentials: ref.watch(credentialsProvider), + hiveCacheStore: ref.watch(hiveCacheStoreProvider), ), ); @@ -67,3 +71,10 @@ class L10n extends _$L10n with SharedPreferenceClientMixin { @riverpod SharedPreferences sharedPreferences(ref) => throw UnimplementedError(); + +@riverpod +Directory? appDirectory(ref) => throw UnimplementedError(); + +@riverpod +HiveCacheStore hiveCacheStore(HiveCacheStoreRef ref) => + HiveCacheStore(ref.watch(appDirectoryProvider)?.path); diff --git a/lib/src/global_providers/global_providers.g.dart b/lib/src/global_providers/global_providers.g.dart index 0cdfb762..c97cfc3c 100644 --- a/lib/src/global_providers/global_providers.g.dart +++ b/lib/src/global_providers/global_providers.g.dart @@ -6,7 +6,7 @@ part of 'global_providers.dart'; // RiverpodGenerator // ************************************************************************** -String _$dioClientKeyHash() => r'aa6e40fe7fd11fd2fd884674cf4d47c1c5f93048'; +String _$dioClientKeyHash() => r'f8ac67719cc287981dabd817e2010e3b8650659a'; /// See also [dioClientKey]. @ProviderFor(dioClientKey) @@ -36,6 +36,35 @@ final sharedPreferencesProvider = ); typedef SharedPreferencesRef = AutoDisposeProviderRef; +String _$appDirectoryHash() => r'598612365ff87cbdf65761e923161467412ec463'; + +/// See also [appDirectory]. +@ProviderFor(appDirectory) +final appDirectoryProvider = AutoDisposeProvider.internal( + appDirectory, + name: r'appDirectoryProvider', + debugGetCreateSourceHash: + const bool.fromEnvironment('dart.vm.product') ? null : _$appDirectoryHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef AppDirectoryRef = AutoDisposeProviderRef; +String _$hiveCacheStoreHash() => r'9b2759c40cc610a05bf24953b306f6dec44cb2e6'; + +/// See also [hiveCacheStore]. +@ProviderFor(hiveCacheStore) +final hiveCacheStoreProvider = AutoDisposeProvider.internal( + hiveCacheStore, + name: r'hiveCacheStoreProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$hiveCacheStoreHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef HiveCacheStoreRef = AutoDisposeProviderRef; String _$authTypeKeyHash() => r'8264b20583c2d0e3c9da5073ff1c13fda7e7fc34'; /// See also [AuthTypeKey]. diff --git a/lib/src/l10n/app_en.arb b/lib/src/l10n/app_en.arb index d3567f50..43fc2da1 100644 --- a/lib/src/l10n/app_en.arb +++ b/lib/src/l10n/app_en.arb @@ -45,6 +45,9 @@ "@buildTime": { "description": "Text title to show build time of Server in About Screen" }, + "@cacheCleared": { + "description": "Toast Text to show after clearing cache" + }, "@cancel": { "description": "Text for Cancel button in a Popup" }, @@ -81,6 +84,9 @@ "@checkForUpdates": { "description": "Button text to check for App Updates" }, + "@clearCache": { + "description": "Button text for clear cache in General Settings screen" + }, "@client": { "description": "Text title for the name of the client in About screen" }, @@ -611,6 +617,7 @@ "bookmarked": "Bookmarked", "browse": "Browse", "buildTime": "Build time", + "cacheCleared": "Cache Cleared", "cancel": "Cancel", "categories": "Categories", "categoryUpdate": "Category Update", @@ -621,6 +628,7 @@ "chapterSortUploadDate": "By Upload Date", "checkForServerUpdates": "Check for Server updates", "checkForUpdates": "Check for updates", + "clearCache": "Clear Cache", "client": "Client", "clientVersion": "Client version", "close": "Close", diff --git a/lib/src/utils/storage/dio/network_module.dart b/lib/src/utils/storage/dio/network_module.dart index f3cc6b9d..eb672e1c 100644 --- a/lib/src/utils/storage/dio/network_module.dart +++ b/lib/src/utils/storage/dio/network_module.dart @@ -27,10 +27,11 @@ class DioNetworkModule { Dio provideDio({ required String baseUrl, required AuthType authType, + HiveCacheStore? hiveCacheStore, String? credentials, }) { final cacheOptions = CacheOptions( - store: HiveCacheStore('dio_cache'), + store: hiveCacheStore, policy: CachePolicy.refreshForceCache, hitCacheOnErrorExcept: [401, 403], priority: CachePriority.normal, diff --git a/lib/src/widgets/async_buttons/async_text_button_icon.dart b/lib/src/widgets/async_buttons/async_text_button_icon.dart index 26f2b83b..466adb19 100644 --- a/lib/src/widgets/async_buttons/async_text_button_icon.dart +++ b/lib/src/widgets/async_buttons/async_text_button_icon.dart @@ -43,7 +43,7 @@ class AsyncTextButtonIcon extends HookWidget { icon: (secondaryIcon != null && !isLocalPrimary.value) ? secondaryIcon! : primaryIcon, - label: (secondaryLabel != null && isLocalPrimary.value) + label: (secondaryLabel != null && !isLocalPrimary.value) ? secondaryLabel! : primaryLabel, ); diff --git a/pubspec.lock b/pubspec.lock index b18abe93..a10474b9 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -502,29 +502,13 @@ packages: source: hosted version: "2.2.0" hive: - dependency: "direct main" + dependency: transitive description: name: hive sha256: "8dcf6db979d7933da8217edcec84e9df1bdb4e4edc7fc77dbd5aa74356d6d941" url: "https://pub.dev" source: hosted version: "2.2.3" - hive_flutter: - dependency: "direct main" - description: - name: hive_flutter - sha256: dca1da446b1d808a51689fb5d0c6c9510c0a2ba01e22805d492c73b68e33eecc - url: "https://pub.dev" - source: hosted - version: "1.1.0" - hive_generator: - dependency: "direct dev" - description: - name: hive_generator - sha256: "65998cc4d2cd9680a3d9709d893d2f6bb15e6c1f92626c3f1fa650b4b3281521" - url: "https://pub.dev" - source: hosted - version: "2.0.0" hooks_riverpod: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 98f97101..f00e8c8d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -24,8 +24,6 @@ dependencies: font_awesome_flutter: ^10.2.1 freezed_annotation: ^2.2.0 go_router: 6.4.1 - hive: ^2.2.3 - hive_flutter: ^1.1.0 hooks_riverpod: ^2.1.1 infinite_scroll_pagination: ^3.2.0 intl: any @@ -51,7 +49,6 @@ dev_dependencies: flutter_test: sdk: flutter freezed: ^2.2.1 - hive_generator: ^2.0.0 json_serializable: ^6.5.4 lints: ^2.0.1 riverpod_generator: ^2.0.0