-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
251 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Windows.Foundation; | ||
using Windows.Storage.Streams; | ||
|
||
namespace MusicX.Helpers; | ||
|
||
public class EmbeddedFileStreamReference(string resourceName, string contentType) : IRandomAccessStreamReference | ||
{ | ||
public IAsyncOperation<IRandomAccessStreamWithContentType> OpenReadAsync() => | ||
OpenReadAsyncInternal().AsAsyncOperation(); | ||
|
||
private async Task<IRandomAccessStreamWithContentType> OpenReadAsyncInternal() | ||
{ | ||
var assembly = typeof(EmbeddedFileStreamReference).Assembly; | ||
await using var manifestResourceStream = assembly.GetManifestResourceStream(resourceName); | ||
|
||
if (manifestResourceStream == null) | ||
throw new FileNotFoundException("Resource not found", resourceName); | ||
|
||
var stream = new InMemoryRandomAccessStream(); | ||
|
||
await using var writeStream = stream.AsStreamForWrite(); | ||
await manifestResourceStream.CopyToAsync(writeStream); | ||
|
||
return new InMemoryRandomAccessStreamWithContentType(stream, contentType); | ||
} | ||
|
||
} | ||
|
||
public sealed class InMemoryRandomAccessStreamWithContentType( | ||
InMemoryRandomAccessStream randomAccessStream, | ||
string contentType) : IRandomAccessStreamWithContentType | ||
{ | ||
public void Dispose() | ||
{ | ||
randomAccessStream.Dispose(); | ||
} | ||
|
||
public IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) => | ||
randomAccessStream.ReadAsync(buffer, count, options); | ||
|
||
public IAsyncOperationWithProgress<uint, uint> WriteAsync(IBuffer buffer) => | ||
randomAccessStream.WriteAsync(buffer); | ||
|
||
public IAsyncOperation<bool> FlushAsync() => randomAccessStream.FlushAsync(); | ||
|
||
public IInputStream GetInputStreamAt(ulong position) => randomAccessStream.GetInputStreamAt(position); | ||
|
||
public IOutputStream GetOutputStreamAt(ulong position) => randomAccessStream.GetOutputStreamAt(position); | ||
|
||
public void Seek(ulong position) => randomAccessStream.Seek(position); | ||
|
||
public IRandomAccessStream CloneStream() => randomAccessStream.CloneStream(); | ||
|
||
public bool CanRead => randomAccessStream.CanRead; | ||
public bool CanWrite => randomAccessStream.CanWrite; | ||
public ulong Position => randomAccessStream.Position; | ||
public ulong Size | ||
{ | ||
get => randomAccessStream.Size; | ||
set => randomAccessStream.Size = value; | ||
} | ||
public string ContentType => contentType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using Windows.ApplicationModel.DataTransfer; | ||
using WinRT; | ||
|
||
namespace MusicX.Helpers; | ||
|
||
public static class DataTransferManagerInterop | ||
{ | ||
private static IDataTransferManagerInterop InteropInstance => DataTransferManager.As<IDataTransferManagerInterop>(); | ||
|
||
public static DataTransferManager GetForWindow(nint appWindow) => DataTransferManager.FromAbi(InteropInstance | ||
.GetForWindow(appWindow, new("A5CAEE9B-8708-49D1-8D36-67D25A8DA00C") /* Guid of IDataTransferManager */)); | ||
|
||
public static void ShowForWindow(nint appWindow) => InteropInstance | ||
.ShowShareUIForWindow(appWindow); | ||
|
||
[ComImport, Guid("3A3DCD6C-3EAB-43DC-BCDE-45671CE800C8")] | ||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] | ||
private interface IDataTransferManagerInterop | ||
{ | ||
nint GetForWindow([In] nint appWindow, [In] ref Guid riid); | ||
void ShowShareUIForWindow(nint appWindow); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Windows.Interop; | ||
using Windows.ApplicationModel.DataTransfer; | ||
using Windows.Storage; | ||
using Windows.Storage.Streams; | ||
using MusicX.Helpers; | ||
using MusicX.Services.Player.Playlists; | ||
using MusicX.Shared.Player; | ||
using NLog; | ||
using Wpf.Ui; | ||
using Wpf.Ui.Extensions; | ||
|
||
namespace MusicX.Services; | ||
|
||
public class ShareService(Logger logger, DownloaderService downloaderService, ISnackbarService snackbarService) | ||
{ | ||
private (PlaylistTrack track, FileInfo file)? _pendingTrack; | ||
private WindowInteropHelper? _window; | ||
|
||
public void AssignWindow(WindowInteropHelper window) | ||
{ | ||
_window = window; | ||
|
||
var manager = DataTransferManagerInterop.GetForWindow(window.Handle); | ||
Check failure on line 27 in MusicX/Services/ShareService.cs GitHub Actions / Build and Package
|
||
|
||
manager.DataRequested += TransferManagerOnDataRequested; | ||
} | ||
|
||
private async void TransferManagerOnDataRequested(DataTransferManager sender, DataRequestedEventArgs args) | ||
{ | ||
var deferral = args.Request.GetDeferral(); | ||
|
||
try | ||
{ | ||
var data = args.Request.Data; | ||
|
||
SetApplicationDetails(data); | ||
|
||
if (_pendingTrack is not null) | ||
{ | ||
var (track, file) = _pendingTrack.Value; | ||
await SetTrackAsync(data, track, file); | ||
} | ||
else | ||
throw new InvalidOperationException("No pending data to share"); | ||
} | ||
catch (Exception e) | ||
{ | ||
logger.Error(e, "Failed to respond to a share request"); | ||
|
||
args.Request.FailWithDisplayText("Упс! Что-то пошло не так"); | ||
} | ||
finally | ||
{ | ||
deferral.Complete(); | ||
} | ||
} | ||
|
||
private async Task SetTrackAsync(DataPackage data, PlaylistTrack track, FileInfo file) | ||
{ | ||
data.Properties.Title = $"{track.GetArtistsString()} - {track.Title}"; | ||
if (track.AlbumId?.CoverUrl is not null) | ||
data.Properties.Thumbnail = RandomAccessStreamReference.CreateFromUri(new Uri(track.AlbumId.CoverUrl)); | ||
|
||
// todo url-only option | ||
// if (track.Data is VkTrackData trackData) | ||
// data.SetWebLink(new Uri($"https://vk.com/audio{trackData.Info}")); | ||
|
||
data.SetStorageItems([await StorageFile.GetFileFromPathAsync(file.FullName)]); | ||
} | ||
|
||
private static void SetApplicationDetails(DataPackage data) | ||
{ | ||
data.Properties.ApplicationName = "MusicX Player"; | ||
data.Properties.Square30x30Logo = new EmbeddedFileStreamReference("MusicX.StoreLogo.scale-30.png", "image/png"); | ||
} | ||
|
||
public async void ShareTrack(PlaylistTrack track) | ||
{ | ||
snackbarService.Show("Подождите...", "Мы готовим трек для отправки", TimeSpan.FromSeconds(5)); | ||
|
||
var file = new FileInfo(Path.Join(Directory.CreateTempSubdirectory("MusicX").FullName, $"{track.GetArtistsString()} - {track.Title}.mp3")); | ||
|
||
// todo fix ffmpeg blocking thread on start | ||
await Task.Run(() => downloaderService.DownloadAudioAsync(track, destinationFile: file)); | ||
|
||
_pendingTrack = (track, file); | ||
|
||
if (_window is not null) | ||
DataTransferManagerInterop.ShowForWindow(_window.Handle); | ||
Check failure on line 93 in MusicX/Services/ShareService.cs GitHub Actions / Build and Package
|
||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters