Solian/lib/bootstrapper.dart

318 lines
10 KiB
Dart
Raw Normal View History

2024-09-17 13:37:20 +00:00
import 'dart:async';
2024-07-26 17:39:20 +00:00
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
2024-07-26 17:39:20 +00:00
import 'package:get/get.dart';
2024-08-02 17:14:42 +00:00
import 'package:package_info_plus/package_info_plus.dart';
2024-07-31 17:21:27 +00:00
import 'package:provider/provider.dart';
2024-09-17 13:37:20 +00:00
import 'package:shared_preferences/shared_preferences.dart';
2024-07-26 17:39:20 +00:00
import 'package:solian/exts.dart';
2024-08-02 17:14:42 +00:00
import 'package:solian/platform.dart';
2024-07-26 17:39:20 +00:00
import 'package:solian/providers/auth.dart';
import 'package:solian/providers/content/channel.dart';
2024-08-01 07:21:43 +00:00
import 'package:solian/providers/content/realm.dart';
2024-07-26 17:39:20 +00:00
import 'package:solian/providers/relation.dart';
2024-07-31 17:21:27 +00:00
import 'package:solian/providers/theme_switcher.dart';
2024-07-26 17:39:20 +00:00
import 'package:solian/providers/websocket.dart';
import 'package:solian/services.dart';
2024-07-26 18:11:59 +00:00
import 'package:solian/widgets/sized_container.dart';
2024-09-17 12:40:44 +00:00
import 'package:flutter_app_update/flutter_app_update.dart';
2024-09-17 13:37:20 +00:00
import 'package:version/version.dart';
2024-07-26 17:39:20 +00:00
class BootstrapperShell extends StatefulWidget {
final Widget child;
const BootstrapperShell({super.key, required this.child});
@override
State<BootstrapperShell> createState() => _BootstrapperShellState();
}
class _BootstrapperShellState extends State<BootstrapperShell> {
bool _isBusy = true;
bool _isErrored = false;
2024-08-02 17:14:42 +00:00
bool _isDismissable = true;
2024-07-26 17:39:20 +00:00
String? _subtitle;
Color get _unFocusColor =>
Theme.of(context).colorScheme.onSurface.withOpacity(0.75);
int _periodCursor = 0;
2024-09-17 13:37:20 +00:00
final Completer _bootCompleter = Completer();
2024-07-26 17:39:20 +00:00
late final List<({String label, Future<void> Function() action})> _periods = [
2024-07-31 17:21:27 +00:00
(
label: 'bsLoadingTheme',
action: () async {
await context.read<ThemeSwitcher>().restoreTheme();
},
),
2024-08-02 17:14:42 +00:00
(
label: 'bsCheckForUpdate',
action: () async {
if (PlatformInfo.isWeb) return;
try {
2024-09-17 13:37:20 +00:00
final prefs = await SharedPreferences.getInstance();
2024-08-02 17:14:42 +00:00
final info = await PackageInfo.fromPlatform();
final localVersionString = '${info.version}+${info.buildNumber}';
final resp = await GetConnect().get(
2024-09-17 12:40:44 +00:00
'https://git.solsynth.dev/api/v1/repos/hydrogen/solian/tags?page=1&limit=1',
2024-08-02 17:14:42 +00:00
);
2024-09-17 13:37:20 +00:00
final remoteVersionString =
(resp.body as List).firstOrNull?['name'] ?? '0.0.0';
final remoteVersion = Version.parse(remoteVersionString ?? '0.0.0');
final localVersion =
Version.parse(localVersionString.split('+').first);
final strictUpdate = prefs.getBool('check_update_strictly') ?? false;
if (remoteVersion > localVersion ||
(remoteVersionString != localVersionString && strictUpdate)) {
2024-08-02 17:14:42 +00:00
setState(() {
_isErrored = true;
2024-09-17 12:40:44 +00:00
_subtitle = 'bsCheckForUpdateDesc'.tr;
2024-08-02 17:14:42 +00:00
});
2024-09-17 12:40:44 +00:00
if (PlatformInfo.isAndroid) {
context
.showConfirmDialog(
'updateAvailable'.tr,
'updateAvailableDesc'.trParams({
2024-09-17 13:37:20 +00:00
'from': localVersionString,
'to': remoteVersionString,
2024-09-17 12:40:44 +00:00
}),
)
.then((result) {
if (result) {
final model = UpdateModel(
'https://files.solsynth.dev/d/production01/solian/app-arm64-v8a-release.apk',
'solian-app-arm64-v8a-release.apk',
'ic_launcher',
'https://testflight.apple.com/join/YJ0lmN6O',
);
AzhonAppUpdate.update(model);
}
});
2024-09-17 13:37:20 +00:00
} else {
setState(() {
_isErrored = true;
_subtitle = 'bsCheckForUpdateDesc'.tr;
});
2024-09-17 12:40:44 +00:00
}
2024-09-17 13:37:20 +00:00
} else if (remoteVersionString != localVersionString) {
_bootCompleter.future.then((_) {
context.showSnackbar('updateMayAvailable'.trParams({
'version': remoteVersionString,
}));
});
2024-08-02 17:14:42 +00:00
}
} catch (e) {
2024-09-17 13:37:20 +00:00
context.showErrorDialog('Unable to check update: $e');
2024-08-02 17:14:42 +00:00
}
},
),
2024-07-26 17:39:20 +00:00
(
label: 'bsCheckingServer',
action: () async {
2024-09-16 03:57:16 +00:00
final client = await ServiceFinder.configureClient('dealer');
2024-07-26 17:39:20 +00:00
final resp = await client.get('/.well-known');
2024-07-27 06:16:49 +00:00
if (resp.statusCode != null && resp.statusCode != 200) {
setState(() {
_isErrored = true;
_subtitle = 'bsCheckingServerDown'.tr;
2024-08-02 17:14:42 +00:00
_isDismissable = false;
2024-07-27 06:16:49 +00:00
});
throw Exception('unable connect to server');
} else if (resp.statusCode == null) {
2024-07-26 17:39:20 +00:00
setState(() {
_isErrored = true;
_subtitle = 'bsCheckingServerFail'.tr;
2024-08-02 17:14:42 +00:00
_isDismissable = false;
2024-07-26 17:39:20 +00:00
});
2024-07-27 06:16:49 +00:00
throw Exception('unable connect to server');
2024-07-26 17:39:20 +00:00
}
},
),
(
label: 'bsAuthorizing',
action: () async {
final AuthProvider auth = Get.find();
await auth.refreshAuthorizeStatus();
if (auth.isAuthorized.isTrue) {
await auth.refreshUserProfile();
}
},
),
(
label: 'bsEstablishingConn',
action: () async {
final AuthProvider auth = Get.find();
if (auth.isAuthorized.isTrue) {
await Get.find<WebSocketProvider>().connect();
}
},
),
(
label: 'bsPreparingData',
action: () async {
final AuthProvider auth = Get.find();
try {
await Future.wait([
if (auth.isAuthorized.isTrue)
Get.find<ChannelProvider>().refreshAvailableChannel(),
if (auth.isAuthorized.isTrue)
Get.find<RelationshipProvider>().refreshRelativeList(),
if (auth.isAuthorized.isTrue)
Get.find<RealmProvider>().refreshAvailableRealms(),
]);
} catch (e) {
context.showErrorDialog(e);
}
2024-07-26 17:39:20 +00:00
},
),
(
label: 'bsRegisteringPushNotify',
action: () async {
final AuthProvider auth = Get.find();
if (auth.isAuthorized.isTrue) {
try {
Get.find<WebSocketProvider>().registerPushNotifications();
} catch (err) {
context.showSnackbar(
'pushNotifyRegisterFailed'.trParams({'reason': err.toString()}),
);
}
}
},
),
];
Future<void> _runPeriods() async {
2024-07-27 06:16:49 +00:00
try {
for (var idx = 0; idx < _periods.length; idx++) {
await _periods[idx].action();
if (_isErrored && !_isDismissable) break;
2024-07-30 18:00:03 +00:00
if (_periodCursor < _periods.length - 1) {
setState(() => _periodCursor++);
}
2024-07-27 06:16:49 +00:00
}
} finally {
setState(() => _isBusy = false);
2024-09-17 13:37:20 +00:00
Future.delayed(const Duration(milliseconds: 100), () {
_bootCompleter.complete();
});
2024-07-26 17:39:20 +00:00
}
}
@override
void initState() {
super.initState();
_runPeriods();
}
@override
Widget build(BuildContext context) {
if (_isBusy || _isErrored) {
return GestureDetector(
child: Material(
color: Theme.of(context).colorScheme.surface,
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
SizedBox(
height: 280,
child: Align(
alignment: Alignment.bottomCenter,
child: ClipRRect(
borderRadius: const BorderRadius.all(Radius.circular(16)),
child:
Image.asset('assets/logo.png', width: 80, height: 80),
),
2024-08-03 09:44:36 +00:00
),
2024-07-26 17:39:20 +00:00
),
Column(
2024-07-27 06:16:49 +00:00
children: [
if (_isErrored && !_isDismissable && !_isBusy)
2024-08-02 17:14:42 +00:00
const Icon(Icons.cancel, size: 24),
2024-08-04 04:59:13 +00:00
if (_isErrored && _isDismissable && !_isBusy)
2024-08-02 17:14:42 +00:00
const Icon(Icons.warning, size: 24),
if ((_isErrored && _isDismissable && _isBusy) || _isBusy)
2024-07-27 06:16:49 +00:00
const SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator(strokeWidth: 3),
),
const Gap(12),
2024-07-27 06:16:49 +00:00
CenteredContainer(
maxWidth: 280,
child: Column(
children: [
2024-08-02 17:14:42 +00:00
if (_subtitle == null)
Text(
'${_periods[_periodCursor].label.tr} (${_periodCursor + 1}/${_periods.length})',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 13,
color: _unFocusColor,
),
2024-07-27 06:16:49 +00:00
),
2024-08-02 17:14:42 +00:00
if (_subtitle != null)
Text(
_subtitle!,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 13,
color: _unFocusColor,
),
).paddingOnly(bottom: 4),
if (!_isBusy && _isErrored && _isDismissable)
Text(
'bsDismissibleErrorHint'.tr,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 13,
color: _unFocusColor,
),
).paddingOnly(bottom: 5),
2024-07-27 06:16:49 +00:00
Text(
'2024 © Solsynth LLC',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 11,
color: _unFocusColor,
),
),
],
2024-07-26 18:11:59 +00:00
),
2024-07-26 17:39:20 +00:00
),
2024-07-27 06:16:49 +00:00
],
),
],
),
2024-07-26 17:39:20 +00:00
),
onTap: () {
if (_isBusy) return;
if (_isDismissable) {
setState(() {
_isBusy = false;
_isErrored = false;
});
2024-09-17 13:37:20 +00:00
Future.delayed(const Duration(milliseconds: 100), () {
_bootCompleter.complete();
});
} else {
setState(() {
_isBusy = true;
_isErrored = false;
_periodCursor = 0;
});
_runPeriods();
}
},
2024-07-26 17:39:20 +00:00
);
}
return widget.child;
}
}