diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a24917..1ccb3e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.1.0 + +- Added `full date pattern` (only available for iOS, macOS and web) +- Fix returning empty string `""` instead of `null` on windows & linux when pattern is unavailable + ## 1.0.0 **BREAKING CHANGES**: diff --git a/README.md b/README.md index a4cac01..3d5d544 100644 --- a/README.md +++ b/README.md @@ -81,11 +81,13 @@ Future main() async { final datePattern = await format.getDatePattern(); final mediumDatePattern = await format.getMediumDatePattern(); final longDatePattern = await format.getLongDatePattern(); + final fullDatePattern = await format.getFullDatePattern(); // available only on iOS, macOS and web final timePattern = await format.getTimePattern(); print(datePattern); // e.g. "M/d/yy" print(mediumDatePattern); // e.g. "MMM d,y" print(longDatePattern); // e.g. "MMMM d,y" + print(fullDatePattern); // e.g. "EEEE, MMMM d, y" print(timePattern); // e.g. "HH:mm" } ``` diff --git a/android/src/main/kotlin/com/dominikkrajcer/system_date_time_format/SystemDateTimeFormatPlugin.kt b/android/src/main/kotlin/com/dominikkrajcer/system_date_time_format/SystemDateTimeFormatPlugin.kt index eeeca07..285ceb6 100644 --- a/android/src/main/kotlin/com/dominikkrajcer/system_date_time_format/SystemDateTimeFormatPlugin.kt +++ b/android/src/main/kotlin/com/dominikkrajcer/system_date_time_format/SystemDateTimeFormatPlugin.kt @@ -31,6 +31,7 @@ class SystemDateTimeFormatPlugin : FlutterPlugin, MethodCallHandler { "getDateFormat" -> result.success(getDateFormat()) "getMediumDateFormat" -> result.success(getMediumDateFormat()) "getLongDateFormat" -> result.success(getLongDateFormat()) + "getFullDateFormat" -> result.success(null) "getTimeFormat" -> result.success(getTimeFormat()) else -> result.notImplemented() } diff --git a/example/lib/fallbacks.dart b/example/lib/fallbacks.dart index e6af717..65ffe87 100644 --- a/example/lib/fallbacks.dart +++ b/example/lib/fallbacks.dart @@ -4,5 +4,6 @@ abstract class Fallbacks { static const datePattern = 'M/d/yy'; static const mediumDatePattern = 'MMM d,y'; static const longDatePattern = 'MMMM d,y'; + static const fullDatePattern = 'EEEE, MMMM d, y'; static const timePattern = 'h:mm a'; } diff --git a/example/lib/main.dart b/example/lib/main.dart index 7db6368..a5003b0 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -19,13 +19,16 @@ class App extends StatelessWidget { patterns.mediumDatePattern ?? Fallbacks.mediumDatePattern; final longDatePattern = patterns.longDatePattern ?? Fallbacks.longDatePattern; + final fullDatePattern = + patterns.fullDatePattern ?? Fallbacks.fullDatePattern; final timePattern = patterns.timePattern ?? Fallbacks.timePattern; final rows = { - 'System short date format pattern:': datePattern, - 'System medium date format pattern:': mediumDatePattern, - 'System long date format pattern:': longDatePattern, - 'System time format pattern:': timePattern, + 'Short date format pattern:': datePattern, + 'Medium date format pattern:': mediumDatePattern, + 'Long date format pattern:': longDatePattern, + 'Full date format pattern:': fullDatePattern, + 'Time format pattern:': timePattern, }; return MaterialApp( diff --git a/example/macos/Podfile.lock b/example/macos/Podfile.lock index 6daf9e3..d2946d9 100644 --- a/example/macos/Podfile.lock +++ b/example/macos/Podfile.lock @@ -19,4 +19,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 353c8bcc5d5b0994e508d035b5431cfe18c1dea7 -COCOAPODS: 1.12.0 +COCOAPODS: 1.13.0 diff --git a/example/macos/Runner.xcodeproj/project.pbxproj b/example/macos/Runner.xcodeproj/project.pbxproj index 680bd94..e57eb6f 100644 --- a/example/macos/Runner.xcodeproj/project.pbxproj +++ b/example/macos/Runner.xcodeproj/project.pbxproj @@ -203,7 +203,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0920; - LastUpgradeCheck = 1300; + LastUpgradeCheck = 1430; ORGANIZATIONNAME = ""; TargetAttributes = { 33CC10EC2044A3C60003C045 = { diff --git a/example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme index 000e6b7..0c5a967 100644 --- a/example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme +++ b/example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme @@ -1,6 +1,6 @@ (); - expect(rowItems.length, 4); + expect(rowItems.length, 5); expect(rowItems[0].value, Stubs.datePattern); expect(rowItems[1].value, Stubs.mediumDatePattern); expect(rowItems[2].value, Stubs.longDatePattern); - expect(rowItems[3].value, Stubs.timePattern); + expect(rowItems[3].value, Stubs.fullDatePattern); + expect(rowItems[4].value, Stubs.timePattern); }); } diff --git a/ios/Classes/SwiftSystemDateTimeFormatPlugin.swift b/ios/Classes/SwiftSystemDateTimeFormatPlugin.swift index d9390f4..bb5eb84 100644 --- a/ios/Classes/SwiftSystemDateTimeFormatPlugin.swift +++ b/ios/Classes/SwiftSystemDateTimeFormatPlugin.swift @@ -16,6 +16,8 @@ public class SwiftSystemDateTimeFormatPlugin: NSObject, FlutterPlugin { result(getMediumDateFormat()) case "getLongDateFormat": result(getLongDateFormat()) + case "getFullDateFormat": + result(getFullDateFormat()) case "getTimeFormat": result(getTimeFormat()) default: @@ -41,6 +43,12 @@ public class SwiftSystemDateTimeFormatPlugin: NSObject, FlutterPlugin { return dateFormatter.dateFormat } + private func getFullDateFormat() -> String { + let dateFormatter = DateFormatter() + dateFormatter.dateStyle = .full + return dateFormatter.dateFormat + } + private func getTimeFormat() -> String { let dateFormatter = DateFormatter() dateFormatter.timeStyle = .short diff --git a/lib/src/extensions/extensions.dart b/lib/src/extensions/extensions.dart new file mode 100644 index 0000000..269137d --- /dev/null +++ b/lib/src/extensions/extensions.dart @@ -0,0 +1,3 @@ +extension NullableStringExtensions on String? { + bool get isNullOrEmpty => this == null || this!.isEmpty; +} diff --git a/lib/src/patterns.dart b/lib/src/patterns.dart index dc89901..97a41cc 100644 --- a/lib/src/patterns.dart +++ b/lib/src/patterns.dart @@ -3,12 +3,14 @@ class Patterns { this.datePattern, this.mediumDatePattern, this.longDatePattern, + this.fullDatePattern, this.timePattern, }); final String? datePattern; final String? mediumDatePattern; final String? longDatePattern; + final String? fullDatePattern; final String? timePattern; @override @@ -19,6 +21,7 @@ class Patterns { datePattern == other.datePattern && mediumDatePattern == other.mediumDatePattern && longDatePattern == other.longDatePattern && + fullDatePattern == other.fullDatePattern && timePattern == other.timePattern; @override @@ -26,5 +29,6 @@ class Patterns { datePattern.hashCode ^ mediumDatePattern.hashCode ^ longDatePattern.hashCode ^ + fullDatePattern.hashCode ^ timePattern.hashCode; } diff --git a/lib/src/system_date_time_format_method_channel.dart b/lib/src/system_date_time_format_method_channel.dart index 9118264..9cfa0aa 100644 --- a/lib/src/system_date_time_format_method_channel.dart +++ b/lib/src/system_date_time_format_method_channel.dart @@ -31,6 +31,13 @@ class MethodChannelSystemDateTimeFormat return methodChannel.invokeMethod('getLongDateFormat'); } + /// Invokes "getFullDateFormat" method on system_date_time_format [methodChannel] + /// returning fullDateFormat + @override + Future getFullDatePattern() { + return methodChannel.invokeMethod('getFullDateFormat'); + } + /// Invokes "getTimeFormat" method on system_date_time_format [methodChannel] /// returning timeFormat @override diff --git a/lib/src/system_date_time_format_platform_interface.dart b/lib/src/system_date_time_format_platform_interface.dart index 0af9156..f74e80c 100644 --- a/lib/src/system_date_time_format_platform_interface.dart +++ b/lib/src/system_date_time_format_platform_interface.dart @@ -33,6 +33,9 @@ abstract class SystemDateTimeFormatPlatformInterface extends PlatformInterface { /// Method signature for returning long version of date format pattern Future getLongDatePattern(); + /// Method signature for returning full version of date format pattern + Future getFullDatePattern(); + /// Method signature for returning time format pattern Future getTimePattern(); } diff --git a/lib/system_date_time_format.dart b/lib/system_date_time_format.dart index 5f32936..4585eea 100644 --- a/lib/system_date_time_format.dart +++ b/lib/system_date_time_format.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:system_date_time_format/src/errors/sdtf_scope_not_found_error.dart'; +import 'package:system_date_time_format/src/extensions/extensions.dart'; import 'package:system_date_time_format/src/patterns.dart'; import 'package:system_date_time_format/src/widgets/sdtf_scope_inherited.dart'; @@ -18,28 +19,48 @@ class SystemDateTimeFormat { static final SystemDateTimeFormat _instance = SystemDateTimeFormat._(); /// Returns a short version of date pattern. + /// Available on iOS, macOS, android, web, windows and linux /// May throw [PlatformException] from [MethodChannel]. - Future getDatePattern() { - return SystemDateTimeFormatPlatformInterface.instance.getDatePattern(); + Future getDatePattern() async { + final pattern = + await SystemDateTimeFormatPlatformInterface.instance.getDatePattern(); + return pattern.isNullOrEmpty ? null : pattern; } /// Returns a medium version of date pattern. + /// Available on iOS, macOS, android and web /// May throw [PlatformException] from [MethodChannel]. - Future getMediumDatePattern() { - return SystemDateTimeFormatPlatformInterface.instance + Future getMediumDatePattern() async { + final pattern = await SystemDateTimeFormatPlatformInterface.instance .getMediumDatePattern(); + return pattern.isNullOrEmpty ? null : pattern; } /// Returns a long version of date pattern. + /// Available on iOS, macOS, android, web, windows and linux /// May throw [PlatformException] from [MethodChannel]. - Future getLongDatePattern() { - return SystemDateTimeFormatPlatformInterface.instance.getLongDatePattern(); + Future getLongDatePattern() async { + final pattern = await SystemDateTimeFormatPlatformInterface.instance + .getLongDatePattern(); + return pattern.isNullOrEmpty ? null : pattern; + } + + /// Returns a full version of date pattern. + /// Available on iOS, macOS and web + /// May throw [PlatformException] from [MethodChannel]. + Future getFullDatePattern() async { + final pattern = await SystemDateTimeFormatPlatformInterface.instance + .getFullDatePattern(); + return pattern.isNullOrEmpty ? null : pattern; } /// Returns time pattern. + /// Available on iOS, macOS, android, web, windows and linux /// May throw [PlatformException] from [MethodChannel]. - Future getTimePattern() { - return SystemDateTimeFormatPlatformInterface.instance.getTimePattern(); + Future getTimePattern() async { + final pattern = + await SystemDateTimeFormatPlatformInterface.instance.getTimePattern(); + return pattern.isNullOrEmpty ? null : pattern; } /// Returns all available date & time patterns. @@ -49,17 +70,18 @@ class SystemDateTimeFormat { datePattern: await getDatePattern(), mediumDatePattern: await getMediumDatePattern(), longDatePattern: await getLongDatePattern(), + fullDatePattern: await getFullDatePattern(), timePattern: await getTimePattern(), ); } static Patterns of(BuildContext context) { - final sDTFScope = + final scope = context.dependOnInheritedWidgetOfExactType(); - if (sDTFScope == null) { + if (scope == null) { throw SDTFScopeNotFoundError(context.widget.runtimeType); } - return sDTFScope.patterns; + return scope.patterns; } } diff --git a/lib/system_date_time_format_web.dart b/lib/system_date_time_format_web.dart index eb81936..3f89055 100644 --- a/lib/system_date_time_format_web.dart +++ b/lib/system_date_time_format_web.dart @@ -8,11 +8,14 @@ import 'package:system_date_time_format/src/system_date_time_format_platform_int @JS('getDateFormat') external String _getDateFormat(); +@JS('getMediumDateFormat') +external String _getMediumDateFormat(); + @JS('getLongDateFormat') external String _getLongDateFormat(); -@JS('getMediumDateFormat') -external String _getMediumDateFormat(); +@JS('getFullDateFormat') +external String _getFullDateFormat(); @JS('getTimeFormat') external String _getTimeFormat(); @@ -44,6 +47,12 @@ class SystemDateTimeFormatWeb extends SystemDateTimeFormatPlatformInterface { return _getLongDateFormat(); } + /// Returns a full version of date format. + @override + Future getFullDatePattern() async { + return _getFullDateFormat(); + } + /// Returns time format. @override Future getTimePattern() async { diff --git a/linux/system_date_time_format_plugin.cc b/linux/system_date_time_format_plugin.cc index f64d1c2..ebd192f 100644 --- a/linux/system_date_time_format_plugin.cc +++ b/linux/system_date_time_format_plugin.cc @@ -176,6 +176,9 @@ static void system_date_time_format_plugin_handle_method_call( response = FL_METHOD_RESPONSE(fl_method_success_response_new(fl_value_new_string(formatted_date))); free(formatted_date); } + else if (strcmp(method, "getFullDateFormat") == 0) { + response = FL_METHOD_RESPONSE(fl_method_success_response_new(fl_value_new_string(""))); + } else if (strcmp(method, "getTimeFormat") == 0) { char* time = nl_langinfo(T_FMT); char* formatted_time = format_time(time); diff --git a/macos/Classes/SystemDateTimeFormatPlugin.swift b/macos/Classes/SystemDateTimeFormatPlugin.swift index 7b4c4f7..2c65836 100644 --- a/macos/Classes/SystemDateTimeFormatPlugin.swift +++ b/macos/Classes/SystemDateTimeFormatPlugin.swift @@ -16,6 +16,8 @@ public class SystemDateTimeFormatPlugin: NSObject, FlutterPlugin { result(getMediumDateFormat()) case "getLongDateFormat": result(getLongDateFormat()) + case "getFullDateFormat": + result(getFullDateFormat()) case "getTimeFormat": result(getTimeFormat()) default: @@ -41,6 +43,12 @@ public class SystemDateTimeFormatPlugin: NSObject, FlutterPlugin { return dateFormatter.dateFormat } + private func getFullDateFormat() -> String { + let dateFormatter = DateFormatter() + dateFormatter.dateStyle = .full + return dateFormatter.dateFormat + } + private func getTimeFormat() -> String { let dateFormatter = DateFormatter() dateFormatter.timeStyle = .short diff --git a/pubspec.yaml b/pubspec.yaml index 86aaea2..2784717 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: system_date_time_format description: A plugin for getting date and time format patterns from device system settings. -version: 1.0.0 +version: 1.1.0 repository: https://github.com/Nikoro/system_date_time_format issue_tracker: https://github.com/Nikoro/system_date_time_format/issues diff --git a/screenshots/web_result.png b/screenshots/web_result.png index ca987c6..c61c6f6 100644 Binary files a/screenshots/web_result.png and b/screenshots/web_result.png differ diff --git a/test/_tools/stubs/stubs.dart b/test/_tools/stubs/stubs.dart index 56406fe..fb75211 100644 --- a/test/_tools/stubs/stubs.dart +++ b/test/_tools/stubs/stubs.dart @@ -6,11 +6,13 @@ abstract class Stubs { static const datePattern = 'dd.MM.y'; static const mediumDatePattern = 'd MMM y'; static const longDatePattern = 'd MMMM y'; + static const fullDatePattern = 'EEEE d MMMM y'; static const timePattern = 'HH:mm'; static const allPatterns = Patterns( datePattern: datePattern, mediumDatePattern: mediumDatePattern, longDatePattern: longDatePattern, + fullDatePattern: fullDatePattern, timePattern: timePattern, ); } diff --git a/test/src/patterns_test.dart b/test/src/patterns_test.dart index 48015a6..71fbd92 100644 --- a/test/src/patterns_test.dart +++ b/test/src/patterns_test.dart @@ -6,12 +6,14 @@ void main() { datePattern: 'datePattern', mediumDatePattern: 'mediumDatePattern', longDatePattern: 'longDatePattern', + fullDatePattern: 'fullDatePattern', timePattern: 'timePattern', ); const patterns2 = Patterns( datePattern: 'datePattern', mediumDatePattern: 'mediumDatePattern', longDatePattern: 'longDatePattern', + fullDatePattern: 'fullDatePattern', timePattern: 'timePattern', ); const patterns3 = Patterns(); diff --git a/test/system_date_time_format/system_date_time_format_test.dart b/test/system_date_time_format/system_date_time_format_test.dart index f1b7806..2864731 100644 --- a/test/system_date_time_format/system_date_time_format_test.dart +++ b/test/system_date_time_format/system_date_time_format_test.dart @@ -17,6 +17,8 @@ void main() { .thenAnswer((_) async => Stubs.mediumDatePattern); when(() => platform.getLongDatePattern()) .thenAnswer((_) async => Stubs.longDatePattern); + when(() => platform.getFullDatePattern()) + .thenAnswer((_) async => Stubs.fullDatePattern); when(() => platform.getTimePattern()) .thenAnswer((_) async => Stubs.timePattern); @@ -35,6 +37,7 @@ void main() { [SystemDateTimeFormat().getDatePattern, Stubs.datePattern], [SystemDateTimeFormat().getMediumDatePattern, Stubs.mediumDatePattern], [SystemDateTimeFormat().getLongDatePattern, Stubs.longDatePattern], + [SystemDateTimeFormat().getFullDatePattern, Stubs.fullDatePattern], [SystemDateTimeFormat().getTimePattern, Stubs.timePattern], ].forEach((input) { final function = input.first as Future Function(); diff --git a/test/system_date_time_format_method_channel_test.dart b/test/system_date_time_format_method_channel_test.dart index fe80464..d6f77d3 100644 --- a/test/system_date_time_format_method_channel_test.dart +++ b/test/system_date_time_format_method_channel_test.dart @@ -17,6 +17,7 @@ void main() { [platform.getDatePattern, Stubs.datePattern], [platform.getMediumDatePattern, Stubs.mediumDatePattern], [platform.getLongDatePattern, Stubs.longDatePattern], + [platform.getFullDatePattern, Stubs.fullDatePattern], [platform.getTimePattern, Stubs.timePattern], ].forEach((input) { final function = input.first as Future Function(); diff --git a/web/system_date_time_format.min.js b/web/system_date_time_format.min.js index 0d59eb5..7a4348d 100644 --- a/web/system_date_time_format.min.js +++ b/web/system_date_time_format.min.js @@ -1 +1 @@ -const getDateTimeFormatPattern=e=>new Intl.DateTimeFormat(void 0,e).formatToParts(new Date).map((e=>{switch(e.type){case"day":return"d".repeat(e.value.length);case"month":return"M".repeat(e.value.length);case"year":return"y".repeat(e.value.length);case"hour":return"h".repeat(e.value.length);case"minute":return"m".repeat(e.value.length);case"second":return"s".repeat(e.value.length);case"literal":return e.value;default:return""}})).join("");getDateFormat=()=>getDateTimeFormatPattern({dateStyle:"short"}),getMediumDateFormat=()=>getDateTimeFormatPattern({dateStyle:"medium"}),getLongDateFormat=()=>getDateTimeFormatPattern({dateStyle:"long"}),getTimeFormat=()=>getDateTimeFormatPattern({timeStyle:"short"}); \ No newline at end of file +const getDateTimeFormatPattern=e=>new Intl.DateTimeFormat(void 0,e).formatToParts(new Date).map((e=>{switch(e.type){case"weekday":return"EEEE";case"day":return"d".repeat(e.value.length);case"month":return"M".repeat(Math.min(4,part.value.length));case"year":return"y".repeat(e.value.length);case"hour":return"h".repeat(e.value.length);case"minute":return"m".repeat(e.value.length);case"second":return"s".repeat(e.value.length);case"literal":return e.value;default:return""}})).join("");getDateFormat=()=>getDateTimeFormatPattern({dateStyle:"short"}),getMediumDateFormat=()=>getDateTimeFormatPattern({dateStyle:"medium"}),getLongDateFormat=()=>getDateTimeFormatPattern({dateStyle:"long"}),getFullDateFormat=()=>getDateTimeFormatPattern({dateStyle:"full"}),getTimeFormat=()=>getDateTimeFormatPattern({timeStyle:"short"}); \ No newline at end of file diff --git a/windows/system_date_time_format_plugin.cpp b/windows/system_date_time_format_plugin.cpp index 1f9ae06..7600158 100644 --- a/windows/system_date_time_format_plugin.cpp +++ b/windows/system_date_time_format_plugin.cpp @@ -44,7 +44,7 @@ namespace system_date_time_format { } if (method_call.method_name() == "getMediumDateFormat") { - result->Success(EncodableValue(SystemDateTimeFormatPlugin::getMediumDateFormat())); + result->Success(EncodableValue("")); return; } @@ -53,6 +53,11 @@ namespace system_date_time_format { return; } + if (method_call.method_name() == "getFullDateFormat") { + result->Success(EncodableValue("")); + return; + } + if (method_call.method_name() == "getTimeFormat") { result->Success(EncodableValue(SystemDateTimeFormatPlugin::getTimeFormat())); return; @@ -65,10 +70,6 @@ namespace system_date_time_format { return getFormat(LOCALE_SSHORTDATE); } - string SystemDateTimeFormatPlugin::getMediumDateFormat() { - return ""; - } - string SystemDateTimeFormatPlugin::getLongDateFormat() { return getFormat(LOCALE_SLONGDATE); } diff --git a/windows/system_date_time_format_plugin.h b/windows/system_date_time_format_plugin.h index 4978e3e..17696d8 100644 --- a/windows/system_date_time_format_plugin.h +++ b/windows/system_date_time_format_plugin.h @@ -30,6 +30,7 @@ namespace system_date_time_format { string getDateFormat(); string getMediumDateFormat(); string getLongDateFormat(); + string getFullDateFormat(); string getTimeFormat(); string getFormat(LCTYPE infoType); };