From e1b61c6977ca69feeb0ddee09f4af53498452245 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Mon, 26 Jun 2023 18:29:03 +0200 Subject: [PATCH] neon: cleanup AppsBloc initialApp --- packages/neon/neon/lib/src/blocs/apps.dart | 54 ++++++++++++++-------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/packages/neon/neon/lib/src/blocs/apps.dart b/packages/neon/neon/lib/src/blocs/apps.dart index 5d3b43a7..b5b00059 100644 --- a/packages/neon/neon/lib/src/blocs/apps.dart +++ b/packages/neon/neon/lib/src/blocs/apps.dart @@ -50,27 +50,22 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates }); appImplementations.listen((final result) { - if (result.hasData) { - final options = _accountsBloc.getOptionsFor(_account); - unawaited( - options.initialApp.stream.first.then((var initialApp) async { - if (initialApp == null) { - if (result.requireData.tryFind('files') != null) { - initialApp = 'files'; - } else if (result.requireData.isNotEmpty) { - // This should never happen, because the files app is always installed and can not be removed, but just in - // case this changes at a later point. - initialApp = result.requireData.first.id; - } - } - if (!activeApp.hasValue && initialApp != null) { - await setActiveApp(initialApp); - } - }), - ); - - unawaited(_checkCompatibility()); + if (!result.hasData) { + return; } + + final options = _accountsBloc.getOptionsFor(_account); + unawaited( + options.initialApp.stream.first.then((var initialApp) async { + initialApp ??= _getInitialAppFallback(); + + if (!activeApp.hasValue && initialApp != null) { + await setActiveApp(initialApp); + } + }), + ); + + unawaited(_checkCompatibility()); }); _capabilitiesBloc.capabilities.listen((final result) { @@ -86,6 +81,25 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates unawaited(refresh()); } + /// Determines the appid of initial app. + /// + /// It requires [appImplementations] to have both a value and data. + /// + /// The files app is always installed and can not be removed so it will be used, but in the + /// case this changes at a later point the first supported app will be returned. + /// + /// Returns null when no app is supported by the server. + String? _getInitialAppFallback() { + final supportedApps = appImplementations.value.requireData; + if (supportedApps.tryFind('files') != null) { + return 'files'; + } else if (supportedApps.isNotEmpty) { + return supportedApps.first.id; + } + + return null; + } + Future _checkCompatibility() async { final apps = appImplementations.valueOrNull; final capabilities = _capabilitiesBloc.capabilities.valueOrNull;