Skip to content

Commit

Permalink
Mac: Tree expand now works properly in SyncMenu. Added implementation…
Browse files Browse the repository at this point in the history
… for detecting free space on Mac. Fixed bugs in SyncMenuPresenter; added a different SelectItems method for mobile/desktop devices.

Related to issue #381.
  • Loading branch information
ycastonguay committed Aug 16, 2013
1 parent 1a2696c commit 4b0b638
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 13 deletions.
99 changes: 92 additions & 7 deletions MPfm/MPfm.MVP/Presenters/SyncMenuPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,88 @@ private void Sync()

private void SelectItems(List<SyncMenuItemEntity> items)
{
#if IOS || ANDROID
SelectItemsMobile(items);
#else
SelectItemsDesktop(items);
#endif
}

private void SelectItemsDesktop(List<SyncMenuItemEntity> items)
{
// The main difference between desktop and mobile is that mobile uses a list view and desktop uses a tree view.
// This makes a difference by removing selections directly on the artist/albums/songs rather than on a list view.
try
{
foreach(var item in items)
{
if(item.ItemType == SyncMenuItemEntityType.Artist)
{
// Add all songs from artist
var songsToAdd = _syncClientService.GetAudioFiles(item.ArtistName);
foreach(var songToAdd in songsToAdd)
if(!_audioFilesToSync.Contains(songToAdd))
_audioFilesToSync.Add(songToAdd);

// Update items
var itemsToUpdate = _items.Where(x => x.ArtistName == item.ArtistName).ToList();
foreach(var itemToUpdate in itemsToUpdate)
itemToUpdate.Selection = StateSelectionType.Selected;
}
else if(item.ItemType == SyncMenuItemEntityType.Album)
{
// Select the whole album
item.Selection = StateSelectionType.Selected;
var songsToAdd = _syncClientService.GetAudioFiles(item.ArtistName, item.AlbumTitle);
foreach(var songToAdd in songsToAdd)
if(!_audioFilesToSync.Contains(songToAdd))
_audioFilesToSync.Add(songToAdd);

// Update song selection
var itemsToSelect = _items.Where(x => x.ArtistName == item.ArtistName && x.AlbumTitle == item.AlbumTitle).ToList();
foreach(var itemToSelect in itemsToSelect)
itemToSelect.Selection = StateSelectionType.Selected;

// Update artist selection
var selection = IsArtistSelected(item.ArtistName);
var itemArtist = _items.FirstOrDefault(x => x.ArtistName == item.ArtistName && x.ItemType == SyncMenuItemEntityType.Artist);
itemArtist.Selection = selection;
}
else if(item.ItemType == SyncMenuItemEntityType.Song)
{
// Update song selection
if(item.Selection == StateSelectionType.Selected)
item.Selection = StateSelectionType.None;
else
item.Selection = StateSelectionType.Selected;

if(!_audioFilesToSync.Contains(item.Song))
_audioFilesToSync.Add(item.Song);

// Update artist selection
var selectionArtist = IsArtistSelected(item.ArtistName);
var itemArtist = _items.FirstOrDefault(x => x.ArtistName == item.ArtistName && x.ItemType == SyncMenuItemEntityType.Artist);
itemArtist.Selection = selectionArtist;

// Update album selection
var selectionAlbum = IsAlbumSelected(item.ArtistName, item.AlbumTitle);
var itemAlbum = _items.FirstOrDefault(x => x.ArtistName == item.ArtistName && x.AlbumTitle == item.AlbumTitle && x.ItemType == SyncMenuItemEntityType.Album);
if(itemAlbum != null)
itemAlbum.Selection = selectionAlbum;
}
}

RefreshSyncTotal();
View.RefreshSelection(_audioFilesToSync);
}
catch(Exception ex)
{
Console.WriteLine("SyncMenuPresenter - SelectItemDesktop - Exception: {0}", ex);
}
}

