Browse Source

refactor(neon,neon_files,neon_news,neon_notes,neon_notifications,app): Use global instance of RequestManager

Signed-off-by: jld3103 <jld3103yt@gmail.com>
pull/495/head
jld3103 1 year ago
parent
commit
6199b3d9f1
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 10
      packages/app/lib/apps.dart
  2. 13
      packages/neon/neon/lib/neon.dart
  3. 2
      packages/neon/neon/lib/src/bloc/bloc.dart
  4. 14
      packages/neon/neon/lib/src/blocs/accounts.dart
  5. 4
      packages/neon/neon/lib/src/blocs/apps.dart
  6. 4
      packages/neon/neon/lib/src/blocs/capabilities.dart
  7. 4
      packages/neon/neon/lib/src/blocs/user_details.dart
  8. 3
      packages/neon/neon/lib/src/models/app_implementation.dart
  9. 1
      packages/neon/neon/lib/src/models/notifications_interface.dart
  10. 6
      packages/neon/neon/lib/src/utils/push_utils.dart
  11. 31
      packages/neon/neon/lib/src/utils/request_manager.dart
  12. 2
      packages/neon/neon/lib/utils.dart
  13. 4
      packages/neon/neon_files/lib/blocs/browser.dart
  14. 4
      packages/neon/neon_files/lib/blocs/files.dart
  15. 3
      packages/neon/neon_files/lib/neon_files.dart
  16. 5
      packages/neon/neon_news/lib/blocs/articles.dart
  17. 7
      packages/neon/neon_news/lib/blocs/news.dart
  18. 3
      packages/neon/neon_news/lib/neon_news.dart
  19. 1
      packages/neon/neon_news/lib/pages/feed.dart
  20. 1
      packages/neon/neon_news/lib/widgets/folder_view.dart
  21. 4
      packages/neon/neon_notes/lib/blocs/notes.dart
  22. 3
      packages/neon/neon_notes/lib/neon_notes.dart
  23. 4
      packages/neon/neon_notifications/lib/blocs/notifications.dart
  24. 3
      packages/neon/neon_notifications/lib/neon_notifications.dart

10
packages/app/lib/apps.dart

@ -1,6 +1,5 @@
import 'package:neon/models.dart';
import 'package:neon/platform.dart';
import 'package:neon/utils.dart';
import 'package:neon_files/neon_files.dart';
import 'package:neon_news/neon_news.dart';
import 'package:neon_notes/neon_notes.dart';
@ -9,12 +8,11 @@ import 'package:shared_preferences/shared_preferences.dart';
List<AppImplementation> getAppImplementations(
final SharedPreferences sharedPreferences,
final RequestManager requestManager,
final NeonPlatform platform,
) =>
[
FilesApp(sharedPreferences, requestManager, platform),
NewsApp(sharedPreferences, requestManager, platform),
NotesApp(sharedPreferences, requestManager, platform),
NotificationsApp(sharedPreferences, requestManager, platform),
FilesApp(sharedPreferences, platform),
NewsApp(sharedPreferences, platform),
NotesApp(sharedPreferences, platform),
NotificationsApp(sharedPreferences, platform),
];

13
packages/neon/neon/lib/neon.dart

