Move reusable code from mobile to common

This will be useful for a future desktop application.
This commit is contained in:
Elias Projahn 2020-05-04 21:49:44 +02:00
parent 6e1255f26e
commit 711b19c998
40 changed files with 813 additions and 581 deletions

View file

@ -1,44 +1,16 @@
import 'package:flutter/services.dart';
import 'package:musicus_common/musicus_common.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<dynamic, dynamic> here, as we get casting errors otherwise. This
// won't be typesafe anyway.
Document.fromJson(Map<dynamic, dynamic> json)
: id = json['id'],
name = json['name'],
parent = json['parent'],
isDirectory = json['isDirectory'];
}
/// Collection of methods that are implemented platform dependent.
class Platform {
class MusicusAndroidPlatform extends MusicusPlatform {
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<List<Document>> getChildren(
String treeUri, String parentId) async {
@override
Future<List<Document>> getChildren(String parentId) async {
final List<Map<dynamic, dynamic>> childrenJson =
await _platform.invokeListMethod(
'getChildren',
{
'treeUri': treeUri,
'treeUri': basePath,
'parentId': parentId,
},
);
@ -48,65 +20,51 @@ class Platform {
.toList();
}
/// Read contents of file.
///
/// [treeId] is the base URI from the SAF, [id] is the document ID of the
/// file.
static Future<String> readFile(String treeUri, String id) async {
@override
Future<String> getIdentifier(String parentId, String fileName) async {
return await _platform.invokeMethod(
'getUriByName',
{
'treeUri': basePath,
'parentId': parentId,
'fileName': fileName,
},
);
}
@override
Future<String> readDocument(String id) async {
return await _platform.invokeMethod(
'readFile',
{
'treeUri': treeUri,
'treeUri': basePath,
'id': id,
},
);
}
/// Get document URI by file name
///
/// [treeId] is the base URI from the SAF, [parentId] is the document ID of
/// the parent directory.
static Future<String> getUriByName(
String treeUri, String parentId, String fileName) async {
return await _platform.invokeMethod(
'getUriByName',
{
'treeUri': treeUri,
'parentId': parentId,
'fileName': fileName,
},
);
}
/// Read contents of file by name
///
/// [treeId] is the base URI from the SAF, [parentId] is the document ID of
/// the parent directory.
static Future<String> readFileByName(
String treeUri, String parentId, String fileName) async {
@override
Future<String> readDocumentByName(String parentId, String fileName) async {
return await _platform.invokeMethod(
'readFileByName',
{
'treeUri': treeUri,
'treeUri': basePath,
'parentId': parentId,
'fileName': fileName,
},
);
}
/// Write to file by name
///
/// [treeId] is the base URI from the SAF, [parentId] is the document ID of
/// the parent directory.
static Future<void> writeFileByName(
String treeUri, String parentId, String fileName, String content) async {
@override
Future<void> writeDocumentByName(
String parentId, String fileName, String contents) async {
await _platform.invokeMethod(
'writeFileByName',
{
'treeUri': treeUri,
'treeUri': basePath,
'parentId': parentId,
'fileName': fileName,
'content': content,
'content': contents,
},
);
}