mirror of
				https://github.com/johrpan/musicus_mobile.git
				synced 2025-10-26 18:57:25 +01:00 
			
		
		
		
	Move platform dependent code to its own class
This commit is contained in:
		
							parent
							
								
									3471fcf78b
								
							
						
					
					
						commit
						448a0de6fa
					
				
					 2 changed files with 53 additions and 24 deletions
				
			
		
							
								
								
									
										50
									
								
								lib/platform.dart
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								lib/platform.dart
									
										
									
									
									
										Normal file
									
								
							|  | @ -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<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 { | ||||||
|  |   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 { | ||||||
|  |     final List<Map<dynamic, dynamic>> childrenJson = | ||||||
|  |         await _platform.invokeListMethod( | ||||||
|  |       'getChildren', | ||||||
|  |       { | ||||||
|  |         'treeUri': treeUri, | ||||||
|  |         'parentId': parentId, | ||||||
|  |       }, | ||||||
|  |     ); | ||||||
|  | 
 | ||||||
|  |     return childrenJson | ||||||
|  |         .map((childJson) => Document.fromJson(childJson)) | ||||||
|  |         .toList(); | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | @ -1,20 +1,7 @@ | ||||||
| import 'package:flutter/material.dart'; | import 'package:flutter/material.dart'; | ||||||
| import 'package:flutter/services.dart'; |  | ||||||
| 
 | 
 | ||||||
| import '../backend.dart'; | import '../backend.dart'; | ||||||
| 
 | import '../platform.dart'; | ||||||
| class Document { |  | ||||||
|   final String id; |  | ||||||
|   final String name; |  | ||||||
|   final String parent; |  | ||||||
|   final bool isDirectory; |  | ||||||
| 
 |  | ||||||
|   Document.fromMap(Map<dynamic, dynamic> map) |  | ||||||
|       : id = map['id'], |  | ||||||
|         name = map['name'], |  | ||||||
|         parent = map['parent'], |  | ||||||
|         isDirectory = map['isDirectory']; |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| class FilesSelector extends StatefulWidget { | class FilesSelector extends StatefulWidget { | ||||||
|   @override |   @override | ||||||
|  | @ -22,8 +9,6 @@ class FilesSelector extends StatefulWidget { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| class _FilesSelectorState extends State<FilesSelector> { | class _FilesSelectorState extends State<FilesSelector> { | ||||||
|   static const platform = MethodChannel('de.johrpan.musicus/platform'); |  | ||||||
| 
 |  | ||||||
|   BackendState backend; |   BackendState backend; | ||||||
|   List<Document> history = []; |   List<Document> history = []; | ||||||
|   List<Document> children = []; |   List<Document> children = []; | ||||||
|  | @ -120,15 +105,9 @@ class _FilesSelectorState extends State<FilesSelector> { | ||||||
|       children = []; |       children = []; | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     final childrenMaps = await platform.invokeListMethod<Map<dynamic, dynamic>>( |     final newChildren = await Platform.getChildren( | ||||||
|       'getChildren', |         backend.musicLibraryUri, history.isNotEmpty ? history.last.id : null); | ||||||
|       { |  | ||||||
|         'treeUri': backend.musicLibraryUri, |  | ||||||
|         'parentId': history.isNotEmpty ? history.last.id : null, |  | ||||||
|       }, |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     final newChildren = childrenMaps.map((m) => Document.fromMap(m)).toList(); |  | ||||||
|     newChildren.sort((d1, d2) { |     newChildren.sort((d1, d2) { | ||||||
|       if (d1.isDirectory != d2.isDirectory) { |       if (d1.isDirectory != d2.isDirectory) { | ||||||
|         return d1.isDirectory ? -1 : 1; |         return d1.isDirectory ? -1 : 1; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Elias Projahn
						Elias Projahn