private void SelectItemsMobile(List<SyncMenuItemEntity> items)
{
try
{
foreach(var item in items)
Expand Down Expand Up @@ -248,7 +330,8 @@ private void SelectItems(List<SyncMenuItemEntity> items)
// Update album selection
var selectionAlbum = IsAlbumSelected(item.ArtistName, item.AlbumTitle);
var itemAlbum = _items.FirstOrDefault(x => x.ArtistName == item.ArtistName && x.AlbumTitle == item.AlbumTitle && x.ItemType == SyncMenuItemEntityType.Album);
itemAlbum.Selection = selectionAlbum;
if(itemAlbum != null)
itemAlbum.Selection = selectionAlbum;
}
}

Expand All @@ -257,9 +340,9 @@ private void SelectItems(List<SyncMenuItemEntity> items)
}
catch(Exception ex)
{
Console.WriteLine("SyncMenuPresenter - SelectItem - Exception: {0}", ex);
Console.WriteLine("SyncMenuPresenter - SelectItemsMobile - Exception: {0}", ex);
}
}
}

private void RemoveItems(List<AudioFile> audioFiles)
{
Expand Down Expand Up @@ -334,7 +417,7 @@ private void ExpandItem(SyncMenuItemEntity item, object userData)
}

//_items.InsertRange(index, items);
View.InsertItems(index + 1, items, userData);
View.InsertItems(index + 1, item, items, userData);
}
break;
case SyncMenuItemEntityType.Album:
Expand All @@ -350,8 +433,10 @@ private void ExpandItem(SyncMenuItemEntity item, object userData)
else
{
int index = _items.FindIndex(x => x.ItemType == SyncMenuItemEntityType.Album && x.ArtistName == item.ArtistName && x.AlbumTitle == item.AlbumTitle);
if(index == -1)
return;

// Fails on Mac because of subitems. is this check necessary?
//if(index == -1)
// return;

var items = new List<SyncMenuItemEntity>();
var songs = _syncClientService.GetAudioFiles(item.ArtistName, item.AlbumTitle);
Expand All @@ -366,7 +451,7 @@ private void ExpandItem(SyncMenuItemEntity item, object userData)
Selection = (selectionCount == 0) ? StateSelectionType.None : StateSelectionType.Selected
});
}
View.InsertItems(index + 1, items, userData);
View.InsertItems(index + 1, item, items, userData);
}
break;
case SyncMenuItemEntityType.Song:
Expand Down
2 changes: 1 addition & 1 deletion MPfm/MPfm.MVP/Views/ISyncMenuView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface ISyncMenuView : IBaseView
void RefreshItems(List<SyncMenuItemEntity> items);
void RefreshSelection(List<AudioFile> audioFiles);
void RefreshSyncTotal(string title, string subtitle, bool enoughFreeSpace);
void InsertItems(int index, List<SyncMenuItemEntity> items, object userData);
void InsertItems(int index, SyncMenuItemEntity parentItem, List<SyncMenuItemEntity> items, object userData);
void RemoveItems(int index, int count, object userData);
}
}
10 changes: 10 additions & 0 deletions MPfm/MPfm.Mac/Classes/MacSyncDeviceSpecifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using MPfm.Library.Objects;
using MPfm.Library.Services;
using MonoMac.Foundation;
using System.IO;

namespace MPfm.Mac
{
Expand All @@ -46,6 +47,15 @@ public string GetDeviceName()

public long GetFreeSpace()
{
string root = Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic));
Console.WriteLine("MacSyncDeviceSpecifications - GetFreeSpace - My music folder: {0} - Root: {1}", Environment.GetFolderPath(Environment.SpecialFolder.MyMusic), root);
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
Console.WriteLine("MacSyncDeviceSpecifications - GetFreeSpace - DriveInfo name: {0} driveType: {1} driveFormat: {2} totalFreeSpace: {3} availableFreeSpace: {4} isReady: {5} rootDirectory: {6}", drive.Name, drive.DriveType.ToString(), drive.DriveFormat.ToString(), drive.TotalFreeSpace, drive.AvailableFreeSpace, drive.IsReady, drive.RootDirectory);
if (drive.RootDirectory.Name == root)
return drive.AvailableFreeSpace;
}

