fluxcloud/lib/sftp_explorer.dart

207 lines
7.3 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:file_selector/file_selector.dart';
2025-07-27 09:56:46 +05:00
import 'package:flutter/material.dart';
2025-08-10 07:10:30 +05:00
import 'package:fluxcloud/main.dart';
2025-08-14 07:07:50 +05:00
import 'package:fluxcloud/sftp_provider.dart';
import 'package:provider/provider.dart';
2025-07-27 09:56:46 +05:00
import 'widgets/operation_buttons.dart';
2025-08-14 07:07:50 +05:00
class SftpExplorer extends StatelessWidget {
const SftpExplorer({super.key});
2025-07-27 09:56:46 +05:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
toolbarHeight: 75,
2025-07-27 09:56:46 +05:00
title: Text('Explorer'),
elevation: 2,
actionsPadding: EdgeInsets.only(right: 20),
leading: IconButton(
onPressed: () {
2025-08-14 07:07:50 +05:00
if (context.read<SftpProvider>().path == '/') {
// TODO: figure this out
// Navigator.pop(context);
}
else {
2025-08-14 07:07:50 +05:00
context.read<SftpProvider>().goToPrevDir();
}
},
icon: Icon(Icons.arrow_back)
),
actions: [
2025-08-14 07:07:50 +05:00
Selector<SftpProvider, double?>(
selector: (_, sftpProvider) => sftpProvider.uploadProgress,
builder: (_, uploadProgress, __) => uploadProgress != null ? Stack(
alignment: Alignment.center,
children: [
TweenAnimationBuilder<double>(
tween: Tween(begin: 0, end: uploadProgress),
duration: Duration(milliseconds: 300),
builder: (context, value, _) => CircularProgressIndicator(strokeWidth: 3, value: value,)
),
IconButton(
onPressed: () {
// TODO: show upload details here
},
icon: Icon(Icons.upload)
),
]
) : const SizedBox.shrink(),
),
2025-08-14 07:07:50 +05:00
Selector<SftpProvider, double?>(
selector: (_, sftpProvider) => sftpProvider.downloadProgress,
builder: (_, downloadProgress, __) => downloadProgress != null ? Stack(
alignment: Alignment.center,
children: [
TweenAnimationBuilder<double>(
tween: Tween(begin: 0, end: downloadProgress),
duration: Duration(milliseconds: 300),
builder: (context, value, _) => CircularProgressIndicator(strokeWidth: 3, value: value,)
),
IconButton(
onPressed: () {
// TODO: show donwload details here
},
icon: Icon(Icons.download)
),
]
) : const SizedBox.shrink(),
),
],
2025-07-27 09:56:46 +05:00
),
floatingActionButton: _buildFABs(context),
2025-08-08 10:33:42 +05:00
body: PopScope(
canPop: false,
onPopInvokedWithResult: (_, _) {
2025-08-14 07:07:50 +05:00
if (context.read<SftpProvider>().path != '/') {
context.read<SftpProvider>().goToPrevDir();
2025-08-08 10:33:42 +05:00
}
},
2025-08-14 07:07:50 +05:00
child: Consumer<SftpProvider>(
builder: (_, sftpProvider, __) => AnimatedSwitcher(
duration: Duration(milliseconds: 300),
transitionBuilder: (child, animation) {
final curved = CurvedAnimation(
parent: animation,
curve: Curves.fastOutSlowIn
2025-08-08 10:33:42 +05:00
);
2025-08-14 07:07:50 +05:00
return FadeTransition(
opacity: curved,
child: ScaleTransition(
scale: Tween<double>(
begin: 0.92,
end: 1
).animate(curved),
child: child,
),
);
},
child: sftpProvider.isLoading ? Center(child: CircularProgressIndicator()) : ListView.builder(
key: ValueKey(sftpProvider.path),
itemCount: sftpProvider.dirContents.length,
itemBuilder: (context, index) {
final dirEntry = sftpProvider.dirContents[index];
return ListTile(
leading: Icon(dirEntry.attr.isDirectory ? Icons.folder : Icons.description),
title: Text(dirEntry.filename),
trailing: OperationButtons(dirEntries: [dirEntry],),
onTap: () {
if (dirEntry.attr.isDirectory) {
sftpProvider.goToDir('${sftpProvider.path}${dirEntry.filename}/');
}
},
);
},
)
),
2025-08-08 10:33:42 +05:00
),
2025-07-27 09:56:46 +05:00
)
);
}
Widget _buildFABs(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.min,
spacing: 10,
children: [
FloatingActionButton(
heroTag: 'create-new-folder',
onPressed: () {
final nameController = TextEditingController();
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Create new folder'),
content: TextField(
controller: nameController,
decoration: InputDecoration(
labelText: 'Enter folder name'
),
),
actions: [
TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')),
TextButton(
onPressed: () async {
2025-08-14 07:07:50 +05:00
final sftpProvider = context.read<SftpProvider>();
2025-08-02 08:59:05 +05:00
try {
2025-08-14 07:07:50 +05:00
await sftpProvider.sftpWorker.mkdir('${sftpProvider.path}${nameController.text}');
sftpProvider.listDir();
2025-08-02 08:59:05 +05:00
}
catch (e) {
if (context.mounted) {
2025-08-10 07:10:30 +05:00
ScaffoldMessenger.of(context).showSnackBar(buildErrorSnackBar(context, e.toString()));
2025-08-02 08:59:05 +05:00
}
}
if (context.mounted) {
Navigator.pop(context);
}
},
child: Text('Ok')
),
],
)
);
},
child: Icon(Icons.create_new_folder),
),
FloatingActionButton(
heroTag: 'upload-file',
onPressed: () async {
2025-08-14 07:07:50 +05:00
final sftpProvider = context.read<SftpProvider>();
final List<String> filePaths;
if (Platform.isAndroid | Platform.isIOS) {
final res = await FilePicker.platform.pickFiles(allowMultiple: true);
filePaths = (res?.paths ?? []).whereType<String>().toList();
2025-07-28 09:20:08 +05:00
}
else {
final files = await openFiles();
filePaths = files.map((file) => file.path).toList();
}
for (final filePath in filePaths) {
try {
2025-08-14 07:07:50 +05:00
await for (final progress in sftpProvider.sftpWorker.uploadFile(sftpProvider.path, filePath)) {
sftpProvider.setUploadProgress(progress);
}
2025-08-14 07:07:50 +05:00
await sftpProvider.listDir();
}
catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(buildErrorSnackBar(context, e.toString()));
}
}
}
2025-08-14 07:07:50 +05:00
sftpProvider.setUploadProgress(null);
sftpProvider.listDir();
},
child: Icon(Icons.upload),
),
],
);
}
2025-07-27 09:56:46 +05:00
}