@ -19,8 +19,7 @@ import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
Future runNeon({
required final Iterable<AppImplementation> Function(SharedPreferences, RequestManager, NeonPlatform)
getAppImplementations,
required final Iterable<AppImplementation> Function(SharedPreferences, NeonPlatform) getAppImplementations,
required final NeonTheme theme,
@visibleForTesting final WidgetsBinding? bindingOverride,
@visibleForTesting final Account? account,
@ -33,10 +32,8 @@ Future runNeon({
final sharedPreferences = await SharedPreferences.getInstance();
final platform = await getNeonPlatform();
final cache = Cache(platform);
await cache.init();
final requestManager = RequestManager(cache);
final allAppImplementations = getAppImplementations(sharedPreferences, requestManager, platform);
await RequestManager.instance.initCache(platform);
final allAppImplementations = getAppImplementations(sharedPreferences, platform);
final packageInfo = await PackageInfo.fromPlatform();
buildUserAgent(packageInfo);
@ -47,7 +44,6 @@ Future runNeon({
);
final accountsBloc = AccountsBloc(
requestManager,
platform,
sharedPreferences,
globalOptions,
@ -86,9 +82,6 @@ Future runNeon({
Provider<GlobalOptions>(
create: (final _) => globalOptions,
),
Provider<RequestManager>(
create: (final _) => requestManager,
),
Provider<AccountsBloc>(
create: (final _) => accountsBloc,
),

2
packages/neon/neon/lib/src/bloc/bloc.dart

@ -32,7 +32,7 @@ abstract class InteractiveBloc extends Bloc {
if (disableTimeout) {
await call();
} else {
await RequestManager.timeout(call);
await RequestManager.instance.timeout(call);
}
await (refresh ?? this.refresh)();

14
packages/neon/neon/lib/src/blocs/accounts.dart

@ -14,7 +14,6 @@ import 'package:neon/src/platform/platform.dart';
import 'package:neon/src/settings/models/storage.dart';
import 'package:neon/src/utils/account_options.dart';
import 'package:neon/src/utils/global_options.dart';
import 'package:neon/src/utils/request_manager.dart';
import 'package:rxdart/rxdart.dart';
import 'package:shared_preferences/shared_preferences.dart';
@ -58,7 +57,6 @@ abstract interface class AccountsBlocStates {
class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocStates {
AccountsBloc(
this._requestManager,
this._platform,
this._sharedPreferences,
this._globalOptions,
@ -100,7 +98,6 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
}
}
final RequestManager _requestManager;
final NeonPlatform _platform;
late final AppStorage _storage = AppStorage('accounts', _sharedPreferences);
final SharedPreferences _sharedPreferences;
@ -237,7 +234,6 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
}
return _appsBlocs[account.id] = AppsBloc(
_requestManager,
getCapabilitiesBlocFor(account),
this,
account,
@ -258,10 +254,7 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
return _capabilitiesBlocs[account.id]!;
}
return _capabilitiesBlocs[account.id] = CapabilitiesBloc(
_requestManager,
account,
);
return _capabilitiesBlocs[account.id] = CapabilitiesBloc(account);
}
/// The userDetailsBloc for the [activeAccount].
@ -277,10 +270,7 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
return _userDetailsBlocs[account.id]!;
}
return _userDetailsBlocs[account.id] = UserDetailsBloc(
_requestManager,
account,
);
return _userDetailsBlocs[account.id] = UserDetailsBloc(account);
}
/// The userStatusBloc for the [activeAccount].

4
packages/neon/neon/lib/src/blocs/apps.dart

@ -41,7 +41,6 @@ abstract class AppsBlocStates {
@internal
class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates {
AppsBloc(
this._requestManager,
this._capabilitiesBloc,
this._accountsBloc,
this._account,
@ -151,7 +150,6 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates
Iterable<AppImplementation> _filteredAppImplementations(final Iterable<String> appIds) =>
_allAppImplementations.where((final a) => appIds.contains(a.id));
final RequestManager _requestManager;
final CapabilitiesBloc _capabilitiesBloc;
final AccountsBloc _accountsBloc;
final Account _account;
@ -195,7 +193,7 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates
@override
Future refresh() async {
await _requestManager
await RequestManager.instance
.wrapNextcloud<List<CoreNavigationEntry>, CoreNavigationGetAppsNavigationResponse200ApplicationJson>(
_account.id,
'apps-apps',

4
packages/neon/neon/lib/src/blocs/capabilities.dart

@ -17,13 +17,11 @@ abstract class CapabilitiesBlocStates {
@internal
class CapabilitiesBloc extends InteractiveBloc implements CapabilitiesBlocEvents, CapabilitiesBlocStates {
CapabilitiesBloc(
this._requestManager,
this._account,
) {
unawaited(refresh());
}
final RequestManager _requestManager;
final Account _account;
@override
@ -38,7 +36,7 @@ class CapabilitiesBloc extends InteractiveBloc implements CapabilitiesBlocEvents
@override
Future refresh() async {
await _requestManager.wrapNextcloud<CoreOcsGetCapabilitiesResponse200ApplicationJson_Ocs_Data,
await RequestManager.instance.wrapNextcloud<CoreOcsGetCapabilitiesResponse200ApplicationJson_Ocs_Data,
CoreOcsGetCapabilitiesResponse200ApplicationJson>(
_account.id,
'capabilities',

4
packages/neon/neon/lib/src/blocs/user_details.dart

@ -17,13 +17,11 @@ abstract class UserDetailsBlocStates {
@internal
class UserDetailsBloc extends InteractiveBloc implements UserDetailsBlocEvents, UserDetailsBlocStates {
UserDetailsBloc(
this._requestManager,
this._account,
) {
unawaited(refresh());
}
final RequestManager _requestManager;
final Account _account;
@override
@ -38,7 +36,7 @@ class UserDetailsBloc extends InteractiveBloc implements UserDetailsBlocEvents,
@override
Future refresh() async {
await _requestManager
await RequestManager.instance
.wrapNextcloud<ProvisioningApiUserDetails, ProvisioningApiUsersGetCurrentUserResponse200ApplicationJson>(
_account.id,
'user-details',

3
packages/neon/neon/lib/src/models/app_implementation.dart

@ -9,7 +9,6 @@ import 'package:neon/src/models/account.dart';
import 'package:neon/src/platform/platform.dart';
import 'package:neon/src/settings/models/nextcloud_app_options.dart';
import 'package:neon/src/settings/models/storage.dart';
import 'package:neon/src/utils/request_manager.dart';
import 'package:neon/src/widgets/drawer_destination.dart';
import 'package:provider/provider.dart';
import 'package:rxdart/rxdart.dart';
@ -18,7 +17,6 @@ import 'package:shared_preferences/shared_preferences.dart';
abstract class AppImplementation<T extends Bloc, R extends NextcloudAppOptions> {
AppImplementation(
final SharedPreferences sharedPreferences,
this.requestManager,
this.platform,
) {
final storage = AppStorage('app-$id', sharedPreferences);
@ -28,7 +26,6 @@ abstract class AppImplementation<T extends Bloc, R extends NextcloudAppOptions>
String get id;
LocalizationsDelegate get localizationsDelegate;
List<Locale> get supportedLocales;
final RequestManager requestManager;
final NeonPlatform platform;
String nameFromLocalization(final AppLocalizations localizations) => localizations.appImplementationName(id);

1
packages/neon/neon/lib/src/models/notifications_interface.dart

@ -6,7 +6,6 @@ abstract interface class NotificationsAppInterface<T extends NotificationsBlocIn
R extends NotificationsOptionsInterface> extends AppImplementation<T, R> {
NotificationsAppInterface(
super.sharedPreferences,
super.requestManager,
super.platform,
);
}

6
packages/neon/neon/lib/src/utils/push_utils.dart

@ -13,12 +13,10 @@ import 'package:neon/src/blocs/accounts.dart';
import 'package:neon/src/models/account.dart';
import 'package:neon/src/models/app_ids.dart';
import 'package:neon/src/models/push_notification.dart';
import 'package:neon/src/platform/platform.dart';
import 'package:neon/src/settings/models/storage.dart';
import 'package:neon/src/theme/colors.dart';
import 'package:neon/src/utils/global.dart';
import 'package:neon/src/utils/localizations.dart';
import 'package:neon/src/utils/request_manager.dart';
import 'package:nextcloud/nextcloud.dart';
import 'package:shared_preferences/shared_preferences.dart';
@ -96,10 +94,6 @@ class PushUtils {
} else {
final localizations = await appLocalizationsFromSystem();
final platform = await getNeonPlatform();
final cache = Cache(platform);
await cache.init();
var accounts = <Account>[];
Account? account;
NotificationsNotification? notification;

31
packages/neon/neon/lib/src/utils/request_manager.dart

@ -1,6 +1,7 @@
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:meta/meta.dart';
import 'package:neon/src/bloc/result.dart';
import 'package:neon/src/platform/platform.dart';
import 'package:nextcloud/nextcloud.dart';
@ -9,13 +10,24 @@ import 'package:rxdart/rxdart.dart';
import 'package:sqflite/sqflite.dart';
import 'package:xml/xml.dart' as xml;
@immutable
class RequestManager {
const RequestManager([
this.cache,
]);
RequestManager();
final Cache? cache;
@visibleForTesting
factory RequestManager.mocked(final RequestManager requestManager) => _requestManager = requestManager;
static RequestManager? _requestManager;
/// Gets the current instance of [RequestManager].
// ignore: prefer_constructors_over_static_methods
static RequestManager get instance => _requestManager ??= RequestManager();
Future initCache(final NeonPlatform platform) async {
_cache = Cache(platform);
await _cache!.init();
}
Cache? _cache;
Future wrapNextcloud<T, R>(
final String clientID,
@ -100,7 +112,7 @@ class RequestManager {
try {
final response = await (disableTimeout ? call() : timeout(call));
await cache?.set(key, await compute(serialize, response));
await _cache?.set(key, await compute(serialize, response));
subject.add(Result.success(unwrap(response)));
} catch (e, s) {
debugPrint(e.toString());
@ -146,8 +158,8 @@ class RequestManager {
) async {
T? cached;
try {
if (cache != null && await cache!.has(key)) {
cached = unwrap(await compute(deserialize, (await cache!.get(key))!));
if (_cache != null && await _cache!.has(key)) {
cached = unwrap(await compute(deserialize, (await _cache!.get(key))!));
}
} catch (e, s) {
debugPrint(e.toString());
@ -167,12 +179,13 @@ class RequestManager {
return false;
}
static Future<T> timeout<T>(
Future<T> timeout<T>(
final Future<T> Function() call,
) =>
call().timeout(const Duration(seconds: 30));
}
@internal
class Cache {
Cache(this._platform);

2
packages/neon/neon/lib/utils.dart

@ -3,5 +3,5 @@ export 'package:neon/src/utils/confirmation_dialog.dart';
export 'package:neon/src/utils/exceptions.dart';
export 'package:neon/src/utils/hex_color.dart';
export 'package:neon/src/utils/rename_dialog.dart';
export 'package:neon/src/utils/request_manager.dart';
export 'package:neon/src/utils/request_manager.dart' hide Cache;
export 'package:neon/src/utils/validators.dart';

4
packages/neon/neon_files/lib/blocs/browser.dart

@ -14,14 +14,12 @@ abstract class FilesBrowserBlocStates {
class FilesBrowserBloc extends InteractiveBloc implements FilesBrowserBlocEvents, FilesBrowserBlocStates {
FilesBrowserBloc(
this._requestManager,
this.options,
this.account,
) {
unawaited(refresh());
}
final RequestManager _requestManager;
final FilesAppSpecificOptions options;
final Account account;
@ -40,7 +38,7 @@ class FilesBrowserBloc extends InteractiveBloc implements FilesBrowserBlocEvents
@override
Future refresh() async {
await _requestManager.wrapWebDav<List<WebDavFile>>(
await RequestManager.instance.wrapWebDav<List<WebDavFile>>(
account.id,
'files-${path.value.join('/')}',
files,

4
packages/neon/neon_files/lib/blocs/files.dart

@ -28,7 +28,6 @@ class FilesBloc extends InteractiveBloc implements FilesBlocEvents, FilesBlocSta
FilesBloc(
this.options,
this.account,
this._requestManager,
this._platform,
) {
options.uploadQueueParallelism.addListener(_uploadParalelismListener);
@ -37,7 +36,6 @@ class FilesBloc extends InteractiveBloc implements FilesBlocEvents, FilesBlocSta
final FilesAppSpecificOptions options;
final Account account;
final RequestManager _requestManager;
final NeonPlatform _platform;
late final browser = getNewFilesBrowserBloc();
@ -185,7 +183,7 @@ class FilesBloc extends InteractiveBloc implements FilesBlocEvents, FilesBlocSta
tasks.add(tasks.value..remove(task));
}
FilesBrowserBloc getNewFilesBrowserBloc() => FilesBrowserBloc(_requestManager, options, account);
FilesBrowserBloc getNewFilesBrowserBloc() => FilesBrowserBloc(options, account);
void _downloadParalelismListener() {
_downloadQueue.parallel = options.downloadQueueParallelism.value;

3
packages/neon/neon_files/lib/neon_files.dart

@ -44,7 +44,7 @@ part 'widgets/browser_view.dart';
part 'widgets/file_preview.dart';
class FilesApp extends AppImplementation<FilesBloc, FilesAppSpecificOptions> {
FilesApp(super.sharedPreferences, super.requestManager, super.platform);
FilesApp(super.sharedPreferences, super.platform);
@override
String id = AppIDs.files;
@ -62,7 +62,6 @@ class FilesApp extends AppImplementation<FilesBloc, FilesAppSpecificOptions> {
FilesBloc buildBloc(final Account account) => FilesBloc(
options,
account,
requestManager,
platform,
);

5
packages/neon/neon_news/lib/blocs/articles.dart

@ -33,7 +33,6 @@ class NewsMainArticlesBloc extends NewsArticlesBloc {
NewsMainArticlesBloc(
super._newsBloc,
super.options,
super.requestManager,
super.account,
);
}
@ -42,7 +41,6 @@ class NewsArticlesBloc extends InteractiveBloc implements NewsArticlesBlocEvents
NewsArticlesBloc(
this._newsBloc,
this.options,
this.requestManager,
this.account, {
this.id,
this.listType,
@ -59,7 +57,6 @@ class NewsArticlesBloc extends InteractiveBloc implements NewsArticlesBlocEvents
final NewsBloc _newsBloc;
final NewsAppSpecificOptions options;
final RequestManager requestManager;
final Account account;
final int? id;
final ListType? listType;
@ -118,7 +115,7 @@ class NewsArticlesBloc extends InteractiveBloc implements NewsArticlesBlocEvents
}
}
await requestManager.wrapNextcloud<List<NewsArticle>, NewsListArticles>(
await RequestManager.instance.wrapNextcloud<List<NewsArticle>, NewsListArticles>(
account.id,
'news-articles-${type.index}-$id-$getRead',
articles,

7
packages/neon/neon_news/lib/blocs/news.dart

@ -31,7 +31,6 @@ abstract class NewsBlocStates {
class NewsBloc extends InteractiveBloc implements NewsBlocEvents, NewsBlocStates, NewsMainArticlesBloc {
NewsBloc(
this.options,
this.requestManager,
this.account,
) {
mainArticlesBloc.articles.listen((final result) {
@ -50,13 +49,11 @@ class NewsBloc extends InteractiveBloc implements NewsBlocEvents, NewsBlocStates
@override
final NewsAppSpecificOptions options;
@override
final RequestManager requestManager;
@override
final Account account;
late final mainArticlesBloc = NewsMainArticlesBloc(
this,
options,
requestManager,
account,
);
@ -95,14 +92,14 @@ class NewsBloc extends InteractiveBloc implements NewsBlocEvents, NewsBlocStates
@override
Future refresh() async {
await Future.wait([
requestManager.wrapNextcloud<List<NewsFolder>, NewsListFolders>(
RequestManager.instance.wrapNextcloud<List<NewsFolder>, NewsListFolders>(
account.id,
'news-folders',
folders,
() async => account.client.news.listFolders(),
(final response) => response.folders.toList(),
),
requestManager.wrapNextcloud<List<NewsFeed>, NewsListFeeds>(
RequestManager.instance.wrapNextcloud<List<NewsFeed>, NewsListFeeds>(
account.id,
'news-feeds',
feeds,

3
packages/neon/neon_news/lib/neon_news.dart

@ -52,7 +52,7 @@ part 'widgets/folder_view.dart';
part 'widgets/folders_view.dart';
class NewsApp extends AppImplementation<NewsBloc, NewsAppSpecificOptions> {
NewsApp(super.sharedPreferences, super.requestManager, super.platform);
NewsApp(super.sharedPreferences, super.platform);
@override
String id = AppIDs.news;
@ -69,7 +69,6 @@ class NewsApp extends AppImplementation<NewsBloc, NewsAppSpecificOptions> {
@override
NewsBloc buildBloc(final Account account) => NewsBloc(
options,
requestManager,
account,
);

1
packages/neon/neon_news/lib/pages/feed.dart

@ -20,7 +20,6 @@ class NewsFeedPage extends StatelessWidget {
bloc: NewsArticlesBloc(
bloc,
bloc.options,
bloc.requestManager,
bloc.account,
id: feed.id,
listType: ListType.feed,

1
packages/neon/neon_news/lib/widgets/folder_view.dart

@ -47,7 +47,6 @@ class _NewsFolderViewState extends State<NewsFolderView> {
bloc: NewsArticlesBloc(
widget.bloc,
widget.bloc.options,
widget.bloc.requestManager,
widget.bloc.account,
id: widget.folder.id,
listType: ListType.folder,

4
packages/neon/neon_notes/lib/blocs/notes.dart

@ -25,14 +25,12 @@ abstract class NotesBlocStates {
class NotesBloc extends InteractiveBloc implements NotesBlocEvents, NotesBlocStates {
NotesBloc(
this.options,
this.requestManager,
this.account,
) {
unawaited(refresh());
}
final NotesAppSpecificOptions options;
final RequestManager requestManager;
final Account account;
@override
@ -46,7 +44,7 @@ class NotesBloc extends InteractiveBloc implements NotesBlocEvents, NotesBlocSta
@override
Future refresh() async {
await requestManager.wrapNextcloud<List<NotesNote>, BuiltList>(
await RequestManager.instance.wrapNextcloud<List<NotesNote>, BuiltList>(
account.id,
'notes-notes',
notes,

3
packages/neon/neon_notes/lib/neon_notes.dart

@ -42,7 +42,7 @@ part 'widgets/notes_floating_action_button.dart';
part 'widgets/notes_view.dart';
class NotesApp extends AppImplementation<NotesBloc, NotesAppSpecificOptions> {
NotesApp(super.sharedPreferences, super.requestManager, super.platform);
NotesApp(super.sharedPreferences, super.platform);
@override
String id = AppIDs.notes;
@ -59,7 +59,6 @@ class NotesApp extends AppImplementation<NotesBloc, NotesAppSpecificOptions> {
@override
NotesBloc buildBloc(final Account account) => NotesBloc(
options,
requestManager,
account,
);

4
packages/neon/neon_notifications/lib/blocs/notifications.dart

@ -16,7 +16,6 @@ class NotificationsBloc extends InteractiveBloc
implements NotificationsBlocInterface, NotificationsBlocEvents, NotificationsBlocStates {
NotificationsBloc(
this.options,
this._requestManager,
this._account,
) {
notifications.listen((final result) {
@ -31,7 +30,6 @@ class NotificationsBloc extends InteractiveBloc
@override
final NotificationsAppSpecificOptions options;
final RequestManager _requestManager;
final Account _account;
late final NeonTimer _timer;
@ -52,7 +50,7 @@ class NotificationsBloc extends InteractiveBloc
@override
Future refresh() async {
await _requestManager.wrapNextcloud<List<NotificationsNotification>, NotificationsListNotifications>(
await RequestManager.instance.wrapNextcloud<List<NotificationsNotification>, NotificationsListNotifications>(
_account.id,
'notifications-notifications',
notifications,

3
packages/neon/neon_notifications/lib/neon_notifications.dart

@ -22,7 +22,7 @@ part 'pages/main.dart';
class NotificationsApp extends AppImplementation<NotificationsBloc, NotificationsAppSpecificOptions>
implements NotificationsAppInterface<NotificationsBloc, NotificationsAppSpecificOptions> {
NotificationsApp(super.sharedPreferences, super.requestManager, super.platform);
NotificationsApp(super.sharedPreferences, super.platform);
@override
String id = AppIDs.notifications;
@ -39,7 +39,6 @@ class NotificationsApp extends AppImplementation<NotificationsBloc, Notification
@override
NotificationsBloc buildBloc(final Account account) => NotificationsBloc(
options,
requestManager,
account,
);

Loading…
Cancel
Save