Skip to content

Commit

Permalink
Improvement: There should be a simple function to check if a file exi…
Browse files Browse the repository at this point in the history
…sts in the cloud.

Fix: Function getAllFiles for Google Drive should return more than 100 files.
  • Loading branch information
timobaehr committed Mar 22, 2024
1 parent 4dde718 commit f779c63
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 164 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
## 0.0.16

* Improvement: There should be a simple function to check if a file exists in the cloud.
* Fix: Function getAllFiles for Google Drive should return more than 100 files
* Refactor: Update libraries google_sign_in and googleapis

## 0.0.15

* Login at web should work
* Fix: Login at web should work

## 0.0.13

Expand Down
181 changes: 110 additions & 71 deletions example/lib/cloud_storage_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class _GoogleDriveDemoState extends State<GoogleDriveDemo> {
body: FutureBuilder(
future: service,
builder: (context, AsyncSnapshot<CloudStorageService> snapshot) {
// FIXME google_sign_in_web
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
Expand Down Expand Up @@ -70,7 +69,6 @@ class _GoogleDriveDemoState extends State<GoogleDriveDemo> {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
Expand All @@ -95,86 +93,66 @@ class _GoogleDriveDemoState extends State<GoogleDriveDemo> {
],
),
FutureBuilder(
future: Future.value(svc.getAllFiles()),
future: Future.value(svc.getAllFiles(
ignoreTrashedFiles: false,
)),
builder: (context, snapshot) {
if (snapshot.hasData) {
List files = snapshot.data!;
return Column(
children: [
Text('Amount of files: ${files.length}'),
Container(
height: 500,
padding:
const EdgeInsets.symmetric(horizontal: 15),
child: GridView.count(
crossAxisCount: 2,
crossAxisSpacing: 15,
mainAxisSpacing: 15,
children: files
.map((e) => Card(
downloadFn: ({required file}) async {
final newfile = await svc
.downloadFile(file: file);
showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text(
'Downloaded content'),
content: Text(newfile
?.content ??
'Unable to download file'),
actions: <Widget>[
TextButton(
style:
TextButton.styleFrom(
textStyle:
Theme.of(context)
.textTheme
.labelLarge,
),
child: const Text('Ok'),
onPressed: () {
Navigator.of(context)
.pop();
},
),
],
);
},
);
},
deleteFn: ({required file}) async {
await svc.deleteFile(file: file);
// fixme refresh view!
setState(() {});
},
file: e,
))
.toList(),
),
Wrap(
runSpacing: 16,
spacing: 16,
children: files
.map((e) => Card(
downloadFn: ({required file}) =>
onDownloadFile(
cloudStorageService: svc,
file: file,
),
deleteFn: (
{required CloudFile<dynamic>
file}) =>
onDelete(
cloudStorageService: svc,
file: file,
),
checkIfExistsFn: ({required file}) =>
checkIfExists(
cloudStorageService: svc,
file: file,
),
file: e,
))
.toList(),
),
],
);
}
return const CircularProgressIndicator();
},
),
OutlinedButton(
onPressed: () async {
final List<int> bytes =
utf8.encode('Das Wandern ist des Müllers Lust.');
final file = GoogleDriveFile(
fileId: null,
fileName: 'wandern.txt',
description: 'Über das Wandern',
parents: [],
bytes: bytes,
);
await svc.uploadFile(file: file);
setState(() {}); // refresh view
},
child: const Text('Upload a random.txt file'),
const Expanded(child: SizedBox.shrink()),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16),
child: OutlinedButton(
onPressed: () async {
final List<int> bytes =
utf8.encode('Das Wandern ist des Müllers Lust.');
final file = GoogleDriveFile(
fileId: null,
fileName: 'wandern.txt',
description: 'Über das Wandern',
parents: [],
bytes: bytes,
);
await svc.uploadFile(file: file);
setState(() {}); // refresh view
},
child: const Text('Upload a random.txt file'),
),
),
],
);
Expand All @@ -183,6 +161,55 @@ class _GoogleDriveDemoState extends State<GoogleDriveDemo> {
),
);
}

Future<void> onDownloadFile({
required CloudStorageService cloudStorageService,
required CloudFile<dynamic> file,
}) async {
final newFile = await cloudStorageService.downloadFile(file: file);
if (mounted) {
showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Downloaded content'),
content: Text(newFile?.content ?? 'Unable to download file'),
actions: <Widget>[
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Ok'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
}

Future<void> checkIfExists({
required CloudStorageService cloudStorageService,
required CloudFile<dynamic> file,
}) async {
final scaffoldMessenger = ScaffoldMessenger.of(context);
final doesFileExist = await cloudStorageService.doesFileExist(file: file);
scaffoldMessenger.showSnackBar(SnackBar(
content: Text('doesFileExist: $doesFileExist'),
));
}

Future<void> onDelete({
required CloudStorageService cloudStorageService,
required CloudFile<dynamic> file,
}) async {
await cloudStorageService.deleteFile(file: file);
// fixme refresh view!
setState(() {});
}
}

class Card extends StatelessWidget {
Expand All @@ -196,26 +223,32 @@ class Card extends StatelessWidget {
required CloudFile<dynamic> file,
}) deleteFn;

final Future<void> Function({
required CloudFile<dynamic> file,
}) checkIfExistsFn;

const Card({
Key? key,
required this.file,
required this.downloadFn,
required this.deleteFn,
required this.checkIfExistsFn,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return Container(
width: 75,
height: 75,
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.circular(10),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(file.file.name),
if (file.trashed)
Text('Trashed', style: Theme.of(context).textTheme.bodySmall),
OutlinedButton(
onPressed: () async {
await downloadFn(file: file);
Expand All @@ -231,6 +264,12 @@ class Card extends StatelessWidget {
},
child: const Text('Delete'),
),
OutlinedButton(
onPressed: () async {
await checkIfExistsFn(file: file);
},
child: const Text('Check if a file exists in the cloud'),
),
],
),
);
Expand Down
27 changes: 15 additions & 12 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,37 @@ class _MyHomePageState extends State<MyHomePage> {

@override
Widget build(BuildContext context) {
final storageTypeOptions = StorageType.values
.map<DropdownMenuItem<StorageType>>(
(StorageType storageType) => DropdownMenuItem(
key: Key(storageType.name),
value: storageType,
child: Text(storageType.name),
),
)
.toList();
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
title: Text('${widget.title} - Login'),
),
body: Column(
children: [
const SizedBox(height: 50),
const Center(
child: Text('fl_cloud_storage'),
child: Text('1. Select your vendor'),
),
const SizedBox(height: 25),
DropdownButton<StorageType>(
value: selection,
items: StorageType.values
.map<DropdownMenuItem<StorageType>>(
(StorageType e) => DropdownMenuItem(
key: Key(e.name),
value: e,
child: Text(e.name),
),
)
.toList(),
items: storageTypeOptions,
onChanged: (StorageType? value) {
setState(() {
selection = value;
});
},
),
const Center(
child: Text('2. Select the scope'),
),
if (selection == StorageType.GOOGLE_DRIVE)
DropdownButton<GoogleDriveScope>(
value: driveScope,
Expand Down
2 changes: 2 additions & 0 deletions example/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 google_sign_in_ios

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FLTGoogleSignInPlugin.register(with: registry.registrar(forPlugin: "FLTGoogleSignInPlugin"))
}
Loading

0 comments on commit f779c63

Please sign in to comment.