Skip to content

Commit

Permalink
Merge pull request #2020 from nextcloud/refactor/neon_framework/repla…
Browse files Browse the repository at this point in the history
…ce-blocs-access-behaviorsubjects
  • Loading branch information
provokateurin authored May 10, 2024
2 parents cd5c316 + d4be81f commit 36207a9
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 40 deletions.
4 changes: 2 additions & 2 deletions packages/neon_framework/lib/neon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ Future<void> runNeon({
..setActiveAccount(account);
}
PushNotificationsBloc(
accountsBloc: accountsBloc,
accountsSubject: accountsBloc.accounts,
globalOptions: globalOptions,
);
final firstLaunchBloc = FirstLaunchBloc(
disabled: firstLaunchDisabled,
);
final nextPushBloc = NextPushBloc(
accountsBloc: accountsBloc,
accountsSubject: accountsBloc.accounts,
globalOptions: globalOptions,
disabled: nextPushDisabled,
);
Expand Down
6 changes: 3 additions & 3 deletions packages/neon_framework/lib/src/blocs/accounts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,9 @@ class _AccountsBloc extends Bloc implements AccountsBloc {

@override
AppsBloc getAppsBlocFor(Account account) => appsBlocs[account] ??= AppsBloc(
capabilitiesBloc: getCapabilitiesBlocFor(account),
accountsBloc: this,
capabilitiesSubject: getCapabilitiesBlocFor(account).capabilities,
account: account,
accountOptions: getOptionsFor(account),
allAppImplementations: allAppImplementations,
);

Expand Down Expand Up @@ -372,7 +372,7 @@ class _AccountsBloc extends Bloc implements AccountsBloc {

@override
UnifiedSearchBloc getUnifiedSearchBlocFor(Account account) => unifiedSearchBlocs[account] ??= UnifiedSearchBloc(
appsBloc: getAppsBlocFor(account),
activeAppSubject: getAppsBlocFor(account).activeApp,
account: account,
);

Expand Down
22 changes: 10 additions & 12 deletions packages/neon_framework/lib/src/blocs/apps.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:neon_framework/src/bloc/bloc.dart';
import 'package:neon_framework/src/bloc/result.dart';
import 'package:neon_framework/src/blocs/accounts.dart';
import 'package:neon_framework/src/blocs/capabilities.dart';
import 'package:neon_framework/src/models/account.dart';
import 'package:neon_framework/src/models/app_implementation.dart';
import 'package:neon_framework/src/models/notifications_interface.dart';
import 'package:neon_framework/src/utils/account_options.dart';
import 'package:neon_framework/src/utils/findable.dart';
import 'package:neon_framework/src/utils/request_manager.dart';
import 'package:neon_framework/src/utils/server_version.dart';
Expand All @@ -25,9 +24,9 @@ abstract class AppsBloc implements InteractiveBloc {
/// Create a new apps bloc.
@internal
factory AppsBloc({
required CapabilitiesBloc capabilitiesBloc,
required AccountsBloc accountsBloc,
required BehaviorSubject<Result<core.OcsGetCapabilitiesResponseApplicationJson_Ocs_Data>> capabilitiesSubject,
required Account account,
required AccountOptions accountOptions,
required BuiltSet<AppImplementation> allAppImplementations,
}) = _AppsBloc;

Expand Down Expand Up @@ -67,9 +66,9 @@ abstract class AppsBloc implements InteractiveBloc {
class _AppsBloc extends InteractiveBloc implements AppsBloc {
/// Creates a new apps bloc.
_AppsBloc({
required this.capabilitiesBloc,
required this.accountsBloc,
required this.capabilitiesSubject,
required this.account,
required this.accountOptions,
required this.allAppImplementations,
}) {
apps.listen((result) {
Expand All @@ -80,7 +79,7 @@ class _AppsBloc extends InteractiveBloc implements AppsBloc {
}
});

capabilitiesBloc.capabilities.listen((result) {
capabilitiesSubject.listen((result) {
notificationsAppImplementation.add(
result.transform(
(data) => data.capabilities.notificationsCapabilities?.notifications != null
Expand Down Expand Up @@ -145,8 +144,7 @@ class _AppsBloc extends InteractiveBloc implements AppsBloc {
return null;
}

final options = accountsBloc.getOptionsFor(account);
for (final fallback in {options.initialApp.value, AppIDs.dashboard, AppIDs.files}) {
for (final fallback in {accountOptions.initialApp.value, AppIDs.dashboard, AppIDs.files}) {
if (supportedApps.tryFind(fallback) != null) {
return fallback;
}
Expand All @@ -161,7 +159,7 @@ class _AppsBloc extends InteractiveBloc implements AppsBloc {

Future<void> checkCompatibility() async {
final apps = appImplementations.valueOrNull;
final capabilities = capabilitiesBloc.capabilities.valueOrNull;
final capabilities = capabilitiesSubject.valueOrNull;

// ignore cached data
if (capabilities == null || apps == null || !capabilities.hasSuccessfulData || !apps.hasSuccessfulData) {
Expand Down Expand Up @@ -216,9 +214,9 @@ class _AppsBloc extends InteractiveBloc implements AppsBloc {
),
);

final CapabilitiesBloc capabilitiesBloc;
final AccountsBloc accountsBloc;
final BehaviorSubject<Result<core.OcsGetCapabilitiesResponseApplicationJson_Ocs_Data>> capabilitiesSubject;
final Account account;
final AccountOptions accountOptions;
final BuiltSet<AppImplementation> allAppImplementations;
final apps = BehaviorSubject<Result<BuiltList<core.NavigationEntry>>>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'dart:async';

import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import 'package:neon_framework/blocs.dart';
import 'package:neon_framework/models.dart';
import 'package:neon_framework/src/bloc/bloc.dart';
import 'package:nextcloud/core.dart';

/// A Bloc checking if the server is in maintenance mode.
Expand Down
12 changes: 6 additions & 6 deletions packages/neon_framework/lib/src/blocs/next_push.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'dart:async';

import 'package:built_collection/built_collection.dart';
import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:neon_framework/src/bloc/bloc.dart';
import 'package:neon_framework/src/blocs/accounts.dart';
import 'package:neon_framework/src/models/account.dart';
import 'package:neon_framework/src/models/disposable.dart';
import 'package:neon_framework/src/utils/global_options.dart';
Expand All @@ -15,7 +15,7 @@ import 'package:rxdart/rxdart.dart';
sealed class NextPushBloc implements Disposable {
@internal
factory NextPushBloc({
required AccountsBloc accountsBloc,
required BehaviorSubject<BuiltList<Account>> accountsSubject,
required GlobalOptions globalOptions,
bool disabled,
}) = _NextPushBloc;
Expand All @@ -26,7 +26,7 @@ sealed class NextPushBloc implements Disposable {

class _NextPushBloc extends Bloc implements NextPushBloc {
_NextPushBloc({
required this.accountsBloc,
required this.accountsSubject,
required this.globalOptions,
bool disabled = false,
}) {
Expand All @@ -36,7 +36,7 @@ class _NextPushBloc extends Bloc implements NextPushBloc {
Rx.merge([
globalOptions.pushNotificationsEnabled.stream,
globalOptions.pushNotificationsDistributor.stream,
accountsBloc.accounts,
accountsSubject,
]).debounceTime(const Duration(milliseconds: 100)).listen((_) async {
if (!globalOptions.pushNotificationsEnabled.enabled || !globalOptions.pushNotificationsEnabled.value) {
return;
Expand All @@ -50,7 +50,7 @@ class _NextPushBloc extends Bloc implements NextPushBloc {
}

var isSupported = false;
for (final account in accountsBloc.accounts.value) {
for (final account in accountsSubject.value) {
if (!supported.containsKey(account)) {
try {
final response = await account.client.uppush.check();
Expand Down Expand Up @@ -85,7 +85,7 @@ class _NextPushBloc extends Bloc implements NextPushBloc {
@override
final log = Logger('NextPushBloc');

final AccountsBloc accountsBloc;
final BehaviorSubject<BuiltList<Account>> accountsSubject;
final GlobalOptions globalOptions;
final supported = <Account, bool>{};

Expand Down
14 changes: 7 additions & 7 deletions packages/neon_framework/lib/src/blocs/push_notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:built_collection/built_collection.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:neon_framework/src/bloc/bloc.dart';
import 'package:neon_framework/src/blocs/accounts.dart';
import 'package:neon_framework/src/models/account.dart';
import 'package:neon_framework/src/platform/platform.dart';
import 'package:neon_framework/src/storage/keys.dart';
Expand All @@ -14,20 +13,21 @@ import 'package:neon_framework/src/utils/global_options.dart';
import 'package:neon_framework/src/utils/push_utils.dart';
import 'package:neon_framework/storage.dart';
import 'package:nextcloud/notifications.dart' as notifications;
import 'package:rxdart/rxdart.dart';
import 'package:unifiedpush/unifiedpush.dart';

/// Bloc for managing push notifications and registration.
sealed class PushNotificationsBloc {
@internal
factory PushNotificationsBloc({
required AccountsBloc accountsBloc,
required BehaviorSubject<BuiltList<Account>> accountsSubject,
required GlobalOptions globalOptions,
}) = _PushNotificationsBloc;
}

class _PushNotificationsBloc extends Bloc implements PushNotificationsBloc {
_PushNotificationsBloc({
required this.accountsBloc,
required this.accountsSubject,
required this.globalOptions,
}) {
if (NeonPlatform.instance.canUsePushNotifications) {
Expand All @@ -42,7 +42,7 @@ class _PushNotificationsBloc extends Bloc implements PushNotificationsBloc {
@override
final log = Logger('PushNotificationsBloc');

final AccountsBloc accountsBloc;
final BehaviorSubject<BuiltList<Account>> accountsSubject;
late final storage = NeonStorage().settingsStore(StorageKeys.lastEndpoint);
final GlobalOptions globalOptions;

Expand All @@ -59,7 +59,7 @@ class _PushNotificationsBloc extends Bloc implements PushNotificationsBloc {
await setupUnifiedPush();

globalOptions.pushNotificationsDistributor.addListener(distributorListener);
accountsListener = accountsBloc.accounts.listen(registerUnifiedPushInstances);
accountsListener = accountsSubject.listen(registerUnifiedPushInstances);
} else {
globalOptions.pushNotificationsDistributor.removeListener(distributorListener);
unawaited(accountsListener?.cancel());
Expand All @@ -72,7 +72,7 @@ class _PushNotificationsBloc extends Bloc implements PushNotificationsBloc {

await UnifiedPush.initialize(
onNewEndpoint: (endpoint, instance) async {
final account = accountsBloc.accounts.value.tryFind(instance);
final account = accountsSubject.value.tryFind(instance);
if (account == null) {
log.fine('Account for $instance not found, can not process endpoint');
return;
Expand Down Expand Up @@ -105,7 +105,7 @@ class _PushNotificationsBloc extends Bloc implements PushNotificationsBloc {
final distributor = globalOptions.pushNotificationsDistributor.value;
final disabled = distributor == null;
final sameDistributor = distributor == await UnifiedPush.getDistributor();
final accounts = accountsBloc.accounts.value;
final accounts = accountsSubject.value;
if (disabled || !sameDistributor) {
await unregisterUnifiedPushInstances(accounts);
}
Expand Down
13 changes: 6 additions & 7 deletions packages/neon_framework/lib/src/blocs/unified_search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:meta/meta.dart';
import 'package:neon_framework/models.dart';
import 'package:neon_framework/src/bloc/bloc.dart';
import 'package:neon_framework/src/bloc/result.dart';
import 'package:neon_framework/src/blocs/apps.dart';
import 'package:neon_framework/src/utils/request_manager.dart';
import 'package:nextcloud/core.dart' as core;
import 'package:rxdart/rxdart.dart';
Expand All @@ -17,7 +16,7 @@ import 'package:rxdart/rxdart.dart';
sealed class UnifiedSearchBloc implements InteractiveBloc {
@internal
factory UnifiedSearchBloc({
required AppsBloc appsBloc,
required BehaviorSubject<AppImplementation> activeAppSubject,
required Account account,
}) = _UnifiedSearchBloc;

Expand All @@ -39,10 +38,10 @@ sealed class UnifiedSearchBloc implements InteractiveBloc {

class _UnifiedSearchBloc extends InteractiveBloc implements UnifiedSearchBloc {
_UnifiedSearchBloc({
required this.appsBloc,
required this.activeAppSubject,
required this.account,
}) {
appsBloc.activeApp.listen((_) {
activeAppSubject.listen((_) {
term = '';
extendedSearchEnabled = false;
results.add(BuiltMap());
Expand All @@ -52,7 +51,7 @@ class _UnifiedSearchBloc extends InteractiveBloc implements UnifiedSearchBloc {
@override
final log = Logger('UnifiedSearchBloc');

final AppsBloc appsBloc;
final BehaviorSubject<AppImplementation> activeAppSubject;
final Account account;
String term = '';
bool extendedSearchEnabled = false;
Expand Down Expand Up @@ -95,7 +94,7 @@ class _UnifiedSearchBloc extends InteractiveBloc implements UnifiedSearchBloc {
}

if (providers.value.hasData) {
final activeApp = appsBloc.activeApp.value;
final activeApp = activeAppSubject.value;

var providerIDs = providers.value.requireData!.map((provider) => provider.id);
if (!extendedSearchEnabled) {
Expand Down Expand Up @@ -175,7 +174,7 @@ class _UnifiedSearchBloc extends InteractiveBloc implements UnifiedSearchBloc {
Iterable<MapEntry<String, Result<core.UnifiedSearchResult>>> sortResults(
BuiltMap<String, Result<core.UnifiedSearchResult>> results,
) sync* {
final activeApp = appsBloc.activeApp.value;
final activeApp = activeAppSubject.value;

// Unlike non-matching providers (below) we don't filter the empty results,
// as the active app is more relevant and we want to know if there are no results for the active app.
Expand Down
4 changes: 3 additions & 1 deletion packages/neon_framework/lib/src/blocs/weather_status.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import 'dart:async';
import 'package:built_collection/built_collection.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:neon_framework/blocs.dart';
import 'package:neon_framework/models.dart';
import 'package:neon_framework/src/bloc/bloc.dart';
import 'package:neon_framework/src/bloc/result.dart';
import 'package:neon_framework/src/blocs/timer.dart';
import 'package:neon_framework/utils.dart';
import 'package:nextcloud/core.dart' as core;
import 'package:nextcloud/weather_status.dart' as weather_status;
Expand Down
2 changes: 1 addition & 1 deletion packages/neon_framework/test/unified_search_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void main() {

account = mockUnifiedSearchAccount();
bloc = UnifiedSearchBloc(
appsBloc: appsBloc,
activeAppSubject: appsBloc.activeApp,
account: account,
);
});
Expand Down

0 comments on commit 36207a9

Please sign in to comment.