Skip to content

Commit

Permalink
iOS: Now loading album art in StartResumePlayback. Fixed bug where Eq…
Browse files Browse the repository at this point in the history
…ualizerPresetDetails failed to load. Many other bug fixes in MobileNavigationManager.
  • Loading branch information
ycastonguay committed Nov 4, 2013
1 parent e10681d commit 44a7f62
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 36 deletions.
1 change: 1 addition & 0 deletions MPfm/MPfm.MVP/MPfm.MVP.iOS.csproj
Expand Up @@ -363,5 +363,6 @@
<Compile Include="Presenters\Interfaces\IMobileMainPresenter.cs" />
<Compile Include="Messages\MobileLibraryBrowserChangeQueryMessage.cs" />
<Compile Include="Messages\MobileLibraryBrowserPopBackstackMessage.cs" />
<Compile Include="Messages\EqualizerPresetSelectedMessage.cs" />
</ItemGroup>
</Project>
36 changes: 36 additions & 0 deletions MPfm/MPfm.MVP/Messages/EqualizerPresetSelectedMessage.cs
@@ -0,0 +1,36 @@
// Copyright © 2011-2013 Yanick Castonguay
//
// This file is part of MPfm.
//
// MPfm is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// MPfm is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MPfm. If not, see <http://www.gnu.org/licenses/>.

using TinyMessenger;
using System;

namespace MPfm.MVP.Messages
{
/// <summary>
/// Message used to notify that the user has selected an equalizer preset.
/// </summary>
public class EqualizerPresetSelectedMessage : TinyMessageBase
{
public Guid EQPresetId { get; set; }

public EqualizerPresetSelectedMessage(object sender, Guid eqPresetId)
: base(sender)
{
this.EQPresetId = eqPresetId;
}
}
}
6 changes: 4 additions & 2 deletions MPfm/MPfm.MVP/Navigation/MobileNavigationManager.cs
Expand Up @@ -128,6 +128,7 @@ public abstract class MobileNavigationManager : INavigationManager

public abstract void PushTabView(MobileNavigationTabType type, IBaseView view);
public abstract void PushDialogView(MobileDialogPresentationType presentationType, string viewTitle, IBaseView sourceView, IBaseView view);
public abstract void PushDialogSubview(MobileDialogPresentationType presentationType, string parentViewTitle, IBaseView view);

public virtual void Start()
{
Expand Down Expand Up @@ -832,8 +833,9 @@ public virtual void CreateEqualizerPresetDetailsView(IBaseView sourceView, Guid
if (_equalizerPresetDetailsView == null)
_equalizerPresetDetailsView = Bootstrapper.GetContainer().Resolve<IEqualizerPresetDetailsView>(new NamedParameterOverloads() { { "presetId", presetId } });

//PushDialogView(MobileDialogPresentationType.Standard, "Equalizer Preset Details", null, _equalizerPresetDetailsView);
PushTabView(MobileNavigationTabType.More, _equalizerPresetDetailsView);
PushDialogSubview(MobileDialogPresentationType.Standard, "Equalizer Presets", _equalizerPresetDetailsView);
//PushDialogView(MobileDialogPresentationType.Standard, "Equalizer Preset Details", _equalizerPresetsView, _equalizerPresetDetailsView);
//PushTabView(MobileNavigationTabType.More, _equalizerPresetDetailsView);
}

public virtual void BindEqualizerPresetDetailsView(IBaseView sourceView, IEqualizerPresetDetailsView view, Guid presetId)
Expand Down
11 changes: 10 additions & 1 deletion MPfm/MPfm.MVP/Presenters/EqualizerPresetDetailsPresenter.cs
Expand Up @@ -37,11 +37,13 @@ public class EqualizerPresetDetailsPresenter : BasePresenter<IEqualizerPresetDet
private EQPreset _preset;
private List<EQPresetBand> _originalPresetBands;

public EqualizerPresetDetailsPresenter(EQPreset preset, ITinyMessengerHub messageHub, IPlayerService playerService, ILibraryService libraryService)
public EqualizerPresetDetailsPresenter(Guid presetId, ITinyMessengerHub messageHub, IPlayerService playerService, ILibraryService libraryService)
{
_messageHub = messageHub;
_playerService = playerService;
_libraryService = libraryService;

var preset = libraryService.SelectEQPreset(presetId);
ChangePreset(preset);
}

Expand All @@ -56,6 +58,13 @@ public override void BindView(IEqualizerPresetDetailsView view)
view.OnSetFaderGain = SetFaderGain;
view.OnRevertPreset = RevertPreset;

_messageHub.Subscribe<EqualizerPresetSelectedMessage>((m) =>
{
_preset = _libraryService.SelectEQPreset(m.EQPresetId);
ChangePreset(_preset);
View.RefreshPreset(_preset);
});

_playerService.ApplyEQPreset(_preset);
View.RefreshPreset(_preset);
}
Expand Down
1 change: 1 addition & 0 deletions MPfm/MPfm.MVP/Presenters/EqualizerPresetsPresenter.cs
Expand Up @@ -213,6 +213,7 @@ private void EditPreset(Guid presetId)
if(preset == null)
return;

_messageHub.PublishAsync<EqualizerPresetSelectedMessage>(new EqualizerPresetSelectedMessage(this, preset.EQPresetId));
_mobileNavigationManager.CreateEqualizerPresetDetailsView(View, preset.EQPresetId);
}
catch(Exception ex)
Expand Down
20 changes: 11 additions & 9 deletions MPfm/MPfm.MVP/Presenters/StartResumePlaybackPresenter.cs
Expand Up @@ -21,6 +21,7 @@
using MPfm.MVP.Views;
using MPfm.Library.Services.Interfaces;
using System.Linq;
using MPfm.Sound.AudioFiles;

namespace MPfm.MVP.Presenters
{
Expand All @@ -32,6 +33,7 @@ public class StartResumePlaybackPresenter : BasePresenter<IStartResumePlaybackVi
private readonly IPlayerService _playerService;
private readonly IAudioFileCacheService _audioFileCacheService;
private CloudDeviceInfo _device;
private AudioFile _audioFile;

public StartResumePlaybackPresenter(CloudDeviceInfo device, IAudioFileCacheService audioFileCacheService, IPlayerService playerService)
{
Expand All @@ -50,23 +52,23 @@ public override void BindView(IStartResumePlaybackView view)

private void RefreshDevice()
{
View.RefreshCloudDeviceInfo(_device);
_audioFile = _audioFileCacheService.AudioFiles.FirstOrDefault(x => x.Id == _device.AudioFileId);
if (_audioFile == null)
{
_audioFile = _audioFileCacheService.AudioFiles.FirstOrDefault(x => x.ArtistName.ToUpper() == _device.ArtistName.ToUpper() &&
x.AlbumTitle.ToUpper() == _device.AlbumTitle.ToUpper() &&
x.Title.ToUpper() == _device.SongTitle.ToUpper());
}
View.RefreshCloudDeviceInfo(_device, _audioFile);
}

private void ResumePlayback()
{
var audioFile = _audioFileCacheService.AudioFiles.FirstOrDefault(x => x.Id == _device.AudioFileId);
if (audioFile == null)
{
audioFile = _audioFileCacheService.AudioFiles.FirstOrDefault(x => x.ArtistName.ToUpper() == _device.ArtistName.ToUpper() &&
x.AlbumTitle.ToUpper() == _device.AlbumTitle.ToUpper() &&
x.Title.ToUpper() == _device.SongTitle.ToUpper());
}
var audioFiles = _audioFileCacheService.SelectAudioFiles(new LibraryQuery() {
ArtistName = _device.ArtistName,
AlbumTitle = _device.AlbumTitle
});
_playerService.Play(audioFiles, audioFile != null ? audioFile.FilePath : string.Empty, 0, false, true);
_playerService.Play(audioFiles, _audioFile != null ? _audioFile.FilePath : string.Empty, 0, false, true);
}
}
}
3 changes: 2 additions & 1 deletion MPfm/MPfm.MVP/Views/IStartResumePlaybackView.cs
Expand Up @@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using MPfm.Library.Objects;
using MPfm.Sound.AudioFiles;

namespace MPfm.MVP.Views
{
Expand All @@ -29,6 +30,6 @@ public interface IStartResumePlaybackView : IBaseView
Action OnResumePlayback { get; set; }

void StartResumePlaybackError(Exception ex);
void RefreshCloudDeviceInfo(CloudDeviceInfo info);
void RefreshCloudDeviceInfo(CloudDeviceInfo info, AudioFile audioFile);
}
}
Expand Up @@ -25,6 +25,9 @@
using MPfm.iOS.Classes.Controllers.Base;
using MPfm.MVP.Navigation;
using MPfm.MVP.Bootstrap;
using System.Threading.Tasks;
using MPfm.iOS.Helpers;
using MPfm.Sound.AudioFiles;

