Skip to content

Commit

Permalink
fix: Added the ability to link with Form.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Jul 4, 2023
1 parent f6eed40 commit e71bab0
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
56 changes: 56 additions & 0 deletions packages/masamune/lib/form/extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
part of masamune;

/// Extension methods for [FormMediaValue].
///
/// [FormMediaValue]の拡張メソッドです。
extension FormMediaValueExtensions on FormMediaValue {
/// Convert to [ModelImageUri].
///
/// If [path] is empty, null is returned.
///
/// Returns an error if the original [FormMediaType] is not [FormMediaType.image].
///
/// [ModelImageUri]に変換します。
///
/// [path]が空の場合はnullを返します。
///
/// 元の[FormMediaType][FormMediaType.image]でない場合はエラーを返します。
ModelImageUri? toModelImageUri() {
if (path.isEmpty) {
return null;
}
final uri = Uri.tryParse(path!);
if (uri == null) {
return null;
}
if (type != FormMediaType.image) {
throw Exception("The original FormMediaType is not FormMediaType.image");
}
return ModelImageUri(uri);
}

/// Convert to [ModelVideoUri].
///
/// If [path] is empty, null is returned.
///
/// Returns an error if the original [FormMediaType] is not [FormMediaType.video].
///
/// [ModelVideoUri]に変換します。
///
/// [path]が空の場合はnullを返します。
///
/// 元の[FormMediaType][FormMediaType.video]でない場合はエラーを返します。
ModelVideoUri? toModelVideoUri() {
if (path.isEmpty) {
return null;
}
final uri = Uri.tryParse(path!);
if (uri == null) {
return null;
}
if (type != FormMediaType.video) {
throw Exception("The original FormMediaType is not FormMediaType.video");
}
return ModelVideoUri(uri);
}
}
1 change: 1 addition & 0 deletions packages/masamune/lib/masamune.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export 'package:masamune_annotation/masamune_annotation.dart';
export 'package:meta/meta.dart' show useResult;

part 'form/form_scoped_widget.dart';
part 'form/extension.dart';
part 'model/model.dart';
part 'model/extension.dart';
part 'prefs/prefs.dart';
Expand Down
35 changes: 35 additions & 0 deletions packages/masamune/lib/model/extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,39 @@ extension ModelImageUriExtensions on ModelImageUri {
ImageProvider image([String defaultAssetURI = "assets/image.png"]) {
return Asset.image(value.toString(), defaultAssetURI);
}

/// Convert to [FormMediaValue].
///
/// If [value] is empty, null is returned.
///
/// [FormMediaValue]に変換します。
///
/// [value]が空の場合はnullを返します。
FormMediaValue? toFormMediaValue() {
final path = value.toString();
if (path.isEmpty) {
return null;
}
return FormMediaValue(path: path, type: FormMediaType.image);
}
}

/// Extension methods for [ModelVideoUri].
///
/// [ModelVideoUri]の拡張メソッドです。
extension ModelVideoUriExtensions on ModelVideoUri {
/// Convert to [FormMediaValue].
///
/// If [value] is empty, null is returned.
///
/// [FormMediaValue]に変換します。
///
/// [value]が空の場合はnullを返します。
FormMediaValue? toFormMediaValue() {
final path = value.toString();
if (path.isEmpty) {
return null;
}
return FormMediaValue(path: path, type: FormMediaType.video);
}
}
6 changes: 3 additions & 3 deletions packages/masamune/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,15 @@ packages:
path: "../katana_model"
relative: true
source: path
version: "2.4.0"
version: "2.5.0"
katana_model_local:
dependency: "direct main"
description:
name: katana_model_local
sha256: "7385af1797643dacdd09604927297012037466766914ce080c80e3440bec5a5e"
sha256: "08f44756c97ab70d5de4a75294e6757ad0f731ddbdc42de8f0b979a8307eab30"
url: "https://pub.dev"
source: hosted
version: "2.1.2"
version: "2.2.0"
katana_prefs:
dependency: "direct main"
description:
Expand Down

0 comments on commit e71bab0

Please sign in to comment.