/
SelectPlaylistPresenter.cs
147 lines (132 loc) · 5.38 KB
/
SelectPlaylistPresenter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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 System;
using System.Collections.Generic;
using System.Linq;
using MPfm.Core;
using MPfm.Library.Objects;
using MPfm.Library.Services.Interfaces;
using MPfm.MVP.Bootstrap;
using MPfm.MVP.Messages;
using MPfm.MVP.Models;
using MPfm.MVP.Navigation;
using MPfm.MVP.Presenters.Interfaces;
using MPfm.MVP.Views;
using MPfm.Sound.Playlists;
using TinyMessenger;
namespace MPfm.MVP.Presenters
{
/// <summary>
/// Select Playlist presenter.
/// </summary>
public class SelectPlaylistPresenter : BasePresenter<ISelectPlaylistView>, ISelectPlaylistPresenter
{
private readonly NavigationManager _navigationManager;
private readonly MobileNavigationManager _mobileNavigationManager;
private readonly ITinyMessengerHub _messengerHub;
private readonly ILibraryService _libraryService;
private readonly IAudioFileCacheService _audioFileCacheService;
private LibraryBrowserEntity _item;
private List<PlaylistEntity> _items;
public SelectPlaylistPresenter(ITinyMessengerHub messengerHub, ILibraryService libraryService, IAudioFileCacheService audioFileCacheService, LibraryBrowserEntity item)
{
_messengerHub = messengerHub;
_libraryService = libraryService;
_audioFileCacheService = audioFileCacheService;
_item = item;
#if IOS || ANDROID
_mobileNavigationManager = Bootstrapper.GetContainer().Resolve<MobileNavigationManager>();
#else
_navigationManager = Bootstrapper.GetContainer().Resolve<NavigationManager>();
#endif
}
public override void BindView(ISelectPlaylistView view)
{
view.OnAddNewPlaylist = AddNewPlaylist;
view.OnSelectPlaylist = SelectPlaylist;
base.BindView(view);
_messengerHub.Subscribe<PlaylistListUpdatedMessage>(message => RefreshPlaylists());
RefreshPlaylists();
}
private void RefreshPlaylists()
{
var playlists = _libraryService.SelectPlaylists().ToList();
_items = new List<PlaylistEntity>();
foreach (var playlist in playlists)
{
var items = _libraryService.SelectPlaylistItems(playlist.PlaylistId);
_items.Add(new PlaylistEntity()
{
PlaylistId = playlist.PlaylistId,
Name = playlist.Name,
LastModified = playlist.LastModified,
ItemCount = items.Count
});
}
View.RefreshPlaylists(_items);
}
private void SelectPlaylist(PlaylistEntity playlist)
{
try
{
// Check if adding a song or an album
if (_item.AudioFile != null)
{
//_playerService.CurrentPlaylist.AddItem(_items[index].AudioFile);
//View.NotifyNewPlaylistItems(string.Format("'{0}' was added at the end of the current playlist.", _items[index].Title));
}
else
{
var items = _libraryService.SelectPlaylistItems(playlist.PlaylistId).ToList();
var audioFileIds = _audioFileCacheService.SelectAudioFiles(_item.Query).Select(x => x.Id).ToList();
// Find a list of new items
var newItems = new List<PlaylistAudioFile>();
int position = items.Count;
foreach (Guid audioFileId in audioFileIds)
{
if (!items.Any(x => x.AudioFileId == audioFileId))
{
newItems.Add(new PlaylistAudioFile(playlist.PlaylistId, audioFileId, position));
position++;
}
}
foreach(var item in newItems)
_libraryService.InsertPlaylistItem(item);
_messengerHub.PublishAsync<PlaylistUpdatedMessage>(new PlaylistUpdatedMessage(this, playlist.PlaylistId, newItems.Count));
}
}
catch (Exception ex)
{
Tracing.Log("An error occured while selecting playlist: " + ex.Message);
View.SelectPlaylistError(ex);
}
}
private void AddNewPlaylist()
{
try
{
var view = _mobileNavigationManager.CreateAddNewPlaylistView();
_mobileNavigationManager.PushDialogView("Add New Playlist", View, view);
}
catch (Exception ex)
{
Tracing.Log("An error occured while creating AddNewPlaylist view: " + ex.Message);
View.SelectPlaylistError(ex);
}
}
}
}