Skip to content

Commit

Permalink
iOS: Migrated to SDK for iOS 7; minimum version is now iOS 7. Fixed m…
Browse files Browse the repository at this point in the history
…ost issues with iOS 7. Now using async instead of ContinueWith because Xamarin.iOS 7 has a strange behavior with ContinueWith even though I was not using async.

Related to issue #405.
  • Loading branch information
ycastonguay committed Sep 27, 2013
1 parent 5e46dc0 commit 0cfe52d
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 55 deletions.
11 changes: 7 additions & 4 deletions MPfm/MPfm.MVP/MPfm.MVP.iOS.csproj
Expand Up @@ -131,7 +131,7 @@
<Reference Include="taglib-sharp">
<HintPath>..\MPfm.Sound\Lib\iOS\taglib-sharp.dll</HintPath>
</Reference>
<Reference Include="TinyIoC.iOS">
<Reference Include="TinyIoC">
<HintPath>..\MPfm.Core\Lib\iOS\TinyIoC.dll</HintPath>
</Reference>
</ItemGroup>
Expand Down Expand Up @@ -188,8 +188,6 @@
<Compile Include="Models\PlaylistEntity.cs" />
<Compile Include="Navigation\MobileNavigationManager.cs" />
<Compile Include="Presenters\AudioPreferencesPresenter.cs" />
<Compile Include="Presenters\AddNewPlaylistPresenter.cs" />
<Compile Include="Presenters\Interfaces\IAddNewPlaylistPresenter.cs" />
<Compile Include="Presenters\Interfaces\ISelectPlaylistPresenter.cs" />
<Compile Include="Presenters\SelectPlaylistPresenter.cs" />
<Compile Include="Presenters\MobileFirstRunPresenter.cs" />
Expand All @@ -207,7 +205,6 @@
<Compile Include="Presenters\Interfaces\IAudioPreferencesPresenter.cs" />
<Compile Include="Presenters\UpdateLibraryPresenter.cs" />
<Compile Include="Models\PlayerPositionEntity.cs" />
<Compile Include="Views\IAddNewPlaylistView.cs" />
<Compile Include="Views\ISelectPlaylistView.cs" />
<Compile Include="Views\IMobileFirstRunView.cs" />
<Compile Include="Views\IDesktopFirstRunView.cs" />
Expand Down Expand Up @@ -323,5 +320,11 @@
<Compile Include="Messages\PlayerPlaylistUpdatedMessage.cs" />
<Compile Include="Views\IDesktopPreferencesView.cs" />
<Compile Include="Views\IDesktopEffectsView.cs" />
<Compile Include="Views\IAddMarkerView.cs" />
<Compile Include="Views\IAddPlaylistView.cs" />
<Compile Include="Presenters\AddMarkerPresenter.cs" />
<Compile Include="Presenters\AddPlaylistPresenter.cs" />
<Compile Include="Presenters\Interfaces\IAddMarkerPresenter.cs" />
<Compile Include="Presenters\Interfaces\IAddPlaylistPresenter.cs" />
</ItemGroup>
</Project>
70 changes: 64 additions & 6 deletions MPfm/MPfm.MVP/Presenters/SplashPresenter.cs
Expand Up @@ -38,6 +38,62 @@ public SplashPresenter(IInitializationService initializationService, IPlayerServ
_playerService = playerService;
}

