275 lines
8.7 KiB
Dart
275 lines
8.7 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:accessibility_tools/accessibility_tools.dart';
|
|
import 'package:device_info_plus/device_info_plus.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_native_splash/flutter_native_splash.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:flutter_riverpod/legacy.dart';
|
|
import 'package:logging/logging.dart';
|
|
import 'package:peertube/features/account/account.dart';
|
|
import 'package:peertube/features/platform/platform.dart';
|
|
import 'package:peertube/features/sentry/sentry.dart';
|
|
import 'package:peertube/features/video/video.dart';
|
|
import 'package:peertube/l10n/app_localizations.dart';
|
|
import 'package:peertube/routes/routes.dart';
|
|
import 'package:peertube/shared/shared.dart';
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
import 'package:sentry_flutter/sentry_flutter.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
Future<void> main() async {
|
|
final widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
|
|
FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
|
|
|
|
await initializeSentryAndRunApp(appRunner: _runApp);
|
|
}
|
|
|
|
Future<void> _runApp() async {
|
|
Logger.root.level = CustomLogger.fromInt(Environment.instance.logLevel);
|
|
|
|
if (kDebugMode) {
|
|
Logger.root.onRecord.listen((record) {
|
|
debugPrint('${record.level.name}: ${record.time}: ${record.message}');
|
|
});
|
|
}
|
|
|
|
final previousOnError = PlatformDispatcher.instance.onError;
|
|
PlatformDispatcher.instance.onError = (error, stack) {
|
|
if (isOfflineError(error)) return true;
|
|
return previousOnError?.call(error, stack) ?? false;
|
|
};
|
|
|
|
final previousFlutterOnError = FlutterError.onError;
|
|
FlutterError.onError = (details) {
|
|
if (isOfflineError(details.exception)) return;
|
|
previousFlutterOnError?.call(details);
|
|
};
|
|
|
|
final (prefs, db, _) = await (
|
|
SharedPreferences.getInstance(),
|
|
DatabaseHelper.initializeDatabase(),
|
|
initUserAgent(),
|
|
).wait;
|
|
|
|
const app = MyApp();
|
|
|
|
runApp(
|
|
ProviderScope(
|
|
overrides: <Override>[
|
|
sharedPreferencesProvider.overrideWithValue(prefs),
|
|
databaseProvider.overrideWithValue(db),
|
|
],
|
|
// observers: [MyProviderObserver()],
|
|
child: Sentry.isEnabled == false
|
|
? app
|
|
: DefaultAssetBundle(bundle: SentryAssetBundle(), child: app),
|
|
),
|
|
);
|
|
}
|
|
|
|
class MyApp extends ConsumerStatefulWidget {
|
|
const MyApp({super.key});
|
|
@override
|
|
ConsumerState<MyApp> createState() => _MyAppState();
|
|
}
|
|
|
|
class _MyAppState extends ConsumerState<MyApp> {
|
|
bool _loading = true;
|
|
String? _pendingNotificationRoute;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
ref.read(deviceTypeProvider.notifier).applyOrientations();
|
|
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!mounted) return;
|
|
FlutterNativeSplash.remove();
|
|
unawaited(_startLoadLastPlatform());
|
|
unawaited(_initializeNotifications());
|
|
_checkDeviceVersion();
|
|
// clearStorage();
|
|
ref.read(videoManageServiceProvider).pauseAllInProgressUploads();
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (_loading) return const LoadingScreen();
|
|
|
|
ref.watch(chromecastBootstrapProvider);
|
|
|
|
return MaterialApp.router(
|
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
|
locale: ref.watch(myLocaleProvider),
|
|
title: 'PeerTube Mobile',
|
|
debugShowCheckedModeBanner: false,
|
|
// showSemanticsDebugger: true,
|
|
builder: (context, child) {
|
|
final routedChild = Platform.isAndroid
|
|
? AndroidPictureInPictureOverlay(child: child)
|
|
: child;
|
|
return kDebugMode
|
|
? AccessibilityTools(
|
|
logLevel: LogLevel.warning,
|
|
child: Theme(
|
|
data: Theme.of(context),
|
|
child: routedChild ?? const SizedBox.shrink(),
|
|
),
|
|
)
|
|
: routedChild ?? const SizedBox.shrink();
|
|
},
|
|
theme: PeerTubeTheme.getThemeData(
|
|
Brightness.light,
|
|
ref.watch(deviceTypeProvider).type,
|
|
),
|
|
darkTheme: PeerTubeTheme.getThemeData(
|
|
Brightness.dark,
|
|
ref.watch(deviceTypeProvider).type,
|
|
),
|
|
themeMode: ref.watch(myThemeProvider),
|
|
routerConfig: ref.watch(appRouterConfigProvider),
|
|
scaffoldMessengerKey: scaffoldMessengerKey,
|
|
);
|
|
}
|
|
|
|
Future<void> clearStorage() async {
|
|
await ref.read(flutterSecureStorageProvider).deleteAll();
|
|
await ref.read(sharedPreferencesProvider).clear();
|
|
}
|
|
|
|
Future<void> _initializeNotifications() async {
|
|
try {
|
|
await NotificationService().initialize(
|
|
onNotificationTap: _onNotificationTap,
|
|
);
|
|
} catch (error, stack) {
|
|
log.warning('Unable to initialize notifications', error, stack);
|
|
}
|
|
}
|
|
|
|
Future<void> _startLoadLastPlatform() async {
|
|
try {
|
|
final connectivity = await ref.read(connectivityChangesProvider.future);
|
|
if (connectivity.isOnline) await loadLastPlatform();
|
|
} catch (error, stack) {
|
|
log.warning('Unable to restore the last platform', error, stack);
|
|
}
|
|
|
|
if (mounted) {
|
|
setState(() {
|
|
_loading = false;
|
|
});
|
|
final route = _pendingNotificationRoute;
|
|
_pendingNotificationRoute = null;
|
|
if (route != null) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
_onNotificationTap(route);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
void _onNotificationTap(String route) {
|
|
if (!mounted || route != '/library/downloads') return;
|
|
if (_loading) {
|
|
_pendingNotificationRoute = route;
|
|
return;
|
|
}
|
|
ref.read(goRouterProvider).go(route);
|
|
}
|
|
|
|
Future<void> loadLastPlatform() async {
|
|
final account = await ref.read(currentConnectedAccountProvider.future);
|
|
|
|
await ref
|
|
.read(currentPlatformProvider.notifier)
|
|
.loadLastPlatform(nextAccount: account);
|
|
|
|
await _checkAccountsAgainstBlocklist();
|
|
}
|
|
|
|
Future<void> _checkAccountsAgainstBlocklist() async {
|
|
try {
|
|
final blocklist = await ref.read(blocklistedHostsProvider.future);
|
|
if (blocklist.isEmpty) return;
|
|
|
|
final currentAccount = await ref.read(
|
|
currentConnectedAccountProvider.future,
|
|
);
|
|
final currentPlatform = ref.read(currentPlatformProvider);
|
|
|
|
final accountService = ref.read(connectedAccountServiceProvider);
|
|
final platformHistoryService = ref.read(platformHistoryServiceProvider);
|
|
|
|
final currentAccountIsBlocked =
|
|
!currentAccount.isLocal &&
|
|
blocklist.contains(currentAccount.host.toLowerCase());
|
|
|
|
final currentPlatformIsBlocked =
|
|
currentPlatform != null &&
|
|
blocklist.contains(currentPlatform.host.toLowerCase());
|
|
|
|
await platformHistoryService.deleteByHosts(blocklist.toList());
|
|
|
|
if (currentAccountIsBlocked) {
|
|
final localAccount = await accountService.getLocalAccount();
|
|
try {
|
|
await ref
|
|
.read(currentConnectedAccountProvider.notifier)
|
|
.loadCurrentAccount(localAccount.id!);
|
|
} catch (e, s) {
|
|
log.warning(
|
|
'Failed to switch to local account after blocklist removal',
|
|
e,
|
|
s,
|
|
);
|
|
}
|
|
await accountService.deleteAccount(currentAccount.id);
|
|
} else if (currentPlatformIsBlocked) {
|
|
await ref
|
|
.read(currentPlatformProvider.notifier)
|
|
.loadLastPlatform(nextAccount: currentAccount);
|
|
}
|
|
|
|
final allAccounts = await accountService.listAllAccountsByLastAccess();
|
|
for (final account in allAccounts) {
|
|
if (!account.isLocal &&
|
|
account.id != currentAccount.id &&
|
|
blocklist.contains(account.host.toLowerCase())) {
|
|
await accountService.deleteAccount(account.id);
|
|
}
|
|
}
|
|
} catch (e, s) {
|
|
log.warning('Failed to apply blocklist', e, s);
|
|
}
|
|
}
|
|
|
|
void _checkDeviceVersion() async {
|
|
if (!mounted) return;
|
|
|
|
if (Platform.isAndroid) {
|
|
final androidInfo = await DeviceInfoPlugin().androidInfo;
|
|
final version = androidInfo.version.release;
|
|
if (compareVersions(version, ANDROID_MIN_VERSION) < 0) {
|
|
ref.read(invalidVersionProvider.notifier).update((_) => true);
|
|
}
|
|
} else if (Platform.isIOS) {
|
|
final iosInfo = await DeviceInfoPlugin().iosInfo;
|
|
final version = iosInfo.systemVersion;
|
|
if (compareVersions(version, IOS_MIN_VERSION) < 0) {
|
|
ref.read(invalidVersionProvider.notifier).update((_) => true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
final invalidVersionProvider = StateProvider<bool>((ref) {
|
|
return false;
|
|
}, name: 'invalidVersionProvider');
|