return 0;
}

Expand Down
1 change: 1 addition & 0 deletions MPfm/MPfm.Mac/Classes/Objects/ImageResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ static ImageResources()
NSImage.ImageNamed("icon_phone"),
NSImage.ImageNamed("icon_tablet"),
NSImage.ImageNamed("icon_user"),
NSImage.ImageNamed("icon_song"),
NSImage.ImageNamed("icon_vinyl"),
NSImage.ImageNamed("icon_windows")
};
Expand Down
2 changes: 2 additions & 0 deletions MPfm/MPfm.Mac/MPfm.Mac.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -436,5 +436,7 @@
<BundleResource Include="Resources\icon_windows%402x.png" />
<BundleResource Include="Resources\icon_android.png" />
<BundleResource Include="Resources\icon_android%402x.png" />
<BundleResource Include="Resources\icon_song.png" />
<BundleResource Include="Resources\icon_song%402x.png" />
</ItemGroup>
</Project>
Binary file added MPfm/MPfm.Mac/Resources/icon_song.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added MPfm/MPfm.Mac/Resources/icon_song@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 35 additions & 5 deletions MPfm/MPfm.Mac/Windows/Controllers/SyncMenuWindowController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ partial void actionAdd(NSObject sender)
List<uint> indexes = outlineView.SelectedRows.ToList();
List<SyncMenuItemEntity> items = new List<SyncMenuItemEntity>();
foreach(uint index in indexes)
items.Add(_items[(int)index].Entity);
{
var test = (SyncMenuItem)outlineView.ItemAtRow((int)index);
items.Add(test.Entity);
//items.Add(_items[(int)index].Entity);
}

OnSelectItems(items);
}
Expand Down Expand Up @@ -253,7 +257,7 @@ public NSView GetViewForItem(NSOutlineView outlineView, NSTableColumn tableColum
view.ImageView.Image = ImageResources.Icons.FirstOrDefault(x => x.Name == "icon_vinyl");
break;
case SyncMenuItemEntityType.Song:
view.ImageView.Image = null;
view.ImageView.Image = ImageResources.Icons.FirstOrDefault(x => x.Name == "icon_song");
if (syncMenuItem.Entity.Song != null)
view.TextField.StringValue = syncMenuItem.Entity.Song.Title;
break;
Expand Down Expand Up @@ -354,11 +358,37 @@ public void RefreshSyncTotal(string title, string subtitle, bool enoughFreeSpace
});
}

public void InsertItems(int index, List<SyncMenuItemEntity> items, object userData)
public void InsertItems(int index, SyncMenuItemEntity parentItem, List<SyncMenuItemEntity> items, object userData)
{
Console.WriteLine("SyncMenuWindowController - InsertItems - index: {0} items.Count: {1}", index, items.Count);
// TODO: Add also a reference to the parent SyncMenuItemEntity, so it will be easier to find than the index on certain platforms.
// i.e. this one will update the _items list with SubItems instead of a flat list like iOS and Android.
InvokeOnMainThread(delegate {
var item = _items.FirstOrDefault(x => x.Entity == parentItem);
// Try to search in subitems
if(item == null)
foreach(var currentItem in _items)
foreach(var subItem in currentItem.SubItems)
if(subItem.Entity == parentItem)
{
item = subItem;
break;
}
if(item == null)
return;

// Clear dummy node and add actual items
item.SubItems.Clear();
foreach(var entity in items)
{
var newItem = new SyncMenuItem(entity);
if(entity.ItemType != SyncMenuItemEntityType.Song)
newItem.SubItems.Add(new SyncMenuItem(new SyncMenuItemEntity(){
ArtistName = "dummy",
AlbumTitle = "dummy"
}));
item.SubItems.Add(newItem);
}
outlineView.ReloadData();
});
}

public void RemoveItems(int index, int count, object userData)
Expand Down

0 comments on commit 4b0b638

Please sign in to comment.