#if IOS
// iOS: Use async because ContinueWith has a strange behavior in Xamarin.iOS 7+. TODO: Check for a solution with Xamarin
public async void Initialize(Action onInitDone)
{
if (_playerService.IsInitialized)
{
onInitDone.Invoke();
return;
}

var task = Task<string>.Factory.StartNew(() =>
{
Console.WriteLine("SplashPresenter - Initialize - Initializing service on another thread...");
View.RefreshStatus("Loading...");
_initializationService.Initialize();
Console.WriteLine("SplashPresenter - Initialize - Initializing service on another thread... DONE!");
return string.Empty;
});
Console.WriteLine("SplashPresenter - Initialize - Before task");
string test = await task;
Console.WriteLine("SplashPresenter - Initialize - After task");

// Initialize player
Console.WriteLine("SplashPresenter - Initialize - Initializing player on main thread...");
Device device = new Device(){
DriverType = DriverType.DirectSound,
Id = -1
};
_playerService.Initialize(device, 44100, 1000, 100);
View.InitDone(true);
onInitDone.Invoke();
Console.WriteLine("SplashPresenter - Initialize - Initializing player on main thread... DONE!");
View.RefreshStatus("Opening app...");

// Task.Factory.StartNew(() =>
// {
// Console.WriteLine("SplashPresenter - Initialize - Initializing service on another thread...");
// View.RefreshStatus("Loading...");
// _initializationService.Initialize();
// }).ContinueWith((a) =>
// {
// // Initialize player
// Console.WriteLine("SplashPresenter - Initialize - Initializing player on main thread...");
// Device device = new Device(){
// DriverType = DriverType.DirectSound,
// Id = -1
// };
// _playerService.Initialize(device, 44100, 1000, 100);
// View.InitDone(true);
// onInitDone.Invoke();
// Console.WriteLine("SplashPresenter - Initialize - Initializing player on main thread... DONE!");
// View.RefreshStatus("Opening app...");
// }, taskScheduler);

}
#else
public void Initialize(Action onInitDone)
{
if (_playerService.IsInitialized)
Expand All @@ -47,26 +103,28 @@ public void Initialize(Action onInitDone)
}

TaskScheduler taskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
#if LINUX
#if LINUX
// Mono on Linux crashes for some reason if FromCurrentSynchronizationContext is used... weird!
taskScheduler = TaskScheduler.Default;
#endif
#endif
Task.Factory.StartNew(() =>
{
{
View.RefreshStatus("Loading...");
_initializationService.Initialize();
}).ContinueWith((a) =>
{
{
// Initialize player
Device device = new Device(){
DriverType = DriverType.DirectSound,
Id = -1
};
_playerService.Initialize(device, 44100, 1000, 100);
View.InitDone(true);
onInitDone.Invoke();
View.InitDone(true);
onInitDone.Invoke();
View.RefreshStatus("Opening app...");
}, taskScheduler);
}
#endif

}
}
2 changes: 2 additions & 0 deletions MPfm/MPfm.Sound/PeakFiles/PeakFileService.cs
Expand Up @@ -210,11 +210,13 @@ public void GeneratePeakFile(string audioFilePath, string peakFilePath)
}
else
{
#if ANDROID
// Get left/right channel values
short leftValue = Base.LowWord(buffer[a]);
short rightValue = Base.HighWord(buffer[a]);
floatLeft[a/2] = (float)leftValue / (float)Int16.MaxValue;
floatRight[a/2] = (float)rightValue / (float)Int16.MaxValue;
#endif
}
}
Expand Down
4 changes: 2 additions & 2 deletions MPfm/MPfm.iOS/Classes/Controllers/Base/BaseViewController.cs
Expand Up @@ -47,7 +47,7 @@ public BaseViewController(Action<IBaseView> onViewReady, string nibName, NSBundl
: base(nibName, bundle)
{
OnViewReady = onViewReady;
}
}

public virtual void ConfirmedBackButton()
{
Expand All @@ -74,8 +74,8 @@ public override void ViewDidLoad()
{
base.ViewDidLoad();

EdgesForExtendedLayout = UIRectEdge.None;
this.NavigationItem.SetHidesBackButton(true, true);

OnViewReady(this);
}

Expand Down
7 changes: 3 additions & 4 deletions MPfm/MPfm.iOS/Classes/Controllers/MarkersViewController.cs
Expand Up @@ -128,9 +128,7 @@ public void RowSelected(UITableView tableView, NSIndexPath indexPath)
if(e.ButtonIndex == 4)
return;
// Add marker
if(OnAddMarker != null)
OnAddMarker((MarkerTemplateNameType)e.ButtonIndex);
OnAddMarkerWithTemplate((MarkerTemplateNameType)e.ButtonIndex);
};

// Must use the tab bar controller to spawn the action sheet correctly. Remember, we're in a UIScrollView...
Expand All @@ -140,7 +138,8 @@ public void RowSelected(UITableView tableView, NSIndexPath indexPath)

#region IMarkersView implementation

