Skip to content
This repository has been archived by the owner on Sep 14, 2024. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Leptopoda committed Apr 21, 2023
1 parent 0507640 commit 9d72ef2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/src/screens/category_screen.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_cache_manager_dio/flutter_cache_manager_dio.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_translate/flutter_translate.dart';
import 'package:nc_cookbook_api/nc_cookbook_api.dart';
Expand Down Expand Up @@ -72,7 +72,7 @@ class _CategoryScreenState extends State<CategoryScreen> {
}

Future<void> refresh() async {
await DefaultCacheManager().emptyCache();
await DioCacheManager.instance.emptyCache();
// ignore: use_build_context_synchronously
BlocProvider.of<CategoriesBloc>(context).add(const CategoriesLoaded());
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/screens/recipes_list_screen.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_cache_manager_dio/flutter_cache_manager_dio.dart';
import 'package:flutter_translate/flutter_translate.dart';
import 'package:nextcloud_cookbook_flutter/src/blocs/recipes_short/recipes_short_bloc.dart';
import 'package:nextcloud_cookbook_flutter/src/widget/recipe_list_item.dart';
Expand All @@ -18,7 +18,7 @@ class RecipesListScreen extends StatefulWidget {

class _RecipesListScreenState extends State<RecipesListScreen> {
Future<void> refresh() async {
await DefaultCacheManager().emptyCache();
await DioCacheManager.instance.emptyCache();
// ignore: use_build_context_synchronously
BlocProvider.of<RecipesShortBloc>(context)
.add(RecipesShortLoaded(category: widget.category));
Expand Down
42 changes: 39 additions & 3 deletions lib/src/services/api_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ part of 'services.dart';

class ApiProvider {
factory ApiProvider() => _apiProvider;
ApiProvider._() {
ApiProvider._();
static final ApiProvider _apiProvider = ApiProvider._();

Future<void> initialize() async {
final auth = UserRepository().currentAppAuthentication;

final client = Dio(
Expand All @@ -23,8 +26,42 @@ class ApiProvider {
);
}

final documentsDir = await getApplicationDocumentsDirectory();

// Global options
final options = CacheOptions(
// A default store is required for interceptor.
store: SembastCacheStore(storePath: documentsDir.path),

// All subsequent fields are optional.

// Default.
policy: CachePolicy.noCache,
// Returns a cached response on error but for statuses 401 & 403.
// Also allows to return a cached response on network errors (e.g. offline usage).
// Defaults to [null].
hitCacheOnErrorExcept: [401, 403],
// Overrides any HTTP directive to delete entry past this duration.
// Useful only when origin server has no cache config or custom behaviour is desired.
// Defaults to [null].
maxStale: const Duration(days: 7),
// Default. Allows 3 cache sets and ease cleanup.
priority: CachePriority.normal,
// Default. Body and headers encryption with your own algorithm.
cipher: null,
// Default. Key builder to retrieve requests.
keyBuilder: CacheOptions.defaultCacheKeyBuilder,
// Default. Allows to cache POST requests.
// Overriding [keyBuilder] is strongly recommended when [true].
allowPostMethod: false,
);

ncCookbookApi = NcCookbookApi(
dio: client,
basePathOverride: '${auth.server}/apps/cookbook',
interceptors: [
BasicAuthInterceptor(),
DioCacheInterceptor(options: options),
],
);

ncCookbookApi.setBasicAuth(
Expand All @@ -37,7 +74,6 @@ class ApiProvider {
miscApi = ncCookbookApi.getMiscApi();
tagsApi = ncCookbookApi.getTagsApi();
}
static final ApiProvider _apiProvider = ApiProvider._();

late NcCookbookApi ncCookbookApi;
late RecipesApi recipeApi;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/services/services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'dart:developer';
import 'package:dio/dio.dart' as dio;
import 'package:dio/dio.dart';
import 'package:dio/io.dart';
import 'package:dio_cache_interceptor/dio_cache_interceptor.dart';
import 'package:dio_cache_interceptor_sembast_storage/dio_cache_interceptor_sembast_storage.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
Expand All @@ -17,6 +19,7 @@ import 'package:nextcloud_cookbook_flutter/src/models/image_response.dart';
import 'package:nextcloud_cookbook_flutter/src/models/timer.dart';
import 'package:nextcloud_cookbook_flutter/src/screens/recipe_import_screen.dart';
import 'package:nextcloud_cookbook_flutter/src/util/url_validator.dart';
import 'package:path_provider/path_provider.dart';
import 'package:timezone/data/latest_10y.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
import 'package:xml/xml.dart';
Expand Down
1 change: 1 addition & 0 deletions lib/src/services/user_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class UserRepository {
ApiProvider().ncCookbookApi.isSupportedSync(version);

Future<APIVersion> fetchApiVersion() async {
await ApiProvider().initialize();
final response = await ApiProvider().miscApi.version();

return response.data!.apiVersion;
Expand Down
9 changes: 8 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ dependencies:
timezone: ^0.9.1

cached_network_image: ^3.0.0
flutter_cache_manager: ^3.3.0
#flutter_cache_manager: ^3.3.0
flutter_cache_manager_dio:
git:
url: https://github.com/josh-burton/flutter_cache_manager_dio.git
ref: dio-5.x
dio_cache_interceptor_sembast_storage: ^0.1.0
dio_cache_interceptor: ^3.4.1
path_provider: ^2.0.14

flutter_typeahead: ^4.3.7

Expand Down

0 comments on commit 9d72ef2

Please sign in to comment.