Skip to content

Commit

Permalink
complete functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil1483 committed Jan 25, 2020
1 parent 65b409b commit 0b04a10
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 47 deletions.
2 changes: 1 addition & 1 deletion lib/helpers/sound_data.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';

class SoundData {
final String url;
String url;
final String name;

SoundData({
Expand Down
59 changes: 47 additions & 12 deletions lib/routes/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:audioplayers/audioplayers.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';

import '../helpers/sound_data.dart';
import '../ui_elements/button.dart';
Expand All @@ -26,23 +27,57 @@ class _HomePageState extends State<HomePage> {
_getSounds();
}

@override
void dispose() {
super.dispose();
_player.dispose();
}

void _getSounds() async {
try {
final ref = _db.collection("soundboard-data");
final QuerySnapshot snap = await ref.getDocuments();
for (DocumentSnapshot doc in snap.documents) {
File file = await DefaultCacheManager().getSingleFile(
doc.data["url"],
);

_data.add(
SoundData(
name: doc.data["name"],
url: file.path,
),
);
setState(() {
for (DocumentSnapshot doc in snap.documents) {
_data.add(
SoundData(
name: doc.data["name"],
url: doc.data["url"],
),
);
}
});

final Directory dir = await getApplicationDocumentsDirectory();
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;
}
setState(() {});

bool dataContains(String name) {
bool result = false;
_data.forEach((SoundData soundData) {
if ("${soundData.name}.mp3" == name) {
result = true;
}
});
return result;
}

dir
.list(recursive: true, followLinks: false)
.listen((FileSystemEntity entity) async {
if (!entity.path.endsWith(".mp3")) return;
final name = entity.path.split("/").last;
if (dataContains(name)) return;

await entity.delete();
});
} catch (e) {
print(e);
}
Expand Down
29 changes: 19 additions & 10 deletions lib/ui_elements/button.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:audioplayers/audioplayers.dart';

import '../helpers/sound_data.dart';
Expand All @@ -20,32 +23,38 @@ class _ButtonState extends State<Button> {
bool _tapped = false;
bool _justTapped = false;
Duration _currentDuration = Duration();
Timer _timer;

@override
void initState() {
super.initState();
widget.audio.onDurationChanged.listen((Duration time) {
widget.audio.onDurationChanged.listen((Duration time) async {
if (_currentDuration != time) {
_currentDuration = time;
if (!_justTapped) setState(() => _tapped = false);
}
});
widget.audio.onAudioPositionChanged.listen((Duration time) async {
try {
if (await widget.audio.getDuration() <= time.inMilliseconds + 500) {
await Future.delayed(Duration(milliseconds: 500));
if (_justTapped) {
if (_timer != null) _timer.cancel();
_timer = Timer(_currentDuration, () {
setState(() => _tapped = false);
});
} else {
setState(() => _tapped = false);
}
} on Exception catch (e) {
print(e);
}
});
}

@override
void dispose() {
super.dispose();
if (_timer != null) _timer.cancel();
}

@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () async {
_currentDuration = null;

await widget.audio.stop();
await widget.audio.play(widget.soundData.url);

Expand Down
25 changes: 2 additions & 23 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,6 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_cache_manager:
dependency: "direct main"
description:
name: flutter_cache_manager
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.3"
flutter_staggered_grid_view:
dependency: "direct main"
description:
Expand All @@ -143,7 +136,7 @@ packages:
source: sdk
version: "0.0.0"
http:
dependency: transitive
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
Expand Down Expand Up @@ -192,7 +185,7 @@ packages:
source: hosted
version: "1.6.4"
path_provider:
dependency: transitive
dependency: "direct main"
description:
name: path_provider
url: "https://pub.dartlang.org"
Expand Down Expand Up @@ -238,13 +231,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.5"
sqflite:
dependency: transitive
description:
name: sqflite
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
stack_trace:
dependency: transitive
description:
Expand All @@ -266,13 +252,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.5"
synchronized:
dependency: transitive
description:
name: synchronized
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
term_glyph:
dependency: transitive
description:
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ dependencies:
flutter_staggered_grid_view: ^0.3.0
audioplayers: ^0.13.7

flutter_cache_manager: ^1.1.3
path_provider: ^1.5.1
http: ^0.12.0+4

firebase_messaging: ^6.0.9
cloud_firestore: ^0.13.0+1
Expand Down

0 comments on commit 0b04a10

Please sign in to comment.