public Action<MarkerTemplateNameType> OnAddMarker { get; set; }
public Action OnAddMarker { get; set; }
public Action<MarkerTemplateNameType> OnAddMarkerWithTemplate { get; set; }
public Action<Marker> OnEditMarker { get; set; }
public Action<Marker> OnSelectMarker { get; set; }
public Action<Marker> OnDeleteMarker { get; set; }
Expand Down
Expand Up @@ -359,6 +359,12 @@ public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
cell.ImageAlbum1.Image = null;
cell.ImageAlbum2.Image = null;
cell.ImageAlbum3.Image = null;
//cell.ImageAlbum1.Alpha = 1;
cell.ImageAlbum2.Alpha = 0.75f;
cell.ImageAlbum3.Alpha = 0.5f;
cell.ImageAlbum1.Tag = 1;
cell.ImageAlbum2.Tag = 2;
cell.ImageAlbum3.Tag = 3;

// Set offset for delete icon
if (indexPath.Row == _deleteCellIndex)
Expand All @@ -373,6 +379,10 @@ public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
if (_browserType == MobileLibraryBrowserType.Songs)
{
cell.IndexTextLabel.Text = item.AudioFile.TrackNumber.ToString();
cell.ImageAlbum1.Hidden = true;
cell.ImageAlbum2.Hidden = true;
cell.ImageAlbum3.Hidden = true;
cell.AlbumCountLabel.Hidden = true;
if (_currentlyPlayingSongId == item.AudioFile.Id)
cell.RightImage.Hidden = false;
else
Expand All @@ -384,11 +394,21 @@ public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
cell.ImageAlbum1.Hidden = item.AlbumTitles.Count >= 1 ? false : true;
cell.ImageAlbum2.Hidden = item.AlbumTitles.Count >= 2 ? false : true;
cell.ImageAlbum3.Hidden = item.AlbumTitles.Count >= 3 ? false : true;
cell.AlbumCountLabel.Hidden = item.AlbumTitles.Count >= 3 ? false : true;
cell.AlbumCountLabel.Hidden = item.AlbumTitles.Count > 3 ? false : true;

int albumFetchCount = item.AlbumTitles.Count >= 3 ? 3 : item.AlbumTitles.Count;
int albumFetchCount = item.AlbumTitles.Count > 3 ? 2 : item.AlbumTitles.Count;
albumFetchCount = item.AlbumTitles.Count == 3 ? 3 : albumFetchCount; //
//albumFetchCount = item.AlbumTitles.Count == 3 ? 3 : item.AlbumTitles.Count; // Do not load a third album art when the count is visible!
for (int a = 0; a < albumFetchCount; a++)

Console.WriteLine("GetCell - title: {0} index: {1} albumFetchCount: {2}", item.Title, indexPath.Row, albumFetchCount);

int startIndex = 0;
if (item.AlbumTitles.Count > 3)
{
startIndex = 1;
albumFetchCount++;
}
for (int a = startIndex; a < albumFetchCount; a++)
{
UIImageView imageAlbum = null;
if (a == 0)
Expand All @@ -399,12 +419,12 @@ public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
imageAlbum = cell.ImageAlbum3;

// Check if album art is cached
string key = _items[indexPath.Row].Query.ArtistName + "_" + _items[indexPath.Row].AlbumTitles[a];
string key = item.Query.ArtistName + "_" + item.AlbumTitles[a];
KeyValuePair<string, UIImage> keyPair = _thumbnailImageCache.FirstOrDefault(x => x.Key == key);
if (keyPair.Equals(default(KeyValuePair<string, UIImage>)))
{
Console.WriteLine("MLBVC - GetCell - OnRequestAlbumArt - index: {0} key: {1}", indexPath.Row, key);
OnRequestAlbumArt(_items[indexPath.Row].Query.ArtistName, _items[indexPath.Row].AlbumTitles[a], imageAlbum);
OnRequestAlbumArt(item.Query.ArtistName, item.AlbumTitles[a], imageAlbum);
}
else
{
Expand Down Expand Up @@ -530,9 +550,9 @@ public void MobileLibraryBrowserError(Exception ex)
});
}

