From 9869bac7175ec704e2d0ff7bb95affbb5a35fba6 Mon Sep 17 00:00:00 2001 From: Hamatoma Date: Tue, 3 Nov 2020 18:50:55 +0100 Subject: [PATCH] daily work --- lib/src/model/combo_base_model.dart | 59 ++++++++++------- lib/src/model/db_reference_model.dart | 10 +++ lib/src/model/standard/user_model.dart | 2 +- lib/src/page/user/user_list_page.dart | 4 +- lib/src/private/bsettings.dart | 2 +- lib/src/widget/callback_controller_bones.dart | 3 +- lib/src/widget/page_controller_bones.dart | 64 ++++++------------- lib/src/widget/view.dart | 17 +++-- lib/src/widget/widget_list.dart | 2 +- test/persistence_cache_test.dart | 6 +- 10 files changed, 86 insertions(+), 83 deletions(-) diff --git a/lib/src/model/combo_base_model.dart b/lib/src/model/combo_base_model.dart index 264644a..f7a6c26 100644 --- a/lib/src/model/combo_base_model.dart +++ b/lib/src/model/combo_base_model.dart @@ -45,7 +45,7 @@ abstract class ComboBaseModel extends FieldModel { texts = parseStringList('texts', map); listType = parseEnum('listType', map, ComboboxListType.values); - listOption = parseString('listOption', map) ?? ''; + listOption = parseString('listOption', map); values = parseValueList('values', map, dataType); switch (listType) { case ComboboxListType.undef: @@ -85,37 +85,48 @@ abstract class ComboBaseModel extends FieldModel { /// and [values]. static ComboboxData createByType( DataType dataType, List texts, Iterable values) { - ComboboxData rc; - switch (dataType) { - case DataType.bool: - rc = ComboboxData(texts, values); - break; - case DataType.currency: - case DataType.int: - case DataType.reference: - rc = ComboboxData(texts, values); - break; - case DataType.date: - case DataType.dateTime: - rc = ComboboxData(texts, values); - break; - case DataType.float: - rc = ComboboxData(texts, values); - break; - case DataType.string: - rc = ComboboxData(texts, values); - break; + if (values != null && values.length == 0) { + values = null; } + ComboboxData rc = ComboboxData(texts, values); return rc; } } -class ComboboxData { +class ComboboxData { final List texts; - final List values; + List _values; WaitState waitState; - ComboboxData(this.texts, this.values, [this.waitState = WaitState.undef]); + ComboboxData(this.texts, this._values, [this.waitState = WaitState.undef]); + + /// Returns the length of [_values]. + int get valuesLength => _values == null ? 0 : _values.length; + + /// Adds a [value] to the [values]. + void addValue(dynamic value) { + if (_values == null) { + _values = [value]; + } else { + _values.add(value); + } + } + + /// Adds a [value] to the [values]. + void insertValue(int index, dynamic value) { + if (_values == null) { + _values = [value]; + } else { + _values.insert(index, value); + } + } + + /// Returns the value at the [index] or null on error. + dynamic value(int index) { + final rc = + _values == null || index >= _values.length ? null : _values[index]; + return rc; + } } enum ComboboxListType { diff --git a/lib/src/model/db_reference_model.dart b/lib/src/model/db_reference_model.dart index aa74170..48137b5 100644 --- a/lib/src/model/db_reference_model.dart +++ b/lib/src/model/db_reference_model.dart @@ -65,6 +65,16 @@ class DbReferenceModel extends ComboBaseModel { maxSize = parseInt('maxSize', map, required: false); rows = parseInt('rows', map, required: false); options = parseOptions('options', map); + label ??= column.label; + toolTip ??= column.toolTip; + filterType ??= column.filterType; + options = parseOptions('options', map); + dataType ??= column.dataType; + texts ??= column.texts; + values ??= column.values; + listOption ??= column.listOption; + listType ??= column.listType; checkOptionsByRegExpr(options, regExprOptions); + options += column.options; } } diff --git a/lib/src/model/standard/user_model.dart b/lib/src/model/standard/user_model.dart index 9fcb1c9..98c68b7 100644 --- a/lib/src/model/standard/user_model.dart +++ b/lib/src/model/standard/user_model.dart @@ -102,7 +102,7 @@ class UserModel extends ModuleModel { "name": "user_name", "column": "user_name", "toolTip": - "Filter bezüglich des Names der anzuzeigenden Einträge: Joker '*' (beliebiger String) ist erlaubt." + "Filter bezüglich des Namens der anzuzeigenden Einträge: Joker '*' (beliebiger String) ist erlaubt." }, { "widgetType": "dbReference", diff --git a/lib/src/page/user/user_list_page.dart b/lib/src/page/user/user_list_page.dart index 84832de..3ff9de2 100644 --- a/lib/src/page/user/user_list_page.dart +++ b/lib/src/page/user/user_list_page.dart @@ -1,7 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_bones/src/widget/callback_controller_bones.dart'; -import '../../widget/widget_list.dart'; import '../../widget/list_form.dart'; import '../application_data.dart'; import 'user_controller.dart'; @@ -29,7 +28,6 @@ class UserListPageState extends State { GlobalKey(debugLabel: 'user_list'); Iterable rowsDeprecated; UserController controller; - WidgetList filters; UserListPageState(this.applicationData); @@ -88,7 +86,7 @@ class UserListPageState extends State { ), ]), ], - filters: filters, + filters: controller.widgetList, errorMessage: applicationData.lastErrorMessage(controller.page.fullName()), ), diff --git a/lib/src/private/bsettings.dart b/lib/src/private/bsettings.dart index aeb84fa..4879c64 100644 --- a/lib/src/private/bsettings.dart +++ b/lib/src/private/bsettings.dart @@ -10,7 +10,7 @@ class BSettings { Persistence persistence; static BSettings lastInstance; - /// Returns the singleton of VSetting. + /// Returns the singleton of BSetting. factory BSettings() { final map = { 'form.card.padding': '16.0', diff --git a/lib/src/widget/callback_controller_bones.dart b/lib/src/widget/callback_controller_bones.dart index 300d0e6..3090924 100644 --- a/lib/src/widget/callback_controller_bones.dart +++ b/lib/src/widget/callback_controller_bones.dart @@ -1,6 +1,5 @@ import 'package:flutter/material.dart'; -import '../model/model_types.dart'; import '../model/combobox_model.dart'; import '../model/combo_base_model.dart'; import '../model/field_model.dart'; @@ -19,7 +18,7 @@ abstract class CallbackControllerBones { void buildRows(); /// Returns the [ComboboxData] (texts and values) of the [ComboboxModel] named [name]. - ComboboxData comboboxData(String name, DataType dataType); + ComboboxData comboboxData(String name); /// Frees all resources. void dispose(); diff --git a/lib/src/widget/page_controller_bones.dart b/lib/src/widget/page_controller_bones.dart index 48a13b2..a100337 100644 --- a/lib/src/widget/page_controller_bones.dart +++ b/lib/src/widget/page_controller_bones.dart @@ -86,64 +86,38 @@ class PageControllerBones implements CallbackControllerBones { /// Completes database based components to the model, e.g. the list for /// comboboxes. void completeModelByPersistence(WidgetModel model) { - if (model is ComboBaseModel) { + if (model is ComboBaseModel && model.listType != null) { if (model.data == null) { if (model.listType == ComboboxListType.explicite) { model.data = ComboBaseModel.createByType( model.dataType, model.texts, model.values); model.data.waitState = WaitState.ready; } else { - model.data = comboboxData(model.name, model.dataType); + model.data = comboboxData(model.name); } } } } - @override - ComboboxData comboboxData(String name, DataType dataType) { - ComboboxData rc; - switch (dataType) { - case DataType.bool: - rc = comboboxDataByType(name); - break; - case DataType.currency: - case DataType.int: - case DataType.reference: - rc = comboboxDataByType(name); - break; - case DataType.date: - case DataType.dateTime: - rc = comboboxDataByType(name); - break; - case DataType.float: - rc = comboboxDataByType(name); - break; - case DataType.string: - comboboxDataByType(name); - break; - } - return rc; - } - - ComboboxData comboboxDataByType(String name) { + ComboboxData comboboxData(String name) { ComboboxData rc = comboboxDataMap.containsKey(name) ? comboboxDataMap[name] - : ComboboxData([], []); + : ComboboxData([], null); comboboxDataMap[name] = rc; if (rc.waitState == WaitState.undef) { ComboBaseModel model = page.fieldByName(name) as ComboBaseModel; if (model != null) { switch (model.listType) { case ComboboxListType.explicite: - rc = ComboboxData(model.texts, model.values, WaitState.ready); + rc = ComboboxData(model.texts, model.values, WaitState.ready); comboboxDataMap[name] = rc; break; case ComboboxListType.undef: - rc = ComboboxData([], [], WaitState.ready); + rc = ComboboxData([], [], WaitState.ready); comboboxDataMap[name] = rc; break; case ComboboxListType.dbColumn: - comboboxDataDb(model, rc); + comboboxDataDb(model, rc); break; case ComboboxListType.configuration: break; @@ -153,28 +127,32 @@ class PageControllerBones implements CallbackControllerBones { return rc; } - void comboboxDataDb(ComboBaseModel model, ComboboxData data) { - // example: role.list;role_displayname role_id;:role_name=% :excluded=0' - final parts = model.listOption.split(';'); - final moduleName = parts[0].split('.'); - final cols = parts[1].split(' '); + void comboboxDataDb(ComboBaseModel model, ComboboxData data) { + // example: xxx.role.list;role_displayname role_id;:role_name=% :excluded=0' + final keyColumnsParams = model.listOption.split(';'); + final nameModuleSql = keyColumnsParams[0].split('.'); + final cols = keyColumnsParams[1].split(' '); final params = {}; - if (parts.length > 2 && parts[2].isNotEmpty) { - parts[2].split(' ').forEach((element) { + if (keyColumnsParams.length > 2 && keyColumnsParams[2].isNotEmpty) { + keyColumnsParams[2].split(' ').forEach((element) { final keyValue = element.split('='); params[keyValue[0]] = keyValue[1]; }); } applicationData.persistence - .list(module: moduleName[0], sqlName: moduleName[1], params: params) + .list(module: nameModuleSql[1], sqlName: nameModuleSql[2], params: params) .then((rows) { rows.forEach((row) { data.texts.add(row[cols[0]]); - data.values.add(row[cols[1]]); + var value = row[cols[1]]; + if (value is String){ + value = StringHelper.fromString(value, model.dataType); + } + data.addValue(value); }); if (model.hasOption('undef')) { data.texts.insert(0, '-'); - data.values.insert(0, null); + data.insertValue(0, null); } data.waitState = WaitState.ready; redraw(RedrawReason.redraw); diff --git a/lib/src/widget/view.dart b/lib/src/widget/view.dart index bd650d9..e297f35 100644 --- a/lib/src/widget/view.dart +++ b/lib/src/widget/view.dart @@ -95,7 +95,7 @@ class View { rc = comboboxByType(model, controller, initialValue); break; case DataType.string: - comboboxByType(model, controller, initialValue); + rc = comboboxByType(model, controller, initialValue); break; } return rc; @@ -105,13 +105,20 @@ class View { /// with an [initialValue]. Widget comboboxByType( FieldModel model, CallbackControllerBones controller, initialValue) { - ComboboxData comboboxData = (model as ComboBaseModel).data; + ComboboxData comboboxData = (model as ComboBaseModel).data; final items = >[]; - for (var ix = 0; ix < comboboxData.texts.length; ix++) { + if (comboboxData == null || comboboxData.texts.length == 0) { items.add(DropdownMenuItem( onTap: controller.getOnTap(model.name, controller), - value: comboboxData.values[ix], - child: Text(comboboxData.texts[ix]))); + value: null, + child: Text('-'))); + } else { + for (var ix = 0; ix < comboboxData.texts.length; ix++) { + items.add(DropdownMenuItem( + onTap: controller.getOnTap(model.name, controller), + value: comboboxData.value(ix), + child: Text(comboboxData.texts[ix]))); + } } final rc = DropdownButtonFormBone( model.name, diff --git a/lib/src/widget/widget_list.dart b/lib/src/widget/widget_list.dart index fd861ee..434367a 100644 --- a/lib/src/widget/widget_list.dart +++ b/lib/src/widget/widget_list.dart @@ -109,7 +109,7 @@ class WidgetList { again = false; for (var model in waitCandidates) { if (model is ComboBaseModel) { - data = controller.comboboxData(model.listOption, model.dataType); + data = controller.comboboxData(model.listOption); if (data != null && data.waitState == WaitState.ready) { model.data = data; waitCandidates.remove(model); diff --git a/test/persistence_cache_test.dart b/test/persistence_cache_test.dart index 69e017e..c05a1f4 100644 --- a/test/persistence_cache_test.dart +++ b/test/persistence_cache_test.dart @@ -45,11 +45,11 @@ void main() async { expect(logger.errors.length, equals(0)); expect(data, isNotNull); expect(data.texts.length, greaterThan(4)); - expect(data.texts.length, equals(data.values.length)); + expect(data.texts.length, equals(data.valuesLength)); expect(data2, isNotNull); expect(data2.texts.length, greaterThan(4)); - expect(data2.texts.length, equals(data2.values.length)); - expect(data.texts.length, equals(data2.values.length + 1)); + expect(data2.texts.length, equals(data2.valuesLength)); + expect(data.texts.length, equals(data2.valuesLength + 1)); expect(cache.leastReasentlyUsed.length, equals(2)); const key3 = 'id3.role.list;role_name role_id;:role_name=%'; var data3 = cache.combobox(key3, hasUndef: false); -- 2.39.5