birzha_mobile/lib/models/attributes/attribute.dart

73 lines
2.1 KiB
Dart

import 'package:birzha/models/settings/settingsModel.dart';
import 'package:birzha/services/textMetaData.dart';
import 'package:birzha/services/validator.dart';
import 'package:flutter/material.dart';
import 'package:birzha/services/modals.dart';
import 'package:birzha/core/orm/orm.dart';
abstract class AttributeSelectableDelegate<T> {
T? get groupValue;
Future<T?> dialogBuilder(BuildContext context);
}
class SingleOptionSelectableDelegate extends AttributeSelectableDelegate<AttributeWithValueNameMixin> {
SingleOptionSelectableDelegate(this.groupValue, this.options);
@override
Future<AttributeWithValueNameMixin?> dialogBuilder(BuildContext context) {
return attributeSelector(context, groupValue, options);
}
@override
final AttributeWithValueNameMixin? groupValue;
final List<AttributeWithValueNameMixin> options;
}
mixin AttributeLabelMixin {
String get label;
String toQueryString();
}
mixin OptionedAttributeMixin<T> on KeyIndexedAttributeMixin<T> {
List<PostAttribute> get options;
}
mixin SelectableAttributeMixin<T, V> on KeyIndexedAttributeMixin<T> {
AttributeSelectableDelegate<V> selectDelegate(V? groupValue);
TextInputMetaData metaData({required V? groupValue, required void Function(V?) onSelected}) {
return TextInputMetaData(
name: label,
label: label,
pickerMode: (context) async {
var option = await selectDelegate(groupValue).dialogBuilder(context);
onSelected(option);
if (option is AttributeWithValueNameMixin) {
return option.value.toString();
} else if (option != null) {
return option.toString();
}
},
validation: Validation(conditions: [(inp) => inp.isEmpty ? 'empty'.translation : null]),
key: key);
}
}
mixin KeyIndexedAttributeMixin<T> on PostAttribute<T> {
String get key;
}
mixin AttributeWithValueNameMixin<T> on PostAttribute<T> {
String get value;
}
abstract class PostAttribute<T> extends Orm<T> with AttributeLabelMixin {
PostAttribute(Map<String, dynamic> jSON) : super(jSON);
@override
String toQueryString() {
return primaryKey.toString();
}
}