mirror of
https://github.com/foss42/apidash.git
synced 2025-08-06 05:32:26 +08:00
refactor importer
This commit is contained in:
50
lib/importer/import_dialog.dart
Normal file
50
lib/importer/import_dialog.dart
Normal file
@ -0,0 +1,50 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:apidash/providers/providers.dart';
|
||||
import 'package:apidash/widgets/widgets.dart';
|
||||
import 'importer.dart';
|
||||
|
||||
void importToCollectionPane(
|
||||
BuildContext context,
|
||||
WidgetRef ref,
|
||||
ScaffoldMessengerState sm,
|
||||
) {
|
||||
// TODO: The dialog must have a feature to paste contents in a text field
|
||||
// Also, a mechanism can be added where on importing a file it shows the
|
||||
// contents in the text field and then the user presses ok to add it to collection
|
||||
showImportDialog(
|
||||
context: context,
|
||||
importFormat: ref.watch(importFormatStateProvider),
|
||||
onImportFormatChange: (format) {
|
||||
if (format != null) {
|
||||
ref.read(importFormatStateProvider.notifier).state = format;
|
||||
}
|
||||
},
|
||||
onFileDropped: (file) {
|
||||
final importFormatType = ref.read(importFormatStateProvider);
|
||||
sm.hideCurrentSnackBar();
|
||||
file.readAsString().then(
|
||||
(content) {
|
||||
kImporter
|
||||
.getHttpRequestModel(importFormatType, content)
|
||||
.then((importedRequestModel) {
|
||||
if (importedRequestModel != null) {
|
||||
ref
|
||||
.read(collectionStateNotifierProvider.notifier)
|
||||
.addRequestModel(importedRequestModel);
|
||||
if (!context.mounted) return;
|
||||
Navigator.of(context).pop();
|
||||
} else {
|
||||
var err = "Unable to parse ${file.name}";
|
||||
sm.showSnackBar(getSnackBar(err, small: false));
|
||||
}
|
||||
});
|
||||
},
|
||||
onError: (e) {
|
||||
var err = "Unable to import ${file.name}";
|
||||
sm.showSnackBar(getSnackBar(err, small: false));
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
Reference in New Issue
Block a user