Rain/lib/main.dart

361 lines
12 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';
2023-10-03 19:23:52 +03:00
import 'package:flutter/foundation.dart';
2023-06-17 20:57:57 +03:00
import 'package:flutter/material.dart';
2023-09-01 20:18:40 +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-10-01 22:38:43 +03:00
import 'package:home_widget/home_widget.dart';
2024-04-10 21:25:23 +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';
2023-10-03 19:23:52 +03:00
import 'package:rain/app/controller/controller.dart';
2024-08-03 18:55:26 +03:00
import 'package:rain/app/data/weather.dart';
2024-02-11 22:00:37 +03:00
import 'package:rain/app/modules/geolocation.dart';
2023-06-17 20:57:57 +03:00
import 'package:rain/app/modules/home.dart';
import 'package:rain/app/modules/onboarding.dart';
import 'package:rain/theme/theme.dart';
2024-08-03 18:55:26 +03:00
import 'package:rain/theme/theme_controller.dart';
import 'package:rain/translation/translation.dart';
2024-07-09 23:03:40 +03:00
import 'package:rain/utils/device_info.dart';
2023-07-17 20:53:23 +03:00
import 'package:time_machine/time_machine.dart';
2023-10-12 12:11:20 +05:30
import 'package:timezone/data/latest_all.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
2023-10-03 19:23:52 +03:00
import 'package:workmanager/workmanager.dart';
2023-06-17 20:57:57 +03:00
late Isar isar;
late Settings settings;
2024-02-11 22:00:37 +03:00
late LocationCache locationCache;
2024-04-10 21:25:23 +03:00
final ValueNotifier<Future<bool>> isOnline =
ValueNotifier(InternetConnection().hasInternetAccess);
2023-06-17 20:57:57 +03:00
2023-10-12 16:30:28 +03:00
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
2023-06-17 20:57:57 +03:00
2023-06-30 21:33:58 +03:00
bool amoledTheme = false;
bool materialColor = false;
2024-01-28 20:16:12 +03:00
bool roundDegree = false;
2024-07-10 21:59:36 +03:00
bool largeElement = 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-07-26 22:20:30 +03:00
String timeStart = '09:00';
String timeEnd = '21:00';
2023-10-12 17:30:22 +03:00
String widgetBackgroundColor = '';
2023-10-12 20:48:23 +03:00
String widgetTextColor = '';
2023-06-29 21:25:00 +03:00
2023-07-04 21:22:29 +03:00
final List appLanguages = [
2023-10-23 21:20:41 +03:00
{'name': 'বাংলা', 'locale': const Locale('bn', 'IN')},
{'name': 'Čeština', 'locale': const Locale('cs', 'CZ')},
2024-02-13 22:32:28 +03:00
{'name': 'Dansk', 'locale': const Locale('da', 'DK')},
2023-07-04 21:22:29 +03:00
{'name': 'Deutsch', 'locale': const Locale('de', 'DE')},
2023-10-22 09:34:08 +03:00
{'name': 'English', 'locale': const Locale('en', 'US')},
2023-07-04 21:22:29 +03:00
{'name': 'Español', 'locale': const Locale('es', 'ES')},
2023-10-22 09:34:08 +03:00
{'name': 'Français', 'locale': const Locale('fr', 'FR')},
// {'name': 'Gaeilge', 'locale': const Locale('ga', 'IE')},
2023-07-04 21:22:29 +03:00
{'name': 'हिन्दी', 'locale': const Locale('hi', 'IN')},
2023-10-30 09:40:48 +03:00
{'name': 'Magyar', 'locale': const Locale('hu', 'HU')},
2023-10-22 09:34:08 +03:00
{'name': 'Italiano', 'locale': const Locale('it', 'IT')},
2024-05-31 10:40:10 +03:00
{'name': '한국어', 'locale': const Locale('ko', 'KR')},
2024-03-03 16:11:30 +03:00
{'name': 'فارسی', 'locale': const Locale('fa', 'IR')},
{'name': 'ქართული', 'locale': const Locale('ka', 'GE')},
2023-10-22 09:34:08 +03:00
{'name': 'Nederlands', 'locale': const Locale('nl', 'NL')},
{'name': 'Polski', 'locale': const Locale('pl', 'PL')},
{'name': 'Português (Brasil)', 'locale': const Locale('pt', 'BR')},
2023-07-24 20:31:39 +03:00
{'name': 'Română', 'locale': const Locale('ro', 'RO')},
2023-10-22 09:34:08 +03:00
{'name': 'Русский', 'locale': const Locale('ru', 'RU')},
{'name': 'Slovenčina', 'locale': const Locale('sk', 'SK')},
{'name': 'Türkçe', 'locale': const Locale('tr', 'TR')},
{'name': 'اردو', 'locale': const Locale('ur', 'PK')},
2024-03-10 17:19:04 +08:00
{'name': '中文(简体)', 'locale': const Locale('zh', 'CN')},
2024-03-10 17:22:53 +08:00
{'name': '中文(繁體)', 'locale': const Locale('zh', 'TW')},
2023-07-04 21:22:29 +03:00
];
2023-10-01 22:38:43 +03:00
const String appGroupId = 'DARK NIGHT';
const String androidWidgetName = 'OreoWidget';
2023-10-03 19:23:52 +03:00
@pragma('vm:entry-point')
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) {
return WeatherController().updateWidget();
});
}
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();
2024-04-10 21:25:23 +03:00
Connectivity()
.onConnectivityChanged
.listen((List<ConnectivityResult> result) {
result.contains(ConnectivityResult.none)
? isOnline.value = Future(() => false)
: isOnline.value = InternetConnection().hasInternetAccess;
2023-09-18 20:12:37 +03:00
});
2024-07-09 23:03:40 +03:00
DeviceFeature().init();
2023-07-09 12:56:16 +05:30
if (Platform.isAndroid) {
2023-10-09 11:25:06 +03:00
Workmanager().initialize(callbackDispatcher, isInDebugMode: kDebugMode);
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-09-18 18:33:43 +03:00
await isarInit();
2023-06-17 20:57:57 +03:00
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
2023-10-12 16:30:28 +03:00
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
2023-10-12 16:30:28 +03:00
.where((DisplayMode m) =>
m.width == active.width && m.height == active.height)
2023-06-17 20:57:57 +03:00
.toList()
2023-10-12 16:30:28 +03:00
..sort((DisplayMode a, DisplayMode b) =>
b.refreshRate.compareTo(a.refreshRate));
final DisplayMode mostOptimalMode =
sameResolution.isNotEmpty ? sameResolution.first : active;
2023-06-17 20:57:57 +03:00
await FlutterDisplayMode.setPreferredMode(mostOptimalMode);
}
Future<void> isarInit() async {
isar = await Isar.open([
SettingsSchema,
MainWeatherCacheSchema,
LocationCacheSchema,
WeatherCardSchema,
], directory: (await getApplicationSupportDirectory()).path);
2023-09-18 16:26:08 +03:00
settings = isar.settings.where().findFirstSync() ?? Settings();
2024-02-11 22:00:37 +03:00
locationCache =
isar.locationCaches.where().findFirstSync() ?? LocationCache();
2023-09-28 22:23:36 +03:00
2023-07-04 21:22:29 +03:00
if (settings.language == null) {
settings.language = '${Get.deviceLocale}';
2023-09-18 16:26:08 +03:00
isar.writeTxnSync(() => isar.settings.putSync(settings));
2023-07-04 21:22:29 +03:00
}
2023-09-28 22:23:36 +03:00
if (settings.theme == null) {
settings.theme = 'system';
isar.writeTxnSync(() => isar.settings.putSync(settings));
}
2023-06-17 20:57:57 +03:00
}
2023-06-29 21:25:00 +03:00
class MyApp extends StatefulWidget {
2023-10-22 09:34:08 +03:00
const MyApp({super.key});
2023-06-29 21:25:00 +03:00
static Future<void> updateAppState(
BuildContext context, {
2023-06-30 21:33:58 +03:00
bool? newAmoledTheme,
bool? newMaterialColor,
2024-01-28 20:16:12 +03:00
bool? newRoundDegree,
2024-07-10 21:59:36 +03:00
bool? newLargeElement,
2023-07-02 21:53:03 +03:00
Locale? newLocale,
2023-07-09 23:41:51 +03:00
int? newTimeRange,
2023-07-26 22:20:30 +03:00
String? newTimeStart,
String? newTimeEnd,
2023-10-12 17:30:22 +03:00
String? newWidgetBackgroundColor,
2023-10-12 20:48:23 +03:00
String? newWidgetTextColor,
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
}
2024-01-28 20:16:12 +03:00
if (newRoundDegree != null) {
state.changeRoundDegree(newRoundDegree);
}
2024-07-10 21:59:36 +03:00
if (newLargeElement != null) {
state.changeLargeElement(newLargeElement);
}
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-07-26 22:20:30 +03:00
if (newTimeStart != null) {
state.changeTimeStart(newTimeStart);
}
if (newTimeEnd != null) {
state.changeTimeEnd(newTimeEnd);
}
2023-10-12 17:30:22 +03:00
if (newWidgetBackgroundColor != null) {
state.changeWidgetBackgroundColor(newWidgetBackgroundColor);
}
2023-10-12 20:48:23 +03:00
if (newWidgetTextColor != null) {
state.changeWidgetTextColor(newWidgetTextColor);
}
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
});
}
2024-01-28 20:16:12 +03:00
void changeRoundDegree(bool newRoundDegree) {
setState(() {
roundDegree = newRoundDegree;
});
}
2024-07-10 21:59:36 +03:00
void changeLargeElement(bool newLargeElement) {
setState(() {
largeElement = newLargeElement;
});
}
2023-07-09 23:41:51 +03:00
void changeTimeRange(int newTimeRange) {
setState(() {
timeRange = newTimeRange;
});
}
2023-07-26 22:20:30 +03:00
void changeTimeStart(String newTimeStart) {
setState(() {
timeStart = newTimeStart;
});
}
void changeTimeEnd(String newTimeEnd) {
setState(() {
timeEnd = newTimeEnd;
});
}
2023-07-02 21:53:03 +03:00
void changeLocale(Locale newLocale) {
setState(() {
locale = newLocale;
});
}
2023-10-12 17:30:22 +03:00
void changeWidgetBackgroundColor(String newWidgetBackgroundColor) {
setState(() {
widgetBackgroundColor = newWidgetBackgroundColor;
});
}
2023-10-12 20:48:23 +03:00
void changeWidgetTextColor(String newWidgetTextColor) {
setState(() {
widgetTextColor = newWidgetTextColor;
});
}
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;
2024-01-28 20:16:12 +03:00
roundDegree = settings.roundDegree;
2024-07-10 21:59:36 +03:00
largeElement = settings.largeElement;
2023-10-12 16:30:28 +03:00
locale = Locale(
settings.language!.substring(0, 2), settings.language!.substring(3));
2023-07-09 23:41:51 +03:00
timeRange = settings.timeRange ?? 1;
2023-07-26 22:20:30 +03:00
timeStart = settings.timeStart ?? '09:00';
timeEnd = settings.timeEnd ?? '21:00';
2023-10-12 17:30:22 +03:00
widgetBackgroundColor = settings.widgetBackgroundColor ?? '';
2023-10-12 20:48:23 +03:00
widgetTextColor = settings.widgetTextColor ?? '';
2023-10-09 11:25:06 +03:00
if (Platform.isAndroid) {
HomeWidget.setAppGroupId(appGroupId);
}
2023-06-29 21:25:00 +03:00
super.initState();
}
2023-06-17 20:57:57 +03:00
@override
Widget build(BuildContext context) {
2024-07-09 23:03:40 +03:00
final edgeToEdgeAvailable = DeviceFeature().isEdgeToEdgeAvailable();
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
2024-07-07 14:49:45 +03:00
return GestureDetector(
onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
child: DynamicColorBuilder(
builder: (lightColorScheme, darkColorScheme) {
2024-07-09 23:03:40 +03:00
final lightMaterialTheme = lightTheme(
lightColorScheme?.surface, lightColorScheme, edgeToEdgeAvailable);
final darkMaterialTheme = darkTheme(
darkColorScheme?.surface, darkColorScheme, edgeToEdgeAvailable);
final darkMaterialThemeOled =
darkTheme(oledColor, darkColorScheme, edgeToEdgeAvailable);
2023-07-19 18:59:53 +03:00
2024-07-07 14:49:45 +03:00
return GetMaterialApp(
themeMode: themeController.theme,
theme: materialColor
? lightColorScheme != null
? lightMaterialTheme
2024-07-09 23:03:40 +03:00
: lightTheme(
lightColor, colorSchemeLight, edgeToEdgeAvailable)
: lightTheme(lightColor, colorSchemeLight, edgeToEdgeAvailable),
2024-07-07 14:49:45 +03:00
darkTheme: amoledTheme
? materialColor
? darkColorScheme != null
? darkMaterialThemeOled
2024-07-09 23:03:40 +03:00
: darkTheme(
oledColor, colorSchemeDark, edgeToEdgeAvailable)
: darkTheme(oledColor, colorSchemeDark, edgeToEdgeAvailable)
2024-07-07 14:49:45 +03:00
: materialColor
? darkColorScheme != null
? darkMaterialTheme
2024-07-09 23:03:40 +03:00
: darkTheme(
darkColor, colorSchemeDark, edgeToEdgeAvailable)
: darkTheme(
darkColor, colorSchemeDark, edgeToEdgeAvailable),
2024-07-07 14:49:45 +03:00
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
translations: Translation(),
locale: locale,
fallbackLocale: const Locale('en', 'US'),
supportedLocales:
appLanguages.map((e) => e['locale'] as Locale).toList(),
debugShowCheckedModeBanner: false,
home: settings.onboard
? (locationCache.city == null) ||
(locationCache.district == null) ||
(locationCache.lat == null) ||
(locationCache.lon == null)
? const SelectGeolocation(isStart: true)
: const HomePage()
: const OnBording(),
title: 'Rain',
);
},
),
2023-06-17 20:57:57 +03:00
);
}
}