diff --git a/packages/katana_model/lib/src/model_field_value.dart b/packages/katana_model/lib/src/model_field_value.dart index 57a31548a..929dc9089 100644 --- a/packages/katana_model/lib/src/model_field_value.dart +++ b/packages/katana_model/lib/src/model_field_value.dart @@ -755,6 +755,41 @@ class ModelTimestamp extends ModelFieldValue { /// これをサーバーに渡すことでサーバー側のタイムスタンプがデータとして保存されます。 const factory ModelTimestamp([DateTime? value]) = _ModelTimestamp; + /// Define the field as a timestamp. + /// + /// The current time is returned in the same way as [DateTime.now]. + /// + /// By passing this to the server, the timestamp on the server side is stored as data. + /// + /// フィールドをタイムスタンプとして定義します。 + /// + /// [DateTime.now]と同じ様に現在時刻が返されます。 + /// + /// これをサーバーに渡すことでサーバー側のタイムスタンプがデータとして保存されます。 + const factory ModelTimestamp.now() = _ModelTimestampWithNow; + + /// Define the field as a timestamp. + /// + /// Returns the date and time specified in [year], [month], [day], [hour], [minute], [second], [millisecond], or [microsecond]. + /// + /// By passing this to the server, the timestamp on the server side is stored as data. + /// + /// フィールドをタイムスタンプとして定義します。 + /// + /// [year]、[month]、[day]、[hour]、[minute]、[second]、[millisecond]、[microsecond]で指定した日時が返されます。 + /// + /// これをサーバーに渡すことでサーバー側のタイムスタンプがデータとして保存されます。 + const factory ModelTimestamp.dateTime( + int year, [ + int? month, + int? day, + int? hour, + int? minute, + int? second, + int? millisecond, + int? microsecond, + ]) = _ModelTimestampWithDateTime; + /// Used to disguise the retrieval of data from the server. /// /// Use for testing purposes. @@ -849,6 +884,53 @@ class ModelTimestamp extends ModelFieldValue { int get hashCode => _value.hashCode; } +@immutable +class _ModelTimestampWithNow extends _ModelTimestamp { + const _ModelTimestampWithNow() : super(); + + @override + DateTime? get _value { + return DateTime.now(); + } +} + +@immutable +class _ModelTimestampWithDateTime extends _ModelTimestamp { + const _ModelTimestampWithDateTime( + this.year, [ + this.month, + this.day, + this.hour, + this.minute, + this.second, + this.millisecond, + this.microsecond, + ]) : super(); + + final int year; + final int? month; + final int? day; + final int? hour; + final int? minute; + final int? second; + final int? millisecond; + final int? microsecond; + + @override + DateTime? get _value { + return DateTime( + year, + month ?? 1, + day ?? 1, + hour ?? 0, + minute ?? 0, + second ?? 0, + millisecond ?? 0, + microsecond ?? 0, + ); + } +} + @immutable class _ModelTimestamp extends ModelTimestamp with ModelFieldValueAsMapMixin { diff --git a/packages/katana_model/lib/src/model_ref.dart b/packages/katana_model/lib/src/model_ref.dart index 6d0eaf823..206ac1895 100644 --- a/packages/katana_model/lib/src/model_ref.dart +++ b/packages/katana_model/lib/src/model_ref.dart @@ -58,14 +58,8 @@ class ModelRefBase extends ModelFieldValue { /// また[adapter]を指定してモデルアダプターを設定することが可能です。 /// /// ミュータブルクラスでかつ[DocumentBase]のインターフェースを備えているため[ModelRefBase]を実装し、[ModelRefMixin]をミックスインすることで[DocumentBase]で置き換えることが可能です。 - factory ModelRefBase.fromPath(String path, [ModelAdapter? adapter]) { - return _ModelRefBase( - DocumentModelQuery( - path.trimQuery().trimString("/"), - adapter: adapter, - ), - ); - } + const factory ModelRefBase.fromPath(String path, [ModelAdapter? adapter]) = + _ModelRefBaseWithPath; /// Convert from [json] map to [ModelRefBase]. /// @@ -113,6 +107,25 @@ class ModelRefBase extends ModelFieldValue { } } +@immutable +class _ModelRefBaseWithPath extends _ModelRefBase { + const _ModelRefBaseWithPath(this.path, [this.adapter]) + : super( + const DocumentModelQuery(""), + ); + + final String path; + final ModelAdapter? adapter; + + @override + DocumentModelQuery get modelQuery { + return DocumentModelQuery( + path.trimQuery().trimString("/"), + adapter: adapter, + ); + } +} + /// [ModelFieldValueConverter] to enable automatic conversion of [ModelRefBase] as [ModelFieldValue]. /// /// [ModelRefBase]を[ModelFieldValue]として自動変換できるようにするための[ModelFieldValueConverter]。 diff --git a/packages/katana_model/lib/src/model_uri.dart b/packages/katana_model/lib/src/model_uri.dart index 0895891bf..b53ee2a72 100644 --- a/packages/katana_model/lib/src/model_uri.dart +++ b/packages/katana_model/lib/src/model_uri.dart @@ -37,9 +37,7 @@ class ModelImageUri extends ModelFieldValue { /// ベースの値を[value]として与えます。空の[Uri]が定義されます。 /// /// 必ず画像URIとしての定義を行いたい場合は[ModelImageUri]を利用してください。 - factory ModelImageUri.parse([String? value]) { - return ModelImageUri(value == null ? null : Uri.parse(value)); - } + const factory ModelImageUri.parse([String? value]) = _ModelImageUriWithUri; /// Used to disguise the retrieval of data from the server. /// @@ -102,6 +100,21 @@ class ModelImageUri extends ModelFieldValue { int get hashCode => _value.hashCode; } +@immutable +class _ModelImageUriWithUri extends _ModelImageUri { + const _ModelImageUriWithUri([this._path]) : super(); + + final String? _path; + + @override + Uri? get _value { + if (_path == null) { + return null; + } + return Uri.parse(_path!); + } +} + @immutable class _ModelImageUri extends ModelImageUri with ModelFieldValueAsMapMixin { const _ModelImageUri([ @@ -282,9 +295,7 @@ class ModelVideoUri extends ModelFieldValue { /// ベースの値を[value]として与えます。空の[Uri]が定義されます。 /// /// 必ず映像URIとしての定義を行いたい場合は[ModelVideoUri]を利用してください。 - factory ModelVideoUri.parse([String? value]) { - return ModelVideoUri(value == null ? null : Uri.parse(value)); - } + const factory ModelVideoUri.parse([String? value]) = _ModelVideoUriWithUri; /// Used to disguise the retrieval of data from the server. /// @@ -347,6 +358,21 @@ class ModelVideoUri extends ModelFieldValue { int get hashCode => _value.hashCode; } +@immutable +class _ModelVideoUriWithUri extends _ModelVideoUri { + const _ModelVideoUriWithUri([this._path]) : super(); + + final String? _path; + + @override + Uri? get _value { + if (_path == null) { + return null; + } + return Uri.parse(_path!); + } +} + @immutable class _ModelVideoUri extends ModelVideoUri with ModelFieldValueAsMapMixin { const _ModelVideoUri([ @@ -527,9 +553,7 @@ class ModelUri extends ModelFieldValue { /// ベースの値を[value]として与えます。空の[Uri]が定義されます。 /// /// 必ずURIとしての定義を行いたい場合は[ModelUri]を利用してください。 - factory ModelUri.parse([String? value]) { - return ModelUri(value == null ? null : Uri.parse(value)); - } + const factory ModelUri.parse([String? value]) = _ModelUriWithUri; /// Used to disguise the retrieval of data from the server. /// @@ -591,6 +615,21 @@ class ModelUri extends ModelFieldValue { int get hashCode => _value.hashCode; } +@immutable +class _ModelUriWithUri extends _ModelUri { + const _ModelUriWithUri([this._path]) : super(); + + final String? _path; + + @override + Uri? get _value { + if (_path == null) { + return null; + } + return Uri.parse(_path!); + } +} + @immutable class _ModelUri extends ModelUri with ModelFieldValueAsMapMixin { const _ModelUri([ diff --git a/packages/katana_model_firestore/pubspec.lock b/packages/katana_model_firestore/pubspec.lock index ce1edb942..2b97c6472 100644 --- a/packages/katana_model_firestore/pubspec.lock +++ b/packages/katana_model_firestore/pubspec.lock @@ -292,7 +292,7 @@ packages: path: "../katana_model" relative: true source: path - version: "2.7.0" + version: "2.7.1" lints: dependency: transitive description: diff --git a/packages/katana_model_local/pubspec.lock b/packages/katana_model_local/pubspec.lock index 081b31ec7..6daf9f4c5 100644 --- a/packages/katana_model_local/pubspec.lock +++ b/packages/katana_model_local/pubspec.lock @@ -232,7 +232,7 @@ packages: path: "../katana_model" relative: true source: path - version: "2.7.0" + version: "2.7.1" lints: dependency: transitive description: