Browse Source

refactor(neon,neon_files,neon_notifications): remove AccountsBloc where possible

Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
pull/742/head
Nikolas Rimikis 1 year ago
parent
commit
2dda9e6c14
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 3
      packages/neon/neon/lib/blocs.dart
  2. 3
      packages/neon/neon/lib/src/blocs/accounts.dart
  3. 42
      packages/neon/neon/lib/src/widgets/cached_image.dart
  4. 2
      packages/neon/neon/test/settings_export_test.dart
  5. 13
      packages/neon/neon_files/lib/widgets/file_preview.dart
  6. 1
      packages/neon/neon_notifications/lib/pages/main.dart

3
packages/neon/neon/lib/blocs.dart

@ -1,5 +1,6 @@
export 'package:neon/src/bloc/bloc.dart';
export 'package:neon/src/bloc/result.dart';
export 'package:neon/src/bloc/result_builder.dart';
export 'package:neon/src/blocs/accounts.dart'; // TODO: Remove access to the AccountsBloc. Apps should not need to access this
// TODO: Remove access to the AccountsBloc. Apps should not need to access this
export 'package:neon/src/blocs/accounts.dart' show AccountsBloc;
export 'package:neon/src/blocs/timer.dart' hide TimerBlocEvents, TimerBlocStates;

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

@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:meta/meta.dart';
import 'package:neon/src/bloc/bloc.dart';
import 'package:neon/src/blocs/apps.dart';
import 'package:neon/src/blocs/capabilities.dart';
@ -17,6 +18,7 @@ import 'package:rxdart/rxdart.dart';
const _keyAccounts = 'accounts';
@internal
abstract interface class AccountsBlocEvents {
/// Logs in the given [account].
///
@ -40,6 +42,7 @@ abstract interface class AccountsBlocEvents {
void setActiveAccount(final Account account);
}
@internal
abstract interface class AccountsBlocStates {
/// All registered accounts.
///

42
packages/neon/neon/lib/src/widgets/cached_image.dart

@ -5,15 +5,20 @@ import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:neon/nextcloud.dart';
import 'package:neon/src/blocs/accounts.dart';
import 'package:neon/src/models/account.dart';
import 'package:neon/src/widgets/exception.dart';
import 'package:neon/src/widgets/linear_progress_indicator.dart';
import 'package:provider/provider.dart';
typedef CacheReviver = FutureOr<Uint8List?> Function(CacheManager cacheManager);
typedef ImageDownloader = FutureOr<Uint8List> Function();
typedef CacheWriter = Future<void> Function(CacheManager cacheManager, Uint8List image);
typedef ErrorWidgetBuilder = Widget? Function(BuildContext, dynamic);
typedef ApiImageDownloader = FutureOr<Uint8List> Function(NextcloudClient client);
class NeonCachedImage extends StatefulWidget {
const NeonCachedImage({
required this.image,
@ -186,3 +191,40 @@ class _NeonCachedImageState extends State<NeonCachedImage> {
iconSize: widget.size?.shortestSide,
);
}
class NeonApiImage extends StatelessWidget {
const NeonApiImage({
required this.getImage,
required this.cacheKey,
this.reviver,
this.writeCache,
this.isSvgHint = false,
this.size,
this.fit,
this.svgColor,
this.iconColor,
this.errorBuilder,
super.key,
});
final ApiImageDownloader getImage;
final String cacheKey;
final CacheReviver? reviver;
final CacheWriter? writeCache;
final bool isSvgHint;
final Size? size;
final BoxFit? fit;
final Color? svgColor;
final Color? iconColor;
final ErrorWidgetBuilder? errorBuilder;
@override
Widget build(final BuildContext context) {
final account = Provider.of<AccountsBloc>(context, listen: false).activeAccount.value!;
return NeonCachedImage.custom(
getImage: () async => getImage(account.client),
cacheKey: '${account.id}-$cacheKey',
);
}
}

2
packages/neon/neon/test/settings_export_test.dart

@ -4,7 +4,7 @@ import 'dart:convert';
import 'dart:typed_data';
import 'package:mocktail/mocktail.dart';
import 'package:neon/blocs.dart';
import 'package:neon/src/blocs/accounts.dart';
import 'package:neon/src/models/account.dart';
import 'package:neon/src/models/app_implementation.dart';
import 'package:neon/src/settings/models/exportable.dart';

13
packages/neon/neon_files/lib/widgets/file_preview.dart

@ -41,9 +41,7 @@ class FilePreview extends StatelessWidget {
valueListenable: bloc.options.showPreviewsOption,
builder: (final context, final showPreviews, final child) {
if (showPreviews && (details.hasPreview ?? false)) {
final account = Provider.of<AccountsBloc>(context, listen: false).activeAccount.value!;
final preview = FilePreviewImage(
account: account,
file: details,
size: size,
);
@ -72,19 +70,17 @@ class FilePreview extends StatelessWidget {
}
}
class FilePreviewImage extends NeonCachedImage {
class FilePreviewImage extends NeonApiImage {
factory FilePreviewImage({
required final Account account,
required final FileDetails file,
required final Size size,
}) {
final width = size.width.toInt();
final height = size.height.toInt();
final path = file.path.join('/');
final cacheKey = '${account.id}-preview-$path-$width-$height';
final cacheKey = 'preview-$path-$width-$height';
return FilePreviewImage._(
account: account,
file: file,
size: size,
cacheKey: cacheKey,
@ -95,15 +91,14 @@ class FilePreviewImage extends NeonCachedImage {
}
FilePreviewImage._({
required final Account account,
required final FileDetails file,
required Size super.size,
required super.cacheKey,
required final String path,
required final int width,
required final int height,
}) : super.custom(
getImage: () async => account.client.core.preview.getPreview(
}) : super(
getImage: (final client) async => client.core.preview.getPreview(
file: path,
x: width,
y: height,

1
packages/neon/neon_notifications/lib/pages/main.dart

@ -89,6 +89,7 @@ class _NotificationsMainPageState extends State<NotificationsMainPage> {
return;
}
if (app != null) {
// TODO: use go_router once implemented
final accountsBloc = Provider.of<AccountsBloc>(context, listen: false);
await accountsBloc.activeAppsBloc.setActiveApp(app.id);
} else {

Loading…
Cancel
Save