relatica/lib/main.dart

106 wiersze
3.5 KiB
Dart

import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:http/http.dart';
import 'package:http_parser/http_parser.dart';
import 'package:logging/logging.dart';
import 'package:provider/provider.dart';
import 'package:result_monad/result_monad.dart';
import 'globals.dart';
import 'models/TimelineIdentifiers.dart';
import 'routes.dart';
import 'services/auth_service.dart';
import 'services/connections_manager.dart';
import 'services/entry_manager_service.dart';
import 'services/notifications_manager.dart';
import 'services/secrets_service.dart';
import 'services/timeline_manager.dart';
import 'utils/app_scrolling_behavior.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await dotenv.load(fileName: '.env');
Logger.root.level = Level.ALL;
MultipartFile.fromBytes('file', Uint8List.fromList([]),
filename: 'hello.jpg', contentType: MediaType('image', 'jpeg'));
// Logger.root.onRecord.listen((event) {
// final logName = event.loggerName.isEmpty ? 'ROOT' : event.loggerName;
// final msg =
// '${event.level.name} - $logName @ ${event.time}: ${event.message}';
// print(msg);
// });
final authService = AuthService();
final secretsService = SecretsService();
final entryManagerService = EntryManagerService();
final timelineManager = TimelineManager();
getIt.registerLazySingleton<ConnectionsManager>(() => ConnectionsManager());
getIt.registerSingleton<EntryManagerService>(entryManagerService);
getIt.registerSingleton<SecretsService>(secretsService);
getIt.registerSingleton<AuthService>(authService);
getIt.registerSingleton<TimelineManager>(timelineManager);
getIt.registerLazySingleton<NotificationsManager>(
() => NotificationsManager());
await secretsService.initialize().andThenSuccessAsync((credentials) async {
if (credentials.isEmpty) {
return;
}
final wasLoggedIn = await authService.getStoredLoginState();
if (wasLoggedIn) {
final result = await authService.signIn(credentials);
print('Startup login result: $result');
if (result.isSuccess) {
print('Getting timeline for ${result.value.credentials.handle}');
timelineManager.updateTimeline(
TimelineIdentifiers.home(), TimelineRefreshType.loadOlder);
}
} else {
print('Was not logged in');
}
});
runApp(const App());
}
class App extends StatelessWidget {
const App({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<AuthService>(
create: (_) => getIt<AuthService>(),
lazy: true,
),
ChangeNotifierProvider<EntryManagerService>(
create: (_) => getIt<EntryManagerService>(),
lazy: true,
),
ChangeNotifierProvider<TimelineManager>(
create: (_) => getIt<TimelineManager>(),
),
ChangeNotifierProvider<NotificationsManager>(
create: (_) => getIt<NotificationsManager>(),
),
],
child: MaterialApp.router(
theme: ThemeData(
primarySwatch: Colors.indigo,
),
debugShowCheckedModeBanner: false,
scrollBehavior: AppScrollingBehavior(),
routerDelegate: appRouter.routerDelegate,
routeInformationProvider: appRouter.routeInformationProvider,
routeInformationParser: appRouter.routeInformationParser,
),
);
}
}