Skip to content

Commit

Permalink
Improve the logic for creating and updating data
Browse files Browse the repository at this point in the history
  • Loading branch information
enm10k committed Jun 15, 2024
1 parent ae47daa commit 0690037
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 273 deletions.
2 changes: 1 addition & 1 deletion lib/common/utils.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// https://stackoverflow.com/questions/52632119/in-dart-syntactically-nice-way-to-cast-dynamic-to-given-type-or-return-null
T? cast<T>(final x) => x is T ? x : null;
T? cast<T>(final x) => x is T ? x : null;
70 changes: 17 additions & 53 deletions lib/features/core/components/cell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:nocodb/features/core/components/editors/datetime.dart';
import 'package:nocodb/features/core/components/modal_editor_wrapper.dart';
import 'package:nocodb/features/core/pages/attachment_editor.dart';
import 'package:nocodb/features/core/providers/providers.dart';
import 'package:nocodb/features/core/utils.dart';
import 'package:nocodb/nocodb_sdk/models.dart';
import 'package:nocodb/nocodb_sdk/symbols.dart';
import 'package:nocodb/nocodb_sdk/types.dart';
Expand Down Expand Up @@ -67,69 +68,32 @@ class Cell {
onTap: _onTap(),
);

Function() _checkboxOnTap() => () async {
await ref
.watch(dataRowsProvider.notifier)
.updateRow(
rowId: rowId!,
data: {column.title: value != true},
)
.then(
(final _) => notifySuccess(
context,
message: value == true ? 'Checked' : 'Unchecked',
),
)
.onError(
(final error, final stackTrace) =>
notifyError(context, error, stackTrace),
);
};

Future<void> update({
required final rowId,
required final title,
required final value,
required final BuildContext context,
required final WidgetRef ref,
required final NcView view,
}) async {
await ref
.watch(dataRowsProvider.notifier)
.updateRow(
rowId: rowId,
data: {title: value},
)
.then(
(final _) => notifySuccess(context, message: 'Updated'),
)
.onError(
(final error, final stackTrace) =>
notifyError(context, error, stackTrace),
);
}

