concurrency work in prep for speeding things up

refactor splash screen to process initialization in a better way
more async tools for async cubit constructors
greatly improved StateMapFollower class
This commit is contained in:
Christien Rioux 2024-04-03 21:55:49 -04:00
parent 8da1dc7d32
commit 9bb20f4dd2
47 changed files with 886 additions and 579 deletions

View file

@ -9,7 +9,6 @@ import 'package:go_router/go_router.dart';
import 'package:stream_transform/stream_transform.dart';
import '../../../account_manager/account_manager.dart';
import '../../init.dart';
import '../../layout/layout.dart';
import '../../settings/settings.dart';
import '../../tools/tools.dart';
@ -24,19 +23,10 @@ final _homeNavKey = GlobalKey<NavigatorState>(debugLabel: 'homeNavKey');
class RouterCubit extends Cubit<RouterState> {
RouterCubit(AccountRepository accountRepository)
: super(const RouterState(
isInitialized: false,
hasAnyAccount: false,
: super(RouterState(
hasAnyAccount: accountRepository.getLocalAccounts().isNotEmpty,
hasActiveChat: false,
)) {
// Watch for changes that the router will care about
Future.delayed(Duration.zero, () async {
await eventualInitialized.future;
emit(state.copyWith(
isInitialized: true,
hasAnyAccount: accountRepository.getLocalAccounts().isNotEmpty));
});
// Subscribe to repository streams
_accountRepositorySubscription = accountRepository.stream.listen((event) {
switch (event) {
@ -63,10 +53,6 @@ class RouterCubit extends Cubit<RouterState> {
/// Our application routes
List<RouteBase> get routes => [
GoRoute(
path: '/',
builder: (context, state) => const IndexPage(),
),
ShellRoute(
navigatorKey: _homeNavKey,
builder: (context, state, child) => HomeShell(
@ -75,11 +61,11 @@ class RouterCubit extends Cubit<RouterState> {
HomeAccountReadyShell(context: context, child: child))),
routes: [
GoRoute(
path: '/home',
path: '/',
builder: (context, state) => const HomeAccountReadyMain(),
),
GoRoute(
path: '/home/chat',
path: '/chat',
builder: (context, state) => const HomeAccountReadyChat(),
),
],
@ -103,17 +89,9 @@ class RouterCubit extends Cubit<RouterState> {
// No matter where we are, if there's not
switch (goRouterState.matchedLocation) {
case '/':
// Wait for initialization to complete
if (!eventualInitialized.isCompleted) {
return null;
}
return state.hasAnyAccount ? '/home' : '/new_account';
case '/new_account':
return state.hasAnyAccount ? '/home' : null;
case '/home':
return state.hasAnyAccount ? '/' : null;
case '/':
if (!state.hasAnyAccount) {
return '/new_account';
}
@ -123,11 +101,11 @@ class RouterCubit extends Cubit<RouterState> {
tabletLandscape: false,
desktop: false)) {
if (state.hasActiveChat) {
return '/home/chat';
return '/chat';
}
}
return null;
case '/home/chat':
case '/chat':
if (!state.hasAnyAccount) {
return '/new_account';
}
@ -137,10 +115,10 @@ class RouterCubit extends Cubit<RouterState> {
tabletLandscape: false,
desktop: false)) {
if (!state.hasActiveChat) {
return '/home';
return '/';
}
} else {
return '/home';
return '/';
}
return null;
case '/settings':