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

302 lines
8.8 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';
2021-04-10 06:28:12 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2022-07-17 21:32:04 +02: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';
2022-07-17 21:32:04 +02:00
import 'package:nc_photos/di_container.dart';
2023-07-17 09:35:45 +02:00
import 'package:nc_photos/entity/pref.dart';
2023-02-20 15:21:35 +01:00
import 'package:nc_photos/entity/sqlite/database.dart' as sql;
2021-04-16 10:45:17 +02:00
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/mobile/android/activity.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';
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';
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';
2021-04-10 06:28:12 +02:00
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();
}
2022-12-16 16:01:04 +01:00
@npLog
2021-04-10 06:28:12 +02:00
class _SplashState extends State<Splash> {
@override
initState() {
super.initState();
2022-06-20 13:49:58 +02:00
WidgetsBinding.instance.addPostFrameCallback((_) {
2022-01-01 21:59:02 +01:00
_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) {
2023-03-13 12:55:39 +01:00
await Pref().setFirstRunTime(clock.now().millisecondsSinceEpoch);
2022-05-17 13:32:19 +02:00
}
2022-01-01 21:59:02 +01:00
if (_shouldUpgrade()) {
2022-07-17 21:35:35 +02:00
setState(() {
_isUpgrading = true;
});
2022-01-01 21:59:02 +01:00
await _handleUpgrade();
2022-07-17 21:35:35 +02:00
setState(() {
_isUpgrading = false;
});
2022-01-01 21:59:02 +01:00
}
2022-07-28 18:59:26 +02:00
unawaited(_exit());
2022-01-01 21:59:02 +01:00
}
2021-04-10 06:28:12 +02:00
@override
build(BuildContext context) {
2022-11-12 10:55:33 +01:00
return Scaffold(
body: WillPopScope(
onWillPop: () => Future.value(false),
child: Builder(builder: (context) => _buildContent(context)),
2021-04-10 06:28:12 +02:00
),
);
}
Widget _buildContent(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Stack(
fit: StackFit.expand,
children: [
Center(
child: Column(
2022-07-17 21:35:35 +02:00
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,
2023-05-26 13:45:46 +02:00
style: Theme.of(context).textTheme.headlineMedium,
2022-07-17 21:35:35 +02:00
),
],
2021-04-10 06:28:12 +02:00
),
),
if (_isUpgrading)
BlocBuilder<_UpgradeCubit, _UpgradeState>(
bloc: _upgradeCubit,
builder: (context, state) {
return Positioned(
left: 0,
right: 0,
bottom: 64,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(state.text),
const SizedBox(height: 8),
if (state.count == null)
const SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator(),
)
else
LinearProgressIndicator(
value: state.current / state.count!,
),
],
),
);
},
),
],
2021-04-10 06:28:12 +02:00
),
);
}
2021-04-16 10:45:17 +02:00
2022-07-17 21:35:35 +02:00
Future<void> _exit() async {
_log.info("[_exit]");
final account = Pref().getCurrentAccount();
if (isNeedSetup()) {
2022-07-28 18:59:26 +02:00
unawaited(Navigator.pushReplacementNamed(context, Setup.routeName));
} else if (account == null) {
2022-07-28 18:59:26 +02:00
unawaited(Navigator.pushReplacementNamed(context, SignIn.routeName));
} else {
2022-07-28 18:59:26 +02:00
unawaited(
Navigator.pushReplacementNamed(context, Home.routeName,
arguments: HomeArguments(account)),
);
2023-08-27 12:58:05 +02:00
if (getRawPlatform() == NpPlatform.android) {
final initialRoute = await Activity.consumeInitialRoute();
if (initialRoute != null) {
2022-07-28 18:59:26 +02:00
unawaited(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);
2022-07-28 18:59:26 +02:00
unawaited(_showChangelogIfAvailable(lastVersion));
2022-07-17 21:35:35 +02:00
// begin upgrade while showing the changelog
try {
_log.info("[_handleUpgrade] Upgrade: $lastVersion -> ${k.version}");
await _upgrade(lastVersion);
_log.info("[_handleUpgrade] Upgrade done");
} finally {
// ensure user has closed the changelog
await _changelogCompleter.future;
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 {
if (lastVersion < 290) {
await _upgrade29(lastVersion);
}
2022-07-17 21:32:04 +02:00
if (lastVersion < 460) {
await _upgrade46(lastVersion);
}
if (lastVersion < 550) {
await _upgrade55(lastVersion);
}
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
}
}
2022-07-17 21:32:04 +02:00
Future<void> _upgrade46(int lastVersion) async {
try {
_log.info("[_upgrade46] insertDbAccounts");
final c = KiwiContainer().resolve<DiContainer>();
await CompatV46.insertDbAccounts(Pref(), c.sqliteDb);
} catch (e, stackTrace) {
_log.shout("[_upgrade46] Failed while clearDefaultCache", e, stackTrace);
2022-07-28 18:59:26 +02:00
unawaited(Pref().setAccounts3(null));
unawaited(Pref().setCurrentAccountIndex(null));
2022-07-17 21:32:04 +02:00
await showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text("Error"),
content: const Text(
"Failed upgrading app, please sign in to your servers again"),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(MaterialLocalizations.of(context).okButtonLabel),
),
],
),
);
}
}
Future<void> _upgrade55(int lastVersion) async {
final c = KiwiContainer().resolve<DiContainer>();
try {
_log.info("[_upgrade55] migrate DB");
await CompatV55.migrateDb(
c.sqliteDb,
onProgress: (current, count) {
_upgradeCubit.setState(
L10n.global().migrateDatabaseProcessingNotification,
current,
count,
);
},
);
} catch (e, stackTrace) {
_log.shout("[_upgrade55] Failed while migrateDb", e, stackTrace);
await c.sqliteDb.use((db) async {
await db.truncate();
final accounts = Pref().getAccounts3Or([]);
for (final a in accounts) {
await db.insertAccountOf(a);
}
});
}
_upgradeCubit.setIntermediate();
}
2022-07-17 21:35:35 +02:00
Future<void> _showChangelogIfAvailable(int lastVersion) async {
if (Changelog.hasContent(lastVersion)) {
try {
await Navigator.of(context).pushNamed(Changelog.routeName,
arguments: ChangelogArguments(lastVersion));
} catch (e, stackTrace) {
_log.severe(
"[_showChangelogIfAvailable] Uncaught exception", e, stackTrace);
} finally {
_changelogCompleter.complete();
}
} else {
_changelogCompleter.complete();
2021-04-16 10:45:17 +02:00
}
}
2022-07-17 21:35:35 +02:00
final _changelogCompleter = Completer();
var _isUpgrading = false;
late final _upgradeCubit = _UpgradeCubit();
2021-04-10 06:28:12 +02:00
}
2022-12-08 16:39:13 +01:00
@toString
class _UpgradeState {
const _UpgradeState(String text, int current, int count)
: this._(text, current, count);
const _UpgradeState.intermediate([String? text])
: this._(text ?? "Updating", 0, null);
const _UpgradeState._(this.text, this.current, this.count);
@override
2022-12-08 16:39:13 +01:00
String toString() => _$toString();
final String text;
final int current;
final int? count;
}
class _UpgradeCubit extends Cubit<_UpgradeState> {
_UpgradeCubit() : super(const _UpgradeState.intermediate());
void setIntermediate() => emit(const _UpgradeState.intermediate());
void setState(String text, int current, int count) =>
emit(_UpgradeState(text, current, count));
}