Skip to content

Commit

Permalink
add file picker service
Browse files Browse the repository at this point in the history
  • Loading branch information
codenameakshay committed Nov 14, 2021
1 parent 92a8cbb commit 42f18dc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
11 changes: 10 additions & 1 deletion lib/pages/home_page.dart
Expand Up @@ -4,6 +4,7 @@ import 'package:desktop_drop/desktop_drop.dart';
import 'package:flutter/material.dart';
import 'package:odin/painters/odin_logo_painter.dart';
import 'package:odin/services/data_service.dart';
import 'package:odin/services/file_picker_service.dart';
import 'package:odin/services/locator.dart';
import 'package:odin/services/zip_service.dart';
import 'package:odin/widgets/window_top_bar.dart';
Expand All @@ -25,6 +26,7 @@ class _HomePageState extends State<HomePage> {
String? _fileLink;
final _ds = locator<DataService>();
final _zs = locator<ZipService>();
final _fps = locator<FilepickerService>();

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -74,7 +76,14 @@ class _HomePageState extends State<HomePage> {
});
},
child: GestureDetector(
onTap: _fileLink != null ? () => launch(_fileLink ?? '') : null,
onTap: _fileLink != null
? () => launch(_fileLink ?? '')
: () async {
final linkFile = await _fps.getFiles();
setState(() {
_fileLink = linkFile;
});
},
child: SizedBox.expand(
child: AnimatedContainer(
duration: const Duration(milliseconds: 300),
Expand Down
31 changes: 31 additions & 0 deletions lib/services/file_picker_service.dart
@@ -0,0 +1,31 @@
import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:odin/services/data_service.dart';
import 'package:odin/services/locator.dart';
import 'package:odin/services/zip_service.dart';

class FilepickerService {
final _dataService = locator<DataService>();
final _zipService = locator<ZipService>();

Future<String?> getFiles() async {
FilePickerResult? result =
await FilePicker.platform.pickFiles(allowMultiple: true);
final String? _path;
if (result != null) {
List<File> files = result.paths.map((path) => File(path!)).toList();
final int length = files.length;
if (length > 1) {
final zippedFile = await _zipService.zipFile(fileToZips: files);
_path = await _dataService.uploadFileAnonymous(zippedFile);
} else {
_path = await _dataService.uploadFileAnonymous(files.first);
}
} else {
// User canceled the picker
_path = null;
}
return _path;
}
}
2 changes: 2 additions & 0 deletions lib/services/locator.dart
@@ -1,5 +1,6 @@
import 'package:get_it/get_it.dart';
import 'package:odin/services/data_service.dart';
import 'package:odin/services/file_picker_service.dart';
import 'package:odin/services/random_service.dart';
import 'package:odin/services/shortner_service.dart';
import 'package:odin/services/zip_service.dart';
Expand All @@ -15,6 +16,7 @@ Future<void> setupLocator() async {
locator.registerLazySingleton<ShortnerService>(() => ShortnerService());
locator.registerLazySingleton<RandomService>(() => RandomService());
locator.registerLazySingleton<ZipService>(() => ZipService());
locator.registerLazySingleton<FilepickerService>(() => FilepickerService());
logger.d('Locator setup in ${stopwatch.elapsed}');
stopwatch.stop();
}
14 changes: 14 additions & 0 deletions pubspec.lock
Expand Up @@ -134,6 +134,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.2"
file_picker:
dependency: "direct main"
description:
name: file_picker
url: "https://pub.dartlang.org"
source: hosted
version: "4.2.3"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -153,6 +160,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
flutter_plugin_android_lifecycle:
dependency: transitive
description:
name: flutter_plugin_android_lifecycle
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.4"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down
6 changes: 1 addition & 5 deletions pubspec.yaml
Expand Up @@ -32,6 +32,7 @@ dependencies:
cupertino_icons: ^1.0.2
desktop_drop: ^0.1.2
dio: ^4.0.3
file_picker: ^4.2.3
flutter:
sdk: flutter
flutter_dotenv: ^5.0.2
Expand All @@ -45,11 +46,6 @@ dependencies:
url_launcher: ^6.0.12

dev_dependencies:
# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^1.0.0
flutter_test:
sdk: flutter
Expand Down

0 comments on commit 42f18dc

Please sign in to comment.