From 448a0de6fae2683d751502d38d18d3b2f01de949 Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Sat, 18 Apr 2020 20:34:03 +0200 Subject: [PATCH] Move platform dependent code to its own class --- lib/platform.dart | 50 ++++++++++++++++++++++++++++++++++++++++ lib/selectors/files.dart | 27 +++------------------- 2 files changed, 53 insertions(+), 24 deletions(-) create mode 100644 lib/platform.dart diff --git a/lib/platform.dart b/lib/platform.dart new file mode 100644 index 0000000..0e25777 --- /dev/null +++ b/lib/platform.dart @@ -0,0 +1,50 @@ +import 'package:flutter/services.dart'; + +/// Object representing a document in Storage Access Framework terms. +class Document { + /// Unique document ID given by the SAF. + final String id; + + /// Name of the document (i.e. file name). + final String name; + + /// Document ID of the parent document. + final String parent; + + /// Whether this document represents a directory. + final bool isDirectory; + + // Use Map here, as we get casting errors otherwise. This + // won't be typesafe anyway. + Document.fromJson(Map json) + : id = json['id'], + name = json['name'], + parent = json['parent'], + isDirectory = json['isDirectory']; +} + +/// Collection of methods that are implemented platform dependent. +class Platform { + static const _platform = MethodChannel('de.johrpan.musicus/platform'); + + /// Get child documents. + /// + /// [treeId] is the base URI as requested from the SAF. + /// [parentId] is the document ID of the parent. If this is null, the children + /// of the tree base will be returned. + static Future> getChildren( + String treeUri, String parentId) async { + final List> childrenJson = + await _platform.invokeListMethod( + 'getChildren', + { + 'treeUri': treeUri, + 'parentId': parentId, + }, + ); + + return childrenJson + .map((childJson) => Document.fromJson(childJson)) + .toList(); + } +} diff --git a/lib/selectors/files.dart b/lib/selectors/files.dart index c4cb09c..577faef 100644 --- a/lib/selectors/files.dart +++ b/lib/selectors/files.dart @@ -1,20 +1,7 @@ import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; import '../backend.dart'; - -class Document { - final String id; - final String name; - final String parent; - final bool isDirectory; - - Document.fromMap(Map map) - : id = map['id'], - name = map['name'], - parent = map['parent'], - isDirectory = map['isDirectory']; -} +import '../platform.dart'; class FilesSelector extends StatefulWidget { @override @@ -22,8 +9,6 @@ class FilesSelector extends StatefulWidget { } class _FilesSelectorState extends State { - static const platform = MethodChannel('de.johrpan.musicus/platform'); - BackendState backend; List history = []; List children = []; @@ -120,15 +105,9 @@ class _FilesSelectorState extends State { children = []; }); - final childrenMaps = await platform.invokeListMethod>( - 'getChildren', - { - 'treeUri': backend.musicLibraryUri, - 'parentId': history.isNotEmpty ? history.last.id : null, - }, - ); + final newChildren = await Platform.getChildren( + backend.musicLibraryUri, history.isNotEmpty ? history.last.id : null); - final newChildren = childrenMaps.map((m) => Document.fromMap(m)).toList(); newChildren.sort((d1, d2) { if (d1.isDirectory != d2.isDirectory) { return d1.isDirectory ? -1 : 1;