add item in lists
MODIFY
packages/isar/lib/src/isar_connect.dart
packages/isar/lib/src/isar_connect.dart
@@ -15,6 +15,7 @@ abstract class _IsarConnect {
ConnectAction.removeQuery: _removeQuery,
ConnectAction.exportJson: _exportJson,
ConnectAction.editProperty: _editProperty,
+ ConnectAction.addInList: _addInList,
};
static bool _initialized = false;
@@ -173,9 +174,7 @@ abstract class _IsarConnect {
return query.exportJson();
}
- static Future<bool> _editProperty(
- Map<String, dynamic> params,
- ) async {
+ static Future<void> _editProperty(Map<String, dynamic> params) async {
final cEdit = ConnectEdit.fromJson(params);
final collection = Isar.getInstance(cEdit.instance)!
.getCollectionByNameInternal(cEdit.collection)!;
@@ -198,7 +197,32 @@ abstract class _IsarConnect {
}
await collection.isar.writeTxn(() async => collection.importJson(objects));
- return true;
+ }
+
+ static Future<void> _addInList(Map<String, dynamic> params) async {
+ final cEdit = ConnectEdit.fromJson(params);
+ final collection = Isar.getInstance(cEdit.instance)!
+ .getCollectionByNameInternal(cEdit.collection)!;
+
+ final query = collection.buildQuery<dynamic>(
+ whereClauses: [IdWhereClause.equalTo(value: cEdit.id)],
+ );
+
+ final objects = await query.exportJson();
+
+ if (objects.isEmpty ||
+ !objects[0].containsKey(cEdit.property) ||
+ objects[0][cEdit.property] is! List) {
+ throw IsarError('Cant get object or property is wrong for add');
+ }
+
+ if (cEdit.index == null) {
+ (objects[0][cEdit.property] as List).add(cEdit.value);
+ } else {
+ (objects[0][cEdit.property] as List).insert(cEdit.index!, cEdit.value);
+ }
+
+ await collection.isar.writeTxn(() async => collection.importJson(objects));
}
static Query<dynamic> _getQuery(Map<String, dynamic> params) {

MODIFY
packages/isar/lib/src/isar_connect_api.dart
packages/isar/lib/src/isar_connect_api.dart
@@ -12,7 +12,8 @@ enum ConnectAction {
removeQuery('ext.isar.removeQuery'),
exportQuery('ext.isar.exportQuery'),
exportJson('ext.isar.exportJson'),
- editProperty('ext.isar.editProperty');
+ editProperty('ext.isar.editProperty'),
+ addInList('ext.isar.addInList');
const ConnectAction(this.method);
@@ -56,6 +57,7 @@ class ConnectQuery {
: null,
);
}
+
final String instance;
final String collection;
final FilterOperation? filter;

MODIFY
packages/isar_inspector/lib/query_table.dart
packages/isar_inspector/lib/query_table.dart
@@ -23,6 +23,7 @@ typedef Editor = void Function(
String property,
int? index,
dynamic value,
+ bool editing,
);
class QueryTable extends ConsumerWidget {
@@ -55,7 +56,7 @@ class QueryTable extends ConsumerWidget {
),
collection: collection,
object: objects[index],
- editor: (id, property, index, value) {
+ editor: (id, property, index, value, editing) {
final edit = ConnectEdit(
instance: ref.read(selectedInstancePod).value!,
collection: collection.name,
@@ -64,7 +65,12 @@ class QueryTable extends ConsumerWidget {
index: index,
value: value,
);
+
+ if (editing) {
ref.read(isarConnectPod.notifier).editProperty(edit);
+ } else {
+ ref.read(isarConnectPod.notifier).addInList(edit);
+ }
},
),
Align(
@@ -272,16 +278,64 @@ class _TableItemState extends State<TableItem> {
),
if (!widget.data.property.isId &&
!widget.data.property.type.isList)
+ PopupMenuItem<dynamic>(
+ onTap: () {
+ WidgetsBinding.instance.addPostFrameCallback(
+ (_) => _edit(),
+ );
+ },
+ child: PopUpMenuRow(
+ icon: const Icon(
+ Icons.edit,
+ size: PopUpMenuRow.iconSize,
+ ),
+ text: widget.data.index != null
+ ? 'Edit item ${widget.data.index!}'
+ : 'Edit property',
+ ),
+ ),
+ if (widget.data.property.type.isList &&
+ widget.data.value != null)
PopupMenuItem<dynamic>(
onTap: () {
WidgetsBinding.instance
- .addPostFrameCallback((_) => _edit());
+ .addPostFrameCallback((_) => _addInList(null));
},
child: const PopUpMenuRow(
- icon: Icon(Icons.edit, size: PopUpMenuRow.iconSize),
- text: 'Edit item',
+ icon: Icon(Icons.add, size: PopUpMenuRow.iconSize),
+ text: 'Add item',
+ ),
+ ),
+ if (widget.data.index != null) ...[
+ PopupMenuItem<dynamic>(
+ onTap: () {
+ WidgetsBinding.instance.addPostFrameCallback(
+ (_) => _addInList(widget.data.index),
+ );
+ },
+ child: PopUpMenuRow(
+ icon: const Icon(
+ Icons.add,
+ size: PopUpMenuRow.iconSize,
+ ),
+ text: 'Add item before ${widget.data.index!}',
+ ),
+ ),
+ PopupMenuItem<dynamic>(
+ onTap: () {
+ WidgetsBinding.instance.addPostFrameCallback(
+ (_) => _addInList(widget.data.index! + 1),
+ );
+ },
+ child: PopUpMenuRow(
+ icon: const Icon(
+ Icons.add,
+ size: PopUpMenuRow.iconSize,
+ ),
+ text: 'Add item after ${widget.data.index!}',
),
),
+ ],
if (widget.data.property.type.isList)
PopupMenuItem<dynamic>(
onTap: () => _setValue(<dynamic>[]),
@@ -324,6 +378,32 @@ class _TableItemState extends State<TableItem> {
}
}
+ Future<void> _addInList(int? index) async {
+ final result = await showDialog<Map<String, dynamic>?>(
+ context: context,
+ builder: (context) {
+ return AlertDialog(
+ content: EditPopup(
+ type: widget.data.property.type.isList
+ ? widget.data.property.type.childType
+ : widget.data.property.type,
+ value: null,
+ ),
+ );
+ },
+ );
+
+ if (result != null) {
+ widget.editor(
+ widget.objectId,
+ widget.data.property.name,
+ index,
+ result['value'],
+ false,
+ );
+ }
+ }
+
Future<void> _copy() async {
ScaffoldMessenger.of(context).removeCurrentSnackBar();
@@ -356,6 +436,7 @@ class _TableItemState extends State<TableItem> {
widget.data.property.name,
widget.data.index,
value,
+ true,
);
}

MODIFY
packages/isar_inspector/lib/state/isar_connect_state_notifier.dart
packages/isar_inspector/lib/state/isar_connect_state_notifier.dart
@@ -181,6 +181,14 @@ class IsarConnectStateNotifier
);
}
+ Future<void> addInList(ConnectEdit edit) async {
+ await _call<dynamic>(
+ ConnectAction.addInList,
+ args: edit.toJson(),
+ timeout: kLongTimeout,
+ );
+ }
+
Future<void> disconnect() async {
await state.value?.vmService.dispose();
}

add item in lists