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

164 lines
5.4 KiB
Dart
Raw Normal View History

2021-09-16 18:24:17 +08:00
import 'dart:async';
2023-03-13 19:55:39 +08:00
import 'package:clock/clock.dart';
2024-08-18 02:01:59 +08:00
import 'package:copy_with/copy_with.dart';
2021-04-10 12:28:12 +08:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2021-04-16 16:45:17 +08:00
import 'package:logging/logging.dart';
2021-07-25 13:00:38 +08:00
import 'package:nc_photos/app_localizations.dart';
2024-08-18 02:01:59 +08: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 15:35:45 +08:00
import 'package:nc_photos/entity/pref.dart';
2021-04-16 16:45:17 +08:00
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/mobile/android/activity.dart';
2023-09-02 02:15:41 +08:00
import 'package:nc_photos/mobile/android/permission_util.dart';
2024-05-25 15:51:42 +08:00
import 'package:nc_photos/protected_page_handler.dart';
2021-09-16 18:24:17 +08:00
import 'package:nc_photos/use_case/compat/v29.dart';
2022-07-18 03:32:04 +08:00
import 'package:nc_photos/use_case/compat/v46.dart';
import 'package:nc_photos/use_case/compat/v55.dart';
2024-08-10 19:13:56 +08:00
import 'package:nc_photos/widget/app_intermediate_circular_progress_indicator.dart';
2022-07-18 03:35:35 +08:00
import 'package:nc_photos/widget/changelog.dart';
2021-04-10 12:28:12 +08: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 23:01:04 +08:00
import 'package:np_codegen/np_codegen.dart';
import 'package:np_db/np_db.dart';
2023-09-02 02:15:41 +08:00
import 'package:np_platform_permission/np_platform_permission.dart';
2023-08-27 18:58:05 +08:00
import 'package:np_platform_util/np_platform_util.dart';
2022-12-08 23:39:13 +08:00
import 'package:to_string/to_string.dart';
part 'splash.g.dart';
2024-08-18 02:01:59 +08:00
part 'splash/bloc.dart';
part 'splash/state_event.dart';
part 'splash/view.dart';
2021-04-10 12:28:12 +08:00
2024-08-18 02:01:59 +08:00
class Splash extends StatelessWidget {
2021-04-10 12:28:12 +08:00
static const routeName = "/splash";
2024-05-28 23:10:33 +08:00
const Splash({super.key});
2021-04-10 12:28:12 +08:00
@override
2024-08-18 02:01:59 +08: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 12:28:12 +08:00
}
2022-12-16 23:01:04 +08:00
@npLog
2024-08-18 02:01:59 +08:00
class _WrappedSplash extends StatelessWidget {
const _WrappedSplash();
2022-01-02 04:59:02 +08:00
2021-04-10 12:28:12 +08:00
@override
2024-08-18 02:01:59 +08: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-18 03:35:35 +08:00
children: [
2024-08-18 02:01:59 +08: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-18 03:35:35 +08:00
),
2024-08-18 02:01:59 +08:00
const Positioned(
left: 0,
right: 0,
bottom: 64,
2024-08-18 02:01:59 +08:00
child: _UpgradeProgressView(),
),
],
),
2024-08-18 02:01:59 +08:00
),
),
2021-04-10 12:28:12 +08:00
),
);
}
2021-04-16 16:45:17 +08:00
2024-08-18 02:01:59 +08:00
void _exit(BuildContext context) {
2022-07-18 03:35:35 +08:00
_log.info("[_exit]");
2024-08-18 02:01:59 +08:00
final account = context.read<PrefController>().currentAccountValue;
if (isNeedSetup()) {
2024-08-18 02:01:59 +08:00
Navigator.of(context).pushReplacementNamed(Setup.routeName);
} else if (account == null) {
2024-08-18 02:01:59 +08:00
Navigator.of(context).pushReplacementNamed(SignIn.routeName);
} else {
2024-05-25 15:51:42 +08:00
Navigator.of(context)
.pushReplacementNamedProtected(Home.routeName,
2024-05-25 15:51:42 +08:00
arguments: HomeArguments(account))
.then((value) async {
if (getRawPlatform() == NpPlatform.android) {
final initialRoute = await Activity.consumeInitialRoute();
if (initialRoute != null) {
2024-08-18 02:01:59 +08:00
unawaited(Navigator.of(context).pushNamed(initialRoute));
2024-05-25 15:51:42 +08:00
}
}
2024-05-25 15:51:42 +08:00
}).onError<ProtectedPageAuthException>((_, __) async {
_log.warning("[_exit] Auth failed");
await Future.delayed(const Duration(seconds: 2));
2024-08-18 02:01:59 +08:00
if (context.mounted) {
_exit(context);
}
2024-05-25 15:51:42 +08:00
return null;
});
}
2021-04-16 16:45:17 +08:00
}
2021-04-10 12:28:12 +08:00
}
2024-08-18 02:01:59 +08: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-18 02:01:59 +08:00
extension on BuildContext {
_Bloc get bloc => read<_Bloc>();
// _State get state => bloc.state;
void addEvent(_Event event) => bloc.add(event);
}