Commit 92ba990e authored by Thomas Nigro's avatar Thomas Nigro

Music Database now uses SemaphoreSlim to prevent DB_Locked error

parent 40fbffbf
......@@ -25,7 +25,7 @@ namespace VLC_WinRT.Commands.MusicLibrary
else if (parameter is int)
{
var id = (int)parameter;
album = Locator.MusicLibraryVM.MusicLibrary.LoadAlbum(id);
album = await Locator.MusicLibraryVM.MusicLibrary.LoadAlbum(id);
}
try
{
......
......@@ -27,7 +27,7 @@ namespace VLC_WinRT.Commands.MusicPlayer
else if (parameter is int)
{
var id = (int) parameter;
albumItem = Locator.MusicLibraryVM.MusicLibrary.LoadAlbum(id);
albumItem = await Locator.MusicLibraryVM.MusicLibrary.LoadAlbum(id);
}
if (albumItem != null)
......
......@@ -35,38 +35,59 @@ namespace VLC_WinRT.Database
}
}
public Task<List<AlbumItem>> LoadAlbumsFromId(int artistId)
public void DeleteAll()
{
using (var db = new SQLiteConnection(DbPath))
{
db.DeleteAll<AlbumItem>();
}
}
public async Task<List<AlbumItem>> LoadAlbumsFromId(int artistId)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
return connection.Table<AlbumItem>().Where(x => x.ArtistId == artistId).OrderBy(x => x.Name).ToListAsync();
var album = await connection.Table<AlbumItem>().Where(x => x.ArtistId == artistId).OrderBy(x => x.Name).ToListAsync();
MusicDatabase.DatabaseOperation.Release();
return album;
}
public Task<int> LoadAlbumsCountFromId(int artistId)
public async Task<int> LoadAlbumsCountFromId(int artistId)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
return connection.Table<AlbumItem>().Where(x => x.ArtistId == artistId).CountAsync();
var count= await connection.Table<AlbumItem>().Where(x => x.ArtistId == artistId).CountAsync();
MusicDatabase.DatabaseOperation.Release();
return count;
}
public async Task<AlbumItem> LoadAlbumViaName(int artistId, string albumName)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
var query = connection.Table<AlbumItem>().Where(x => x.Name.Equals(albumName)).Where(x => x.ArtistId == artistId);
var result = await query.ToListAsync();
return result.FirstOrDefault();
var album= result.FirstOrDefault();
MusicDatabase.DatabaseOperation.Release();
return album;
}
public AlbumItem LoadAlbum(int albumId)
public async Task<AlbumItem> LoadAlbum(int albumId)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
using (var connection = new SQLiteConnection(DbPath))
{
var query = connection.Table<AlbumItem>().Where(x => x.Id.Equals(albumId));
var result = query.ToList();
return result.FirstOrDefault();
var album =result.FirstOrDefault();
MusicDatabase.DatabaseOperation.Release();
return album;
}
}
public async Task<List<AlbumItem>> LoadAlbums(Expression<Func<AlbumItem, bool>> compare = null)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
AsyncTableQuery<AlbumItem> query;
if (compare == null)
......@@ -77,11 +98,14 @@ namespace VLC_WinRT.Database
{
query = connection.Table<AlbumItem>().Where(compare);
}
return await query.ToListAsync();
var albums= await query.ToListAsync();
MusicDatabase.DatabaseOperation.Release();
return albums;
}
public async Task<List<AlbumItem>> Load(Expression<Func<AlbumItem, bool>> compare = null)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
AsyncTableQuery<AlbumItem> query;
if (compare == null)
......@@ -92,25 +116,34 @@ namespace VLC_WinRT.Database
{
query = connection.Table<AlbumItem>().Where(compare);
}
return await query.ToListAsync();
var albums= await query.ToListAsync();
MusicDatabase.DatabaseOperation.Release();
return albums;
}
public Task<List<AlbumItem>> Contains(string column, string value)
public async Task<List<AlbumItem>> Contains(string column, string value)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
return connection.QueryAsync<AlbumItem>($"SELECT * FROM {nameof(AlbumItem)} WHERE {column} LIKE '%{value}%';", new string[] { });
var list = await connection.QueryAsync<AlbumItem>($"SELECT * FROM {nameof(AlbumItem)} WHERE {column} LIKE '%{value}%';", new string[] { });
MusicDatabase.DatabaseOperation.Release();
return list;
}
public Task Update(AlbumItem album)
public async Task Update(AlbumItem album)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
return connection.UpdateAsync(album);
await connection.UpdateAsync(album);
MusicDatabase.DatabaseOperation.Release();
}
public Task Add(AlbumItem album)
public async Task Add(AlbumItem album)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
return connection.InsertAsync(album);
await connection.InsertAsync(album);
MusicDatabase.DatabaseOperation.Release();
}
public void Remove(AlbumItem album)
......
......@@ -35,20 +35,35 @@ namespace VLC_WinRT.Database
}
}
public Task<int> Count()
public void DeleteAll()
{
using (var db = new SQLiteConnection(DbPath))
{
db.DeleteAll<ArtistItem>();
}
}
public async Task<int> Count()
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
return connection.Table<ArtistItem>().CountAsync();
var count = await connection.Table<ArtistItem>().CountAsync();
MusicDatabase.DatabaseOperation.Release();
return count;
}
public Task<ArtistItem> At(int index)
public async Task<ArtistItem> At(int index)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
return connection.Table<ArtistItem>().ElementAtAsync(index);
var artist = await connection.Table<ArtistItem>().ElementAtAsync(index);
MusicDatabase.DatabaseOperation.Release();
return artist;
}
public async Task<List<ArtistItem>> Load(Expression<Func<ArtistItem, bool>> compare = null)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
AsyncTableQuery<ArtistItem> query;
if (compare == null)
......@@ -59,42 +74,54 @@ namespace VLC_WinRT.Database
{
query = connection.Table<ArtistItem>().Where(compare);
}
return await query.ToListAsync();
var artists = await query.ToListAsync();
MusicDatabase.DatabaseOperation.Release();
return artists;
}
public ArtistItem LoadViaArtistName(string artistName)
public async Task<ArtistItem> LoadViaArtistName(string artistName)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
using (var db = new SQLiteConnection(DbPath))
{
var query = db.Table<ArtistItem>().Where(x => x.Name.Equals(artistName));
return query.FirstOrDefault();
var artist= query.FirstOrDefault();
MusicDatabase.DatabaseOperation.Release();
return artist;
}
}
public Task Update(ArtistItem artist)
public async Task Update(ArtistItem artist)
{
var connection = new SQLiteAsyncConnection(DbPath);
return connection.UpdateAsync(artist);
await connection.UpdateAsync(artist);
}
public Task Add(ArtistItem artist)
public async Task Add(ArtistItem artist)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
return connection.InsertAsync(artist);
await connection.InsertAsync(artist);
MusicDatabase.DatabaseOperation.Release();
}
public async Task<ArtistItem> LoadArtist(int artistId)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
var query = connection.Table<ArtistItem>().Where(x => x.Id.Equals(artistId));
var result = await query.ToListAsync();
return result.FirstOrDefault();
var artist= result.FirstOrDefault();
MusicDatabase.DatabaseOperation.Release();
return artist;
}
public Task Remove(ArtistItem artist)
public async Task Remove(ArtistItem artist)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
return connection.DeleteAsync(artist);
await connection.DeleteAsync(artist);
MusicDatabase.DatabaseOperation.Release();
}
}
}
......@@ -4,5 +4,6 @@
{
void Initialize();
void Drop();
void DeleteAll();
}
}
......@@ -75,5 +75,13 @@ namespace VLC_WinRT.Database
{
throw new NotImplementedException();
}
public void DeleteAll()
{
using (var db = new SQLiteConnection(DbPath))
{
db.DeleteAll<KeyboardAction>();
}
}
}
}
......@@ -30,7 +30,15 @@ namespace VLC_WinRT.Database
db.DropTable<StreamMedia>();
}
}
public void DeleteAll()
{
using (var db = new SQLiteConnection(DbPath))
{
db.DeleteAll<StreamMedia>();
}
}
public async Task<List<StreamMedia>> Load()
{
var connection = new SQLiteAsyncConnection(DbPath);
......
......@@ -26,6 +26,14 @@ namespace VLC_WinRT.Database
}
}
public void DeleteAll()
{
using (var db = new SQLiteConnection(DbPath))
{
db.DeleteAll<TrackCollection>();
}
}
public async Task<TrackCollection> LoadFromName(string name)
{
var connection = new SQLiteAsyncConnection(DbPath);
......
......@@ -34,99 +34,135 @@ namespace VLC_WinRT.Database
}
}
public void DeleteAll()
{
using (var db = new SQLiteConnection(DbPath))
{
db.DeleteAll<TrackItem>();
}
}
public async Task<bool> DoesTrackExist(string path)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
bool b = false;
var connection = new SQLiteAsyncConnection(DbPath);
var query = connection.Table<TrackItem>().Where(x => x.Path == path);
b = await query.CountAsync() != 0;
MusicDatabase.DatabaseOperation.Release();
return b;
}
public async Task<TrackItem> LoadTrackByPath(string path)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
var query = connection.Table<TrackItem>().Where(x => x.Path == path);
if (await query.CountAsync() > 0)
{
return await query.FirstOrDefaultAsync();
var track = await query.FirstOrDefaultAsync();
MusicDatabase.DatabaseOperation.Release();
return track;
}
return null;
}
public async Task<TrackItem> LoadTrack(int trackId)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
var query = connection.Table<TrackItem>().Where(x => x.Id.Equals(trackId));
var result = await query.ToListAsync();
MusicDatabase.DatabaseOperation.Release();
return result.FirstOrDefault();
}
public async Task<TrackItem> LoadTrack(int artistId, int albumId, string trackName)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
var query = connection.Table<TrackItem>().Where(x => x.Name.Equals(trackName)).Where(x => x.ArtistId == artistId).Where(x => x.AlbumId == albumId);
var result = await query.ToListAsync();
MusicDatabase.DatabaseOperation.Release();
return result.FirstOrDefault();
}
public List<TrackItem> LoadTracksByAlbumId(int albumId)
public async Task<List<TrackItem>> LoadTracksByAlbumId(int albumId)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
using (var connection = new SQLiteConnection(DbPath))
{
var query = connection.Table<TrackItem>().Where(x => x.AlbumId == albumId).OrderBy(x => x.DiscNumber).ThenBy(x => x.Index);
return query.ToList();
var tracks = query.ToList();
MusicDatabase.DatabaseOperation.Release();
return tracks;
}
}
public async Task<TrackItem> GetFirstTrackOfAlbumId(int albumId)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
var query = connection.Table<TrackItem>().Where(x => x.AlbumId == albumId);
var result = await query.FirstOrDefaultAsync();
MusicDatabase.DatabaseOperation.Release();
return result;
}
public async Task<string> GetFirstTrackPathByAlbumId(int albumId)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var trackItem = await GetFirstTrackOfAlbumId(albumId);
MusicDatabase.DatabaseOperation.Release();
return trackItem?.Path;
}
public async Task<List<TrackItem>> LoadTracksByArtistId(int artistId)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
var query = connection.Table<TrackItem>().Where(x => x.ArtistId == artistId);
return await query.ToListAsync();
var tracks= await query.ToListAsync();
MusicDatabase.DatabaseOperation.Release();
return tracks;
}
public async Task<bool> IsEmpty()
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
int c = await connection.Table<TrackItem>().CountAsync();
MusicDatabase.DatabaseOperation.Release();
return c == 0;
}
public async Task<List<TrackItem>> LoadTracks()
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
var query = connection.Table<TrackItem>().OrderBy(x => x.Name);
return await query.ToListAsync();
var tracks= await query.ToListAsync();
MusicDatabase.DatabaseOperation.Release();
return tracks;
}
public Task Update(TrackItem track)
public async Task Update(TrackItem track)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
return connection.UpdateAsync(track);
await connection.UpdateAsync(track);
MusicDatabase.DatabaseOperation.Release();
}
public async Task Add(TrackItem track)
{
await MusicDatabase.DatabaseOperation.WaitAsync();
var connection = new SQLiteAsyncConnection(DbPath);
var query = connection.Table<TrackItem>().Where(x => x.Path == track.Path);
var result = await query.ToListAsync();
if (result.Count == 0)
await connection.InsertAsync(track);
MusicDatabase.DatabaseOperation.Release();
}
public Task Remove(TrackItem track)
......
......@@ -33,6 +33,14 @@ namespace VLC_WinRT.Database
}
}
public void DeleteAll()
{
using (var db = new SQLiteConnection(DbPath))
{
db.DeleteAll<TracklistItem>();
}
}
public Task<List<TracklistItem>> LoadTracks(TrackCollection trackCollection)
{
var connection = new SQLiteAsyncConnection(DbPath);
......
......@@ -36,6 +36,14 @@ namespace VLC_WinRT.Database
}
}
public void DeleteAll()
{
using (var db = new SQLiteConnection(DbPath))
{
db.DeleteAll<VideoItem>();
}
}
public async Task<VideoItem> GetFromPath(String path)
{
var conn = new SQLiteAsyncConnection(DbPath);
......
......@@ -443,7 +443,7 @@ namespace VLC_WinRT.Helpers.MusicLibrary
}
#endregion
public async void AddArtist(ArtistItem artist)
public void AddArtist(ArtistItem artist)
{
Artists.Add(artist);
}
......@@ -577,9 +577,24 @@ namespace VLC_WinRT.Helpers.MusicLibrary
return groupedAlbums;
}
public IEnumerable<IGrouping<string, ArtistItem>> OrderArtists()
public ObservableCollection<GroupItemList<ArtistItem>> OrderArtists()
{
return Artists?.GroupBy(x => string.IsNullOrEmpty(x.Name) ? Strings.UnknownString : (char.IsLetter(x.Name.ElementAt(0)) ? x.Name.ToUpper().ElementAt(0).ToString() : Strings.UnknownString));
var groupedArtists = new ObservableCollection<GroupItemList<ArtistItem>>();
var groupQuery = from artist in Artists
orderby artist.Name
group artist by Strings.HumanizedArtistFirstLetter(artist.Name) into a
select new { GroupName = a.Key, Items = a };
foreach (var g in groupQuery)
{
GroupItemList<ArtistItem> artists = new GroupItemList<ArtistItem>();
artists.Key = g.GroupName;
foreach (var artist in g.Items)
{
artists.Add(artist);
}
groupedArtists.Add(artists);
}
return groupedArtists;
}
public IEnumerable<IGrouping<char, TrackItem>> OrderTracks()
......@@ -755,7 +770,7 @@ namespace VLC_WinRT.Helpers.MusicLibrary
{
try
{
var tracks = trackDatabase.LoadTracksByAlbumId(album.Id);
var tracks = await trackDatabase.LoadTracksByAlbumId(album.Id);
await App.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
album.Tracks = tracks;
......@@ -798,9 +813,9 @@ namespace VLC_WinRT.Helpers.MusicLibrary
return trackDatabase.LoadTracksByArtistId(id);
}
public List<TrackItem> LoadTracksByAlbumId(int id)
public async Task<List<TrackItem>> LoadTracksByAlbumId(int id)
{
return trackDatabase.LoadTracksByAlbumId(id);
return await trackDatabase.LoadTracksByAlbumId(id);
}
public async Task<ArtistItem> LoadArtist(int id)
......@@ -808,12 +823,12 @@ namespace VLC_WinRT.Helpers.MusicLibrary
return await artistDatabase.LoadArtist(id);
}
public ArtistItem LoadViaArtistName(string name)
public async Task<ArtistItem> LoadViaArtistName(string name)
{
return artistDatabase.LoadViaArtistName(name);
return await artistDatabase.LoadViaArtistName(name);
}
public AlbumItem LoadAlbum(int id)
public Task<AlbumItem> LoadAlbum(int id)
{
return albumDatabase.LoadAlbum(id);
}
......
......@@ -90,7 +90,7 @@ namespace VLC_WinRT.Helpers
{
await Locator.MediaPlaybackViewModel.TrackCollection.ResetCollection();
}
var trackItems = Locator.MusicLibraryVM.MusicLibrary.LoadTracksByAlbumId(albumId);
var trackItems = await Locator.MusicLibraryVM.MusicLibrary.LoadTracksByAlbumId(albumId);
await Locator.MediaPlaybackViewModel.TrackCollection.Add(trackItems);
if (play)
{
......
......@@ -126,6 +126,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Database\ArtistDatabase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Database\IDatabase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Database\KeyboardActionDatabase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Database\MusicDatabase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Database\StreamsDatabase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Database\TrackCollectionDatabase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Database\TrackDatabase.cs" />
......
......@@ -218,7 +218,7 @@ namespace VLC_WinRT.ViewModels.MusicVM
if (CurrentTrack == null) return;
if (CurrentArtist == null) return;
if (CurrentAlbum != null && CurrentAlbum.Id == CurrentTrack.AlbumId) return;
CurrentAlbum = Locator.MusicLibraryVM.MusicLibrary.LoadAlbum(CurrentTrack.AlbumId);
CurrentAlbum = await Locator.MusicLibraryVM.MusicLibrary.LoadAlbum(CurrentTrack.AlbumId);
}
......
......@@ -118,7 +118,7 @@ namespace Slide2D.Images
images.Add(new Img(Locator.MusicPlayerVM.CurrentArtist.Picture));
newPic = true;
}
var album = Locator.MusicLibraryVM.MusicLibrary.LoadAlbum(Locator.MusicPlayerVM.CurrentTrack.AlbumId);
var album = await Locator.MusicLibraryVM.MusicLibrary.LoadAlbum(Locator.MusicPlayerVM.CurrentTrack.AlbumId);
if (album != null)
{
if (album.IsPictureLoaded)
......
......@@ -132,7 +132,7 @@ namespace VLC_WinRT.UI.Legacy.Views.UserControls
FavoriteAppBarButton.Command = Album.FavoriteAlbum;
FavoriteAppBarButton.CommandParameter = Album;
TracksListView.ItemsSource = Locator.MusicLibraryVM.MusicLibrary.LoadTracksByAlbumId(Album.Id);
TracksListView.ItemsSource = await Locator.MusicLibraryVM.MusicLibrary.LoadTracksByAlbumId(Album.Id);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment