nc-photos/lib/widget/splash.dart

139 lines
3.7 KiB
Dart
Raw Normal View History

2021-04-10 06:28:12 +02:00
import 'package:flutter/material.dart';
import 'package:flutter/widgets.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;
import 'package:nc_photos/k.dart' as k;
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/pref.dart';
import 'package:nc_photos/theme.dart';
import 'package:nc_photos/widget/home.dart';
import 'package:nc_photos/widget/setup.dart';
import 'package:nc_photos/widget/sign_in.dart';
class Splash extends StatefulWidget {
static const routeName = "/splash";
2021-07-23 22:05:57 +02:00
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();
2021-07-23 22:05:57 +02:00
WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
2021-04-16 10:45:17 +02:00
if (_shouldUpgrade()) {
_handleUpgrade();
} else {
_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(
2021-07-25 07:00:38 +02:00
L10n.of(context).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
void _initTimedExit() {
Future.delayed(const Duration(seconds: 1)).then((_) {
final account = Pref.inst().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));
}
});
}
bool _shouldUpgrade() {
2021-07-23 22:05:57 +02:00
final lastVersion = Pref.inst().getLastVersionOr(k.version);
2021-04-16 10:45:17 +02:00
return lastVersion < k.version;
}
void _handleUpgrade() {
2021-07-23 22:05:57 +02:00
final lastVersion = Pref.inst().getLastVersionOr(k.version);
2021-04-16 10:45:17 +02:00
// ...
final change = _gatherChangelog(lastVersion);
if (change.isNotEmpty) {
showDialog(
context: context,
builder: (context) => AlertDialog(
2021-07-25 07:00:38 +02:00
title: Text(L10n.of(context).changelogTitle),
2021-04-16 10:45:17 +02:00
content: SingleChildScrollView(
child: Text(change),
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(MaterialLocalizations.of(context).okButtonLabel),
)
],
),
).whenComplete(() {
_initTimedExit();
Pref.inst().setLastVersion(k.version);
});
} else {
_initTimedExit();
Pref.inst().setLastVersion(k.version);
}
}
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
.where((element) => element != null)
.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
}