Skip to content

Commit

Permalink
add caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil1483 committed Jan 19, 2020
1 parent 19591a9 commit db4ed25
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lib/routes/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:audioplayers/audioplayers.dart';
import 'package:audioplayers/audio_cache.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';

import '../helpers/sound_data.dart';

Expand All @@ -16,8 +19,7 @@ class _HomePageState extends State<HomePage> {

final List<SoundData> _data = [];

final AudioPlayer _audio = new AudioPlayer();
final AudioCache _audioCache = new AudioCache();
static final AudioPlayer _audio = AudioPlayer();

@override
void initState() {
Expand All @@ -30,7 +32,16 @@ class _HomePageState extends State<HomePage> {
final ref = _db.collection("soundboard-data");
final QuerySnapshot snap = await ref.getDocuments();
for (DocumentSnapshot doc in snap.documents) {
_data.add(SoundData.fromJson(doc.data));
File file = await DefaultCacheManager().getSingleFile(
doc.data["url"],
);

_data.add(
SoundData(
name: doc.data["name"],
url: file.path,
),
);
}
setState(() {});
} catch (e) {
Expand All @@ -57,11 +68,8 @@ class _HomePageState extends State<HomePage> {
print("playing: ${_data[index].url}");

final String url = _data[index].url;
if (url.contains("https://")) {
await _audio.play(url);
} else {
await _audioCache.play(url);
}

await _audio.play(url);
},
child: Container(
color: Colors.orange,
Expand Down
21 changes: 21 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ 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 Down Expand Up @@ -231,6 +238,13 @@ 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 @@ -252,6 +266,13 @@ 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
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ dependencies:
flutter_staggered_grid_view: ^0.3.0
audioplayers: ^0.13.7

flutter_cache_manager: ^1.1.3

firebase_messaging: ^6.0.9
cloud_firestore: ^0.13.0+1

Expand Down

0 comments on commit db4ed25

Please sign in to comment.