From de1d6c85e5014691c8a148ae9f7fcc23aa0e38a8 Mon Sep 17 00:00:00 2001 From: NaggaDIM Date: Mon, 16 Dec 2024 02:35:30 +0300 Subject: [PATCH] temp --- android/settings.gradle | 4 +- lib/application.dart | 9 ++ .../controllers/application_controller.dart | 30 ++++- .../calendar_schedule_controller.dart | 24 ++++ .../controllers/schedule_controller.dart | 14 +- lib/application/services/schedule.dart | 69 ++++++++-- lib/main.dart | 6 +- .../components/current_status_component.dart | 10 +- lib/ui/screens/calendar_screen.dart | 121 ++++++++++++++++++ lib/ui/screens/home_screen.dart | 12 +- pubspec.lock | 90 ++++++++++++- pubspec.yaml | 1 + 12 files changed, 351 insertions(+), 39 deletions(-) create mode 100644 lib/application/controllers/calendar_schedule_controller.dart create mode 100644 lib/ui/screens/calendar_screen.dart diff --git a/android/settings.gradle b/android/settings.gradle index 1d6d19b..5802c22 100644 --- a/android/settings.gradle +++ b/android/settings.gradle @@ -19,8 +19,8 @@ pluginManagement { plugins { id "dev.flutter.flutter-plugin-loader" version "1.0.0" - id "com.android.application" version "7.3.0" apply false - id "org.jetbrains.kotlin.android" version "1.7.10" apply false + id "com.android.application" version "7.3.1" apply false + id "org.jetbrains.kotlin.android" version "2.1.0" apply false } include ":app" diff --git a/lib/application.dart b/lib/application.dart index 7d2c1e7..74e5808 100644 --- a/lib/application.dart +++ b/lib/application.dart @@ -1,5 +1,9 @@ +import 'dart:developer'; + import 'package:flutter/material.dart'; import 'package:get/get.dart'; +import 'package:get_storage/get_storage.dart'; +import 'package:reverse_nn/application/controllers/application_controller.dart'; import 'package:reverse_nn/pages/home_page.dart'; import 'package:reverse_nn/ui/screens/home_screen.dart'; @@ -8,6 +12,10 @@ class ReversNNApplication extends StatelessWidget { @override Widget build(BuildContext context) { + final ApplicationController controller = Get.put(ApplicationController()); + final themeMode = controller.getThemeModeFromStorage(); + controller.setThemeMode(themeMode); + return GetMaterialApp( title: 'Реверс НН', theme: ThemeData( @@ -18,6 +26,7 @@ class ReversNNApplication extends StatelessWidget { colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal, brightness: Brightness.dark), useMaterial3: true, ), + themeMode: themeMode, home: const HomeScreen(), defaultTransition: Transition.noTransition, diff --git a/lib/application/controllers/application_controller.dart b/lib/application/controllers/application_controller.dart index 8597bb8..81c3c42 100644 --- a/lib/application/controllers/application_controller.dart +++ b/lib/application/controllers/application_controller.dart @@ -2,18 +2,36 @@ import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; +import 'package:get_storage/get_storage.dart'; class ApplicationController extends GetxController { + final _storage = GetStorage(); + IconData getThemeIcon() => Get.isDarkMode ? Icons.light_mode : Icons.dark_mode; - void toggleTheme() { - Get.changeThemeMode( - Get.isDarkMode - ? ThemeMode.light - : ThemeMode.dark - ); + ThemeMode getThemeModeFromStorage() { + ThemeMode mode = ThemeMode.system; + final themeKey = _storage.read('current_theme_mode'); + + if(themeKey == 'light') { mode = ThemeMode.light; } + if(themeKey == 'dark') { mode = ThemeMode.dark; } + + return mode; + } + + void setThemeMode(ThemeMode themeMode) { + Get.changeThemeMode(themeMode); update(); } + + void toggleTheme() { + _storage.write('current_theme_mode', Get.isDarkMode ? 'light' : 'dark'); + setThemeMode( + Get.isDarkMode + ? ThemeMode.light + : ThemeMode.dark + ); + } } \ No newline at end of file diff --git a/lib/application/controllers/calendar_schedule_controller.dart b/lib/application/controllers/calendar_schedule_controller.dart new file mode 100644 index 0000000..213e1fe --- /dev/null +++ b/lib/application/controllers/calendar_schedule_controller.dart @@ -0,0 +1,24 @@ +import 'package:get/get.dart'; +import 'package:reverse_nn/application/services/schedule.dart'; +import 'package:reverse_nn/ui/screens/calendar_screen.dart'; + +class CalendarScheduleController extends GetxController { + Rx date = DateTime.now().copyWith(hour: 12, minute: 00).obs; + RxBool loading = false.obs; + Rx>?> schedule = null.obs; + + @override void onReady() { + super.onReady(); + loadSchedule(date.value); + } + + void loadSchedule(DateTime date) async { + loading = true.obs; update(); + schedule = (await ScheduleService().getScheduleByDate(date)).obs; + loading = false.obs; update(); + } + + static void openScreen() async { + Get.to(() => const CalendarScreen()); + } +} \ No newline at end of file diff --git a/lib/application/controllers/schedule_controller.dart b/lib/application/controllers/schedule_controller.dart index 8462e68..8bfeb17 100644 --- a/lib/application/controllers/schedule_controller.dart +++ b/lib/application/controllers/schedule_controller.dart @@ -1,3 +1,5 @@ +import 'dart:developer'; + import 'package:get/get.dart'; import 'package:reverse_nn/application/services/schedule.dart'; @@ -6,9 +8,13 @@ class ScheduleController extends GetxController { @override void onReady() { super.onReady(); - ScheduleService().getCurrentStatus().then((currentSchedule) { - this.currentSchedule = currentSchedule.obs; - update(); - }); + updateCurrentSchedule(); + } + + void updateCurrentSchedule() async { + currentSchedule = (await ScheduleService().getCurrentStatus()).obs; + update(); + + Future.delayed(const Duration(seconds: 5), updateCurrentSchedule); } } \ No newline at end of file diff --git a/lib/application/services/schedule.dart b/lib/application/services/schedule.dart index 15fbf82..c65036c 100644 --- a/lib/application/services/schedule.dart +++ b/lib/application/services/schedule.dart @@ -1,6 +1,7 @@ import 'dart:core'; import 'dart:convert'; import 'dart:developer'; +import 'package:flutter/material.dart'; import 'package:intl/intl.dart' show DateFormat; import 'package:flutter/services.dart'; @@ -85,7 +86,31 @@ class ScheduleService { if(scheduleKey == null) return null; - return await getScheduleByKey(scheduleKey); + List>? schedule = await getScheduleByKey(scheduleKey); + if(schedule == null) return null; + + int durationOffset = 0; + for (var i = 0; i < schedule.length; i++) { + final scheduleItem = schedule[i]; + final int duration = (scheduleItem['duration'] as int); + final DateTime start = datetimeStartWithOffset.copyWith().add(Duration(minutes: durationOffset)); + DateTime end = datetimeStartWithOffset.copyWith().add(Duration(minutes: durationOffset + duration)); + durationOffset += duration; + + bool showEndDate = false; + if(i == (schedule.length -1)) { + final scheduleNextDay = await getScheduleByDate(datetime.copyWith().add(const Duration(days: 1))); + final firstScheduleElement = scheduleNextDay?[0]; + if(firstScheduleElement != null && (firstScheduleElement['direction'] as String) == (scheduleItem['direction'] as String)) { + end = end.add(Duration(minutes: firstScheduleElement['duration'] as int)); + showEndDate = true; + } + } + + schedule[i]['start'] = start; + schedule[i]['end'] = end; + } + return schedule; } Future?> getCurrentStatus() async { @@ -101,32 +126,50 @@ class ScheduleService { } int durationOffset = 0; - for (final Map scheduleItem in schedule) { + for (var i = 0; i < schedule.length; i++) { + final scheduleItem = schedule[i]; final int duration = (scheduleItem['duration'] as int); final DateTime start = datetimeStartWithOffset.copyWith().add(Duration(minutes: durationOffset)); - final DateTime end = datetimeStartWithOffset.copyWith().add(Duration(minutes: durationOffset + duration)); + DateTime end = datetimeStartWithOffset.copyWith().add(Duration(minutes: durationOffset + duration)); durationOffset += duration; + bool showEndDate = false; + + if(i == (schedule.length -1)) { + final scheduleNextDay = await getScheduleByDate(now.copyWith().add(const Duration(days: 1))); + final firstScheduleElement = scheduleNextDay?[0]; + if(firstScheduleElement != null && (firstScheduleElement['direction'] as String) == (scheduleItem['direction'] as String)) { + end = end.add(Duration(minutes: firstScheduleElement['duration'] as int)); + showEndDate = true; + } + } + if(now.isAfter(start) && now.isBefore(end)) { return { "direction": scheduleItem['direction'] as String, "start": start, - "end": end + "end": end, + "need_show_end_date": showEndDate, }; } } return null; + } + static String formatDirection(String direction) { + switch(direction) { + case 'in_city': return 'В город'; + case 'out_city': return 'Из города'; + default: return 'Переключение'; + } + } - - // final datetimeStartWithOffset = now.copyWith(hour: 0, minute: 0, second: 0, microsecond: 0, millisecond: 0) - // .add(dayOffset); - // - // final DateTime usedScheduleDatetime = now.isAfter(datetimeStartWithOffset) - // ? now - // : now.copyWith().subtract(const Duration(days: 1)); - - + static IconData getIconByDirection(String? direction) { + switch(direction) { + case 'in_city': return Icons.input_outlined; + case 'out_city': return Icons.output_outlined; + default: return Icons.change_circle_outlined; + } } } \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index e528b7d..a66286f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,10 @@ import 'package:flutter/material.dart'; +import 'package:get_storage/get_storage.dart'; import 'package:reverse_nn/application.dart'; -void main() => runApp(const ReversNNApplication()); +void main() async { + await GetStorage.init(); + runApp(const ReversNNApplication()); +} diff --git a/lib/ui/components/current_status_component.dart b/lib/ui/components/current_status_component.dart index 559f36c..bd16bcb 100644 --- a/lib/ui/components/current_status_component.dart +++ b/lib/ui/components/current_status_component.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:intl/intl.dart' show DateFormat; import 'package:reverse_nn/application/controllers/schedule_controller.dart'; +import 'package:reverse_nn/application/services/schedule.dart'; class CurrentStatusComponent extends StatelessWidget { const CurrentStatusComponent({super.key}); @@ -22,15 +23,16 @@ class CurrentStatusComponent extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center, children: [ - const Icon(Icons.exit_to_app, size: 96), + Icon(ScheduleService.getIconByDirection(scheduleController.currentSchedule.value?['direction']), size: 96), const SizedBox(width: 16), Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ if(scheduleController.currentSchedule.value != null) ...[ - Text(scheduleController.currentSchedule.value?['direction'] ?? '', style: Theme.of(context).textTheme.headlineLarge), - Text('C ${formatDate(scheduleController.currentSchedule.value?['start'])} До ${formatDate(scheduleController.currentSchedule.value!['end'])}', style: Theme.of(context).textTheme.titleMedium) + Text(ScheduleService.formatDirection(scheduleController.currentSchedule.value?['direction'] ?? ''), style: Theme.of(context).textTheme.headlineLarge), + Text('C\t\t\t\t${formatDate(scheduleController.currentSchedule.value?['start'], showDate: scheduleController.currentSchedule.value!['need_show_end_date'])}', style: Theme.of(context).textTheme.titleMedium), + Text('До\t${formatDate(scheduleController.currentSchedule.value!['end'], showDate: scheduleController.currentSchedule.value!['need_show_end_date'])}', style: Theme.of(context).textTheme.titleMedium), ] ], ) @@ -42,6 +44,6 @@ class CurrentStatusComponent extends StatelessWidget { ); } - String formatDate(DateTime date) => DateFormat('hh:mm').format(date); + String formatDate(DateTime date, {bool showDate = false}) => DateFormat(showDate ? 'hh:mm (dd.MM.yyyy)' : 'hh:mm').format(date); } \ No newline at end of file diff --git a/lib/ui/screens/calendar_screen.dart b/lib/ui/screens/calendar_screen.dart new file mode 100644 index 0000000..1c09fd2 --- /dev/null +++ b/lib/ui/screens/calendar_screen.dart @@ -0,0 +1,121 @@ +import 'package:flutter/material.dart'; +import 'package:get/get.dart'; +import 'package:reverse_nn/application/controllers/calendar_schedule_controller.dart'; +import 'package:reverse_nn/application/services/schedule.dart'; +import 'package:reverse_nn/ui/layouts/application_layout.dart'; +import 'package:intl/intl.dart' show DateFormat; + +class CalendarScreen extends StatelessWidget { + const CalendarScreen({super.key}); + + @override + Widget build(BuildContext context) { + final CalendarScheduleController controller = Get.put(CalendarScheduleController()); + + return ApplicationLayout( + body: RefreshIndicator( + onRefresh: () async { controller.loadSchedule(controller.date.value); }, + child: SingleChildScrollView( + physics: const AlwaysScrollableScrollPhysics(), + + child: GetBuilder( + builder: (controller) { + return Padding( + padding: const EdgeInsets.all(8.0), + child: Column( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + _SelectedDateWidget(date: controller.date.value), + + if(controller.loading.value) const Padding( + padding: EdgeInsets.all(40), + child: Center(child: CircularProgressIndicator()) + ), + + if(!controller.loading.value && controller.schedule.value != null) ListView.builder( + shrinkWrap: true, + itemBuilder: (_, index) => _ScheduleItemWidget(item: controller.schedule.value![index]), + itemCount: controller.schedule.value!.length + ), + + + + // if(!controller.loading.value && controller.schedule.value != null) ...controller.schedule.value!.map((element) { + // return _ScheduleItemWidget(item: element); + // }), + ], + ), + ); + } + ), + ), + ), + ); + } +} + +class _SelectedDateWidget extends StatelessWidget { + final DateTime date; + const _SelectedDateWidget({super.key, required this.date}); + + @override + Widget build(BuildContext context) { + return Container( + padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 10), + decoration: BoxDecoration( + color: Theme.of(context).colorScheme.primaryContainer, + borderRadius: BorderRadius.circular(10), + ), + alignment: Alignment.center, + child: Text( + formatDate(date), + style: Theme.of(context).textTheme.displayMedium, + ), + ); + } + + String formatDate(DateTime date) { + return DateFormat('dd.MM.yyyy').format(date); + } +} + +class _ScheduleItemWidget extends StatelessWidget { + final Map item; + const _ScheduleItemWidget({super.key, required this.item}); + + @override + Widget build(BuildContext context) { + return Container( + margin: const EdgeInsets.only(top: 10), + padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 10), + decoration: BoxDecoration( + color: Theme.of(context).colorScheme.secondaryContainer, + borderRadius: BorderRadius.circular(10), + ), + alignment: Alignment.center, + child: Row( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + Icon(ScheduleService.getIconByDirection(item['direction'] as String), size: 56), + Expanded(child: Column( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Text(ScheduleService.formatDirection(item['direction'] ?? ''), style: Theme.of(context).textTheme.headlineLarge), + Text('C\t\t\t\t${formatDate(item['start'])}', style: Theme.of(context).textTheme.titleMedium), + Text('До\t${formatDate(item['end'])}', style: Theme.of(context).textTheme.titleMedium), + ], + )) + ], + ), + ); + } + + String formatDate(DateTime date) { + return DateFormat('dd.MM.yyyy').format(date); + } +} + + diff --git a/lib/ui/screens/home_screen.dart b/lib/ui/screens/home_screen.dart index 56bbd10..367bd1e 100644 --- a/lib/ui/screens/home_screen.dart +++ b/lib/ui/screens/home_screen.dart @@ -2,6 +2,7 @@ import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; +import 'package:reverse_nn/application/controllers/calendar_schedule_controller.dart'; import 'package:reverse_nn/application/controllers/home_controller.dart'; import 'package:reverse_nn/application/services/schedule.dart'; import 'package:reverse_nn/ui/components/current_status_component.dart'; @@ -28,14 +29,9 @@ class HomeScreen extends GetWidget { mainAxisSpacing: 10, crossAxisSpacing: 10, ), - children: [ - GridMenuItem(icon: Icons.calendar_month, label: 'Расписание', onTap: () { - // log(DateTime(2024, 8, 1, 0, 0, 0).toIso8601String()); - ScheduleService().getCurrentStatus().then((val) { - log(val.toString()); - }); - }), - const GridMenuItem(icon: Icons.monetization_on, label: 'Поддержать автора'), + children: const [ + GridMenuItem(icon: Icons.calendar_month, label: 'Расписание', onTap: CalendarScheduleController.openScreen), + // const GridMenuItem(icon: Icons.monetization_on, label: 'Поддержать автора'), // GridMenuItem(), // GridMenuItem(), ] diff --git a/pubspec.lock b/pubspec.lock index 9090701..6d4926c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -97,6 +97,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.3.1" + ffi: + dependency: transitive + description: + name: ffi + sha256: "16ed7b077ef01ad6170a3d0c57caa4a112a38d7a2ed5602e0aca9ca6f3d98da6" + url: "https://pub.dev" + source: hosted + version: "2.1.3" flutter: dependency: "direct main" description: flutter @@ -131,6 +139,14 @@ packages: url: "https://pub.dev" source: hosted version: "4.6.6" + get_storage: + dependency: "direct main" + description: + name: get_storage + sha256: "39db1fffe779d0c22b3a744376e86febe4ade43bf65e06eab5af707dc84185a2" + url: "https://pub.dev" + source: hosted + version: "2.1.1" http: dependency: transitive description: @@ -235,6 +251,54 @@ packages: url: "https://pub.dev" source: hosted version: "1.9.0" + path_provider: + dependency: transitive + description: + name: path_provider + sha256: "50c5dd5b6e1aaf6fb3a78b33f6aa3afca52bf903a8a5298f53101fdaee55bbcd" + url: "https://pub.dev" + source: hosted + version: "2.1.5" + path_provider_android: + dependency: transitive + description: + name: path_provider_android + sha256: "4adf4fd5423ec60a29506c76581bc05854c55e3a0b72d35bb28d661c9686edf2" + url: "https://pub.dev" + source: hosted + version: "2.2.15" + path_provider_foundation: + dependency: transitive + description: + name: path_provider_foundation + sha256: "4843174df4d288f5e29185bd6e72a6fbdf5a4a4602717eed565497429f179942" + url: "https://pub.dev" + source: hosted + version: "2.4.1" + path_provider_linux: + dependency: transitive + description: + name: path_provider_linux + sha256: f7a1fe3a634fe7734c8d3f2766ad746ae2a2884abe22e241a8b301bf5cac3279 + url: "https://pub.dev" + source: hosted + version: "2.2.1" + path_provider_platform_interface: + dependency: transitive + description: + name: path_provider_platform_interface + sha256: "88f5779f72ba699763fa3a3b06aa4bf6de76c8e5de842cf6f29e2e06476c2334" + url: "https://pub.dev" + source: hosted + version: "2.1.2" + path_provider_windows: + dependency: transitive + description: + name: path_provider_windows + sha256: bd6f00dbd873bfb70d0761682da2b3a2c2fccc2b9e84c495821639601d81afe7 + url: "https://pub.dev" + source: hosted + version: "2.3.0" petitparser: dependency: transitive description: @@ -243,6 +307,22 @@ packages: url: "https://pub.dev" source: hosted version: "6.0.2" + platform: + dependency: transitive + description: + name: platform + sha256: "5d6b1b0036a5f331ebc77c850ebc8506cbc1e9416c27e59b439f917a902a4984" + url: "https://pub.dev" + source: hosted + version: "3.1.6" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02" + url: "https://pub.dev" + source: hosted + version: "2.1.8" sky_engine: dependency: transitive description: flutter @@ -336,6 +416,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.5.1" + xdg_directories: + dependency: transitive + description: + name: xdg_directories + sha256: "7a3f37b05d989967cdddcbb571f1ea834867ae2faa29725fd085180e0883aa15" + url: "https://pub.dev" + source: hosted + version: "1.1.0" xml: dependency: transitive description: @@ -354,4 +442,4 @@ packages: version: "3.1.2" sdks: dart: ">=3.5.0 <4.0.0" - flutter: ">=3.18.0-18.0.pre.54" + flutter: ">=3.24.0" diff --git a/pubspec.yaml b/pubspec.yaml index e4e4ca8..2cfed19 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,6 +38,7 @@ dependencies: cupertino_icons: ^1.0.6 get: ^4.6.6 intl: ^0.20.0 + get_storage: ^2.1.1 dev_dependencies: flutter_test: