Skip to content

Commit

Permalink
Implement disposeIdentifier method
Browse files Browse the repository at this point in the history
  • Loading branch information
amake authored and hpoul committed Apr 14, 2021
1 parent 82c7776 commit 3b89a99
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@ class FilePickerWritableImpl(
copyContentUriAndReturn(result, fileUri)
}

fun disposeIdentifier(identifier: String) {
val activity = requireActivity()
val contentResolver = activity.applicationContext.contentResolver
val takeFlags: Int = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
contentResolver.releasePersistableUriPermission(Uri.parse(identifier), takeFlags)
}

private fun requireActivity() = (plugin.activity
?: throw FilePickerException("Illegal state, expected activity to be there."))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ class FilePickerWritablePlugin : FlutterPlugin, MethodCallHandler,
?: throw FilePickerException("Expected argument 'path'")
impl.writeFileWithIdentifier(result, identifier, File(path))
}
"disposeIdentifier" -> {
val identifier = call.argument<String>("identifier")
?: throw FilePickerException("Expected argument 'identifier'")
impl.disposeIdentifier(identifier)
result.success(null)
}
else -> {
result.notImplemented()
}
Expand Down
2 changes: 2 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ class FileInfoDisplay extends StatelessWidget {
),
IconButton(
onPressed: () async {
await FilePickerWritable()
.disposeIdentifier(fileInfo.identifier);
final appData = await appDataBloc.store.load();
await appDataBloc.store.save(appData.copyWith(
files: appData.files
Expand Down
3 changes: 3 additions & 0 deletions ios/Classes/SwiftFilePickerWritablePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public class SwiftFilePickerWritablePlugin: NSObject, FlutterPlugin {
throw FilePickerError.invalidArguments(message: "Expected 'identifier' and 'path' arguments.")
}
try writeFile(identifier: identifier, path: path, result: result)
case "disposeIdentifier":
// iOS doesn't have a concept of disposing identifiers (bookmarks)
result(nil)
default:
result(FlutterMethodNotImplemented)
}
Expand Down
12 changes: 12 additions & 0 deletions lib/src/file_picker_writable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,18 @@ class FilePickerWritable {
return _resultToFileInfo(result);
}

/// Dispose of a persistable identifier, removing it from your app's list of
/// accessible files. Afterwards, you will need the user to re-pick the file
/// in order to access it again.
///
/// Some platforms (Android) limit how many identifiers your app can persist
/// at once. Use this method to remove identifiers you no longer need.
Future<void> disposeIdentifier(String identifier) async {
_logger.finest('disposeIdentifier()');
return _channel
.invokeMethod<void>('disposeIdentifier', {'identifier': identifier});
}

FileInfo _resultToFileInfo(Map<String, String> result) {
return FileInfo(
identifier: result['identifier']!,
Expand Down

0 comments on commit 3b89a99

Please sign in to comment.