2025-09-23 13:43:01 +05:30
|
|
|
import 'dart:async';
|
|
|
|
|
import 'dart:developer' as developer;
|
2025-09-25 23:22:48 +05:30
|
|
|
import 'package:flutter/material.dart';
|
2025-11-05 14:12:58 +05:30
|
|
|
import 'package:flutter/rendering.dart';
|
2025-10-02 21:33:58 +05:30
|
|
|
import 'package:flutter/scheduler.dart';
|
2025-08-10 01:20:45 +05:30
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
|
import 'core/widgets/error_boundary.dart';
|
|
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
|
|
|
|
|
|
|
|
import 'core/providers/app_providers.dart';
|
2025-10-01 16:55:44 +05:30
|
|
|
import 'core/persistence/hive_bootstrap.dart';
|
|
|
|
|
import 'core/persistence/persistence_migrator.dart';
|
|
|
|
|
import 'core/persistence/persistence_providers.dart';
|
2025-09-22 14:36:43 +05:30
|
|
|
import 'core/router/app_router.dart';
|
2025-08-10 01:20:45 +05:30
|
|
|
import 'features/auth/providers/unified_auth_providers.dart';
|
|
|
|
|
import 'core/auth/auth_state_manager.dart';
|
2025-08-20 22:15:26 +05:30
|
|
|
import 'core/utils/debug_logger.dart';
|
2025-09-23 13:43:01 +05:30
|
|
|
import 'core/utils/system_ui_style.dart';
|
2025-08-16 20:27:44 +05:30
|
|
|
|
2025-08-23 20:09:43 +05:30
|
|
|
import 'package:conduit/l10n/app_localizations.dart';
|
2025-08-28 12:59:48 +05:30
|
|
|
import 'core/services/share_receiver_service.dart';
|
2025-09-02 21:19:07 +05:30
|
|
|
import 'core/providers/app_startup_providers.dart';
|
2025-08-10 01:20:45 +05:30
|
|
|
|
2025-09-23 13:43:01 +05:30
|
|
|
developer.TimelineTask? _startupTimeline;
|
|
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
runZonedGuarded(
|
|
|
|
|
() async {
|
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
|
|
|
|
|
|
// Global error handlers
|
|
|
|
|
FlutterError.onError = (FlutterErrorDetails details) {
|
2025-09-25 22:36:42 +05:30
|
|
|
DebugLogger.error(
|
|
|
|
|
'flutter-error',
|
|
|
|
|
scope: 'app/framework',
|
|
|
|
|
error: details.exception,
|
|
|
|
|
);
|
2025-09-23 13:43:01 +05:30
|
|
|
final stack = details.stack;
|
|
|
|
|
if (stack != null) {
|
2025-09-25 23:22:48 +05:30
|
|
|
debugPrintStack(stackTrace: stack);
|
2025-09-23 13:43:01 +05:30
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
WidgetsBinding.instance.platformDispatcher.onError = (error, stack) {
|
2025-09-25 22:36:42 +05:30
|
|
|
DebugLogger.error(
|
|
|
|
|
'platform-error',
|
|
|
|
|
scope: 'app/platform',
|
|
|
|
|
error: error,
|
|
|
|
|
stackTrace: stack,
|
|
|
|
|
);
|
2025-09-25 23:22:48 +05:30
|
|
|
debugPrintStack(stackTrace: stack);
|
2025-09-23 13:43:01 +05:30
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Start startup timeline instrumentation
|
|
|
|
|
_startupTimeline = developer.TimelineTask();
|
|
|
|
|
_startupTimeline!.start('app_startup');
|
|
|
|
|
_startupTimeline!.instant('bindings_initialized');
|
|
|
|
|
|
2025-10-20 23:56:40 +05:30
|
|
|
// Edge-to-edge is now handled natively in MainActivity.kt for Android 15+
|
|
|
|
|
// No need for SystemUiMode.edgeToEdge which is deprecated
|
|
|
|
|
_startupTimeline?.instant('edge_to_edge_configured');
|
2025-09-23 13:43:01 +05:30
|
|
|
|
|
|
|
|
const secureStorage = FlutterSecureStorage(
|
|
|
|
|
aOptions: AndroidOptions(
|
|
|
|
|
sharedPreferencesName: 'conduit_secure_prefs',
|
|
|
|
|
preferencesKeyPrefix: 'conduit_',
|
|
|
|
|
resetOnError: false,
|
|
|
|
|
),
|
|
|
|
|
iOptions: IOSOptions(
|
|
|
|
|
accountName: 'conduit_secure_storage',
|
|
|
|
|
synchronizable: false,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
_startupTimeline!.instant('secure_storage_ready');
|
|
|
|
|
|
2025-10-11 12:44:35 +05:30
|
|
|
// Initialize Hive (now optimized with migration state caching)
|
2025-10-01 16:55:44 +05:30
|
|
|
final hiveBoxes = await HiveBootstrap.instance.ensureInitialized();
|
|
|
|
|
_startupTimeline!.instant('hive_ready');
|
|
|
|
|
|
2025-10-11 12:44:35 +05:30
|
|
|
// Run migration check (now fast-pathed after first run)
|
2025-10-01 16:55:44 +05:30
|
|
|
final migrator = PersistenceMigrator(hiveBoxes: hiveBoxes);
|
|
|
|
|
await migrator.migrateIfNeeded();
|
|
|
|
|
_startupTimeline!.instant('migration_complete');
|
|
|
|
|
|
2025-09-23 13:43:01 +05:30
|
|
|
// Finish timeline after first frame paints
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
_startupTimeline?.instant('first_frame_rendered');
|
|
|
|
|
_startupTimeline?.finish();
|
|
|
|
|
_startupTimeline = null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
runApp(
|
|
|
|
|
ProviderScope(
|
|
|
|
|
overrides: [
|
|
|
|
|
secureStorageProvider.overrideWithValue(secureStorage),
|
2025-10-01 16:55:44 +05:30
|
|
|
hiveBoxesProvider.overrideWithValue(hiveBoxes),
|
2025-09-23 13:43:01 +05:30
|
|
|
],
|
|
|
|
|
child: const ConduitApp(),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
developer.Timeline.instantSync('runApp_called');
|
|
|
|
|
},
|
|
|
|
|
(error, stack) {
|
2025-09-25 22:36:42 +05:30
|
|
|
DebugLogger.error(
|
|
|
|
|
'zone-error',
|
|
|
|
|
scope: 'app',
|
|
|
|
|
error: error,
|
|
|
|
|
stackTrace: stack,
|
|
|
|
|
);
|
2025-09-25 23:22:48 +05:30
|
|
|
debugPrintStack(stackTrace: stack);
|
2025-09-23 13:43:01 +05:30
|
|
|
},
|
2025-08-10 01:20:45 +05:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ConduitApp extends ConsumerStatefulWidget {
|
|
|
|
|
const ConduitApp({super.key});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
ConsumerState<ConduitApp> createState() => _ConduitAppState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _ConduitAppState extends ConsumerState<ConduitApp> {
|
2025-09-23 13:43:01 +05:30
|
|
|
Brightness? _lastAppliedOverlayBrightness;
|
2025-08-10 01:20:45 +05:30
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
2025-10-02 21:33:58 +05:30
|
|
|
// Delay heavy provider initialization until after the first frame so the
|
|
|
|
|
// initial paint stays responsive.
|
2025-09-16 20:10:53 +05:30
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) => _initializeAppState());
|
2025-08-10 01:20:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _initializeAppState() {
|
2025-09-25 22:36:42 +05:30
|
|
|
DebugLogger.auth('init', scope: 'app');
|
2025-08-10 01:20:45 +05:30
|
|
|
|
2025-10-02 21:33:58 +05:30
|
|
|
void queueInit(void Function() action, {Duration delay = Duration.zero}) {
|
|
|
|
|
Future<void>.delayed(delay, () {
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
action();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queueInit(() => ref.read(authStateManagerProvider));
|
|
|
|
|
queueInit(
|
|
|
|
|
() => ref.read(authApiIntegrationProvider),
|
|
|
|
|
delay: const Duration(milliseconds: 16),
|
|
|
|
|
);
|
2025-12-02 21:22:34 +05:30
|
|
|
// Note: defaultModelAutoSelectionProvider is now initialized in
|
|
|
|
|
// AppStartupFlow after authentication to avoid loading tools too early
|
2025-10-02 21:33:58 +05:30
|
|
|
queueInit(
|
|
|
|
|
() => ref.read(shareReceiverInitializerProvider),
|
2025-12-02 21:22:34 +05:30
|
|
|
delay: const Duration(milliseconds: 24),
|
2025-10-02 21:33:58 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
SchedulerBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
ref.read(appStartupFlowProvider.notifier).start();
|
|
|
|
|
});
|
2025-08-10 01:20:45 +05:30
|
|
|
}
|
|
|
|
|
|
2025-09-22 14:36:43 +05:30
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-10 01:20:45 +05:30
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-09-30 14:54:24 +05:30
|
|
|
final themeMode = ref.watch(appThemeModeProvider.select((mode) => mode));
|
2025-09-22 14:36:43 +05:30
|
|
|
final router = ref.watch(goRouterProvider);
|
2025-09-30 14:54:24 +05:30
|
|
|
final locale = ref.watch(appLocaleProvider);
|
2025-10-02 01:58:12 +05:30
|
|
|
final lightTheme = ref.watch(appLightThemeProvider);
|
|
|
|
|
final darkTheme = ref.watch(appDarkThemeProvider);
|
2025-08-23 20:09:43 +05:30
|
|
|
|
2025-09-23 13:43:01 +05:30
|
|
|
return ErrorBoundary(
|
|
|
|
|
child: MaterialApp.router(
|
|
|
|
|
routerConfig: router,
|
|
|
|
|
onGenerateTitle: (context) => AppLocalizations.of(context)!.appTitle,
|
2025-10-02 01:58:12 +05:30
|
|
|
theme: lightTheme,
|
|
|
|
|
darkTheme: darkTheme,
|
2025-09-23 13:43:01 +05:30
|
|
|
themeMode: themeMode,
|
|
|
|
|
debugShowCheckedModeBanner: false,
|
|
|
|
|
locale: locale,
|
|
|
|
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
|
|
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
|
|
|
|
localeListResolutionCallback: (deviceLocales, supported) {
|
|
|
|
|
if (locale != null) return locale;
|
|
|
|
|
if (deviceLocales == null || deviceLocales.isEmpty) {
|
2025-08-24 20:27:11 +05:30
|
|
|
return supported.first;
|
2025-09-23 13:43:01 +05:30
|
|
|
}
|
2025-11-24 16:08:55 +05:30
|
|
|
final resolved = _resolveSupportedLocale(deviceLocales, supported);
|
|
|
|
|
return resolved ?? supported.first;
|
2025-09-23 13:43:01 +05:30
|
|
|
},
|
|
|
|
|
builder: (context, child) {
|
|
|
|
|
final brightness = Theme.of(context).brightness;
|
|
|
|
|
if (_lastAppliedOverlayBrightness != brightness) {
|
|
|
|
|
_lastAppliedOverlayBrightness = brightness;
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
applySystemUiOverlayStyleOnce(brightness: brightness);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
final mediaQuery = MediaQuery.of(context);
|
2025-11-05 14:12:58 +05:30
|
|
|
final safeChild = child ?? const SizedBox.shrink();
|
|
|
|
|
|
2025-09-23 13:43:01 +05:30
|
|
|
return MediaQuery(
|
|
|
|
|
data: mediaQuery.copyWith(
|
|
|
|
|
textScaler: mediaQuery.textScaler.clamp(
|
|
|
|
|
minScaleFactor: 1.0,
|
|
|
|
|
maxScaleFactor: 3.0,
|
2025-08-25 11:16:09 +05:30
|
|
|
),
|
2025-09-23 13:43:01 +05:30
|
|
|
),
|
2025-11-05 14:12:58 +05:30
|
|
|
child: _KeyboardDismissOnScroll(child: safeChild),
|
2025-09-23 13:43:01 +05:30
|
|
|
);
|
|
|
|
|
},
|
2025-08-10 01:20:45 +05:30
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-11-24 16:08:55 +05:30
|
|
|
|
|
|
|
|
bool _prefersTraditionalChinese(Locale deviceLocale) {
|
|
|
|
|
final script = deviceLocale.scriptCode?.toLowerCase();
|
|
|
|
|
if (script == 'hant') return true;
|
|
|
|
|
|
|
|
|
|
final country = deviceLocale.countryCode?.toUpperCase();
|
|
|
|
|
return country == 'TW' || country == 'HK' || country == 'MO';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Locale? _resolveSupportedLocale(
|
|
|
|
|
List<Locale>? deviceLocales,
|
|
|
|
|
Iterable<Locale> supported,
|
|
|
|
|
) {
|
|
|
|
|
if (deviceLocales == null || deviceLocales.isEmpty) return null;
|
|
|
|
|
|
|
|
|
|
for (final device in deviceLocales) {
|
|
|
|
|
final prefersTraditional = _prefersTraditionalChinese(device);
|
|
|
|
|
final deviceLanguage = device.languageCode.toLowerCase();
|
|
|
|
|
final deviceScript = device.scriptCode?.toLowerCase();
|
|
|
|
|
final deviceCountry = device.countryCode?.toUpperCase();
|
|
|
|
|
|
|
|
|
|
// Pass 1: match language with script (or preferred Traditional)
|
|
|
|
|
for (final loc in supported) {
|
|
|
|
|
final languageMatches =
|
|
|
|
|
loc.languageCode.toLowerCase() == deviceLanguage;
|
|
|
|
|
if (!languageMatches) continue;
|
|
|
|
|
|
|
|
|
|
final locScript = loc.scriptCode?.toLowerCase();
|
|
|
|
|
final scriptMatches =
|
|
|
|
|
locScript != null &&
|
|
|
|
|
locScript.isNotEmpty &&
|
|
|
|
|
(locScript == deviceScript ||
|
|
|
|
|
(loc.languageCode == 'zh' &&
|
|
|
|
|
locScript == 'hant' &&
|
|
|
|
|
prefersTraditional));
|
|
|
|
|
if (!scriptMatches) continue;
|
|
|
|
|
|
|
|
|
|
final locCountry = loc.countryCode?.toUpperCase();
|
|
|
|
|
final countryMatches =
|
|
|
|
|
locCountry == null ||
|
|
|
|
|
locCountry.isEmpty ||
|
|
|
|
|
locCountry == deviceCountry;
|
|
|
|
|
|
|
|
|
|
if (countryMatches) {
|
|
|
|
|
return loc;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pass 2: prefer Traditional Chinese when applicable
|
|
|
|
|
if (prefersTraditional) {
|
|
|
|
|
for (final loc in supported) {
|
|
|
|
|
if (loc.languageCode == 'zh' && loc.scriptCode == 'Hant') {
|
|
|
|
|
return loc;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pass 3: language-only match
|
|
|
|
|
for (final loc in supported) {
|
|
|
|
|
if (loc.languageCode.toLowerCase() == deviceLanguage) {
|
|
|
|
|
return loc;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2025-08-10 01:20:45 +05:30
|
|
|
}
|
2025-11-05 14:12:58 +05:30
|
|
|
|
|
|
|
|
/// Dismisses the soft keyboard whenever the user scrolls.
|
|
|
|
|
class _KeyboardDismissOnScroll extends StatelessWidget {
|
|
|
|
|
const _KeyboardDismissOnScroll({required this.child});
|
|
|
|
|
|
|
|
|
|
final Widget child;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return NotificationListener<UserScrollNotification>(
|
|
|
|
|
onNotification: (notification) {
|
|
|
|
|
if (notification.direction == ScrollDirection.idle) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
final focusedNode = FocusManager.instance.primaryFocus;
|
|
|
|
|
if (focusedNode != null && focusedNode.hasFocus) {
|
|
|
|
|
focusedNode.unfocus();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
child: child,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|