Skip to content

Commit

Permalink
add audio tip card
Browse files Browse the repository at this point in the history
  • Loading branch information
Codel1417 committed May 30, 2024
1 parent 63da2d5 commit 204c81b
Showing 1 changed file with 96 additions and 88 deletions.
184 changes: 96 additions & 88 deletions lib/Frontend/pages/custom_audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:tail_app/Backend/Definitions/Action/base_action.dart';
import 'package:tail_app/Backend/audio.dart';
import 'package:uuid/uuid.dart';

import '../Widgets/tutorial_card.dart';
import '../intn_defs.dart';

final Logger _audioLogger = Logger('Audio');
Expand All @@ -25,96 +26,103 @@ class _CustomAudioState extends ConsumerState<CustomAudio> {
Widget build(BuildContext context) {
List<AudioAction> userAudioActions = ref.watch(userAudioActionsProvider);
return Scaffold(
appBar: AppBar(
title: Text(audioPage()),
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () async {
_audioLogger.info("Opening file dialog");
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.audio,
);
if (result != null) {
_audioLogger.info("Selected file");
PlatformFile file = result.files.first;
final Directory appDir = await getApplicationSupportDirectory();
Directory audioDir = Directory("${appDir.path}/audio");
await audioDir.create();
File storedAudioFilePath = File("${audioDir.path}/${file.name}");
_audioLogger.info("File path ${storedAudioFilePath.path}");
File selectedFile = File(file.path!);
_audioLogger.info("Selected file Path ${selectedFile.path}");
Stream<List<int>> openRead = selectedFile.openRead();
IOSink ioSinkWrite = storedAudioFilePath.openWrite();
await ioSinkWrite.addStream(openRead);
ioSinkWrite.close();
_audioLogger.info("Wrote file to app storage");
AudioAction action = AudioAction(
name: file.name.substring(0, file.name.lastIndexOf(".")).replaceAll("_", " ").replaceAll("-", " "),
uuid: const Uuid().v4(),
file: storedAudioFilePath.path,
appBar: AppBar(
title: Text(audioPage()),
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () async {
_audioLogger.info("Opening file dialog");
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.audio,
);
setState(() {
ref.read(userAudioActionsProvider.notifier).add(action);
});
}
//Open File Picker
},
icon: const Icon(Icons.add),
label: Text(audioAdd())),
body: ListView.builder(
itemCount: userAudioActions.length,
shrinkWrap: true,
itemBuilder: (context, index) {
AudioAction audioAction = userAudioActions[index];
return ListTile(
title: Text(audioAction.name),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
onPressed: () {},
tooltip: audioEdit(), //TODO: Edit record name
icon: const Icon(Icons.edit),
),
IconButton(
onPressed: () {
showDialog<bool>(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text(audioDelete()),
content: Text(audioDeleteDescription()),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context, false),
child: Text(cancel()),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: Text(ok()),
),
],
if (result != null) {
_audioLogger.info("Selected file");
PlatformFile file = result.files.first;
final Directory appDir = await getApplicationSupportDirectory();
Directory audioDir = Directory("${appDir.path}/audio");
await audioDir.create();
File storedAudioFilePath = File("${audioDir.path}/${file.name}");
_audioLogger.info("File path ${storedAudioFilePath.path}");
File selectedFile = File(file.path!);
_audioLogger.info("Selected file Path ${selectedFile.path}");
Stream<List<int>> openRead = selectedFile.openRead();
IOSink ioSinkWrite = storedAudioFilePath.openWrite();
await ioSinkWrite.addStream(openRead);
ioSinkWrite.close();
_audioLogger.info("Wrote file to app storage");
AudioAction action = AudioAction(
name: file.name.substring(0, file.name.lastIndexOf(".")).replaceAll("_", " ").replaceAll("-", " "),
uuid: const Uuid().v4(),
file: storedAudioFilePath.path,
);
setState(() {
ref.read(userAudioActionsProvider.notifier).add(action);
});
}
//Open File Picker
},
icon: const Icon(Icons.add),
label: Text(audioAdd())),
body: ListView(
children: [
PageInfoCard(
text: audioTipCard(),
),
ListView.builder(
itemCount: userAudioActions.length,
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (context, index) {
AudioAction audioAction = userAudioActions[index];
return ListTile(
title: Text(audioAction.name),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
onPressed: () {},
tooltip: audioEdit(), //TODO: Edit record name
icon: const Icon(Icons.edit),
),
).then((value) async {
if (value == true) {
ref.read(userAudioActionsProvider.notifier).remove(audioAction);
File storedAudioFilePath = File(audioAction.file);
await storedAudioFilePath.delete();
setState(() {
_audioLogger.info("Deleted audio file");
});
}
});
}, //TODO: Show dialog, then delete record and file.
tooltip: audioDelete(),
icon: const Icon(Icons.delete),
)
],
IconButton(
onPressed: () {
showDialog<bool>(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text(audioDelete()),
content: Text(audioDeleteDescription()),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context, false),
child: Text(cancel()),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: Text(ok()),
),
],
),
).then((value) async {
if (value == true) {
ref.read(userAudioActionsProvider.notifier).remove(audioAction);
File storedAudioFilePath = File(audioAction.file);
await storedAudioFilePath.delete();
setState(() {
_audioLogger.info("Deleted audio file");
});
}
});
}, //TODO: Show dialog, then delete record and file.
tooltip: audioDelete(),
icon: const Icon(Icons.delete),
)
],
),
onTap: () => playSound(audioAction.file),
);
},
),
onTap: () => playSound(audioAction.file),
);
},
),
);
],
));
}
}

0 comments on commit 204c81b

Please sign in to comment.