Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Update utils

vishnukvmda7a162d37505
diff --git a/mobile/packages/utils/lib/date_time_util.dart b/mobile/packages/utils/lib/date_time_util.dart
new file mode 100644
index 0000000000..0d566904a4
--- /dev/null
+++ b/mobile/packages/utils/lib/date_time_util.dart
@@ -0,0 +1,217 @@
+import 'package:intl/intl.dart';
+
+const Set<int> monthWith31Days = {1, 3, 5, 7, 8, 10, 12};
+const Set<int> monthWith30Days = {4, 6, 9, 11};
+Map<int, String> _months = {
+ 1: "Jan",
+ 2: "Feb",
+ 3: "March",
+ 4: "April",
+ 5: "May",
+ 6: "Jun",
+ 7: "July",
+ 8: "Aug",
+ 9: "Sep",
+ 10: "Oct",
+ 11: "Nov",
+ 12: "Dec",
+};
+
+Map<int, String> _fullMonths = {
+ 1: "January",
+ 2: "February",
+ 3: "March",
+ 4: "April",
+ 5: "May",
+ 6: "June",
+ 7: "July",
+ 8: "August",
+ 9: "September",
+ 10: "October",
+ 11: "November",
+ 12: "December",
+};
+
+Map<int, String> _days = {
+ 1: "Mon",
+ 2: "Tue",
+ 3: "Wed",
+ 4: "Thu",
+ 5: "Fri",
+ 6: "Sat",
+ 7: "Sun",
+};
+
+final currentYear = int.parse(DateTime.now().year.toString());
+const searchStartYear = 1970;
+
+//Jun 2022
+String getMonthAndYear(DateTime dateTime) {
+ return "${_months[dateTime.month]!} ${dateTime.year}";
+}
+
+//Thu, 30 Jun
+String getDayAndMonth(DateTime dateTime) {
+ return "${_days[dateTime.weekday]!}, ${dateTime.day} ${_months[dateTime.month]!}";
+}
+
+//30 Jun, 2022
+String getDateAndMonthAndYear(DateTime dateTime) {
+ return "${dateTime.day} ${_months[dateTime.month]!}, ${dateTime.year}";
+}
+
+String getDay(DateTime dateTime) {
+ return _days[dateTime.weekday]!;
+}
+
+String getMonth(DateTime dateTime) {
+ return _months[dateTime.month]!;
+}
+
+String getFullMonth(DateTime dateTime) {
+ return _fullMonths[dateTime.month]!;
+}
+
+String getAbbreviationOfYear(DateTime dateTime) {
+ return (dateTime.year % 100).toString();
+}
+
+//14:32
+String getTime(DateTime dateTime) {
+ final hours =
+ dateTime.hour > 9 ? dateTime.hour.toString() : "0${dateTime.hour}";
+ final minutes =
+ dateTime.minute > 9 ? dateTime.minute.toString() : "0${dateTime.minute}";
+ return "$hours:$minutes";
+}
+
+//11:22 AM
+String getTimeIn12hrFormat(DateTime dateTime) {
+ return DateFormat.jm().format(dateTime);
+}
+
+//Thu, Jun 30, 2022 - 14:32
+String getFormattedTime(DateTime dateTime) {
+ return "${getDay(dateTime)}, ${getMonth(dateTime)} ${dateTime.day}, ${dateTime.year} - ${getTime(dateTime)}";
+}
+
+//30 Jun'22
+String getFormattedDate(DateTime dateTime) {
+ return "${dateTime.day} ${getMonth(dateTime)}'${getAbbreviationOfYear(dateTime)}";
+}
+
+String getFullDate(DateTime dateTime) {
+ return "${getDay(dateTime)}, ${getMonth(dateTime)} ${dateTime.day} ${dateTime.year}";
+}
+
+String daysLeft(int futureTime) {
+ final int daysLeft = ((futureTime - DateTime.now().microsecondsSinceEpoch) /
+ Duration.microsecondsPerDay)
+ .ceil();
+ return '$daysLeft day${daysLeft <= 1 ? "" : "s"}';
+}
+
+String formatDuration(Duration position) {
+ final ms = position.inMilliseconds;
+
+ int seconds = ms ~/ 1000;
+ final int hours = seconds ~/ 3600;
+ seconds = seconds % 3600;
+ final minutes = seconds ~/ 60;
+ seconds = seconds % 60;
+
+ final hoursString = hours >= 10
+ ? '$hours'
+ : hours == 0
+ ? '00'
+ : '0$hours';
+
+ final minutesString = minutes >= 10
+ ? '$minutes'
+ : minutes == 0
+ ? '00'
+ : '0$minutes';
+
+ final secondsString = seconds >= 10
+ ? '$seconds'
+ : seconds == 0
+ ? '00'
+ : '0$seconds';
+
+ final formattedTime =
+ '${hoursString == '00' ? '' : '$hoursString:'}$minutesString:$secondsString';
+
+ return formattedTime;
+}
+
+bool isLeapYear(DateTime dateTime) {
+ final year = dateTime.year;
+ if (year % 4 == 0) {
+ if (year % 100 == 0) {
+ if (year % 400 == 0) {
+ return true;
+ } else {
+ return false;
+ }
+ } else {
+ return true;
+ }
+ } else {
+ return false;
+ }
+}
+
+String getDayTitle(int timestamp) {
+ final date = DateTime.fromMicrosecondsSinceEpoch(timestamp);
+ final now = DateTime.now();
+ var title = getDayAndMonth(date);
+ if (date.year == now.year && date.month == now.month) {
+ if (date.day == now.day) {
+ title = "Today";
+ } else if (date.day == now.day - 1) {
+ title = "Yesterday";
+ }
+ }
+ if (date.year != DateTime.now().year) {
+ title += " ${date.year}";
+ }
+ return title;
+}
+
+String secondsToHHMMSS(int value) {
+ int h, m, s;
+ h = value ~/ 3600;
+ m = ((value - h * 3600)) ~/ 60;
+ s = value - (h * 3600) - (m * 60);
+ final String hourLeft = h.toString().length < 2 ? "0$h" : h.toString();
+
+ final String minuteLeft = m.toString().length < 2 ? "0$m" : m.toString();
+
+ final String secondsLeft = s.toString().length < 2 ? "0$s" : s.toString();
+
+ final String result = "$hourLeft:$minuteLeft:$secondsLeft";
+
+ return result;
+}
+
+bool isValidDate({
+ required int day,
+ required int month,
+ required int year,
+}) {
+ if (day < 0 || day > 31 || month < 0 || month > 12 || year < 0) {
+ return false;
+ }
+ if (monthWith30Days.contains(month) && day > 30) {
+ return false;
+ }
+ if (month == 2) {
+ if (day > 29) {
+ return false;
+ }
+ if (day == 29 && year % 4 != 0) {
+ return false;
+ }
+ }
+ return true;
+}
diff --git a/mobile/packages/utils/lib/platform_util.dart b/mobile/packages/utils/lib/platform_util.dart
index e84349c301..6ad91b0d88 100644
--- a/mobile/packages/utils/lib/platform_util.dart
+++ b/mobile/packages/utils/lib/platform_util.dart
@@ -43,14 +43,14 @@ class PlatformUtil {
if (Platform.isAndroid || Platform.isIOS) {
await FileSaver.instance.saveAs(
name: fileName,
- ext: extension,
+ fileExtension: extension,
bytes: bytes,
mimeType: type,
);
} else {
await FileSaver.instance.saveFile(
name: fileName,
- ext: extension,
+ fileExtension: extension,
bytes: bytes,
mimeType: type,
);
diff --git a/mobile/packages/utils/pubspec.lock b/mobile/packages/utils/pubspec.lock
index ecc483f51a..982e79b216 100644
--- a/mobile/packages/utils/pubspec.lock
+++ b/mobile/packages/utils/pubspec.lock
@@ -224,10 +224,10 @@ packages:
dependency: "direct main"
description:
name: file_saver
- sha256: "017a127de686af2d2fbbd64afea97052d95f2a0f87d19d25b87e097407bf9c1e"
+ sha256: "9d93db09bd4da9e43238f9dd485360fc51a5c138eea5ef5f407ec56e58079ac0"
url: "https://pub.dev"
source: hosted
- version: "0.2.14"
+ version: "0.3.1"
fixnum:
dependency: transitive
description:
@@ -700,18 +700,18 @@ packages:
dependency: "direct main"
description:
name: share_plus
- sha256: fce43200aa03ea87b91ce4c3ac79f0cecd52e2a7a56c7a4185023c271fbfa6da
+ sha256: b2961506569e28948d75ec346c28775bb111986bb69dc6a20754a457e3d97fa0
url: "https://pub.dev"
source: hosted
- version: "10.1.4"
+ version: "11.0.0"
share_plus_platform_interface:
dependency: transitive
description:
name: share_plus_platform_interface
- sha256: cc012a23fc2d479854e6c80150696c4a5f5bb62cb89af4de1c505cf78d0a5d0b
+ sha256: "1032d392bc5d2095a77447a805aa3f804d2ae6a4d5eef5e6ebb3bd94c1bc19ef"
url: "https://pub.dev"
source: hosted
- version: "5.0.2"
+ version: "6.0.0"
shared_preferences:
dependency: transitive
description:
diff --git a/mobile/packages/utils/pubspec.yaml b/mobile/packages/utils/pubspec.yaml
index 648b0f0ac6..3a6a77e4ea 100644
--- a/mobile/packages/utils/pubspec.yaml
+++ b/mobile/packages/utils/pubspec.yaml
@@ -13,7 +13,7 @@ dependencies:
# Core dependencies
archive: ^4.0.7
email_validator: ^3.0.0
- file_saver: ^0.2.14
+ file_saver: ^0.3.0
flutter_email_sender: ^7.0.0
ente_logging:
path: ../../packages/logging
@@ -26,7 +26,7 @@ dependencies:
package_info_plus: ^8.1.1
path: ^1.9.1
path_provider: ^2.1.5
- share_plus: ^10.1.2
+ share_plus: ^11.0.0
url_launcher: ^6.3.1
window_manager: ^0.5.0