provider yayy
This commit is contained in:
parent
075003f1ad
commit
08bbeb2619
6 changed files with 180 additions and 139 deletions
|
@ -4,8 +4,10 @@ import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||||
import 'package:fluxcloud/connection.dart';
|
import 'package:fluxcloud/connection.dart';
|
||||||
import 'package:fluxcloud/sftp_explorer.dart';
|
import 'package:fluxcloud/sftp_explorer.dart';
|
||||||
|
import 'package:fluxcloud/sftp_provider.dart';
|
||||||
import 'package:fluxcloud/sftp_worker.dart';
|
import 'package:fluxcloud/sftp_worker.dart';
|
||||||
import 'package:fluxcloud/widgets/add_server_modal.dart';
|
import 'package:fluxcloud/widgets/add_server_modal.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
class SftpConnectionList extends StatefulWidget {
|
class SftpConnectionList extends StatefulWidget {
|
||||||
const SftpConnectionList({
|
const SftpConnectionList({
|
||||||
|
@ -80,7 +82,10 @@ class _SftpConnectionListState extends State<SftpConnectionList> {
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
final sftpWorker = await SftpWorker.spawn(_connections[index]);
|
final sftpWorker = await SftpWorker.spawn(_connections[index]);
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
Navigator.push(context, MaterialPageRoute(builder: (context) => SftpExplorer(sftpWorker: sftpWorker,)));
|
Navigator.push(context, MaterialPageRoute(builder: (context) => ChangeNotifierProvider(
|
||||||
|
create: (_) => SftpProvider(sftpWorker),
|
||||||
|
child: SftpExplorer()
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
borderRadius: BorderRadius.circular(10),
|
borderRadius: BorderRadius.circular(10),
|
||||||
|
|
|
@ -1,54 +1,16 @@
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:dartssh2/dartssh2.dart';
|
|
||||||
import 'package:file_picker/file_picker.dart';
|
import 'package:file_picker/file_picker.dart';
|
||||||
import 'package:file_selector/file_selector.dart';
|
import 'package:file_selector/file_selector.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:fluxcloud/main.dart';
|
import 'package:fluxcloud/main.dart';
|
||||||
import 'package:fluxcloud/sftp_worker.dart';
|
import 'package:fluxcloud/sftp_provider.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
import 'widgets/operation_buttons.dart';
|
import 'widgets/operation_buttons.dart';
|
||||||
|
|
||||||
class SftpExplorer extends StatefulWidget {
|
class SftpExplorer extends StatelessWidget {
|
||||||
const SftpExplorer({super.key, required this.sftpWorker});
|
const SftpExplorer({super.key});
|
||||||
|
|
||||||
final SftpWorker sftpWorker;
|
|
||||||
|
|
||||||
@override
|
|
||||||
State<SftpExplorer> createState() => _SftpExplorerState();
|
|
||||||
}
|
|
||||||
|
|
||||||
class _SftpExplorerState extends State<SftpExplorer> {
|
|
||||||
|
|
||||||
String path = '/';
|
|
||||||
|
|
||||||
bool _isLoading = true;
|
|
||||||
late List<SftpName> _dirContents;
|
|
||||||
|
|
||||||
double? _uploadProgress;
|
|
||||||
double? _downloadProgress;
|
|
||||||
|
|
||||||
void _setDownloadProgress(double? progress) => setState(() => _downloadProgress = progress);
|
|
||||||
|
|
||||||
@override
|
|
||||||
void initState() {
|
|
||||||
super.initState();
|
|
||||||
_listDir();
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _listDir() async {
|
|
||||||
setState(() => _isLoading = true);
|
|
||||||
try {
|
|
||||||
_dirContents = await widget.sftpWorker.listdir(path);
|
|
||||||
}
|
|
||||||
catch (e) {
|
|
||||||
if (mounted) {
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(buildErrorSnackBar(context, e.toString()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
setState(() => _isLoading = false);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
@ -60,25 +22,24 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
||||||
actionsPadding: EdgeInsets.only(right: 20),
|
actionsPadding: EdgeInsets.only(right: 20),
|
||||||
leading: IconButton(
|
leading: IconButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
if (path == '/') {
|
if (context.read<SftpProvider>().path == '/') {
|
||||||
// TODO: figure this out
|
// TODO: figure this out
|
||||||
// Navigator.pop(context);
|
// Navigator.pop(context);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
path = path.substring(0, path.length - 1);
|
context.read<SftpProvider>().goToPrevDir();
|
||||||
path = path.substring(0, path.lastIndexOf('/')+1);
|
|
||||||
_listDir();
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
icon: Icon(Icons.arrow_back)
|
icon: Icon(Icons.arrow_back)
|
||||||
),
|
),
|
||||||
actions: [
|
actions: [
|
||||||
if (_uploadProgress != null)
|
Selector<SftpProvider, double?>(
|
||||||
Stack(
|
selector: (_, sftpProvider) => sftpProvider.uploadProgress,
|
||||||
|
builder: (_, uploadProgress, __) => uploadProgress != null ? Stack(
|
||||||
alignment: Alignment.center,
|
alignment: Alignment.center,
|
||||||
children: [
|
children: [
|
||||||
TweenAnimationBuilder<double>(
|
TweenAnimationBuilder<double>(
|
||||||
tween: Tween(begin: 0, end: _uploadProgress),
|
tween: Tween(begin: 0, end: uploadProgress),
|
||||||
duration: Duration(milliseconds: 300),
|
duration: Duration(milliseconds: 300),
|
||||||
builder: (context, value, _) => CircularProgressIndicator(strokeWidth: 3, value: value,)
|
builder: (context, value, _) => CircularProgressIndicator(strokeWidth: 3, value: value,)
|
||||||
),
|
),
|
||||||
|
@ -89,13 +50,15 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
||||||
icon: Icon(Icons.upload)
|
icon: Icon(Icons.upload)
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
) : const SizedBox.shrink(),
|
||||||
),
|
),
|
||||||
if (_downloadProgress != null)
|
Selector<SftpProvider, double?>(
|
||||||
Stack(
|
selector: (_, sftpProvider) => sftpProvider.downloadProgress,
|
||||||
|
builder: (_, downloadProgress, __) => downloadProgress != null ? Stack(
|
||||||
alignment: Alignment.center,
|
alignment: Alignment.center,
|
||||||
children: [
|
children: [
|
||||||
TweenAnimationBuilder<double>(
|
TweenAnimationBuilder<double>(
|
||||||
tween: Tween(begin: 0, end: _downloadProgress),
|
tween: Tween(begin: 0, end: downloadProgress),
|
||||||
duration: Duration(milliseconds: 300),
|
duration: Duration(milliseconds: 300),
|
||||||
builder: (context, value, _) => CircularProgressIndicator(strokeWidth: 3, value: value,)
|
builder: (context, value, _) => CircularProgressIndicator(strokeWidth: 3, value: value,)
|
||||||
),
|
),
|
||||||
|
@ -106,6 +69,7 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
||||||
icon: Icon(Icons.download)
|
icon: Icon(Icons.download)
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
) : const SizedBox.shrink(),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
@ -113,13 +77,12 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
||||||
body: PopScope(
|
body: PopScope(
|
||||||
canPop: false,
|
canPop: false,
|
||||||
onPopInvokedWithResult: (_, _) {
|
onPopInvokedWithResult: (_, _) {
|
||||||
if (path != '/') {
|
if (context.read<SftpProvider>().path != '/') {
|
||||||
path = path.substring(0, path.length - 1);
|
context.read<SftpProvider>().goToPrevDir();
|
||||||
path = path.substring(0, path.lastIndexOf('/')+1);
|
|
||||||
_listDir();
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
child: AnimatedSwitcher(
|
child: Consumer<SftpProvider>(
|
||||||
|
builder: (_, sftpProvider, __) => AnimatedSwitcher(
|
||||||
duration: Duration(milliseconds: 300),
|
duration: Duration(milliseconds: 300),
|
||||||
transitionBuilder: (child, animation) {
|
transitionBuilder: (child, animation) {
|
||||||
final curved = CurvedAnimation(
|
final curved = CurvedAnimation(
|
||||||
|
@ -137,25 +100,25 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
child: _isLoading ? Center(child: CircularProgressIndicator()) : ListView.builder(
|
child: sftpProvider.isLoading ? Center(child: CircularProgressIndicator()) : ListView.builder(
|
||||||
key: ValueKey(path),
|
key: ValueKey(sftpProvider.path),
|
||||||
itemCount: _dirContents.length,
|
itemCount: sftpProvider.dirContents.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
final dirEntry = _dirContents[index];
|
final dirEntry = sftpProvider.dirContents[index];
|
||||||
return ListTile(
|
return ListTile(
|
||||||
leading: Icon(dirEntry.attr.isDirectory ? Icons.folder : Icons.description),
|
leading: Icon(dirEntry.attr.isDirectory ? Icons.folder : Icons.description),
|
||||||
title: Text(dirEntry.filename),
|
title: Text(dirEntry.filename),
|
||||||
trailing: OperationButtons(sftpWorker: widget.sftpWorker, path: path, dirEntries: [dirEntry], listDir: _listDir, setDownloadProgress: _setDownloadProgress,),
|
trailing: OperationButtons(dirEntries: [dirEntry],),
|
||||||
onTap: () {
|
onTap: () {
|
||||||
if (dirEntry.attr.isDirectory) {
|
if (dirEntry.attr.isDirectory) {
|
||||||
path = '$path${dirEntry.filename}/';
|
sftpProvider.goToDir('${sftpProvider.path}${dirEntry.filename}/');
|
||||||
_listDir();
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -183,9 +146,10 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
||||||
TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')),
|
TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')),
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
|
final sftpProvider = context.read<SftpProvider>();
|
||||||
try {
|
try {
|
||||||
await widget.sftpWorker.mkdir('$path${nameController.text}');
|
await sftpProvider.sftpWorker.mkdir('${sftpProvider.path}${nameController.text}');
|
||||||
_listDir();
|
sftpProvider.listDir();
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
|
@ -207,6 +171,7 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
||||||
FloatingActionButton(
|
FloatingActionButton(
|
||||||
heroTag: 'upload-file',
|
heroTag: 'upload-file',
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
|
final sftpProvider = context.read<SftpProvider>();
|
||||||
final List<String> filePaths;
|
final List<String> filePaths;
|
||||||
if (Platform.isAndroid | Platform.isIOS) {
|
if (Platform.isAndroid | Platform.isIOS) {
|
||||||
final res = await FilePicker.platform.pickFiles(allowMultiple: true);
|
final res = await FilePicker.platform.pickFiles(allowMultiple: true);
|
||||||
|
@ -218,10 +183,10 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
||||||
}
|
}
|
||||||
for (final filePath in filePaths) {
|
for (final filePath in filePaths) {
|
||||||
try {
|
try {
|
||||||
await for (final progress in widget.sftpWorker.uploadFile(path, filePath)) {
|
await for (final progress in sftpProvider.sftpWorker.uploadFile(sftpProvider.path, filePath)) {
|
||||||
setState(() => _uploadProgress = progress);
|
sftpProvider.setUploadProgress(progress);
|
||||||
}
|
}
|
||||||
await _listDir();
|
await sftpProvider.listDir();
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
|
@ -229,8 +194,8 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setState(() => _uploadProgress = null);
|
sftpProvider.setUploadProgress(null);
|
||||||
_listDir();
|
sftpProvider.listDir();
|
||||||
},
|
},
|
||||||
child: Icon(Icons.upload),
|
child: Icon(Icons.upload),
|
||||||
),
|
),
|
||||||
|
|
57
lib/sftp_provider.dart
Normal file
57
lib/sftp_provider.dart
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
import 'package:dartssh2/dartssh2.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:fluxcloud/sftp_worker.dart';
|
||||||
|
|
||||||
|
class SftpProvider extends ChangeNotifier {
|
||||||
|
final SftpWorker _sftpWorker;
|
||||||
|
|
||||||
|
String _path = '/';
|
||||||
|
bool _isLoading = false;
|
||||||
|
late List<SftpName> _dirContents;
|
||||||
|
|
||||||
|
double? _uploadProgress;
|
||||||
|
double? _downloadProgress;
|
||||||
|
|
||||||
|
SftpProvider(this._sftpWorker) {
|
||||||
|
listDir();
|
||||||
|
}
|
||||||
|
|
||||||
|
SftpWorker get sftpWorker => _sftpWorker;
|
||||||
|
|
||||||
|
String get path => _path;
|
||||||
|
bool get isLoading => _isLoading;
|
||||||
|
List<SftpName> get dirContents => _dirContents;
|
||||||
|
|
||||||
|
double? get uploadProgress => _uploadProgress;
|
||||||
|
double? get downloadProgress => _downloadProgress;
|
||||||
|
|
||||||
|
Future<void> listDir() async {
|
||||||
|
_isLoading = true;
|
||||||
|
notifyListeners();
|
||||||
|
_dirContents = await _sftpWorker.listdir(_path);
|
||||||
|
_isLoading = false;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
void goToPrevDir() {
|
||||||
|
_path = _path.substring(0, _path.length - 1);
|
||||||
|
_path = _path.substring(0, _path.lastIndexOf('/')+1);
|
||||||
|
listDir();
|
||||||
|
}
|
||||||
|
|
||||||
|
void goToDir(String path) {
|
||||||
|
_path = path;
|
||||||
|
listDir();
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUploadProgress(double? progress) {
|
||||||
|
_uploadProgress = progress;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
void setDownloadProgress(double? progress) {
|
||||||
|
_downloadProgress = progress;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,23 +1,20 @@
|
||||||
import 'package:dartssh2/dartssh2.dart';
|
import 'package:dartssh2/dartssh2.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:fluxcloud/main.dart';
|
import 'package:fluxcloud/main.dart';
|
||||||
import 'package:fluxcloud/sftp_worker.dart';
|
import 'package:fluxcloud/sftp_provider.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
class OperationButtons extends StatelessWidget {
|
class OperationButtons extends StatelessWidget {
|
||||||
const OperationButtons({
|
const OperationButtons({
|
||||||
super.key,
|
super.key, required this.dirEntries,
|
||||||
required this.sftpWorker, required this.path, required this.dirEntries, required this.listDir, required this.setDownloadProgress,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
final SftpWorker sftpWorker;
|
|
||||||
final String path;
|
|
||||||
final List<SftpName> dirEntries;
|
final List<SftpName> dirEntries;
|
||||||
final Function listDir;
|
|
||||||
final Function(double? progress) setDownloadProgress;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
final sftpProvider = context.read<SftpProvider>();
|
||||||
return Row(
|
return Row(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
|
@ -54,8 +51,8 @@ class OperationButtons extends StatelessWidget {
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
try {
|
try {
|
||||||
await sftpWorker.rename('$path${dirEntry.filename}', '$path${newNameController.text}');
|
await sftpProvider.sftpWorker.rename('${sftpProvider.path}${dirEntry.filename}', '${sftpProvider.path}${newNameController.text}');
|
||||||
listDir();
|
sftpProvider.listDir();
|
||||||
}
|
}
|
||||||
on SftpStatusError catch (e) {
|
on SftpStatusError catch (e) {
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
|
@ -97,14 +94,14 @@ class OperationButtons extends StatelessWidget {
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
for (final dirEntry in dirEntries) {
|
for (final dirEntry in dirEntries) {
|
||||||
try {
|
try {
|
||||||
await sftpWorker.remove(dirEntry, path);
|
await sftpProvider.sftpWorker.remove(dirEntry, sftpProvider.path);
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(buildErrorSnackBar(context, e.toString()));
|
ScaffoldMessenger.of(context).showSnackBar(buildErrorSnackBar(context, e.toString()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
listDir();
|
sftpProvider.listDir();
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
Navigator.pop(context);
|
Navigator.pop(context);
|
||||||
}
|
}
|
||||||
|
@ -125,8 +122,8 @@ class OperationButtons extends StatelessWidget {
|
||||||
if (downloadsDir == null) return;
|
if (downloadsDir == null) return;
|
||||||
for (final dirEntry in dirEntries) {
|
for (final dirEntry in dirEntries) {
|
||||||
try {
|
try {
|
||||||
await for (final progress in sftpWorker.downloadFile(dirEntry, path, downloadsDir.path)) {
|
await for (final progress in sftpProvider.sftpWorker.downloadFile(dirEntry, sftpProvider.path, downloadsDir.path)) {
|
||||||
setDownloadProgress(progress);
|
sftpProvider.setDownloadProgress(progress);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
|
@ -135,7 +132,7 @@ class OperationButtons extends StatelessWidget {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setDownloadProgress(null);
|
sftpProvider.setDownloadProgress(null);
|
||||||
},
|
},
|
||||||
icon: Icon(Icons.download)
|
icon: Icon(Icons.download)
|
||||||
)
|
)
|
||||||
|
|
16
pubspec.lock
16
pubspec.lock
|
@ -320,6 +320,14 @@ packages:
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.16.0"
|
version: "1.16.0"
|
||||||
|
nested:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: nested
|
||||||
|
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.0"
|
||||||
path:
|
path:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
|
@ -408,6 +416,14 @@ packages:
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.9.1"
|
version: "3.9.1"
|
||||||
|
provider:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: provider
|
||||||
|
sha256: "4abbd070a04e9ddc287673bf5a030c7ca8b685ff70218720abab8b092f53dd84"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "6.1.5"
|
||||||
sky_engine:
|
sky_engine:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description: flutter
|
description: flutter
|
||||||
|
|
|
@ -15,6 +15,7 @@ dependencies:
|
||||||
path: ^1.9.1
|
path: ^1.9.1
|
||||||
file_picker: ^10.2.0
|
file_picker: ^10.2.0
|
||||||
path_provider: ^2.1.5
|
path_provider: ^2.1.5
|
||||||
|
provider: ^6.1.5
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue