Skip to content

Commit

Permalink
add file reader \ test
Browse files Browse the repository at this point in the history
  • Loading branch information
Luwirispok committed Nov 19, 2022
1 parent b906b19 commit 632a927
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 6 deletions.
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion flutter.compileSdkVersion
compileSdkVersion 33
ndkVersion flutter.ndkVersion

compileOptions {
Expand Down
8 changes: 5 additions & 3 deletions lib/data/repositories/docum/templates.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:developer';

import 'package:dio/dio.dart';
import 'package:fastreport/data/repositories/apiRequests.dart';
import 'package:fastreport/domain/repositories/docum/templates.dart';
Expand All @@ -14,9 +16,9 @@ class TemplatesRepositoryImpl implements TemplatesRepository {

@override
createTemplate(id, name, base64) {
print(id);
print(name);
print(base64);
log(id);
log(name);
log(base64);
ApiRequest.postApiHelper(
Dio(),
{"name": '$name.frx', 'content': '$base64'},
Expand Down
108 changes: 106 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import 'dart:convert';
import 'dart:developer';
import 'dart:io';

import 'package:fastreport/data/repositories/docum/templates.dart';
import 'package:fastreport/domain/repositories/getFolderRepositoryId/getFolderRepositoryId.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

void main() {
runApp(const MyApp());
}

var _file;
var _fileBase64;
var _catalog;


class MyApp extends StatelessWidget {
const MyApp({super.key});

Expand Down Expand Up @@ -34,11 +45,43 @@ class _MyHomePageState extends State<MyHomePage> {
void _incrementCounter() async {
final request = await GetIdentRepositoryImp().getIdForFolder();
final String id = request['id'];
final lists = await TemplatesRepositoryImpl().createTemplate(id, 'Cock',
'PD94bWwgdmVyc2lvbj0iMS4wIiBlb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EYXRhQmFuZCBOYW1lPSJEYXRhMSIgVG9wPSI3NC4xNSIgV2lkdGg9IjcxOC4yIiBIZWlnaHQ9Ijc1LjYiLz4KICAgIDxQYWdlRm9vdGVyQmFuZCBOYW1lPSJQYWdlRm9vdGVyMSIgVG9wPSIxNTMuNzUiIFdpZHRoPSI3MTguMiIgSGVpZ2h0PSIxOC45Ii8+CiAgPC9SZXBvcnRQYWdlPgo8L1JlcG9ydD4=',
);
lists;
}

void _buildFile() async {
FilePickerResult? result = await FilePicker.platform.pickFiles();

if (result != null) {
PlatformFile file = result.files.first;
print(file.name);
print(file.bytes);
print(file.size);
print(file.extension);
print(file.path);
} else {
// User canceled the picker
}

if (result != null) {
File file = File(result.files.single.path!);
_file = file;

log(file.toString());
var fileBytes = file.readAsBytesSync();
// log(fileBytes.toString());
String base64 = base64Encode(fileBytes);
_fileBase64 = base64;
log(base64);
} else {
// User canceled the picker
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -48,6 +91,26 @@ class _MyHomePageState extends State<MyHomePage> {
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
_buildFile();
},
child: const Text('выбор файла'),
),
ElevatedButton(
onPressed: () {
_buildCatalog();
},
child: const Text('каталог'),
),
ElevatedButton(
onPressed: () {
saveFile();
},
child: const Text('Сохранить'),
)
],
),
),
floatingActionButton: FloatingActionButton(
Expand All @@ -57,4 +120,45 @@ class _MyHomePageState extends State<MyHomePage> {
),
);
}

Future<void> _buildCatalog() async {
String? selectedDirectory = await FilePicker.platform.getDirectoryPath();

if (selectedDirectory == null) {
// User canceled the picker
} else{
log(selectedDirectory.toString());
_catalog = selectedDirectory;
}
}

Future<void> _buildSave() async {
String? outputFile = await FilePicker.platform.saveFile(
dialogTitle: 'Please select an output file:',
fileName: 'lib/data/repositories/docum/templates.dart',
);

if (outputFile == null) {
// User canceled the picker
}
}

Future<String> getFilePath() async {
Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
// String appDocumentsPath = '${appDocumentsDirectory.path}/folder';
String appDocumentsPath = _catalog;
String filePath = '$appDocumentsPath/text.txt';
await Directory(appDocumentsPath).create(recursive: true);
log(appDocumentsDirectory.absolute.toString());
log(appDocumentsPath);
return filePath;
}

void saveFile() async {
File file = File(await getFilePath());
log(file.path);
file.writeAsString(
"This is my demo text that will be saved to : demoTextFile.txt");
}

}
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import FlutterMacOS
import Foundation

import path_provider_macos

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
}
125 changes: 125 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
ffi:
dependency: transitive
description:
name: ffi
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
file:
dependency: transitive
description:
name: file
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.4"
file_picker:
dependency: "direct main"
description:
name: file_picker
url: "https://pub.dartlang.org"
source: hosted
version: "5.2.2"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -69,18 +90,37 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
flutter_plugin_android_lifecycle:
dependency: transitive
description:
name: flutter_plugin_android_lifecycle
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.7"
flutter_test:
dependency: "direct dev"
description: flutter
source: sdk
version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.2"
js:
dependency: transitive
description:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.4"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -116,6 +156,76 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.2"
path_provider:
dependency: "direct main"
description:
name: path_provider
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.11"
path_provider_android:
dependency: transitive
description:
name: path_provider_android
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.21"
path_provider_ios:
dependency: transitive
description:
name: path_provider_ios
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.11"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.7"
path_provider_macos:
dependency: transitive
description:
name: path_provider_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.6"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.5"
path_provider_windows:
dependency: transitive
description:
name: path_provider_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
platform:
dependency: transitive
description:
name: platform
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
process:
dependency: transitive
description:
name: process
url: "https://pub.dartlang.org"
source: hosted
version: "4.2.4"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -177,5 +287,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.2"
win32:
dependency: transitive
description:
name: win32
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.1"
xdg_directories:
dependency: transitive
description:
name: xdg_directories
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0+2"
sdks:
dart: ">=2.18.2 <3.0.0"
flutter: ">=3.0.0"
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ dependencies:


cupertino_icons: ^1.0.2
file_picker: ^5.2.2
path_provider: ^2.0.11

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 632a927

Please sign in to comment.