Skip to content

Commit

Permalink
Improve table load performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernthedev committed Nov 22, 2023
1 parent 1fdf765 commit ca191f1
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 263 deletions.
86 changes: 37 additions & 49 deletions lib/pages/browser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ enum FileCreation { File, Folder }
class _DeviceBrowserPageState extends State<DeviceBrowserPage> {
bool list = true;
bool _dragging = false;
late Future<List<String>?> _fileListingFuture;
// late Future<SharedPreferences> preferences;
Map<String, FileBrowserDataWrapper> fileCache = {}; // date time cache
late Future<List<FileBrowserMetadata>?> _fileListingFuture;

late ListenableHolder<void> onForwardClick;
late ListenableHolder<void> onBackClick;
Expand Down Expand Up @@ -304,11 +302,11 @@ class _DeviceBrowserPageState extends State<DeviceBrowserPage> {
);
}

FutureBuilder<List<String>?> _fileView() {
FutureBuilder<List<FileBrowserMetadata>?> _fileView() {
return FutureBuilder(
future: _fileListingFuture,
key: ValueKey(_fileListingFuture),
builder: (BuildContext context, AsyncSnapshot<List<String>?> snapshot) {
builder: (context, snapshot) {
if (snapshot.hasError && snapshot.error != null) {
return Center(
child: Text(snapshot.error.toString()),
Expand All @@ -321,10 +319,6 @@ class _DeviceBrowserPageState extends State<DeviceBrowserPage> {
var filteredList =
_filteredFiles(snapshot.data!).toList(growable: false);

// filteredList = filteredList
// .where((value) => value.endsWith("/"))
// .followedBy(filteredList.where((value) => !value.endsWith("/")))
// .toList(growable: false);
return list ? _viewAsList(filteredList) : _viewAsGrid(filteredList);
}

Expand Down Expand Up @@ -376,10 +370,12 @@ class _DeviceBrowserPageState extends State<DeviceBrowserPage> {
);
}

Iterable<String> _filteredFiles(Iterable<String> files) {
Iterable<FileBrowserMetadata> _filteredFiles(
Iterable<FileBrowserMetadata> files) {
var filter = widget._filterController.text.toLowerCase();
if (filter.isEmpty) return files;
return files.where((element) => element.toLowerCase().contains(filter));
return files.where(
(element) => element.fullFilePath.toLowerCase().contains(filter));
}

Column _leftPanel() {
Expand Down Expand Up @@ -458,12 +454,28 @@ class _DeviceBrowserPageState extends State<DeviceBrowserPage> {
debugPrint("Loading $newPath");

setState(() {
fileCache = {};

_fileListingFuture =
Adb.getFilesInDirectory(widget.serial, newPath).then((value) {
debugPrint("Finished adb");
return value;
Adb.getFilesInDirectory(widget.serial, newPath).then((list) async {
if (list == null) {
return null;
}

var entries = list.map((e) async {
var dateTime = await Adb.getFileModifiedDate(widget.serial, e);
var size = await Adb.getFileSize(widget.serial, e);

return FileBrowserMetadata(
browser: widget._fileBrowser,
modifiedTime: dateTime,
fileSize: size,
fullFilePath: e,
isDirectory: e.endsWith("/"),
onWatch: _watchFile,
serial: widget.serial,
);
}).toList(growable: false);

return Future.wait(entries);
});
});
}
Expand Down Expand Up @@ -574,7 +586,7 @@ class _DeviceBrowserPageState extends State<DeviceBrowserPage> {
snackBar.close();
}

GridView _viewAsGrid(List<String> files) {
GridView _viewAsGrid(List<FileBrowserMetadata> files) {
return GridView.builder(
key: ValueKey(files),
controller: widget._scrollController,
Expand All @@ -589,51 +601,27 @@ class _DeviceBrowserPageState extends State<DeviceBrowserPage> {
itemCount: files.length,
itemBuilder: (context, index) {
var file = files[index];
var isDir = file.endsWith("/");
var fileData = fileCache.putIfAbsent(
file,
() => FileBrowserDataWrapper(FileBrowserMetadata(
isDirectory: isDir,
initialFilePath: file,
modifiedTime: Adb.getFileModifiedDate(widget.serial, file),
fileSize: Adb.getFileSize(widget.serial, file),
onWatch: _watchFile,
browser: widget._fileBrowser,
serial: widget.serial,
)));

return GridTile(
child: FileCardWidget(
key: ValueKey(file),
isCard: true,
fileWrapper: fileData,
fileWrapper: file,
));
});
}

_viewAsList(List<String> files) {
_viewAsList(List<FileBrowserMetadata> files) {
// var isDir = file.endsWith("/");

debugPrint("Viewing");
return FileDataTable(
return Align(
alignment: Alignment.topCenter,
child: FileDataTable(
scrollController: widget._scrollController,
originalFileData: files.map((file) {
final isDir = file.endsWith("/");

return fileCache.putIfAbsent(
file,
() => FileBrowserDataWrapper(FileBrowserMetadata(
modifiedTime: Adb.getFileModifiedDate(widget.serial, file),
fileSize: !isDir
? Adb.getFileSize(widget.serial, file)
: Future.value(null),
isDirectory: isDir,
initialFilePath: file,
onWatch: _watchFile,
browser: widget._fileBrowser,
serial: widget.serial,
)));
}).toList(growable: false));
originalFileData: files,
),
);
}

Future<void> _watchFile(String source, String savePath) async {
Expand Down
13 changes: 0 additions & 13 deletions lib/utils/file_data.dart

This file was deleted.

51 changes: 18 additions & 33 deletions lib/widgets/browser/file_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,39 @@ typedef WatchFileCallback = Future<void> Function(
String source, String savePath);

@immutable
class FileBrowserMetadata {
final Future<DateTime?> modifiedTime;
final Future<int?> fileSize;
final String initialFilePath;
class FileBrowserMetadata with FileDataState {
final DateTime? modifiedTime;
final int? fileSize;
final String fullFilePath;
final bool isDirectory;
final FileBrowser browser;
final String serial;
final WatchFileCallback onWatch;

const FileBrowserMetadata({
Key? key,
required this.initialFilePath,
required this.fullFilePath,
required this.isDirectory,
required this.browser,
required this.modifiedTime,
required this.fileSize,
required this.serial,
required this.onWatch,
});

@override
// TODO: implement fileData
FileBrowserMetadata get fileData => this;
}

mixin FileDataState {
FileBrowserMetadata get fileData;

// Set in constructor or initState
late String fullFilePath;
bool editable = false;

/// Just the file name
String get friendlyFileName => Adb.adbPathContext.basename(fullFilePath);
String get friendlyFileName =>
Adb.adbPathContext.basename(fileData.fullFilePath);

Future<void> copyPathToClipboard() {
return FlutterClipboard.copy(fileData.initialFilePath);
return FlutterClipboard.copy(fileData.fullFilePath);
}

IconData getIcon() {
Expand All @@ -62,11 +62,11 @@ mixin FileDataState {
return;
}

fileData.browser.navigateToDirectory(fullFilePath);
fileData.browser.navigateToDirectory(fileData.fullFilePath);
}

Future<void> openTempFile() async {
String questPath = fileData.initialFilePath;
String questPath = fileData.fullFilePath;
String fileName = friendlyFileName;

var temp = await getTemporaryDirectory();
Expand All @@ -90,7 +90,7 @@ mixin FileDataState {
}

Future<void> removeFileDialog(BuildContext context) async {
String path = fileData.initialFilePath;
String path = fileData.fullFilePath;
bool file = !fileData.isDirectory;
await showDialog<void>(
context: context,
Expand Down Expand Up @@ -132,7 +132,7 @@ mixin FileDataState {
}

Future<String?> saveFileToDesktop() async {
String source = fileData.initialFilePath;
String source = fileData.fullFilePath;

final savePath = await getSaveLocation(suggestedName: friendlyFileName);

Expand All @@ -147,32 +147,17 @@ mixin FileDataState {
if (savePath == null) {
return;
}
return fileData.onWatch(fileData.initialFilePath, savePath);
return fileData.onWatch(fileData.fullFilePath, savePath);
}

/// return true if modified
Future<bool> renameFile(String newName) async {
String source = fullFilePath;
String source = fileData.fullFilePath;
var task = Adb.moveFile(fileData.serial, source,
Adb.adbPathContext.join(Adb.adbPathContext.dirname(source), newName));

if (newName != friendlyFileName) {
fullFilePath = Adb.adbPathContext
.join(Adb.adbPathContext.dirname(fullFilePath), newName);
}
await task;

return newName != friendlyFileName;
}
}

final class FileBrowserDataWrapper with FileDataState {
@override
final FileBrowserMetadata fileData;

bool downloading = false;

FileBrowserDataWrapper(this.fileData) {
fullFilePath = fileData.initialFilePath;
}
}
Loading

0 comments on commit ca191f1

Please sign in to comment.