public void RefreshAlbumArtCell(string artistName, string albumTitle, byte[] albumArtData, object userData)
public async void RefreshAlbumArtCell(string artistName, string albumTitle, byte[] albumArtData, object userData)
{
Console.WriteLine("MLBVC - RefreshAlbumArtCell - artistName: {0} albumTitle: {1} browserType: {2}", artistName, albumTitle, _browserType);
//Console.WriteLine("MLBVC - RefreshAlbumArtCell - artistName: {0} albumTitle: {1} browserType: {2}", artistName, albumTitle, _browserType);
// Note: cannot call UIScreen.MainScreen in a background thread!
// int height = 44;
// InvokeOnMainThread(() => {
Expand All @@ -551,35 +571,42 @@ public void RefreshAlbumArtCell(string artistName, string albumTitle, byte[] alb
InvokeOnMainThread(() => { // We have to use the main thread to fetch the scale
height = (int)(height * UIScreen.MainScreen.Scale);
});
Task<UIImage>.Factory.StartNew(() => {
//Console.WriteLine("MLBVC - RefreshAlbumArtCell - Task - artistName: {0} albumTitle: {1} browserType: {2} height: {3}", artistName, albumTitle, _browserType, height);
using (NSData imageData = NSData.FromArray(albumArtData))

var task = Task<UIImage>.Factory.StartNew(() => {
Console.WriteLine("MLBVC - RefreshAlbumArtCell - artistName: {0} albumTitle: {1} browserType: {2} height: {3}", artistName, albumTitle, _browserType, height);
try
{
using (UIImage image = UIImage.LoadFromData(imageData))
using (NSData imageData = NSData.FromArray(albumArtData))
{
if (image != null)
using (UIImage imageFullSize = UIImage.LoadFromData(imageData))
{
try
if (imageFullSize != null)
{
UIImage imageResized = CoreGraphicsHelper.ScaleImage(image, height);
UIImage imageResized = CoreGraphicsHelper.ScaleImage(imageFullSize, height);
Console.WriteLine("MLBVC - RefreshAlbumArtCell - Image resized!");
return imageResized;
}
catch (Exception ex)
{
Console.WriteLine("Error resizing image " + artistName + " - " + albumTitle + ": " + ex.Message);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error resizing image " + artistName + " - " + albumTitle + ": " + ex.Message);
}
Console.WriteLine("MLBVC - RefreshAlbumArtCell - Returning null");
return null;
}).ContinueWith(t => {
UIImage image = t.Result;
});

//}).ContinueWith(t => {

//UIImage image = t.Result;
UIImage image = await task;
Console.WriteLine("MLBVC - RefreshAlbumArtCell - ContinueWith - artistName: {0} albumTitle: {1} browserType: {2} userData==null: {3} image==null: {4}", artistName, albumTitle, _browserType, userData == null, image == null);
if(image == null)
return;

InvokeOnMainThread(() => {
//Console.WriteLine("MLBVC - RefreshAlbumArtCell - ContinueWith - artistName: {0} albumTitle: {1} browserType: {2} userData==null: {3}", artistName, albumTitle, _browserType, userData == null);
switch (_browserType)
{
case MobileLibraryBrowserType.Artists:
Expand All @@ -606,8 +633,15 @@ public void RefreshAlbumArtCell(string artistName, string albumTitle, byte[] alb
var imageView = (UIImageView)userData;
imageView.Alpha = 0;
imageView.Image = image;
float alpha = 1;
if(imageView.Tag == 3)
alpha = 0.5f;
else if(imageView.Tag == 2)
alpha = 0.75f;
UIView.Animate(0.2, () => {
imageView.Alpha = 1;
imageView.Alpha = alpha;
});
}
Expand Down Expand Up @@ -655,7 +689,7 @@ public void RefreshAlbumArtCell(string artistName, string albumTitle, byte[] alb
// });
// }
});
}, TaskScheduler.FromCurrentSynchronizationContext());
//}, TaskScheduler.FromCurrentSynchronizationContext());
}

public void RefreshLibraryBrowser(IEnumerable<LibraryBrowserEntity> entities, MobileLibraryBrowserType browserType, string navigationBarTitle, string navigationBarSubtitle, string breadcrumb, bool isPopBackstack, bool isBackstackEmpty)
Expand Down

0 comments on commit 0cfe52d

Please sign in to comment.