From 164d9b1763214ecef2ab441562910b7eae9aecb3 Mon Sep 17 00:00:00 2001 From: Emil Djupvik Date: Tue, 4 Feb 2020 21:30:47 +0100 Subject: [PATCH] add sharing --- .../app/src/main/res/xml/provider_paths.xml | 4 ++++ android/settings_aar.gradle | 1 + lib/providers/app_data.dart | 24 +++++++++++++------ lib/ui_elements/button.dart | 21 ++++++++++++++++ pubspec.lock | 7 ++++++ pubspec.yaml | 2 ++ 6 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 android/app/src/main/res/xml/provider_paths.xml create mode 100644 android/settings_aar.gradle diff --git a/android/app/src/main/res/xml/provider_paths.xml b/android/app/src/main/res/xml/provider_paths.xml new file mode 100644 index 0000000..ffa74ab --- /dev/null +++ b/android/app/src/main/res/xml/provider_paths.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/android/settings_aar.gradle b/android/settings_aar.gradle new file mode 100644 index 0000000..e7b4def --- /dev/null +++ b/android/settings_aar.gradle @@ -0,0 +1 @@ +include ':app' diff --git a/lib/providers/app_data.dart b/lib/providers/app_data.dart index dd74cdc..c585140 100644 --- a/lib/providers/app_data.dart +++ b/lib/providers/app_data.dart @@ -49,6 +49,16 @@ class AppData with ChangeNotifier { _onButtonTapped.add(url); } + Future writeFile({String url, String name, Directory dir}) async { + if (dir == null) dir = await getExternalStorageDirectory(); + final File file = File('${dir.path}/$name.mp3'); + if (!await file.exists()) { + http.Response response = await http.get(url); + await file.writeAsBytes(response.bodyBytes, flush: true); + } + return file.path; + } + void _getSounds() async { try { final ref = _db.collection("soundboard-data"); @@ -67,14 +77,14 @@ class AppData with ChangeNotifier { }); notifyListeners(); - final Directory dir = await getApplicationDocumentsDirectory(); + final Directory dir = await getExternalStorageDirectory(); + for (SoundData soundData in _data) { - final File file = File('${dir.path}/${soundData.name}.mp3'); - if (!await file.exists()) { - http.Response response = await http.get(soundData.url); - await file.writeAsBytes(response.bodyBytes, flush: true); - } - soundData.url = file.path; + soundData.url = await writeFile( + url: soundData.url, + name: soundData.name, + dir: dir, + ); } bool dataContains(String name) { diff --git a/lib/ui_elements/button.dart b/lib/ui_elements/button.dart index 0a0798a..4e4d7e5 100644 --- a/lib/ui_elements/button.dart +++ b/lib/ui_elements/button.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart'; import 'package:audioplayers/audioplayers.dart'; import 'package:provider/provider.dart'; import 'package:auto_size_text/auto_size_text.dart'; +import 'package:flutter_share/flutter_share.dart'; import '../providers/app_data.dart'; import '../helpers/sound_data.dart'; @@ -69,6 +70,22 @@ class _ButtonState extends State