riverpod refactor
This commit is contained in:
parent
33ade0479f
commit
d61c6bd49c
8 changed files with 745 additions and 87 deletions
|
@ -1,8 +1,9 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:fluxcloud/sftp_connection_list.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MainApp());
|
||||
runApp(ProviderScope(child: const MainApp()));
|
||||
}
|
||||
|
||||
class MainApp extends StatelessWidget {
|
||||
|
|
75
lib/providers/sftp_state.dart
Normal file
75
lib/providers/sftp_state.dart
Normal file
|
@ -0,0 +1,75 @@
|
|||
import 'package:dartssh2/dartssh2.dart';
|
||||
import 'package:fluxcloud/sftp_worker.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'sftp_state.g.dart';
|
||||
|
||||
class SftpState {
|
||||
final String path;
|
||||
final bool isLoading;
|
||||
final List<SftpName> dirContents;
|
||||
|
||||
final double? uploadProgress;
|
||||
final double? downloadProgress;
|
||||
|
||||
SftpState({
|
||||
required this.path,
|
||||
required this.isLoading,
|
||||
required this.dirContents,
|
||||
required this.uploadProgress,
|
||||
required this.downloadProgress
|
||||
});
|
||||
|
||||
SftpState copyWith({
|
||||
String? path,
|
||||
bool? isLoading,
|
||||
List<SftpName>? dirContents,
|
||||
double? uploadProgress,
|
||||
double? downloadProgress,
|
||||
}) => SftpState(
|
||||
path: path ?? this.path,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
dirContents: dirContents ?? this.dirContents,
|
||||
uploadProgress: uploadProgress ?? this.uploadProgress,
|
||||
downloadProgress: downloadProgress ?? this.downloadProgress
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@riverpod
|
||||
class SftpNotifier extends _$SftpNotifier {
|
||||
|
||||
@override
|
||||
SftpState build(SftpWorker sftpWorker) {
|
||||
Future.microtask(listDir);
|
||||
return SftpState(path: '/', isLoading: false, dirContents: [], uploadProgress: null, downloadProgress: null);
|
||||
}
|
||||
|
||||
|
||||
Future<void> listDir() async {
|
||||
state = state.copyWith(isLoading: true);
|
||||
final dirContents = await sftpWorker.listdir(state.path);
|
||||
state = state.copyWith(isLoading: false, dirContents: dirContents);
|
||||
}
|
||||
|
||||
void goToDir(String path) {
|
||||
state = state.copyWith(path: path);
|
||||
listDir();
|
||||
}
|
||||
|
||||
void goToPrevDir() {
|
||||
String path = state.path.substring(0, state.path.length - 1);
|
||||
path = path.substring(0, path.lastIndexOf('/')+1);
|
||||
state = state.copyWith(path: path);
|
||||
listDir();
|
||||
}
|
||||
|
||||
void setUploadProgress(double? progress) {
|
||||
state = state.copyWith(uploadProgress: progress);
|
||||
}
|
||||
|
||||
void setDownloadProgress(double? progress) {
|
||||
state = state.copyWith(downloadProgress: progress);
|
||||
}
|
||||
|
||||
}
|
161
lib/providers/sftp_state.g.dart
Normal file
161
lib/providers/sftp_state.g.dart
Normal file
|
@ -0,0 +1,161 @@
|
|||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'sftp_state.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$sftpNotifierHash() => r'819f80135b516bf1d8d3e7dd9022edf3b23a9e0c';
|
||||
|
||||
/// Copied from Dart SDK
|
||||
class _SystemHash {
|
||||
_SystemHash._();
|
||||
|
||||
static int combine(int hash, int value) {
|
||||
// ignore: parameter_assignments
|
||||
hash = 0x1fffffff & (hash + value);
|
||||
// ignore: parameter_assignments
|
||||
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
|
||||
return hash ^ (hash >> 6);
|
||||
}
|
||||
|
||||
static int finish(int hash) {
|
||||
// ignore: parameter_assignments
|
||||
hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
|
||||
// ignore: parameter_assignments
|
||||
hash = hash ^ (hash >> 11);
|
||||
return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
|
||||
}
|
||||
}
|
||||
|
||||
abstract class _$SftpNotifier extends BuildlessAutoDisposeNotifier<SftpState> {
|
||||
late final SftpWorker sftpWorker;
|
||||
|
||||
SftpState build(SftpWorker sftpWorker);
|
||||
}
|
||||
|
||||
/// See also [SftpNotifier].
|
||||
@ProviderFor(SftpNotifier)
|
||||
const sftpNotifierProvider = SftpNotifierFamily();
|
||||
|
||||
/// See also [SftpNotifier].
|
||||
class SftpNotifierFamily extends Family<SftpState> {
|
||||
/// See also [SftpNotifier].
|
||||
const SftpNotifierFamily();
|
||||
|
||||
/// See also [SftpNotifier].
|
||||
SftpNotifierProvider call(SftpWorker sftpWorker) {
|
||||
return SftpNotifierProvider(sftpWorker);
|
||||
}
|
||||
|
||||
@override
|
||||
SftpNotifierProvider getProviderOverride(
|
||||
covariant SftpNotifierProvider provider,
|
||||
) {
|
||||
return call(provider.sftpWorker);
|
||||
}
|
||||
|
||||
static const Iterable<ProviderOrFamily>? _dependencies = null;
|
||||
|
||||
@override
|
||||
Iterable<ProviderOrFamily>? get dependencies => _dependencies;
|
||||
|
||||
static const Iterable<ProviderOrFamily>? _allTransitiveDependencies = null;
|
||||
|
||||
@override
|
||||
Iterable<ProviderOrFamily>? get allTransitiveDependencies =>
|
||||
_allTransitiveDependencies;
|
||||
|
||||
@override
|
||||
String? get name => r'sftpNotifierProvider';
|
||||
}
|
||||
|
||||
/// See also [SftpNotifier].
|
||||
class SftpNotifierProvider
|
||||
extends AutoDisposeNotifierProviderImpl<SftpNotifier, SftpState> {
|
||||
/// See also [SftpNotifier].
|
||||
SftpNotifierProvider(SftpWorker sftpWorker)
|
||||
: this._internal(
|
||||
() => SftpNotifier()..sftpWorker = sftpWorker,
|
||||
from: sftpNotifierProvider,
|
||||
name: r'sftpNotifierProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$sftpNotifierHash,
|
||||
dependencies: SftpNotifierFamily._dependencies,
|
||||
allTransitiveDependencies:
|
||||
SftpNotifierFamily._allTransitiveDependencies,
|
||||
sftpWorker: sftpWorker,
|
||||
);
|
||||
|
||||
SftpNotifierProvider._internal(
|
||||
super._createNotifier, {
|
||||
required super.name,
|
||||
required super.dependencies,
|
||||
required super.allTransitiveDependencies,
|
||||
required super.debugGetCreateSourceHash,
|
||||
required super.from,
|
||||
required this.sftpWorker,
|
||||
}) : super.internal();
|
||||
|
||||
final SftpWorker sftpWorker;
|
||||
|
||||
@override
|
||||
SftpState runNotifierBuild(covariant SftpNotifier notifier) {
|
||||
return notifier.build(sftpWorker);
|
||||
}
|
||||
|
||||
@override
|
||||
Override overrideWith(SftpNotifier Function() create) {
|
||||
return ProviderOverride(
|
||||
origin: this,
|
||||
override: SftpNotifierProvider._internal(
|
||||
() => create()..sftpWorker = sftpWorker,
|
||||
from: from,
|
||||
name: null,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
debugGetCreateSourceHash: null,
|
||||
sftpWorker: sftpWorker,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
AutoDisposeNotifierProviderElement<SftpNotifier, SftpState> createElement() {
|
||||
return _SftpNotifierProviderElement(this);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return other is SftpNotifierProvider && other.sftpWorker == sftpWorker;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
var hash = _SystemHash.combine(0, runtimeType.hashCode);
|
||||
hash = _SystemHash.combine(hash, sftpWorker.hashCode);
|
||||
|
||||
return _SystemHash.finish(hash);
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated('Will be removed in 3.0. Use Ref instead')
|
||||
// ignore: unused_element
|
||||
mixin SftpNotifierRef on AutoDisposeNotifierProviderRef<SftpState> {
|
||||
/// The parameter `sftpWorker` of this provider.
|
||||
SftpWorker get sftpWorker;
|
||||
}
|
||||
|
||||
class _SftpNotifierProviderElement
|
||||
extends AutoDisposeNotifierProviderElement<SftpNotifier, SftpState>
|
||||
with SftpNotifierRef {
|
||||
_SftpNotifierProviderElement(super.provider);
|
||||
|
||||
@override
|
||||
SftpWorker get sftpWorker => (origin as SftpNotifierProvider).sftpWorker;
|
||||
}
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
|
|
@ -1,57 +1,23 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:dartssh2/dartssh2.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:file_selector/file_selector.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:fluxcloud/main.dart';
|
||||
import 'package:fluxcloud/providers/sftp_state.dart';
|
||||
import 'package:fluxcloud/sftp_worker.dart';
|
||||
|
||||
import 'widgets/operation_buttons.dart';
|
||||
|
||||
class SftpExplorer extends StatefulWidget {
|
||||
class SftpExplorer extends ConsumerWidget {
|
||||
const SftpExplorer({super.key, required this.sftpWorker});
|
||||
|
||||
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
|
||||
Widget build(BuildContext context) {
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final sftpState = ref.watch(sftpNotifierProvider(sftpWorker));
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
toolbarHeight: 75,
|
||||
|
@ -60,25 +26,23 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
|||
actionsPadding: EdgeInsets.only(right: 20),
|
||||
leading: IconButton(
|
||||
onPressed: () {
|
||||
if (path == '/') {
|
||||
if (sftpState.path == '/') {
|
||||
// TODO: figure this out
|
||||
// Navigator.pop(context);
|
||||
}
|
||||
else {
|
||||
path = path.substring(0, path.length - 1);
|
||||
path = path.substring(0, path.lastIndexOf('/')+1);
|
||||
_listDir();
|
||||
ref.read(sftpNotifierProvider(sftpWorker).notifier).goToPrevDir();
|
||||
}
|
||||
},
|
||||
icon: Icon(Icons.arrow_back)
|
||||
),
|
||||
actions: [
|
||||
if (_uploadProgress != null)
|
||||
if (sftpState.uploadProgress != null)
|
||||
Stack(
|
||||
alignment: Alignment.center,
|
||||
children: [
|
||||
TweenAnimationBuilder<double>(
|
||||
tween: Tween(begin: 0, end: _uploadProgress),
|
||||
tween: Tween(begin: 0, end: sftpState.uploadProgress),
|
||||
duration: Duration(milliseconds: 300),
|
||||
builder: (context, value, _) => CircularProgressIndicator(strokeWidth: 3, value: value,)
|
||||
),
|
||||
|
@ -90,12 +54,12 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
|||
),
|
||||
]
|
||||
),
|
||||
if (_downloadProgress != null)
|
||||
if (sftpState.downloadProgress != null)
|
||||
Stack(
|
||||
alignment: Alignment.center,
|
||||
children: [
|
||||
TweenAnimationBuilder<double>(
|
||||
tween: Tween(begin: 0, end: _downloadProgress),
|
||||
tween: Tween(begin: 0, end: sftpState.downloadProgress),
|
||||
duration: Duration(milliseconds: 300),
|
||||
builder: (context, value, _) => CircularProgressIndicator(strokeWidth: 3, value: value,)
|
||||
),
|
||||
|
@ -109,14 +73,12 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
|||
),
|
||||
],
|
||||
),
|
||||
floatingActionButton: _buildFABs(context),
|
||||
floatingActionButton: _buildFABs(context, ref),
|
||||
body: PopScope(
|
||||
canPop: false,
|
||||
onPopInvokedWithResult: (_, _) {
|
||||
if (path != '/') {
|
||||
path = path.substring(0, path.length - 1);
|
||||
path = path.substring(0, path.lastIndexOf('/')+1);
|
||||
_listDir();
|
||||
if (sftpState.path != '/') {
|
||||
ref.read(sftpNotifierProvider(sftpWorker).notifier).goToPrevDir();
|
||||
}
|
||||
},
|
||||
child: AnimatedSwitcher(
|
||||
|
@ -137,19 +99,18 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
|||
),
|
||||
);
|
||||
},
|
||||
child: _isLoading ? Center(child: CircularProgressIndicator()) : ListView.builder(
|
||||
key: ValueKey(path),
|
||||
itemCount: _dirContents.length,
|
||||
child: sftpState.isLoading ? Center(child: CircularProgressIndicator()) : ListView.builder(
|
||||
key: ValueKey(sftpState.path),
|
||||
itemCount: sftpState.dirContents.length,
|
||||
itemBuilder: (context, index) {
|
||||
final dirEntry = _dirContents[index];
|
||||
final dirEntry = sftpState.dirContents[index];
|
||||
return ListTile(
|
||||
leading: Icon(dirEntry.attr.isDirectory ? Icons.folder : Icons.description),
|
||||
title: Text(dirEntry.filename),
|
||||
trailing: OperationButtons(sftpWorker: widget.sftpWorker, path: path, dirEntries: [dirEntry], listDir: _listDir, setDownloadProgress: _setDownloadProgress,),
|
||||
trailing: OperationButtons(sftpWorker: sftpWorker, dirEntries: [dirEntry],),
|
||||
onTap: () {
|
||||
if (dirEntry.attr.isDirectory) {
|
||||
path = '$path${dirEntry.filename}/';
|
||||
_listDir();
|
||||
ref.read(sftpNotifierProvider(sftpWorker).notifier).goToDir('${sftpState.path}${dirEntry.filename}/');
|
||||
}
|
||||
},
|
||||
);
|
||||
|
@ -160,7 +121,8 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
|||
);
|
||||
}
|
||||
|
||||
Widget _buildFABs(BuildContext context) {
|
||||
Widget _buildFABs(BuildContext context, WidgetRef ref) {
|
||||
final sftpState = ref.read(sftpNotifierProvider(sftpWorker));
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
spacing: 10,
|
||||
|
@ -184,8 +146,8 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
|||
TextButton(
|
||||
onPressed: () async {
|
||||
try {
|
||||
await widget.sftpWorker.mkdir('$path${nameController.text}');
|
||||
_listDir();
|
||||
await sftpWorker.mkdir('${sftpState.path}${nameController.text}');
|
||||
ref.read(sftpNotifierProvider(sftpWorker).notifier).listDir();
|
||||
}
|
||||
catch (e) {
|
||||
if (context.mounted) {
|
||||
|
@ -217,16 +179,10 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
|||
filePaths = files.map((file) => file.path).toList();
|
||||
}
|
||||
try {
|
||||
bool start = true;
|
||||
await for (final progress in widget.sftpWorker.uploadFiles(path, filePaths)) {
|
||||
if (start) {
|
||||
start = false;
|
||||
_listDir();
|
||||
}
|
||||
setState(() => _uploadProgress = progress);
|
||||
await for (final progress in sftpWorker.uploadFiles(sftpState.path, filePaths)) {
|
||||
ref.read(sftpNotifierProvider(sftpWorker).notifier).setUploadProgress(progress);
|
||||
if (progress == 1) {
|
||||
// TODO: fix: next file also starts to show
|
||||
_listDir();
|
||||
ref.read(sftpNotifierProvider(sftpWorker).notifier).listDir();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -235,8 +191,8 @@ class _SftpExplorerState extends State<SftpExplorer> {
|
|||
ScaffoldMessenger.of(context).showSnackBar(buildErrorSnackBar(context, e.toString()));
|
||||
}
|
||||
}
|
||||
setState(() => _uploadProgress = null);
|
||||
_listDir();
|
||||
ref.read(sftpNotifierProvider(sftpWorker).notifier).setUploadProgress(null);
|
||||
ref.read(sftpNotifierProvider(sftpWorker).notifier).listDir();
|
||||
},
|
||||
child: Icon(Icons.upload),
|
||||
),
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
import 'package:dartssh2/dartssh2.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:fluxcloud/main.dart';
|
||||
import 'package:fluxcloud/providers/sftp_state.dart';
|
||||
import 'package:fluxcloud/sftp_worker.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
class OperationButtons extends StatelessWidget {
|
||||
class OperationButtons extends ConsumerWidget {
|
||||
const OperationButtons({
|
||||
super.key,
|
||||
required this.sftpWorker, required this.path, required this.dirEntries, required this.listDir, required this.setDownloadProgress,
|
||||
required this.sftpWorker, required this.dirEntries,
|
||||
});
|
||||
|
||||
final SftpWorker sftpWorker;
|
||||
final String path;
|
||||
final List<SftpName> dirEntries;
|
||||
final Function listDir;
|
||||
final Function(double? progress) setDownloadProgress;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final sftpState = ref.watch(sftpNotifierProvider(sftpWorker));
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
|
@ -54,8 +54,8 @@ class OperationButtons extends StatelessWidget {
|
|||
TextButton(
|
||||
onPressed: () async {
|
||||
try {
|
||||
await sftpWorker.rename('$path${dirEntry.filename}', '$path${newNameController.text}');
|
||||
listDir();
|
||||
await sftpWorker.rename('${sftpState.path}${dirEntry.filename}', '${sftpState.path}${newNameController.text}');
|
||||
ref.read(sftpNotifierProvider(sftpWorker).notifier).listDir();
|
||||
}
|
||||
on SftpStatusError catch (e) {
|
||||
if (context.mounted) {
|
||||
|
@ -97,14 +97,14 @@ class OperationButtons extends StatelessWidget {
|
|||
onPressed: () async {
|
||||
for (final dirEntry in dirEntries) {
|
||||
try {
|
||||
await sftpWorker.remove(dirEntry, path);
|
||||
await sftpWorker.remove(dirEntry, sftpState.path);
|
||||
}
|
||||
catch (e) {
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(buildErrorSnackBar(context, e.toString()));
|
||||
}
|
||||
}
|
||||
listDir();
|
||||
ref.read(sftpNotifierProvider(sftpWorker).notifier).listDir();
|
||||
if (context.mounted) {
|
||||
Navigator.pop(context);
|
||||
}
|
||||
|
@ -124,10 +124,10 @@ class OperationButtons extends StatelessWidget {
|
|||
final downloadsDir = await getDownloadsDirectory();
|
||||
if (downloadsDir == null) return;
|
||||
try {
|
||||
await for (final progress in sftpWorker.downloadFiles(dirEntries, path, downloadsDir.path)) {
|
||||
setDownloadProgress(progress);
|
||||
await for (final progress in sftpWorker.downloadFiles(dirEntries, sftpState.path, downloadsDir.path)) {
|
||||
ref.read(sftpNotifierProvider(sftpWorker).notifier).setDownloadProgress(progress);
|
||||
}
|
||||
setDownloadProgress(null);
|
||||
ref.read(sftpNotifierProvider(sftpWorker).notifier).setDownloadProgress(null);
|
||||
}
|
||||
catch (e) {
|
||||
if (context.mounted) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue