Skip to content

Commit

Permalink
add sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil1483 committed Feb 4, 2020
1 parent 789f0fc commit 164d9b1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
4 changes: 4 additions & 0 deletions android/app/src/main/res/xml/provider_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
1 change: 1 addition & 0 deletions android/settings_aar.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ':app'
24 changes: 17 additions & 7 deletions lib/providers/app_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ class AppData with ChangeNotifier {
_onButtonTapped.add(url);
}

Future<String> 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");
Expand All @@ -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) {
Expand Down
21 changes: 21 additions & 0 deletions lib/ui_elements/button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -69,6 +70,22 @@ class _ButtonState extends State<Button> with SingleTickerProviderStateMixin {
_controller.forward();
}

Future<void> _share() async {
String path = widget.soundData.url;
if (path.contains("https://")) {
final AppData appData = Provider.of<AppData>(context, listen: false);
path = await appData.writeFile(
name: widget.soundData.name,
url: path,
);
}

await FlutterShare.shareFile(
title: "A PewDiePie sound",
filePath: path,
);
}

@override
Widget build(BuildContext context) {
return ScaleTransition(
Expand All @@ -78,6 +95,10 @@ class _ButtonState extends State<Button> with SingleTickerProviderStateMixin {
),
alignment: Alignment.center,
child: GestureDetector(
onLongPress: () async {
Feedback.forLongPress(context);
await _share();
},
onTap: () async {
_currentDuration = null;

Expand Down
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.4"
flutter_share:
dependency: "direct main"
description:
name: flutter_share
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2+1"
flutter_staggered_grid_view:
dependency: "direct main"
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ dependencies:

auto_size_text: ^2.1.0

flutter_share: ^1.0.2+1

dev_dependencies:
flutter_test:
sdk: flutter
Expand Down

0 comments on commit 164d9b1

Please sign in to comment.