Skip to content

Commit

Permalink
1.1.0 (#15)
Browse files Browse the repository at this point in the history
* add full date format pattern

* fix returning empty strings instead of null on windows & linux when pattern is unavailable

* update README & documentation

* update CHANGELOG

* bump version to 1.1.0
  • Loading branch information
Nikoro authored Feb 13, 2024
1 parent f1f184b commit 4dc530d
Show file tree
Hide file tree
Showing 32 changed files with 133 additions and 37 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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**:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ Future<void> 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"
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
1 change: 1 addition & 0 deletions example/lib/fallbacks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
11 changes: 7 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion example/macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 353c8bcc5d5b0994e508d035b5431cfe18c1dea7

COCOAPODS: 1.12.0
COCOAPODS: 1.13.0
2 changes: 1 addition & 1 deletion example/macos/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0920;
LastUpgradeCheck = 1300;
LastUpgradeCheck = 1430;
ORGANIZATIONNAME = "";
TargetAttributes = {
33CC10EC2044A3C60003C045 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1300"
LastUpgradeVersion = "1430"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.0"
version: "1.1.0"
term_glyph:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions example_with_tests/lib/fallbacks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
11 changes: 7 additions & 4 deletions example_with_tests/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion example_with_tests/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.0"
version: "1.1.0"
term_glyph:
dependency: transitive
description:
Expand Down
4 changes: 3 additions & 1 deletion example_with_tests/test/_tools/stubs/stubs.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import 'package:system_date_time_format/system_date_time_format.dart';
import 'package:system_date_time_format/src/patterns.dart';

abstract class Stubs {
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,
);
}
7 changes: 4 additions & 3 deletions example_with_tests/test/app_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void main() {
});

testWidgets(
'App displays 4 RowItems with correct Date & Time format patterns',
'App displays 5 RowItems with correct Date & Time format patterns',
(tester) async {
await tester.pumpWidget(
SDTFScope(format: mockSDTFormat, child: const App()),
Expand All @@ -25,10 +25,11 @@ void main() {

final rowItems = tester.widgetListByType<RowItem>();

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);
});
}
8 changes: 8 additions & 0 deletions ios/Classes/SwiftSystemDateTimeFormatPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class SwiftSystemDateTimeFormatPlugin: NSObject, FlutterPlugin {
result(getMediumDateFormat())
case "getLongDateFormat":
result(getLongDateFormat())
case "getFullDateFormat":
result(getFullDateFormat())
case "getTimeFormat":
result(getTimeFormat())
default:
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/src/extensions/extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extension NullableStringExtensions on String? {
bool get isNullOrEmpty => this == null || this!.isEmpty;
}
4 changes: 4 additions & 0 deletions lib/src/patterns.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,12 +21,14 @@ class Patterns {
datePattern == other.datePattern &&
mediumDatePattern == other.mediumDatePattern &&
longDatePattern == other.longDatePattern &&
fullDatePattern == other.fullDatePattern &&
timePattern == other.timePattern;

@override
int get hashCode =>
datePattern.hashCode ^
mediumDatePattern.hashCode ^
longDatePattern.hashCode ^
fullDatePattern.hashCode ^
timePattern.hashCode;
}
7 changes: 7 additions & 0 deletions lib/src/system_date_time_format_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class MethodChannelSystemDateTimeFormat
return methodChannel.invokeMethod<String>('getLongDateFormat');
}

/// Invokes "getFullDateFormat" method on system_date_time_format [methodChannel]
/// returning fullDateFormat
@override
Future<String?> getFullDatePattern() {
return methodChannel.invokeMethod<String>('getFullDateFormat');
}

/// Invokes "getTimeFormat" method on system_date_time_format [methodChannel]
/// returning timeFormat
@override
Expand Down
3 changes: 3 additions & 0 deletions lib/src/system_date_time_format_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ abstract class SystemDateTimeFormatPlatformInterface extends PlatformInterface {
/// Method signature for returning long version of date format pattern
Future<String?> getLongDatePattern();

/// Method signature for returning full version of date format pattern
Future<String?> getFullDatePattern();

/// Method signature for returning time format pattern
Future<String?> getTimePattern();
}
44 changes: 33 additions & 11 deletions lib/system_date_time_format.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<String?> getDatePattern() {
return SystemDateTimeFormatPlatformInterface.instance.getDatePattern();
Future<String?> 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<String?> getMediumDatePattern() {
return SystemDateTimeFormatPlatformInterface.instance
Future<String?> 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<String?> getLongDatePattern() {
return SystemDateTimeFormatPlatformInterface.instance.getLongDatePattern();
Future<String?> 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<String?> 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<String?> getTimePattern() {
return SystemDateTimeFormatPlatformInterface.instance.getTimePattern();
Future<String?> getTimePattern() async {
final pattern =
await SystemDateTimeFormatPlatformInterface.instance.getTimePattern();
return pattern.isNullOrEmpty ? null : pattern;
}

/// Returns all available date & time patterns.
Expand All @@ -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<SDTFScopeInherited>();
if (sDTFScope == null) {
if (scope == null) {
throw SDTFScopeNotFoundError(context.widget.runtimeType);
}

return sDTFScope.patterns;
return scope.patterns;
}
}
13 changes: 11 additions & 2 deletions lib/system_date_time_format_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -44,6 +47,12 @@ class SystemDateTimeFormatWeb extends SystemDateTimeFormatPlatformInterface {
return _getLongDateFormat();
}

/// Returns a full version of date format.
@override
Future<String> getFullDatePattern() async {
return _getFullDateFormat();
}

/// Returns time format.
@override
Future<String> getTimePattern() async {
Expand Down
3 changes: 3 additions & 0 deletions linux/system_date_time_format_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions macos/Classes/SystemDateTimeFormatPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class SystemDateTimeFormatPlugin: NSObject, FlutterPlugin {
result(getMediumDateFormat())
case "getLongDateFormat":
result(getLongDateFormat())
case "getFullDateFormat":
result(getFullDateFormat())
case "getTimeFormat":
result(getTimeFormat())
default:
Expand All @@ -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
Expand Down
Loading

0 comments on commit 4dc530d

Please sign in to comment.