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

251 lines
6.9 KiB
Dart
Raw Normal View History

2021-09-16 12:24:17 +02:00
import 'dart:async';
2021-04-10 06:28:12 +02:00
import 'package:flutter/material.dart';
2022-01-15 11:35:15 +01:00
import 'package:kiwi/kiwi.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';
2021-04-16 10:45:17 +02:00
import 'package:nc_photos/changelog.dart' as changelog;
2022-01-15 11:35:15 +01:00
import 'package:nc_photos/di_container.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';
import 'package:nc_photos/platform/k.dart' as platform_k;
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/pref.dart';
2022-01-15 11:35:15 +01:00
import 'package:nc_photos/snack_bar_manager.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/theme.dart';
2021-09-16 12:24:17 +02:00
import 'package:nc_photos/use_case/compat/v29.dart';
import 'package:nc_photos/use_case/compat/v37.dart';
2022-01-15 11:35:15 +01:00
import 'package:nc_photos/use_case/db_compat/v5.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/widget/home.dart';
2021-09-16 12:24:17 +02:00
import 'package:nc_photos/widget/processing_dialog.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/widget/setup.dart';
import 'package:nc_photos/widget/sign_in.dart';
class Splash extends StatefulWidget {
static const routeName = "/splash";
2021-09-15 08:58:06 +02:00
const Splash({
Key? key,
}) : super(key: key);
2021-04-10 06:28:12 +02:00
@override
createState() => _SplashState();
}
class _SplashState extends State<Splash> {
@override
initState() {
super.initState();
2022-01-01 21:59:02 +01:00
WidgetsBinding.instance!.addPostFrameCallback((_) {
_doWork();
2021-04-10 06:28:12 +02:00
});
}
2022-01-01 21:59:02 +01:00
Future<void> _doWork() async {
2022-05-17 13:32:19 +02:00
if (Pref().getFirstRunTime() == null) {
await Pref().setFirstRunTime(DateTime.now().millisecondsSinceEpoch);
}
2022-01-01 21:59:02 +01:00
if (_shouldUpgrade()) {
await _handleUpgrade();
}
2022-01-15 11:35:15 +01:00
await _migrateDb();
2022-01-01 21:59:02 +01:00
_initTimedExit();
}
2021-04-10 06:28:12 +02:00
@override
build(BuildContext context) {
return AppTheme(
child: Scaffold(
body: Builder(builder: (context) => _buildContent(context)),
),
);
}
Widget _buildContent(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.cloud,
size: 96,
2021-07-13 18:05:25 +02:00
color: Theme.of(context).colorScheme.primary,
2021-04-10 06:28:12 +02:00
),
const SizedBox(height: 8),
Text(
L10n.global().appTitle,
2021-04-10 06:28:12 +02:00
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.headline4,
)
],
),
),
);
}
2021-04-16 10:45:17 +02:00
Future<void> _initTimedExit() async {
await Future.delayed(const Duration(seconds: 1));
final account = Pref().getCurrentAccount();
if (isNeedSetup()) {
Navigator.pushReplacementNamed(context, Setup.routeName);
} else if (account == null) {
Navigator.pushReplacementNamed(context, SignIn.routeName);
} else {
Navigator.pushReplacementNamed(context, Home.routeName,
arguments: HomeArguments(account));
if (platform_k.isAndroid) {
final initialRoute = await Activity.consumeInitialRoute();
if (initialRoute != null) {
Navigator.pushNamed(context, initialRoute);
}
2021-04-16 10:45:17 +02:00
}
}
2021-04-16 10:45:17 +02:00
}
bool _shouldUpgrade() {
2021-10-27 22:40:54 +02:00
final lastVersion = Pref().getLastVersionOr(k.version);
2021-04-16 10:45:17 +02:00
return lastVersion < k.version;
}
2022-01-01 21:59:02 +01:00
Future<void> _handleUpgrade() async {
2021-09-16 12:24:17 +02:00
try {
2021-10-27 22:40:54 +02:00
final lastVersion = Pref().getLastVersionOr(k.version);
2021-09-16 12:24:17 +02:00
await _upgrade(lastVersion);
2021-04-16 10:45:17 +02:00
2021-09-16 12:24:17 +02:00
final change = _gatherChangelog(lastVersion);
if (change.isNotEmpty) {
await showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(L10n.global().changelogTitle),
content: SingleChildScrollView(
2022-05-21 13:52:20 +02:00
child: Text(
change,
style: const TextStyle(fontSize: 14),
),
2021-09-16 12:24:17 +02:00
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(MaterialLocalizations.of(context).okButtonLabel),
)
],
2021-04-16 10:45:17 +02:00
),
2021-09-16 12:24:17 +02:00
);
}
2022-01-01 21:59:02 +01:00
} catch (e, stackTrace) {
_log.shout("[_handleUpgrade] Failed while upgrade", e, stackTrace);
2021-09-16 12:24:17 +02:00
} finally {
2022-01-01 21:59:02 +01:00
await Pref().setLastVersion(k.version);
2021-04-16 10:45:17 +02:00
}
}
2021-09-16 12:24:17 +02:00
Future<void> _upgrade(int lastVersion) async {
2022-01-01 21:59:02 +01:00
bool isShowDialog = false;
void showUpdateDialog() {
if (!isShowDialog) {
isShowDialog = true;
showDialog(
context: context,
builder: (_) => ProcessingDialog(
text: L10n.global().genericProcessingDialogContent,
),
);
}
}
2021-09-16 12:24:17 +02:00
if (lastVersion < 290) {
2022-01-01 21:59:02 +01:00
showUpdateDialog();
2021-09-16 12:24:17 +02:00
await _upgrade29(lastVersion);
}
if (lastVersion < 370) {
showUpdateDialog();
await _upgrade37(lastVersion);
}
2022-01-01 21:59:02 +01:00
if (isShowDialog) {
Navigator.of(context).pop();
}
2021-09-16 12:24:17 +02:00
}
Future<void> _upgrade29(int lastVersion) async {
try {
2022-01-01 21:59:02 +01:00
_log.info("[_upgrade29] clearDefaultCache");
await CompatV29.clearDefaultCache();
} catch (e, stackTrace) {
_log.shout("[_upgrade29] Failed while clearDefaultCache", e, stackTrace);
// just leave the cache then
2021-09-16 12:24:17 +02:00
}
}
Future<void> _upgrade37(int lastVersion) async {
final c = KiwiContainer().resolve<DiContainer>();
return CompatV37.setAppDbMigrationFlag(c.appDb);
}
2022-01-15 11:35:15 +01:00
Future<void> _migrateDb() async {
bool isShowDialog = false;
void showUpdateDialog() {
if (!isShowDialog) {
isShowDialog = true;
showDialog(
context: context,
builder: (_) => ProcessingDialog(
text: L10n.global().migrateDatabaseProcessingNotification,
),
);
}
}
final c = KiwiContainer().resolve<DiContainer>();
if (await DbCompatV5.isNeedMigration(c.appDb)) {
showUpdateDialog();
try {
await DbCompatV5.migrate(c.appDb);
} catch (_) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global().migrateDatabaseFailureNotification),
duration: k.snackBarDurationNormal,
));
}
}
if (await CompatV37.isAppDbNeedMigration(c.appDb)) {
showUpdateDialog();
try {
await CompatV37.migrateAppDb(c.appDb);
} catch (_) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global().migrateDatabaseFailureNotification),
duration: k.snackBarDurationNormal,
));
}
}
2022-01-15 11:35:15 +01:00
if (isShowDialog) {
Navigator.of(context).pop();
}
}
2021-04-16 10:45:17 +02:00
String _gatherChangelog(int from) {
2021-06-15 17:48:24 +02:00
if (from < 100) {
from *= 10;
}
final fromMajor = from ~/ 10;
2021-04-16 10:45:17 +02:00
try {
return changelog.contents
2021-06-15 17:48:24 +02:00
.sublist(fromMajor)
2021-04-16 10:45:17 +02:00
.reversed
.whereType<String>()
.map((e) => e.trim())
2021-04-16 10:45:17 +02:00
.join("\n\n");
} catch (e, stacktrace) {
_log.severe("[_gatherChangelog] Failed", e, stacktrace);
return "";
}
}
static final _log = Logger("widget.splash._SplashState");
2021-04-10 06:28:12 +02:00
}