11import 'dart:io' ;
22import 'package:file_picker/file_picker.dart' ;
33import 'package:flutter/material.dart' ;
4+ import 'package:flutter/services.dart' ;
45import 'package:flutter_map/flutter_map.dart' ;
56import 'package:fluttertoast/fluttertoast.dart' ;
67import 'package:get/get.dart' ;
@@ -114,6 +115,15 @@ class AddOrUpdateAlarmController extends GetxController {
114115 final RxInt hours = 0. obs, minutes = 0. obs, meridiemIndex = 0. obs;
115116 final List <RxString > meridiem = ['AM' .obs, 'PM' .obs];
116117
118+
119+ TextEditingController inputHrsController = TextEditingController ();
120+ TextEditingController inputMinutesController = TextEditingController ();
121+
122+
123+ final isTimePicker = false .obs;
124+ final isAM = true .obs;
125+ int ? _previousDisplayHour;
126+
117127 Future <List <UserModel ?>> fetchUserDetailsForSharedUsers () async {
118128 List <UserModel ?> userDetails = [];
119129
@@ -891,6 +901,10 @@ class AddOrUpdateAlarmController extends GetxController {
891901 }
892902
893903 // If there's an argument sent, we are in update mode
904+
905+
906+ isTimePicker.value = true ;
907+ initTimeTextField ();
894908 }
895909
896910 void addListeners () {
@@ -1009,6 +1023,8 @@ class AddOrUpdateAlarmController extends GetxController {
10091023 await FirestoreDb .updateAlarm (updatedModel.ownerId, updatedModel);
10101024 }
10111025 }
1026+ inputHrsController.dispose ();
1027+ inputMinutesController.dispose ();
10121028 }
10131029
10141030 AlarmModel updatedAlarmModel () {
@@ -1412,6 +1428,123 @@ class AddOrUpdateAlarmController extends GetxController {
14121428 );
14131429 }
14141430 }
1431+
1432+ void changeDatePicker () {
1433+ isTimePicker.value = ! isTimePicker.value;
1434+ if (isTimePicker.value) {
1435+ initTimeTextField ();
1436+ }
1437+ }
1438+
1439+ void changePeriod (String period) {
1440+ isAM.value = period == 'AM' ;
1441+ }
1442+
1443+ void confirmTimeInput () {
1444+ setTime ();
1445+ changeDatePicker ();
1446+ }
1447+
1448+ void toggleIfAtBoundary () {
1449+ if (! settingsController.is24HrsEnabled.value) {
1450+ final rawHourText = inputHrsController.text.trim ();
1451+ int newHour;
1452+ try {
1453+ newHour = int .parse (rawHourText);
1454+ } catch (e) {
1455+ debugPrint ("toggleIfAtBoundary error parsing hour: $e " );
1456+ return ;
1457+ }
1458+
1459+ if (newHour == 0 ) {
1460+ newHour = 12 ;
1461+ }
1462+ if (_previousDisplayHour != null ) {
1463+ if ((_previousDisplayHour == 11 && newHour == 12 ) ||
1464+ (_previousDisplayHour == 12 && newHour == 11 )) {
1465+ isAM.value = ! isAM.value;
1466+ }
1467+ }
1468+ _previousDisplayHour = newHour;
1469+ }
1470+ }
1471+
1472+ void setTime () {
1473+ selectedTime.value = selectedTime.value;
1474+ toggleIfAtBoundary ();
1475+
1476+ try {
1477+ int hour = int .parse (inputHrsController.text);
1478+ if (! settingsController.is24HrsEnabled.value) {
1479+ if (isAM.value) {
1480+ if (hour == 12 ) hour = 0 ;
1481+ } else {
1482+ if (hour != 12 ) hour = hour + 12 ;
1483+ }
1484+ }
1485+
1486+ int minute = int .parse (inputMinutesController.text);
1487+ final time = TimeOfDay (hour: hour, minute: minute);
1488+ DateTime today = DateTime .now ();
1489+ DateTime tomorrow = today.add (const Duration (days: 1 ));
1490+
1491+ bool isNextDay = (time.hour == today.hour && time.minute < today.minute) || (time.hour < today.hour);
1492+ bool isNextMonth = isNextDay && (today.day > tomorrow.day);
1493+ bool isNextYear = isNextMonth && (today.month > tomorrow.month);
1494+ int day = isNextDay ? tomorrow.day : today.day;
1495+ int month = isNextMonth ? tomorrow.month : today.month;
1496+ int year = isNextYear ? tomorrow.year : today.year;
1497+ selectedTime.value = DateTime (year, month, day, time.hour, time.minute);
1498+
1499+ if (! settingsController.is24HrsEnabled.value) {
1500+ if (selectedTime.value.hour == 0 ) {
1501+ hours.value = 12 ;
1502+ } else if (selectedTime.value.hour > 12 ) {
1503+ hours.value = selectedTime.value.hour - 12 ;
1504+ } else {
1505+ hours.value = selectedTime.value.hour;
1506+ }
1507+ } else {
1508+ hours.value = convert24 (selectedTime.value.hour, meridiemIndex.value);
1509+ }
1510+ minutes.value = selectedTime.value.minute;
1511+ if (selectedTime.value.hour >= 12 ) {
1512+ meridiemIndex.value = 1 ;
1513+ } else {
1514+ meridiemIndex.value = 0 ;
1515+ }
1516+ } catch (e) {
1517+ debugPrint (e.toString ());
1518+ }
1519+ }
1520+
1521+ int convert24 (int value, int meridiemIndex) {
1522+ if (! settingsController.is24HrsEnabled.value) {
1523+ if (meridiemIndex == 0 ) {
1524+ if (value == 12 ) {
1525+ value = value - 12 ;
1526+ }
1527+ } else {
1528+ if (value != 12 ) {
1529+ value = value + 12 ;
1530+ }
1531+ }
1532+ }
1533+ return value;
1534+ }
1535+
1536+ void initTimeTextField () {
1537+ isAM.value = selectedTime.value.hour < 12 ;
1538+
1539+ inputHrsController.text = settingsController.is24HrsEnabled.value
1540+ ? selectedTime.value.hour.toString ()
1541+ : (selectedTime.value.hour == 0
1542+ ? '12'
1543+ : (selectedTime.value.hour > 12
1544+ ? (selectedTime.value.hour - 12 ).toString ()
1545+ : selectedTime.value.hour.toString ()));
1546+ inputMinutesController.text = selectedTime.value.minute.toString ().padLeft (2 , '0' );
1547+ }
14151548}
14161549
14171550 int orderedCountryCode (Country countryA, Country countryB) {
@@ -1421,3 +1554,25 @@ class AddOrUpdateAlarmController extends GetxController {
14211554
14221555 return int .parse (dialCodeA).compareTo (int .parse (dialCodeB));
14231556 }
1557+
1558+ class LimitRange extends TextInputFormatter {
1559+ LimitRange (this .minRange, this .maxRange) : assert (minRange < maxRange);
1560+ final int minRange;
1561+ final int maxRange;
1562+
1563+ @override
1564+ TextEditingValue formatEditUpdate (TextEditingValue oldValue, TextEditingValue newValue) {
1565+ try {
1566+ if (newValue.text.isEmpty) {
1567+ return newValue;
1568+ }
1569+ int value = int .parse (newValue.text);
1570+ if (value < minRange) return TextEditingValue (text: minRange.toString ());
1571+ else if (value > maxRange) return TextEditingValue (text: maxRange.toString ());
1572+ return newValue;
1573+ } catch (e) {
1574+ debugPrint (e.toString ());
1575+ return newValue;
1576+ }
1577+ }
1578+ }
0 commit comments