revanced-manager/lib/main.dart

153 lines
4.8 KiB
Dart
Raw Normal View History

import 'package:animations/animations.dart';
2022-07-30 21:09:59 +02:00
import 'package:flutter/material.dart';
2022-08-07 01:37:12 +02:00
import 'package:flutter_i18n/flutter_i18n.dart';
2022-08-07 02:13:27 +02:00
// ignore: depend_on_referenced_packages
2022-08-07 01:37:12 +02:00
import 'package:flutter_localizations/flutter_localizations.dart';
2022-08-06 23:35:35 +02:00
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/app/app.router.dart';
import 'package:revanced_manager/main_viewmodel.dart';
2022-08-25 01:51:47 +02:00
import 'package:revanced_manager/services/manager_api.dart';
import 'package:revanced_manager/services/patcher_api.dart';
2022-08-06 23:35:35 +02:00
import 'package:revanced_manager/theme.dart';
import 'package:revanced_manager/ui/views/home/home_view.dart';
import 'package:revanced_manager/ui/views/patcher/patcher_view.dart';
2022-08-14 20:40:34 +02:00
import 'package:revanced_manager/ui/views/root_checker/root_checker_view.dart';
2022-08-10 14:30:28 +02:00
import 'package:revanced_manager/ui/views/settings/settings_view.dart';
import 'package:stacked/stacked.dart';
import 'package:stacked_services/stacked_services.dart';
2022-08-11 21:05:03 +02:00
import 'package:stacked_themes/stacked_themes.dart';
2022-07-30 21:09:59 +02:00
2022-08-11 21:05:03 +02:00
Future main() async {
await ThemeManager.initialise();
2022-08-25 01:51:47 +02:00
await setupLocator();
WidgetsFlutterBinding.ensureInitialized();
2022-07-30 21:09:59 +02:00
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
2022-08-11 21:05:03 +02:00
return ThemeBuilder(
defaultThemeMode: ThemeMode.dark,
2022-08-01 13:30:06 +02:00
darkTheme: darkTheme,
2022-08-11 21:05:03 +02:00
lightTheme: lightTheme,
builder: (context, regularTheme, darkTheme, themeMode) => MaterialApp(
debugShowCheckedModeBanner: false,
title: 'ReVanced Manager',
theme: lightTheme,
darkTheme: darkTheme,
themeMode: themeMode,
navigatorKey: StackedService.navigatorKey,
onGenerateRoute: StackedRouter().onGenerateRoute,
2022-08-14 20:40:34 +02:00
home: FutureBuilder<Widget>(
future: _init(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return snapshot.data!;
} else {
return Center(
child: CircularProgressIndicator(
color: Theme.of(context).colorScheme.secondary,
),
);
}
},
),
2022-08-11 21:05:03 +02:00
localizationsDelegates: [
FlutterI18nDelegate(
translationLoader: FileTranslationLoader(
fallbackFile: 'en',
basePath: 'assets/i18n',
),
2022-08-07 01:37:12 +02:00
),
2022-08-11 21:05:03 +02:00
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
),
2022-07-31 10:00:11 +02:00
);
}
2022-08-14 20:40:34 +02:00
Future<Widget> _init() async {
2022-08-25 01:51:47 +02:00
await locator<ManagerAPI>().initialize();
await locator<PatcherAPI>().initialize();
2022-08-25 01:51:47 +02:00
bool? isRooted = locator<ManagerAPI>().isRooted();
2022-08-14 20:40:34 +02:00
if (isRooted != null) {
return const Navigation();
}
return const RootCheckerView();
}
2022-07-31 10:00:11 +02:00
}
class Navigation extends StatelessWidget {
const Navigation({Key? key}) : super(key: key);
2022-07-31 10:00:11 +02:00
@override
Widget build(BuildContext context) {
return ViewModelBuilder<MainViewModel>.reactive(
viewModelBuilder: () => locator<MainViewModel>(),
2022-08-09 01:01:06 +02:00
builder: (context, model, child) => Scaffold(
body: PageTransitionSwitcher(
duration: const Duration(milliseconds: 400),
transitionBuilder: (
Widget child,
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
return FadeThroughTransition(
animation: animation,
secondaryAnimation: secondaryAnimation,
fillColor: Theme.of(context).colorScheme.surface,
child: child,
);
},
child: getViewForIndex(model.currentIndex),
),
bottomNavigationBar: NavigationBar(
onDestinationSelected: model.setIndex,
selectedIndex: model.currentIndex,
2022-08-07 01:37:12 +02:00
destinations: <Widget>[
NavigationDestination(
2022-08-11 22:17:10 +02:00
icon: const Icon(
Icons.dashboard,
),
2022-08-07 01:37:12 +02:00
label: FlutterI18n.translate(
context,
'main.dashboardTab',
),
),
NavigationDestination(
2022-08-07 01:37:12 +02:00
icon: const Icon(Icons.build),
label: FlutterI18n.translate(
context,
'main.patcherTab',
),
),
2022-08-10 14:30:28 +02:00
NavigationDestination(
icon: const Icon(Icons.settings),
label: FlutterI18n.translate(
context,
'main.settingsTab',
),
),
],
),
2022-07-30 21:09:59 +02:00
),
);
}
2022-08-06 23:35:35 +02:00
Widget getViewForIndex(int index) {
switch (index) {
case 0:
return const HomeView();
case 1:
return const PatcherView();
2022-08-10 14:30:28 +02:00
case 2:
return SettingsView();
default:
return const HomeView();
}
}
2022-07-30 21:09:59 +02:00
}