namespace MPfm.iOS
{
Expand All @@ -37,14 +40,16 @@ public StartResumePlaybackViewController()

public override void ViewDidLoad()
{
btnResume.Alpha = 0.7f;
btnResume.SetImage(UIImage.FromBundle("Images/Buttons/select"));
btnCancel.Alpha = 0.7f;
btnCancel.SetImage(UIImage.FromBundle("Images/Buttons/cancel"));
imageIcon.Image = UIImage.FromBundle("Images/WhiteIcons/android");

base.ViewDidLoad();

var navigationManager = Bootstrapper.GetContainer().Resolve<MobileNavigationManager>();
navigationManager.BindStartResumePlaybackView(this);

}

partial void actionResume(NSObject sender)
Expand Down Expand Up @@ -81,7 +86,7 @@ public void StartResumePlaybackError(Exception ex)
});
}

public void RefreshCloudDeviceInfo(CloudDeviceInfo device)
public async void RefreshCloudDeviceInfo(CloudDeviceInfo device, AudioFile audioFile)
{
InvokeOnMainThread(() => {
lblDeviceName.Text = device.DeviceName;
Expand All @@ -91,6 +96,74 @@ public void RefreshCloudDeviceInfo(CloudDeviceInfo device)
lblSongTitle.Text = device.SongTitle;
lblTimestamp.Text = string.Format("Last updated: {0} {1}", device.Timestamp.ToShortDateString(), device.Timestamp.ToLongTimeString());
});

int height = 44;
InvokeOnMainThread(() => {
try
{
height = (int)(imageAlbum.Bounds.Height * UIScreen.MainScreen.Scale);
UIView.Animate(0.3, () => {
imageAlbum.Alpha = 0;
});
}
catch(Exception ex)
{
Console.WriteLine("PlayerViewController - RefreshSongInformation - Failed to set image view album art alpha: {0}", ex);
}
});

// Load album art + resize in another thread
var task = Task<UIImage>.Factory.StartNew(() => {
try
{
byte[] bytesImage = AudioFile.ExtractImageByteArrayForAudioFile(audioFile.FilePath);
using (NSData imageData = NSData.FromArray(bytesImage))
{
using (UIImage imageFullSize = UIImage.LoadFromData(imageData))
{
if (imageFullSize != null)
{
try
{
UIImage imageResized = CoreGraphicsHelper.ScaleImage(imageFullSize, height);
return imageResized;
}
catch (Exception ex)
{
Console.WriteLine("Error resizing image " + audioFile.ArtistName + " - " + audioFile.AlbumTitle + ": " + ex.Message);
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("StartResumePlaybackViewController - RefreshCloudDeviceInfo - Failed to process image: {0}", ex);
}
return null;
});
//}).ContinueWith(t => {
UIImage image = await task;
if(image == null)
return;

InvokeOnMainThread(() => {
try
{
imageAlbum.Alpha = 0;
imageAlbum.Image = image;
UIView.Animate(0.3, () => {
imageAlbum.Alpha = 1;
});
}
catch(Exception ex)
{
Console.WriteLine("StartResumePlaybackViewController - RefreshCloudDeviceInfo - Failed to set image after processing: {0}", ex);
}
});
//}, TaskScheduler.FromCurrentSynchronizationContext());
}

#endregion
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions MPfm/MPfm.iOS/Classes/Navigation/iOSNavigationManager.cs
Expand Up @@ -70,6 +70,11 @@ public override void PushDialogView(MobileDialogPresentationType presentationTyp
AppDelegate.PushDialogView(presentationType, viewTitle, (UIViewController)view);
}

public override void PushDialogSubview(MobileDialogPresentationType presentationType, string parentViewTitle, IBaseView view)
{
AppDelegate.PushDialogSubview(presentationType, parentViewTitle, (UIViewController)view);
}

public override void CreateSplashView()
{
var view = Bootstrapper.GetContainer().Resolve<ISplashView>();
Expand Down
Binary file added MPfm/MPfm.iOS/Images/WhiteIcons/android.png
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.iOS/Images/WhiteIcons/android@2x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions MPfm/MPfm.iOS/MPfm.iOS.csproj
Expand Up @@ -412,6 +412,7 @@
<Folder Include="Images\Player\" />
<Folder Include="Classes\Providers\" />
<Folder Include="Classes\Services\" />
<Folder Include="Images\WhiteIcons\" />
</ItemGroup>
<ItemGroup>
<Content Include="Fonts\Junction.otf" />
Expand Down Expand Up @@ -549,6 +550,8 @@
<BundleResource Include="Images\Icons\icon_library%402x.png" />
<BundleResource Include="Images\Icons\icon_resume.png" />
<BundleResource Include="Images\Icons\icon_resume%402x.png" />
<BundleResource Include="Images\WhiteIcons\android.png" />
<BundleResource Include="Images\WhiteIcons\android%402x.png" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MPfm.Core\MPfm.Core.iOS.csproj">
Expand Down

0 comments on commit 44a7f62

Please sign in to comment.