Skip to content

Commit

Permalink
Fix providers
Browse files Browse the repository at this point in the history
  • Loading branch information
enm10k committed Jun 13, 2024
1 parent ad6fb78 commit 9cbe883
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 136 deletions.
6 changes: 3 additions & 3 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ SPEC CHECKSUMS:
file_picker: 09aa5ec1ab24135ccd7a1621c46c84134bfd6655
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
flutter_downloader: b7301ae057deadd4b1650dc7c05375f10ff12c39
flutter_secure_storage: 23fc622d89d073675f2eaa109381aefbcf5a49be
flutter_secure_storage: d33dac7ae2ea08509be337e775f6b59f1ff45f12
image_picker_ios: c560581cceedb403a6ff17f2f816d7fea1421fc1
path_provider_foundation: 3784922295ac71e43754bd15e0653ccfd36a147c
path_provider_foundation: 2b6b4c569c0fb62ec74538f866245ac84301af46
SDWebImage: dfe95b2466a9823cf9f0c6d01217c06550d7b29a
shared_preferences_foundation: b4c3b4cddf1c21f02770737f147a3f5da9d39695
shared_preferences_foundation: fcdcbc04712aee1108ac7fda236f363274528f78
sqflite: 673a0e54cc04b7d6dba8d24fb8095b31c3a99eec
SwiftyGif: 706c60cf65fa2bc5ee0313beece843c8eb8194d4

Expand Down
19 changes: 4 additions & 15 deletions lib/features/core/components/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,10 @@ class Editor extends HookConsumerWidget {
type: DateTimeType.fromUITypes(column.uidt),
);
case UITypes.attachment:
return ProviderScope(
overrides: [
attachmentEditorProvider.overrideWith((ref) {
return (value ?? [])
.map<NcAttachedFile>(
(e) => NcAttachedFile.fromJson(e as Map<String, dynamic>),
)
.toList();
}),
],
child: AttachmentEditor(
rowId: rowId,
column: column,
onUpdate: onUpdate,
),
return AttachmentEditor(
rowId: rowId,
column: column,
onUpdate: onUpdate,
);
default:
return TextEditor(
Expand Down
23 changes: 1 addition & 22 deletions lib/features/core/components/editors/attachment.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

Expand Down Expand Up @@ -56,31 +55,11 @@ class AttachmentEditor extends HookConsumerWidget {
);
}

// TODO: Organize initialization.
// NOTE: The initialization process needs to be the same for both AttachmentEditor and AttachmentEditorPage.
// If they differ, file synchronization will be lost when navigating between the two components.
final view = ref.watch(viewProvider);
if (view == null) {
return const SizedBox();
}
final table = ref.watch(tableProvider);
final rows = ref.watch(dataRowsProvider(view)).valueOrNull;
if (table == null || rows == null) {
return const SizedBox();
}

final row = rows.list.firstWhereOrNull((row) {
return table.getPkFromRow(row) == rowId;
});
if (row == null) {
return const SizedBox();
}

final files = (row[column.title] ?? [])
.map<NcAttachedFile>(
(e) => NcAttachedFile.fromJson(e as Map<String, dynamic>),
)
.toList() as List<NcAttachedFile>;
final files = ref.watch(attachmentsProvider(view, rowId, column.title));

return GestureDetector(
onTap: () {
Expand Down
32 changes: 4 additions & 28 deletions lib/features/core/pages/attachment_editor.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:io';

import 'package:collection/collection.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
Expand Down Expand Up @@ -58,7 +57,7 @@ class AttachmentEditorPage extends HookConsumerWidget {
Future<void> uploadFile(
BuildContext context,
WidgetRef ref,
Refreshable<AttachedFiles> notifier,
Refreshable<Attachments> notifier,
FileUploadType type,
FnOnUpdate onUpdate,
) async {
Expand Down Expand Up @@ -109,7 +108,7 @@ class AttachmentEditorPage extends HookConsumerWidget {
Future<void> downloadFile(
BuildContext context,
WidgetRef ref,
Refreshable<AttachedFiles> notifier,
Refreshable<Attachments> notifier,
NcAttachedFile file,
) async {
final downloadDir = await getFileDownloadDirectory();
Expand Down Expand Up @@ -202,26 +201,10 @@ class AttachmentEditorPage extends HookConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
// TODO: Organize initialization.
// NOTE: The initialization process needs to be the same for both AttachmentEditor and AttachmentEditorPage.
// If they differ, file synchronization will be lost when navigating between the two components.
final view = ref.watch(viewProvider);
if (view == null) {
return const SizedBox();
}
final table = ref.watch(tableProvider);
final rows = ref.watch(dataRowsProvider(view)).valueOrNull;
if (table == null || rows == null) {
return const SizedBox();
}

final row = rows.list.firstWhereOrNull((row) {
return table.getPkFromRow(row) == rowId;
});
if (row == null) {
return const SizedBox();
}

onUpdate(row) {
final dataRowsNotifier = ref.read(dataRowsProvider(view).notifier);
dataRowsNotifier
Expand All @@ -234,16 +217,9 @@ class AttachmentEditorPage extends HookConsumerWidget {
);
}

// TODO: Adjust the design when files is empty.
final files = (row[columnTitle] ?? [])
.map<NcAttachedFile>(
(e) => NcAttachedFile.fromJson(e as Map<String, dynamic>),
)
.toList() as List<NcAttachedFile>;
logger.info(files);

final provider = attachedFilesProvider(files, columnTitle);
final provider = attachmentsProvider(view, rowId, columnTitle);
final notifier = provider.notifier;
final files = ref.watch(provider);

return Scaffold(
appBar: AppBar(
Expand Down
11 changes: 8 additions & 3 deletions lib/features/core/pages/row_editor.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
Expand Down Expand Up @@ -86,8 +87,12 @@ class RowEditor extends HookConsumerWidget {
required BuildContext context,
required String? rowId,
}) {
final rowData =
rowId != null ? ref.watch(dataRowProvider(view, rowId)) : {};
final rows = ref.watch(dataRowsProvider(view)).valueOrNull?.list ?? [];
final table = ref.watch(tableProvider);
final rowData = rows.firstWhereOrNull((row) {
return table?.getPkFromRow(row) == rowId;
}) ??
{};

final viewColumns = ref.watch(viewColumnListProvider(view.id)).valueOrNull;
assert(viewColumns != null);
Expand All @@ -113,7 +118,7 @@ class RowEditor extends HookConsumerWidget {
);

return [...rqds, ...optionals].map((c) {
final initialValue = rowData?[c.title];
final initialValue = rowData[c.title];

return Column(
children: [
Expand Down
63 changes: 39 additions & 24 deletions lib/features/core/providers/providers.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:core';

import 'package:collection/collection.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
Expand Down Expand Up @@ -228,7 +226,6 @@ class DataRows extends _$DataRows {
return populate(rowList, table, tables.relationMap);
}

// Future<void> fetchMore(NcPageInfo pageInfo) async {
Future<void> loadNextPage() async {
final isLoaded = ref.watch(isLoadedProvider);
if (!isLoaded) {
Expand Down Expand Up @@ -354,8 +351,34 @@ class DataRows extends _$DataRows {
);
return newRow;
}

Map<String, dynamic> getRow(String? rowId) {
if (rowId == null) {
return {};
}

final table = ref.watch(tableProvider);
final rows = state.valueOrNull?.list ?? [];
return rows.firstWhereOrNull((row) {
return table?.getPkFromRow(row) == rowId;
}) ??
{};
}
}

// @riverpod
// class DataRow extends _$DataRow {
// @override
// Map<String, dynamic>? build(NcView view, String? rowId) {
// final table = ref.watch(tableProvider);
// final rows = ref.watch(dataRowsProvider(view)).valueOrNull;
//
// return rows?.list.firstWhereOrNull((row) {
// return table?.getPkFromRow(row) == rowId;
// });
// }
// }

final rowNestedWhereProvider =
StateProvider.family<Where?, NcTableColumn>((ref, column) => null);

Expand Down Expand Up @@ -526,29 +549,21 @@ class SortList extends _$SortList {
}

@riverpod
class DataRow extends _$DataRow {
class Attachments extends _$Attachments {
@override
Map<String, dynamic>? build(NcView view, String rowId) {
List<NcAttachedFile> build(NcView view, String? rowId, String columnTitle) {
final rows = ref.watch(dataRowsProvider(view)).valueOrNull?.list ?? [];
final table = ref.watch(tableProvider);
final rows = ref.watch(dataRowsProvider(view)).valueOrNull;
if (table == null || rows == null) {
return null;
}

return rows.list.firstWhereOrNull((row) {
return table.getPkFromRow(row) == rowId;
});
}
}

@riverpod
List<NcAttachedFile> attachmentEditor(AttachmentEditorRef ref) =>
throw UnimplementedError();

@riverpod
class AttachedFiles extends _$AttachedFiles {
@override
List<NcAttachedFile> build(List<NcAttachedFile> files, String columnTitle) {
final row = rows.firstWhereOrNull((row) {
return table?.getPkFromRow(row) == rowId;
}) ??
{};

final files = (row[columnTitle] ?? [])
.map<NcAttachedFile>(
(e) => NcAttachedFile.fromJson(e as Map<String, dynamic>),
)
.toList() as List<NcAttachedFile>;
return files;
}

Expand Down
Loading

0 comments on commit 9cbe883

Please sign in to comment.