nc-photos/app/lib/widget/splash.dart

164 lines
5.4 KiB
Dart
Raw Normal View History

2021-09-16 12:24:17 +02:00
import 'dart:async';
2023-03-13 12:55:39 +01:00
import 'package:clock/clock.dart';
2024-08-17 20:01:59 +02:00
import 'package:copy_with/copy_with.dart';
2021-04-10 06:28:12 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2021-04-16 10:45:17 +02:00
import 'package:logging/logging.dart';
2021-07-25 07:00:38 +02:00
import 'package:nc_photos/app_localizations.dart';
2024-08-17 20:01:59 +02:00
import 'package:nc_photos/bloc_util.dart';
import 'package:nc_photos/controller/pref_controller.dart';
import 'package:nc_photos/db/entity_converter.dart';
2023-07-17 09:35:45 +02:00
import 'package:nc_photos/entity/pref.dart';
2021-04-16 10:45:17 +02:00
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/mobile/android/activity.dart';
2023-09-01 20:15:41 +02:00
import 'package:nc_photos/mobile/android/permission_util.dart';
2024-05-25 09:51:42 +02:00
import 'package:nc_photos/protected_page_handler.dart';
2021-09-16 12:24:17 +02:00
import 'package:nc_photos/use_case/compat/v29.dart';
2022-07-17 21:32:04 +02:00
import 'package:nc_photos/use_case/compat/v46.dart';
import 'package:nc_photos/use_case/compat/v55.dart';
2024-08-10 13:13:56 +02:00
import 'package:nc_photos/widget/app_intermediate_circular_progress_indicator.dart';
2022-07-17 21:35:35 +02:00
import 'package:nc_photos/widget/changelog.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/widget/home.dart';
import 'package:nc_photos/widget/setup.dart';
import 'package:nc_photos/widget/sign_in.dart';
2022-12-16 16:01:04 +01:00
import 'package:np_codegen/np_codegen.dart';
import 'package:np_db/np_db.dart';
2023-09-01 20:15:41 +02:00
import 'package:np_platform_permission/np_platform_permission.dart';
2023-08-27 12:58:05 +02:00
import 'package:np_platform_util/np_platform_util.dart';
2022-12-08 16:39:13 +01:00
import 'package:to_string/to_string.dart';
part 'splash.g.dart';
2024-08-17 20:01:59 +02:00
part 'splash/bloc.dart';
part 'splash/state_event.dart';
part 'splash/view.dart';
2021-04-10 06:28:12 +02:00
2024-08-17 20:01:59 +02:00
class Splash extends StatelessWidget {
2021-04-10 06:28:12 +02:00
static const routeName = "/splash";
2024-05-28 17:10:33 +02:00
const Splash({super.key});
2021-04-10 06:28:12 +02:00
@override
2024-08-17 20:01:59 +02:00
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => _Bloc(
prefController: context.read(),
npDb: context.read(),
)..add(const _Init()),
child: const _WrappedSplash(),
);
}
2021-04-10 06:28:12 +02:00
}
2022-12-16 16:01:04 +01:00
@npLog
2024-08-17 20:01:59 +02:00
class _WrappedSplash extends StatelessWidget {
const _WrappedSplash();
2022-01-01 21:59:02 +01:00
2021-04-10 06:28:12 +02:00
@override
2024-08-17 20:01:59 +02:00
Widget build(BuildContext context) {
return PopScope(
canPop: false,
child: Scaffold(
body: MultiBlocListener(
listeners: [
_BlocListenerT<int?>(
selector: (state) => state.changelogFromVersion,
listener: (context, changelogFromVersion) {
if (changelogFromVersion != null) {
Navigator.of(context)
.pushNamed(Changelog.routeName,
arguments: ChangelogArguments(changelogFromVersion))
.whenComplete(() {
if (context.mounted) {
context.addEvent(const _ChangelogDismissed());
}
});
}
},
),
_BlocListenerT<bool>(
selector: (state) => state.isDone,
listener: (context, isDone) {
if (isDone) {
_exit(context);
}
},
),
],
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Stack(
fit: StackFit.expand,
2022-07-17 21:35:35 +02:00
children: [
2024-08-17 20:01:59 +02:00
Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.cloud,
size: 96,
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(height: 8),
Text(
L10n.global().appTitle,
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.headlineMedium,
),
],
2022-07-17 21:35:35 +02:00
),
2024-08-17 20:01:59 +02:00
const Positioned(
left: 0,
right: 0,
bottom: 64,
2024-08-17 20:01:59 +02:00
child: _UpgradeProgressView(),
),
],
),
2024-08-17 20:01:59 +02:00
),
),
2021-04-10 06:28:12 +02:00
),
);
}
2021-04-16 10:45:17 +02:00
2024-08-17 20:01:59 +02:00
void _exit(BuildContext context) {
2022-07-17 21:35:35 +02:00
_log.info("[_exit]");
2024-08-17 20:01:59 +02:00
final account = context.read<PrefController>().currentAccountValue;
if (isNeedSetup()) {
2024-08-17 20:01:59 +02:00
Navigator.of(context).pushReplacementNamed(Setup.routeName);
} else if (account == null) {
2024-08-17 20:01:59 +02:00
Navigator.of(context).pushReplacementNamed(SignIn.routeName);
} else {
2024-05-25 09:51:42 +02:00
Navigator.of(context)
.pushReplacementNamedProtected(Home.routeName,
2024-05-25 09:51:42 +02:00
arguments: HomeArguments(account))
.then((value) async {
if (getRawPlatform() == NpPlatform.android) {
final initialRoute = await Activity.consumeInitialRoute();
if (initialRoute != null) {
2024-08-17 20:01:59 +02:00
unawaited(Navigator.of(context).pushNamed(initialRoute));
2024-05-25 09:51:42 +02:00
}
}
2024-05-25 09:51:42 +02:00
}).onError<ProtectedPageAuthException>((_, __) async {
_log.warning("[_exit] Auth failed");
await Future.delayed(const Duration(seconds: 2));
2024-08-17 20:01:59 +02:00
if (context.mounted) {
_exit(context);
}
2024-05-25 09:51:42 +02:00
return null;
});
}
2021-04-16 10:45:17 +02:00
}
2021-04-10 06:28:12 +02:00
}
2024-08-17 20:01:59 +02:00
// typedef _BlocBuilder = BlocBuilder<_Bloc, _State>;
// typedef _BlocListener = BlocListener<_Bloc, _State>;
typedef _BlocListenerT<T> = BlocListenerT<_Bloc, _State, T>;
typedef _BlocSelector<T> = BlocSelector<_Bloc, _State, T>;
2024-08-17 20:01:59 +02:00
extension on BuildContext {
_Bloc get bloc => read<_Bloc>();
// _State get state => bloc.state;
void addEvent(_Event event) => bloc.add(event);
}