mirror of
https://github.com/weyne85/rustdesk.git
synced 2025-10-29 17:00:05 +00:00
before del file_manager pub
This commit is contained in:
@@ -2,10 +2,13 @@ import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:file_manager/enums/sort_by.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'model.dart';
|
||||
|
||||
// BIG TODO remove file manager !
|
||||
|
||||
|
||||
// enum FileType {
|
||||
// Dir = 0,
|
||||
@@ -17,23 +20,28 @@ import 'model.dart';
|
||||
|
||||
class FileDirectory {
|
||||
// List<Entry> entries = [];
|
||||
List<FileSystemEntity> entries = [];
|
||||
List<RemoteFileSystemEntity> entries = [];
|
||||
int id = 0;
|
||||
String path = "";
|
||||
|
||||
FileDirectory();
|
||||
|
||||
FileDirectory.fromJson(Map<String, dynamic> json) {
|
||||
FileDirectory.fromJson(Map<String, dynamic> json,SortBy sort) {
|
||||
id = json['id'];
|
||||
path = json['path'];
|
||||
if (json['entries'] != null) {
|
||||
entries = <FileSystemEntity>[];
|
||||
entries = <RemoteFileSystemEntity>[];
|
||||
json['entries'].forEach((v) {
|
||||
entries.add(new Entry.fromJson(v).toFileSystemEntity(path));
|
||||
entries.add(new Entry.fromJson(v).toRemoteFileSystemEntity(path));
|
||||
});
|
||||
entries = _sortRemoteEntitiesList(entries, sort);
|
||||
}
|
||||
}
|
||||
|
||||
changeSortStyle(SortBy sort){
|
||||
entries = _sortRemoteEntitiesList(entries, sort);
|
||||
}
|
||||
|
||||
// Map<String, dynamic> toJson() {
|
||||
// final Map<String, dynamic> data = new Map<String, dynamic>();
|
||||
// data['entries'] = this.entries.map((v) => v.toJson()).toList();
|
||||
@@ -42,7 +50,7 @@ class FileDirectory {
|
||||
// return data;
|
||||
// }
|
||||
|
||||
clear(){
|
||||
clear() {
|
||||
entries = [];
|
||||
id = 0;
|
||||
path = "";
|
||||
@@ -64,13 +72,8 @@ class Entry {
|
||||
size = json['size'];
|
||||
}
|
||||
|
||||
FileSystemEntity toFileSystemEntity(String parentPath){
|
||||
// is dir
|
||||
if(entryType<=3){
|
||||
return RemoteDir("$parentPath/$name");
|
||||
}else {
|
||||
return RemoteFile("$parentPath/$name",modifiedTime,size);
|
||||
}
|
||||
RemoteFileSystemEntity toRemoteFileSystemEntity(String parentPath) {
|
||||
return RemoteFileSystemEntity.from("$parentPath/$name", entryType, modifiedTime, size);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
@@ -83,36 +86,43 @@ class Entry {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// TODO 使用工厂单例模式
|
||||
|
||||
class RemoteFileModel extends ChangeNotifier{
|
||||
class RemoteFileModel extends ChangeNotifier {
|
||||
|
||||
SortBy _sortStyle = SortBy.name;
|
||||
SortBy get sortStyle => _sortStyle;
|
||||
|
||||
FileDirectory _currentRemoteDir = FileDirectory();
|
||||
|
||||
FileDirectory get currentRemoteDir => _currentRemoteDir;
|
||||
|
||||
tryUpdateRemoteDir(String fd){
|
||||
tryUpdateRemoteDir(String fd) {
|
||||
debugPrint("tryUpdateRemoteDir:$fd");
|
||||
try{
|
||||
final fileDir = FileDirectory.fromJson(jsonDecode(fd));
|
||||
try {
|
||||
final fileDir = FileDirectory.fromJson(jsonDecode(fd),_sortStyle);
|
||||
_currentRemoteDir = fileDir;
|
||||
debugPrint("_currentRemoteDir:${_currentRemoteDir.path}");
|
||||
notifyListeners();
|
||||
}catch(e){
|
||||
} catch (e) {
|
||||
debugPrint("tryUpdateRemoteDir fail:$fd");
|
||||
}
|
||||
}
|
||||
|
||||
goToParentDirectory(){
|
||||
goToParentDirectory() {
|
||||
var parentPath = "";
|
||||
if(_currentRemoteDir.path == ""){
|
||||
if (_currentRemoteDir.path == "") {
|
||||
parentPath = "";
|
||||
}else{
|
||||
} else {
|
||||
parentPath = Directory(_currentRemoteDir.path).parent.path;
|
||||
}
|
||||
FFI.setByName("read_remote_dir", parentPath);
|
||||
}
|
||||
|
||||
changeSortStyle(SortBy sort){
|
||||
_currentRemoteDir.changeSortStyle(sort);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_currentRemoteDir.clear();
|
||||
@@ -120,20 +130,88 @@ class RemoteFileModel extends ChangeNotifier{
|
||||
}
|
||||
}
|
||||
|
||||
// int entryType = 4;
|
||||
// int modifiedTime = 0;
|
||||
// String name = "";
|
||||
// int size = 0;
|
||||
|
||||
class RemoteDir extends FileSystemEntity implements Directory{
|
||||
|
||||
// int entryType = 4;
|
||||
// int modifiedTime = 0;
|
||||
// String name = "";
|
||||
// int size = 0;
|
||||
|
||||
|
||||
class RemoteFileSystemEntity extends FileSystemEntity {
|
||||
int entryType;
|
||||
int modifiedTime;
|
||||
String path;
|
||||
int size;
|
||||
|
||||
RemoteDir(this.path);
|
||||
RemoteFileSystemEntity(this.path,
|
||||
this.entryType,
|
||||
this.modifiedTime,
|
||||
this.size);
|
||||
|
||||
|
||||
// 工厂模式 自动输出两个类型
|
||||
factory RemoteFileSystemEntity.from(
|
||||
String path,
|
||||
int entryType,
|
||||
int modifiedTime,
|
||||
int size
|
||||
) {
|
||||
if (entryType > 3) {
|
||||
return RemoteFile(path,
|
||||
entryType,
|
||||
modifiedTime,
|
||||
size);
|
||||
} else {
|
||||
return RemoteFile(path,
|
||||
entryType,
|
||||
modifiedTime,
|
||||
size);
|
||||
}
|
||||
}
|
||||
|
||||
DateTime lastModifiedSync() {
|
||||
return DateTime.fromMillisecondsSinceEpoch(modifiedTime * 1000);
|
||||
}
|
||||
|
||||
int lengthSync() {
|
||||
return size;
|
||||
}
|
||||
|
||||
bool isFile(){
|
||||
return entryType > 3;
|
||||
}
|
||||
|
||||
@override
|
||||
// TODO: implement absolute
|
||||
FileSystemEntity get absolute => throw UnimplementedError();
|
||||
|
||||
@override
|
||||
Future<bool> exists() {
|
||||
// TODO: implement exists
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
bool existsSync() {
|
||||
// TODO: implement existsSync
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<FileSystemEntity> rename(String newPath) {
|
||||
// TODO: implement rename
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
FileSystemEntity renameSync(String newPath) {
|
||||
// TODO: implement renameSync
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
||||
|
||||
class RemoteDir extends RemoteFileSystemEntity implements Directory {
|
||||
RemoteDir(path, entryType, modifiedTime, size)
|
||||
: super(path, entryType, modifiedTime, size);
|
||||
|
||||
@override
|
||||
// TODO: implement absolute
|
||||
Directory get absolute => throw UnimplementedError();
|
||||
@@ -174,13 +252,15 @@ class RemoteDir extends FileSystemEntity implements Directory{
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<FileSystemEntity> list({bool recursive = false, bool followLinks = true}) {
|
||||
Stream<FileSystemEntity> list(
|
||||
{bool recursive = false, bool followLinks = true}) {
|
||||
// TODO: implement list
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
List<FileSystemEntity> listSync({bool recursive = false, bool followLinks = true}) {
|
||||
List<FileSystemEntity> listSync(
|
||||
{bool recursive = false, bool followLinks = true}) {
|
||||
// TODO: implement listSync
|
||||
throw UnimplementedError();
|
||||
}
|
||||
@@ -196,32 +276,12 @@ class RemoteDir extends FileSystemEntity implements Directory{
|
||||
// TODO: implement renameSync
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class RemoteFile extends FileSystemEntity implements File {
|
||||
class RemoteFile extends RemoteFileSystemEntity implements File {
|
||||
|
||||
// int entryType = 4;
|
||||
// int modifiedTime = 0;
|
||||
// String name = "";
|
||||
// int size = 0;
|
||||
|
||||
RemoteFile(this.path,this.modifiedTime,this.size);
|
||||
|
||||
var path;
|
||||
var modifiedTime;
|
||||
var size;
|
||||
|
||||
|
||||
@override
|
||||
DateTime lastModifiedSync() {
|
||||
return DateTime.fromMillisecondsSinceEpoch(modifiedTime * 1000);
|
||||
}
|
||||
|
||||
@override
|
||||
int lengthSync() {
|
||||
return size;
|
||||
}
|
||||
RemoteFile(path, entryType, modifiedTime, size)
|
||||
: super(path, entryType, modifiedTime, size);
|
||||
|
||||
// ***************************
|
||||
|
||||
@@ -379,29 +439,120 @@ class RemoteFile extends FileSystemEntity implements File {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<File> writeAsBytes(List<int> bytes, {FileMode mode = FileMode.write, bool flush = false}) {
|
||||
Future<File> writeAsBytes(List<int> bytes,
|
||||
{FileMode mode = FileMode.write, bool flush = false}) {
|
||||
// TODO: implement writeAsBytes
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
void writeAsBytesSync(List<int> bytes, {FileMode mode = FileMode.write, bool flush = false}) {
|
||||
void writeAsBytesSync(List<int> bytes,
|
||||
{FileMode mode = FileMode.write, bool flush = false}) {
|
||||
// TODO: implement writeAsBytesSync
|
||||
}
|
||||
|
||||
@override
|
||||
Future<File> writeAsString(String contents, {FileMode mode = FileMode.write, Encoding encoding = utf8, bool flush = false}) {
|
||||
Future<File> writeAsString(String contents,
|
||||
{FileMode mode = FileMode.write,
|
||||
Encoding encoding = utf8,
|
||||
bool flush = false}) {
|
||||
// TODO: implement writeAsString
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
void writeAsStringSync(String contents, {FileMode mode = FileMode.write, Encoding encoding = utf8, bool flush = false}) {
|
||||
void writeAsStringSync(String contents,
|
||||
{FileMode mode = FileMode.write,
|
||||
Encoding encoding = utf8,
|
||||
bool flush = false}) {
|
||||
// TODO: implement writeAsStringSync
|
||||
}
|
||||
|
||||
@override
|
||||
// TODO: implement absolute
|
||||
File get absolute => throw UnimplementedError();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class _PathStat {
|
||||
final String path;
|
||||
final DateTime dateTime;
|
||||
_PathStat(this.path, this.dateTime);
|
||||
}
|
||||
|
||||
// code from file_manager pkg after edit
|
||||
List<RemoteFileSystemEntity> _sortRemoteEntitiesList(
|
||||
List<RemoteFileSystemEntity> list, SortBy sortType) {
|
||||
if (sortType == SortBy.name) {
|
||||
// making list of only folders.
|
||||
final dirs = list.where((element) => element is Directory).toList();
|
||||
// sorting folder list by name.
|
||||
dirs.sort((a, b) => a.path.toLowerCase().compareTo(b.path.toLowerCase()));
|
||||
|
||||
// making list of only flies.
|
||||
final files = list.where((element) => element is File).toList();
|
||||
// sorting files list by name.
|
||||
files.sort((a, b) => a.path.toLowerCase().compareTo(b.path.toLowerCase()));
|
||||
|
||||
// first folders will go to list (if available) then files will go to list.
|
||||
return [...dirs, ...files];
|
||||
} else if (sortType == SortBy.date) {
|
||||
// making the list of Path & DateTime
|
||||
List<_PathStat> _pathStat = [];
|
||||
for (RemoteFileSystemEntity e in list) {
|
||||
_pathStat.add(_PathStat(e.path, e.lastModifiedSync()));
|
||||
}
|
||||
|
||||
// sort _pathStat according to date
|
||||
_pathStat.sort((b, a) => a.dateTime.compareTo(b.dateTime));
|
||||
|
||||
// sorting [list] accroding to [_pathStat]
|
||||
list.sort((a, b) => _pathStat
|
||||
.indexWhere((element) => element.path == a.path)
|
||||
.compareTo(_pathStat.indexWhere((element) => element.path == b.path)));
|
||||
return list;
|
||||
} else if (sortType == SortBy.type) {
|
||||
// making list of only folders.
|
||||
final dirs = list.where((element) => element is Directory).toList();
|
||||
|
||||
// sorting folders by name.
|
||||
dirs.sort((a, b) => a.path.toLowerCase().compareTo(b.path.toLowerCase()));
|
||||
|
||||
// making the list of files
|
||||
final files = list.where((element) => element is File).toList();
|
||||
|
||||
// sorting files list by extension.
|
||||
files.sort((a, b) => a.path
|
||||
.toLowerCase()
|
||||
.split('.')
|
||||
.last
|
||||
.compareTo(b.path.toLowerCase().split('.').last));
|
||||
return [...dirs, ...files];
|
||||
} else if (sortType == SortBy.size) {
|
||||
// create list of path and size
|
||||
Map<String, int> _sizeMap = {};
|
||||
for (RemoteFileSystemEntity e in list) {
|
||||
_sizeMap[e.path] = e.lengthSync();
|
||||
}
|
||||
|
||||
// making list of only folders.
|
||||
final dirs = list.where((element) => element is Directory).toList();
|
||||
// sorting folder list by name.
|
||||
dirs.sort((a, b) => a.path.toLowerCase().compareTo(b.path.toLowerCase()));
|
||||
|
||||
// making list of only flies.
|
||||
final files = list.where((element) => element is File).toList();
|
||||
|
||||
// creating sorted list of [_sizeMapList] by size.
|
||||
final List<MapEntry<String, int>> _sizeMapList = _sizeMap.entries.toList();
|
||||
_sizeMapList.sort((b, a) => a.value.compareTo(b.value));
|
||||
|
||||
// sort [list] according to [_sizeMapList]
|
||||
files.sort((a, b) => _sizeMapList
|
||||
.indexWhere((element) => element.key == a.path)
|
||||
.compareTo(
|
||||
_sizeMapList.indexWhere((element) => element.key == b.path)));
|
||||
return [...dirs, ...files];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user