Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preview for html and csv added #175

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/consts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ const Map<String, Map<String, List<ResponseBodyView>>>
kTypeText: {
kSubTypeDefaultViewOptions: kRawBodyViewOptions,
kSubTypeCss: kCodeRawBodyViewOptions,
kSubTypeHtml: kCodeRawBodyViewOptions,
kSubTypeHtml: kPreviewCodeRawBodyViewOptions,
kSubTypeCsv: kPreviewCodeRawBodyViewOptions,
kSubTypeJavascript: kCodeRawBodyViewOptions,
kSubTypeMarkdown: kCodeRawBodyViewOptions,
kSubTypeTextXml: kCodeRawBodyViewOptions,
Expand Down Expand Up @@ -483,9 +484,15 @@ const kImageError =
const kSvgError =
"There seems to be an issue rendering this SVG image. Please raise an issue in API Dash GitHub repo so that we can resolve it.";

const kCsvError =
"There seems to be an issue rendering this CSV image. Please raise an issue in API Dash GitHub repo so that we can resolve it.";

const kPdfError =
"There seems to be an issue rendering this pdf. Please raise an issue in API Dash GitHub repo so that we can resolve it.";

const kHtmlError =
"There seems to be an issue rendering this HTML document. Please raise an issue in API Dash GitHub repo so that we can resolve it.";

const kAudioError =
"There seems to be an issue playing this audio. Please raise an issue in API Dash GitHub repo so that we can resolve it.";

Expand Down
75 changes: 75 additions & 0 deletions lib/widgets/previewer.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'dart:convert';
import 'package:csv/csv.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import 'package:printing/printing.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:vector_graphics_compiler/vector_graphics_compiler.dart';
Expand Down Expand Up @@ -30,6 +33,21 @@ class Previewer extends StatefulWidget {
}

class _PreviewerState extends State<Previewer> {
late List<List<dynamic>> csvData;

@override
void initState() {
super.initState();
csvData = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is csvData state being maintained in Previewer Widget.
Everything related to csv shall happen in a separate widget and the purpose of this widget is to call the right widget based on mimetype.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should i create a new widget for CsvPreviewer and move the logic there?

Copy link
Contributor Author

@BrawlerXull BrawlerXull Feb 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am really sorry for the silly mistakes I'll take care of writting clean code from now onwards

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it should be a new widget.
Also, CSV & HTML previewers should be in separate PRs.
Not the same PR.

}

void processCsv(String body) {
print("hello");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is print("hello") here?

csvData = const CsvToListConverter().convert(body, eol: '\n');
print(csvData);
setState(() {});
}

@override
Widget build(BuildContext context) {
if (widget.type == kTypeApplication && widget.subtype == kSubTypeJson) {
Expand All @@ -54,6 +72,63 @@ class _PreviewerState extends State<Previewer> {
return const ErrorMessage(message: kSvgError);
}
}
if (widget.type == kTypeText && widget.subtype == kSubTypeHtml) {
try {
return SingleChildScrollView(
child: Row(
children: [
Flexible(
child: HtmlWidget(
widget.body,
),
),
],
),
);
} catch (e) {
return const ErrorMessage(message: kHtmlError);
}
}
if (widget.type == kTypeText && widget.subtype == kSubTypeCsv) {
processCsv(widget.body);
try {
print(csvData[1]);

return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: DataTable(
columns: csvData[0]
.map(
(item) => DataColumn(
label: Text(
item.toString(),
),
),
)
.toList(),
rows: csvData
.skip(1)
.map(
(csvrow) => DataRow(
cells: csvrow
.map(
(csvItem) => DataCell(
Text(
csvItem.toString(),
),
),
)
.toList(),
),
)
.toList(),
),
);
} catch (e) {
return const ErrorMessage(message: kCsvError);
}
}

if (widget.type == kTypeImage) {
return Image.memory(
widget.bytes,
Expand Down
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ dependencies:
url: https://github.com/foss42/json_data_explorer.git
ref: 9fa58d7b51e65174ab11cbcae17bba88a4194dde
scrollable_positioned_list: ^0.2.3

flutter_widget_from_html: ^0.14.11
file_picker: ^6.1.1
flutter_svg: ^2.0.9
vector_graphics_compiler: ^1.1.9+1
code_builder: ^4.9.0
dart_style: ^2.3.4
json_text_field: ^1.1.0
csv: ^5.1.1

dev_dependencies:
flutter_test:
Expand Down
45 changes: 45 additions & 0 deletions test/widgets/previewer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,49 @@ void main() {
await tester.pumpAndSettle();
expect(find.text(kSvgError), findsOneWidget);
});

testWidgets('Testing when type/subtype is text/csv', (tester) async {
String csvDataString =
'Id,Name,Age\n1,John Doe,40\n2,Dbestech,41\n3,Voldermort,71\n4,Joe Biden,80\n5,Ryo Hanamura,35';

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Previewer(
type: kTypeText,
subtype: kSubTypeCsv,
bytes: Uint8List.fromList([]),
body: csvDataString,
),
),
),
);

expect(find.byType(DataTable), findsOneWidget);
expect(find.text('John Doe'), findsOneWidget);
expect(find.text('41'), findsOneWidget);
});
testWidgets('Testing when type/subtype is text/html', (tester) async {

String htmlDataString = '<div><p>Hello, <strong>World</strong>!</p></div>';

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Previewer(
type: kTypeText,
subtype: kSubTypeHtml,
bytes: Uint8List.fromList([]),
body: htmlDataString,
),
),
),
);

expect(find.byType(SingleChildScrollView), findsOneWidget);
expect(find.byType(Row), findsOneWidget);

expect(find.text('Hello, '), findsOneWidget);
expect(find.text('World'), findsOneWidget);
});
}