Function() _onTap() {
if (rowId == null) {
return () => notifyError(context, 'Table has no PK.', null);
}

final view = ref.watch(viewProvider)!;
updateWrapper(final String value) async {
await update(
rowId: rowId,
title: column.title,
value: value,
context: context,
ref: ref,
view: view,
);
await upsert(context, ref, rowId!, {
column.title: value,
});
}

switch (c.uidt) {
case UITypes.checkbox:
assert(value is bool?);
return _checkboxOnTap();
return () async {
await upsert(
context,
ref,
rowId,
{column.title: value != true},
onUpdate: (final _) => notifySuccess(
context,
message: value != true ? 'Checked' : 'Unchecked',
),
);
};
case UITypes.dateTime:
case UITypes.date:
case UITypes.time:
Expand Down Expand Up @@ -266,7 +230,7 @@ class Cell {
);
case UITypes.links:
final number = cast<num>(value);
final unit = (number!= null && 1 < number) ? c.plural : c.singular;
final unit = (number != null && 1 < number) ? c.plural : c.singular;

return SimpleText('$number $unit');
case UITypes.attachment:
Expand Down
59 changes: 23 additions & 36 deletions lib/features/core/components/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:nocodb/common/flash_wrapper.dart';
import 'package:nocodb/common/logger.dart';
import 'package:nocodb/features/core/components/editors/attachment.dart';
import 'package:nocodb/features/core/components/editors/checkbox.dart';
Expand All @@ -12,8 +11,8 @@ import 'package:nocodb/features/core/components/editors/link_to_another_record_b
import 'package:nocodb/features/core/components/editors/multi_select.dart';
import 'package:nocodb/features/core/components/editors/single_select.dart';
import 'package:nocodb/features/core/components/editors/text_editor.dart';
import 'package:nocodb/features/core/pages/row_editor.dart';
import 'package:nocodb/features/core/providers/providers.dart';
import 'package:nocodb/features/core/utils.dart';
import 'package:nocodb/nocodb_sdk/models.dart' as model;
import 'package:nocodb/nocodb_sdk/models.dart';
import 'package:nocodb/nocodb_sdk/symbols.dart';
Expand All @@ -38,57 +37,35 @@ class Editor extends HookConsumerWidget {
'column: ${column.title}, rqd: ${column.rqd}, rowId: $rowId, value: $value',
);

final onUpdate = isNew
? (final Map<String, dynamic> data) {
final form = ref.read(formProvider);
final newForm = {...form, ...data};
logger.info('form updated: $newForm');
ref.watch(formProvider.notifier).state = newForm;
}
: (final data) async {
await ref
.read(dataRowsProvider.notifier)
.updateRow(
rowId: rowId!,
data: data,
)
.then(
(final _) {
if (context.mounted) {
notifySuccess(context, message: 'Updated.');
}
},
).onError(
(final error, final stackTrace) =>
notifyError(context, error, stackTrace),
);
};
onUpdateWrapper(final NcRow row) async {
await upsert(context, ref, rowId, row);
}

switch (column.uidt) {
case UITypes.checkbox:
return CheckboxEditor(
column: column,
initialValue: value == true,
onUpdate: onUpdate,
onUpdate: onUpdateWrapper,
);
case UITypes.singleSelect:
return SingleSelectEditor(
column: column,
initialValue: value,
onUpdate: onUpdate,
onUpdate: onUpdateWrapper,
);
case UITypes.multiSelect:
final List<String> initialValue =
value is String ? (value as String).split(',') : [];
return MultiSelectEditor(
column: column,
initialValue: initialValue..sort(),
onUpdate: onUpdate,
onUpdate: onUpdateWrapper,
);
case UITypes.number:
return TextEditor(
column: column,
onUpdate: onUpdate,
onUpdate: onUpdateWrapper,
initialValue: value,
isNew: isNew,
maxLines: null,
Expand All @@ -100,7 +77,7 @@ class Editor extends HookConsumerWidget {
case UITypes.decimal:
return TextEditor(
column: column,
onUpdate: onUpdate,
onUpdate: onUpdateWrapper,
initialValue: value,
isNew: isNew,
maxLines: null,
Expand All @@ -113,7 +90,7 @@ class Editor extends HookConsumerWidget {
case UITypes.longText:
return TextEditor(
column: column,
onUpdate: onUpdate,
onUpdate: onUpdateWrapper,
initialValue: value,
isNew: isNew,
maxLines: null,
Expand Down Expand Up @@ -144,20 +121,30 @@ class Editor extends HookConsumerWidget {
case UITypes.time:
return DateTimeEditor(
column: column,
onUpdate: onUpdate,
onUpdate: onUpdateWrapper,
initialValue: value,
type: DateTimeType.fromUITypes(column.uidt),
);
case UITypes.attachment:
// return ProviderScope(
// overrides: [
// formProvider.overrideWith((final ref) => {}),
// ],
// child: AttachmentEditor(
// rowId: rowId,
// column: column,
// onUpdate: onUpdateWrapper,
// ),
// );
return AttachmentEditor(
rowId: rowId,
column: column,
onUpdate: onUpdate,
onUpdate: onUpdateWrapper,
);
default:
return TextEditor(
column: column,
onUpdate: onUpdate,
onUpdate: onUpdateWrapper,
initialValue: value,
isNew: isNew,
);
Expand Down
33 changes: 2 additions & 31 deletions lib/features/core/components/editors/attachment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,15 @@ class AttachmentEditor extends HookConsumerWidget {

@override
Widget build(final BuildContext context, final WidgetRef ref) {
if (rowId == null) {
return InkWell(
onTap: () async {
await showDialog(
context: context,
builder: (final context) => AlertDialog(
title: const Text('Record is not yet created.'),
content: const Text(
'Once record is created, you can attach files.',
),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('OK'),
),
],
),
);
},
child: buildEmpty(),
);
}

final view = ref.watch(viewProvider);
if (view == null) {
return const SizedBox();
}
final files = ref.watch(attachmentsProvider(view, rowId, column.title));
final files = ref.watch(attachmentsProvider(rowId, column.title));

return GestureDetector(
onTap: () async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (final context) =>
AttachmentEditorPage(rowId!, column.title),
AttachmentEditorPage(rowId, column.title),
),
);
},
Expand Down
Loading

0 comments on commit 0690037

Please sign in to comment.