Rain/lib/main.dart

254 lines
8.4 KiB
Dart
Raw Normal View History

2023-07-09 12:56:16 +05:30
import 'dart:io';
2023-06-17 20:57:57 +03:00
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
2023-06-29 21:25:00 +03:00
import 'package:flutter/services.dart';
2023-06-17 20:57:57 +03:00
import 'package:flutter_displaymode/flutter_displaymode.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_timezone/flutter_timezone.dart';
import 'package:get/get.dart';
2023-07-24 22:58:25 +03:00
import 'package:internet_connection_checker_plus/internet_connection_checker_plus.dart';
2023-06-17 20:57:57 +03:00
import 'package:isar/isar.dart';
import 'package:path_provider/path_provider.dart';
import 'package:rain/app/modules/home.dart';
import 'package:rain/app/modules/onboarding.dart';
import 'package:rain/theme/theme.dart';
2023-07-17 20:53:23 +03:00
import 'package:time_machine/time_machine.dart';
2023-06-17 20:57:57 +03:00
import 'app/data/weather.dart';
import 'translation/translation.dart';
import 'theme/theme_controller.dart';
import 'package:timezone/data/latest_all.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
late Isar isar;
late Settings settings;
final ValueNotifier<Future<bool>> isDeviceConnectedNotifier =
2023-07-24 22:58:25 +03:00
ValueNotifier(InternetConnection().hasInternetAccess);
2023-06-17 20:57:57 +03:00
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
2023-06-30 21:33:58 +03:00
bool amoledTheme = false;
bool materialColor = false;
2023-07-02 21:53:03 +03:00
Locale locale = const Locale('en', 'US');
2023-07-09 23:41:51 +03:00
int timeRange = 1;
2023-06-29 21:25:00 +03:00
2023-07-04 21:22:29 +03:00
final List appLanguages = [
{'name': 'English', 'locale': const Locale('en', 'US')},
{'name': 'Русский', 'locale': const Locale('ru', 'RU')},
{'name': 'italiano', 'locale': const Locale('it', 'IT')},
{'name': 'Deutsch', 'locale': const Locale('de', 'DE')},
{'name': 'Français', 'locale': const Locale('fr', 'FR')},
{'name': 'Türkçe', 'locale': const Locale('tr', 'TR')},
{'name': 'Brasileiro', 'locale': const Locale('pt', 'BR')},
{'name': 'Español', 'locale': const Locale('es', 'ES')},
{'name': 'Slovenčina', 'locale': const Locale('sk', 'SK')},
{'name': 'Nederlands', 'locale': const Locale('nl', 'NL')},
{'name': 'हिन्दी', 'locale': const Locale('hi', 'IN')},
2023-07-24 20:31:39 +03:00
{'name': 'Română', 'locale': const Locale('ro', 'RO')},
2023-07-25 17:11:22 +03:00
{'name': '中文', 'locale': const Locale('zh', 'CN')},
2023-07-04 21:22:29 +03:00
];
2023-06-17 20:57:57 +03:00
void main() async {
2023-07-17 20:53:23 +03:00
final String timeZoneName;
2023-06-17 20:57:57 +03:00
WidgetsFlutterBinding.ensureInitialized();
2023-06-29 21:25:00 +03:00
SystemChrome.setSystemUIOverlayStyle(
const SystemUiOverlayStyle(
systemNavigationBarColor: Colors.black,
),
);
2023-06-17 20:57:57 +03:00
await isarInit();
2023-07-09 12:56:16 +05:30
if (Platform.isAndroid) {
2023-07-09 23:41:51 +03:00
await setOptimalDisplayMode();
2023-07-09 12:56:16 +05:30
}
2023-07-17 20:53:23 +03:00
if (Platform.isAndroid || Platform.isIOS) {
timeZoneName = await FlutterTimezone.getLocalTimezone();
} else {
timeZoneName = '${DateTimeZone.local}';
}
tz.initializeTimeZones();
tz.setLocalLocation(tz.getLocation(timeZoneName));
2023-06-17 20:57:57 +03:00
Connectivity()
.onConnectivityChanged
.listen((ConnectivityResult result) async {
if (result != ConnectivityResult.none) {
2023-07-24 22:58:25 +03:00
isDeviceConnectedNotifier.value = InternetConnection().hasInternetAccess;
2023-06-17 20:57:57 +03:00
} else {
isDeviceConnectedNotifier.value = Future(() => false);
}
});
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
2023-07-09 12:56:16 +05:30
const DarwinInitializationSettings initializationSettingsDarwin =
DarwinInitializationSettings();
2023-07-17 20:53:23 +03:00
const LinuxInitializationSettings initializationSettingsLinux =
LinuxInitializationSettings(defaultActionName: 'Rain');
2023-07-09 12:56:16 +05:30
const InitializationSettings initializationSettings = InitializationSettings(
2023-07-17 20:53:23 +03:00
android: initializationSettingsAndroid,
iOS: initializationSettingsDarwin,
linux: initializationSettingsLinux,
);
2023-06-17 20:57:57 +03:00
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
2023-06-29 21:25:00 +03:00
runApp(const MyApp());
2023-06-17 20:57:57 +03:00
}
Future<void> setOptimalDisplayMode() async {
final List<DisplayMode> supported = await FlutterDisplayMode.supported;
final DisplayMode active = await FlutterDisplayMode.active;
final List<DisplayMode> sameResolution = supported
.where((DisplayMode m) =>
m.width == active.width && m.height == active.height)
.toList()
..sort((DisplayMode a, DisplayMode b) =>
b.refreshRate.compareTo(a.refreshRate));
final DisplayMode mostOptimalMode =
sameResolution.isNotEmpty ? sameResolution.first : active;
await FlutterDisplayMode.setPreferredMode(mostOptimalMode);
}
Future<void> isarInit() async {
isar = await Isar.open([
SettingsSchema,
MainWeatherCacheSchema,
LocationCacheSchema,
WeatherCardSchema,
], directory: (await getApplicationSupportDirectory()).path);
settings = await isar.settings.where().findFirst() ?? Settings();
2023-07-04 21:22:29 +03:00
if (settings.language == null) {
settings.language = '${Get.deviceLocale}';
isar.writeTxn(() async => isar.settings.put(settings));
}
2023-06-17 20:57:57 +03:00
}
2023-06-29 21:25:00 +03:00
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
static Future<void> updateAppState(
BuildContext context, {
2023-06-30 21:33:58 +03:00
bool? newAmoledTheme,
bool? newMaterialColor,
2023-07-02 21:53:03 +03:00
Locale? newLocale,
2023-07-09 23:41:51 +03:00
int? newTimeRange,
2023-06-29 21:25:00 +03:00
}) async {
final state = context.findAncestorStateOfType<_MyAppState>()!;
2023-06-30 21:33:58 +03:00
if (newAmoledTheme != null) {
state.changeAmoledTheme(newAmoledTheme);
}
if (newMaterialColor != null) {
state.changeMarerialTheme(newMaterialColor);
2023-06-29 21:25:00 +03:00
}
2023-07-02 21:53:03 +03:00
if (newLocale != null) {
state.changeLocale(newLocale);
}
2023-07-09 23:41:51 +03:00
if (newTimeRange != null) {
state.changeTimeRange(newTimeRange);
}
2023-06-29 21:25:00 +03:00
}
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
2023-06-17 20:57:57 +03:00
final themeController = Get.put(ThemeController());
2023-06-30 21:33:58 +03:00
void changeAmoledTheme(bool newAmoledTheme) {
2023-06-29 21:25:00 +03:00
setState(() {
2023-06-30 21:33:58 +03:00
amoledTheme = newAmoledTheme;
});
}
void changeMarerialTheme(bool newMaterialColor) {
setState(() {
materialColor = newMaterialColor;
2023-06-29 21:25:00 +03:00
});
}
2023-07-09 23:41:51 +03:00
void changeTimeRange(int newTimeRange) {
setState(() {
timeRange = newTimeRange;
});
}
2023-07-02 21:53:03 +03:00
void changeLocale(Locale newLocale) {
setState(() {
locale = newLocale;
});
}
2023-06-29 21:25:00 +03:00
@override
void initState() {
2023-06-30 21:33:58 +03:00
amoledTheme = settings.amoledTheme;
materialColor = settings.materialColor;
2023-07-04 21:22:29 +03:00
locale = Locale(
2023-07-09 23:41:51 +03:00
settings.language!.substring(0, 2), settings.language!.substring(3));
timeRange = settings.timeRange ?? 1;
2023-06-29 21:25:00 +03:00
super.initState();
}
2023-06-17 20:57:57 +03:00
@override
Widget build(BuildContext context) {
return DynamicColorBuilder(
builder: (lightColorScheme, darkColorScheme) {
2023-07-19 18:59:53 +03:00
final lightMaterialTheme =
lightTheme(lightColorScheme?.surface, lightColorScheme);
final darkMaterialTheme =
darkTheme(darkColorScheme?.surface, darkColorScheme);
final darkMaterialThemeOled = darkTheme(oledColor, darkColorScheme);
2023-06-17 20:57:57 +03:00
return GetMaterialApp(
themeMode: themeController.theme,
2023-07-19 18:59:53 +03:00
theme: materialColor
? lightColorScheme != null
? lightMaterialTheme
: lightTheme(lightColor, colorSchemeLight)
: lightTheme(lightColor, colorSchemeLight),
darkTheme: amoledTheme
? materialColor
? darkColorScheme != null
? darkMaterialThemeOled
: darkTheme(oledColor, colorSchemeDark)
: darkTheme(oledColor, colorSchemeDark)
: materialColor
? darkColorScheme != null
? darkMaterialTheme
: darkTheme(darkColor, colorSchemeDark)
: darkTheme(darkColor, colorSchemeDark),
2023-06-17 20:57:57 +03:00
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
translations: Translation(),
2023-07-04 21:22:29 +03:00
locale: locale,
2023-06-17 20:57:57 +03:00
fallbackLocale: const Locale('en', 'US'),
supportedLocales: const [
Locale('en', 'US'),
Locale('ru', 'RU'),
Locale('it', 'IT'),
Locale('de', 'DE'),
Locale('fr', 'FR'),
Locale('tr', 'TR'),
Locale('pt', 'BR'),
Locale('es', 'ES'),
Locale('sk', 'SK'),
Locale('nl', 'NL'),
2023-07-24 20:31:39 +03:00
Locale('hi', 'IN'),
2023-07-25 17:11:22 +03:00
Locale('ro', 'RO'),
Locale('zh', 'CN'),
2023-06-17 20:57:57 +03:00
],
debugShowCheckedModeBanner: false,
home: settings.onboard == false
? const OnboardingPage()
: const HomePage(),
);
},
);
}
}