Skip to content

Commit

Permalink
fix: tagger stuck in some scenarios
Browse files Browse the repository at this point in the history
& other tagger fixes
  • Loading branch information
MSOB7YY committed Feb 29, 2024
1 parent 81929ab commit 9a9b25c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
22 changes: 20 additions & 2 deletions android/app/src/main/kotlin/com/example/namida/FAudioTagger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import java.io.File
import java.io.FileOutputStream
import java.nio.ByteBuffer
import java.util.Base64
import java.util.concurrent.CompletableFuture
import kotlin.collections.List
import kotlin.collections.Map
import kotlinx.coroutines.*
Expand All @@ -39,6 +40,7 @@ public class FAudioTagger : FlutterPlugin, MethodCallHandler {
lateinit var channel: MethodChannel
lateinit var binaryMessenger: BinaryMessenger
val eventChannels = HashMap<Long, BetterEventChannel>()
val streamCompleters = HashMap<Long, CompletableFuture<Number>>()
lateinit var context: Context

companion object {
Expand Down Expand Up @@ -110,6 +112,17 @@ public class FAudioTagger : FlutterPlugin, MethodCallHandler {
result.error("Failure", "path parameter isn't provided", "")
}
}
"streamReady" -> {
val streamKey = call.argument<Long?>("streamKey") ?: 0
val count = call.argument<Number?>("count") ?: 0
val completer = streamCompleters.get(streamKey)
if (completer != null) {
completer.complete(count)
result.success(true)
} else {
result.success(false)
}
}
"readAllDataAsStream" -> {
val paths = call.argument<List<String>?>("paths")
if (paths != null) {
Expand All @@ -124,9 +137,14 @@ public class FAudioTagger : FlutterPlugin, MethodCallHandler {
BetterEventChannel(binaryMessenger, "faudiotagger/stream/" + streamKey)
)
val eventChannel = eventChannels.get(streamKey)!!
result.success(true)
_addLogsUser()

streamCompleters.set(streamKey, CompletableFuture<Number>())
result.success(streamKey)

CoroutineScope(Dispatchers.IO).launch {
_addLogsUser()
// waiting for confirmation before posting to stream
streamCompleters.get(streamKey)!!.get()
for (p in paths) {
val map =
readAllData(
Expand Down
16 changes: 9 additions & 7 deletions lib/controller/tagger_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ class FAudioTaggerController {
"artworkIdentifiers": _defaultGroupArtworksByAlbum ? _defaultAlbumIdentifier.map((e) => e.index).toList() : null,
});
final channelEvent = EventChannel('faudiotagger/stream/$streamKey');
return channelEvent.receiveBroadcastStream().map((event) {
final stream = channelEvent.receiveBroadcastStream().map((event) {
final message = event as Map<Object?, Object?>;
final map = message.cast<String, dynamic>();
return map;
});
_channel.invokeMethod("streamReady", {"streamKey": streamKey, "count": paths.length});
return stream;
}

Future<String?> writeTags({
Expand Down Expand Up @@ -246,7 +248,7 @@ class FAudioTaggerController {
editedTags.updateAll((key, value) => value.trimAll());
}

final shouldUpdateArtwork = imagePath != '';
final imageFile = imagePath != '' ? File(imagePath) : null;

String oldComment = '';
if (commentToInsert != '') {
Expand All @@ -260,7 +262,7 @@ class FAudioTaggerController {
)
: FTags(
path: '',
artwork: FArtwork(file: shouldUpdateArtwork ? File(imagePath) : null),
artwork: FArtwork(file: imageFile),
title: editedTags[TagField.title],
album: editedTags[TagField.album],
artist: editedTags[TagField.artist],
Expand Down Expand Up @@ -303,15 +305,13 @@ class FAudioTaggerController {

await file.executeAndKeepStats(
() async {
bool didUpdate = false;

// -- 1. try tagger
error = await FAudioTaggerController.inst.writeTags(
path: track.path,
tags: newTags,
);

// didUpdate = error == null || error == '';
bool didUpdate = error == null || error == '';

if (!didUpdate) {
// -- 2. try with ffmpeg
Expand Down Expand Up @@ -347,7 +347,9 @@ class FAudioTaggerController {
}
if (didUpdate) {
final trExt = track.toTrackExt();
tracksMap[track] = trExt.copyWithTag(tag: newTags);
final newTrExt = trExt.copyWithTag(tag: newTags);
tracksMap[track] = newTrExt;
if (imageFile != null) await imageFile.copy(newTrExt.pathToImage);
}
printo('Did Update Metadata: $didUpdate', isError: !didUpdate);
if (onEdit != null) onEdit(didUpdate, error, track);
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/dialogs/edit_tags_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ Future<void> _editSingleTrackTagsDialog(Track track, Color? colorScheme) async {
key: Key(currentImagePath.value),
thumbnailSize: Get.width / 3,
bytes: currentImagePath.value != '' ? null : artwork?.bytes,
path: currentImagePath.value != '' ? currentImagePath.value : track.pathToImage,
path: currentImagePath.value != '' ? currentImagePath.value : null,
onTopWidgets: [
Positioned(
bottom: 0,
Expand Down

0 comments on commit 9a9b25c

Please sign in to comment.