Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions An2WinFileTransfer/An2WinFileTransfer/Services/DeviceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,40 @@ namespace An2WinFileTransfer.Services
{
public class DeviceService
{
private readonly IEnumerable<MediaDevice> _mediaDevices;

public DeviceService()
{
_mediaDevices = MediaDevice.GetDevices();
}

public IEnumerable<string> GetConnectedDeviceNames()
{
return MediaDevice.GetDevices().Select(d => d.FriendlyName);
return _mediaDevices.Select(d => d.FriendlyName);
}

public MediaDevice ConnectToDevice(string deviceName)
{
var device = MediaDevice.GetDevices()
.FirstOrDefault(d => d.FriendlyName == deviceName);
var device = _mediaDevices.FirstOrDefault(d => d.FriendlyName == deviceName);

if (device == null)
{
throw new InvalidOperationException($"Device '{deviceName}' not found.");
}

device.Connect();
if (!device.IsConnected)
{
device.Connect();
}

return device;
}

public void DisconnectDevice(MediaDevice device)
{
if (device?.IsConnected == true)
if (device == null) return;

if (device.IsConnected)
{
device.Disconnect();
}
Expand Down
5 changes: 1 addition & 4 deletions An2WinFileTransfer/An2WinFileTransfer/UI/Forms/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using An2WinFileTransfer.Extensions;
using An2WinFileTransfer.Models;
using An2WinFileTransfer.Services;
using MediaDevices;

namespace An2WinFileTransfer.UI.Forms
{
Expand Down Expand Up @@ -169,16 +168,14 @@ private IEnumerable<FileType> LoadFileTypes()

private void PopulateDeviceList()
{
_connectedDevices = MediaDevice.GetDevices().Select(d => d.FriendlyName);
_connectedDevices = _deviceService.GetConnectedDeviceNames();

#if !DEBUG
if (_connectedDevices.IsNullOrEmpty())
{
MessageBox.Show("No connected MTP devices found. Please connect your device and enable file transfer mode.",
"No Devices Found", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
#endif

comboBoxDeviceNames.Items.Clear();
comboBoxDeviceNames.Items.AddRange(_connectedDevices.OrderBy(n => n).ToArray());
Expand Down