예제 #1
0
        public bool Delete(Schedule schedule)
        {
            using var transaction = _sqlService.BeginTransaction();
            try
            {
                var valid = true;

                valid &= RemoveBillAndInstallment(schedule, transaction);
                valid &= transaction.Delete(schedule) > 0;

                if (valid)
                {
                    transaction.Commit();
                }
                else
                {
                    transaction.Rollback();
                }

                return(valid);
            }
            catch (Exception)
            {
                transaction.Rollback();
                throw;
            }
            finally
            {
                transaction.Close();
            }
        }
예제 #2
0
        public void LoadLibrary()
        {
            if (IsLibraryLoaded)
            {
                return;
            }

            /*
             * Sqlite makes a transaction to create a shared lock
             * Wrapping it in one single transactions assures it is only lock and release once
             */
            _sqlService.BeginTransaction();

            var songs   = _sqlService.SelectAll <Song>().OrderByDescending(p => p.Id).ToList();
            var artists = _sqlService.SelectAll <Artist>().OrderByDescending(p => p.Id).ToList();
            var albums  = _sqlService.SelectAll <Album>().OrderByDescending(p => p.Id).ToList();

            Stations =
                new OptimizedObservableCollection <RadioStation>(
                    _sqlService.SelectAll <RadioStation>().OrderByDescending(p => p.Id).ToList());

            // Let go of the lock
            _sqlService.Commit();

            var isForeground = _dispatcher != null;

            foreach (var song in songs)
            {
                song.Artist = artists.FirstOrDefault(p => p.Id == song.ArtistId);
                song.Album  = albums.FirstOrDefault(p => p.Id == song.AlbumId);
            }

            if (isForeground)
            {
                _dispatcher.RunAsync(() => Songs.AddRange(songs)).Wait();
            }
            else
            {
                Songs.AddRange(songs);
            }

            if (isForeground)
            {
                foreach (var artist in artists)
                {
                    _dispatcher.RunAsync(
                        () =>
                    {
                        var artworkPath = string.Format(_artistArtworkFilePath, artist.Id);
                        artist.Artwork  = artist.HasArtwork
                                ? _bitmapFactory.CreateImage(
                            new Uri(_localFilePrefix + artworkPath))
                                : null;

                        if (ScaledImageSize != 0 && artist.Artwork != null)
                        {
                            artist.Artwork.SetDecodedPixel(ScaledImageSize);
                        }
                    }).Wait();
                }
            }

            foreach (var album in albums)
            {
                album.Songs.AddRange(songs.Where(p => !p.IsTemp && p.AlbumId == album.Id).OrderBy(p => p.TrackNumber));
                album.PrimaryArtist = artists.FirstOrDefault(p => p.Id == album.PrimaryArtistId);

                if (isForeground)
                {
                    _dispatcher.RunAsync(
                        () =>
                    {
                        var artworkPath = string.Format(_artworkFilePath, album.Id);
                        if (album.HasArtwork)
                        {
                            var path = _localFilePrefix + artworkPath;

                            album.Artwork = _bitmapFactory.CreateImage(new Uri(path));

                            if (ScaledImageSize != 0)
                            {
                                album.Artwork.SetDecodedPixel(ScaledImageSize);

                                album.MediumArtwork = _bitmapFactory.CreateImage(new Uri(path));
                                album.MediumArtwork.SetDecodedPixel(ScaledImageSize / 2);

                                album.SmallArtwork = _bitmapFactory.CreateImage(new Uri(path));
                                album.SmallArtwork.SetDecodedPixel(50);
                            }
                        }
                        else
                        {
                            var artwork = album.PrimaryArtist.HasArtwork
                                    ? album.PrimaryArtist.Artwork
                                    : _missingArtwork;
                            album.Artwork       = artwork;
                            album.MediumArtwork = artwork;
                            album.SmallArtwork  = artwork;
                        }
                    }).Wait();
                }
            }

            if (isForeground)
            {
                _dispatcher.RunAsync(() => Albums.AddRange(albums)).Wait();
            }
            else
            {
                Albums.AddRange(albums);
            }

            foreach (var artist in artists)
            {
                artist.Songs.AddRange(songs.Where(p => !p.IsTemp && p.ArtistId == artist.Id).OrderBy(p => p.Name));
                artist.Albums.AddRange(
                    albums.Where(p => p.PrimaryArtistId == artist.Id && p.Songs.Count > 0).OrderBy(p => p.Name));

                var songsAlbums = artist.Songs.Select(p => p.Album);
                artist.Albums.AddRange(songsAlbums.Where(p => p.Songs.Count > 0 && !artist.Albums.Contains(p)));
            }

            if (isForeground)
            {
                _dispatcher.RunAsync(() => Artists.AddRange(artists)).Wait();
            }
            else
            {
                Artists.AddRange(artists);
            }

            IsLibraryLoaded = true;

            LoadQueue(songs);
            LoadPlaylists();

            if (!isForeground)
            {
                return;
            }

            var corruptSongs =
                Songs.ToList()
                .Where(p => string.IsNullOrEmpty(p.Name) || p.Album == null || p.Artist == null)
                .ToList();

            foreach (var corruptSong in corruptSongs)
            {
                DeleteSongAsync(corruptSong).Wait();
            }

            _dispatcher.RunAsync(
                () =>
            {
                if (LibraryLoaded != null)
                {
                    LibraryLoaded(this, null);
                }
            }).Wait();

            try
            {
                CleanupFiles(albums, artists);
            }
            catch
            {
                // ignored
            }
        }
예제 #3
0
        /// <summary>
        /// Synchronizes the new songs.
        /// </summary>
        /// <param name="onlineSongs">
        /// The online songs.
        /// </param>
        /// <returns>
        /// Task.
        /// </returns>
        private async Task PullSyncNewSongsAsync(List <CloudSong> onlineSongs)
        {
            var collectionSongs = _collectionService.Songs.Where(p => p.SongState != SongState.Local &&
                                                                 !p.IsTemp).ToList();

            // nothing different
            if (onlineSongs.Count - collectionSongs.Count == 0)
            {
                return;
            }

            // Wrap everything in one transaction
            _sqlService.BeginTransaction();

            // when syncing over 20 new songs is best to supress the collection (avoiding unnecessary ui work)
            var reset = (onlineSongs.Count - collectionSongs.Count) > 20;

            if (reset)
            {
                await _dispatcherHelper.RunAsync(
                    () =>
                {
                    if (OnLargeSyncStarted != null)
                    {
                        OnLargeSyncStarted(null, EventArgs.Empty);
                    }

                    _collectionService.Songs.SuppressEvents   = true;
                    _collectionService.Artists.SuppressEvents = true;
                    _collectionService.Albums.SuppressEvents  = true;
                });
            }

            foreach (var onlineSong in from onlineSong in onlineSongs
                     let localSong = collectionSongs.FirstOrDefault(p => p.CloudId == onlineSong.Id)
                                     where localSong == null
                                     select onlineSong)
            {
                if (LastSyncTime > onlineSong.CreatedAt)
                {
                    await _mobileServiceClient.GetTable <CloudSong>().DeleteAsync(onlineSong).ConfigureAwait(false);

                    continue;
                }

                // Cloud song is not in the collection, check if it was saved before syncing
                var collSong = collectionSongs.FirstOrDefault(p => p.ProviderId == onlineSong.ProviderId);
                if (collSong != null)
                {
                    collSong.CloudId = onlineSong.Id;
                    await _sqlService.UpdateItemAsync(collSong).ConfigureAwait(false);
                }
                else
                {
                    if (onlineSong.Album == null | onlineSong.Artist == null)
                    {
                        await _mobileServiceClient.GetTable <CloudSong>().DeleteAsync(onlineSong).ConfigureAwait(false);

                        continue;
                    }

                    if (onlineSong.Album.PrimaryArtist == null)
                    {
                        onlineSong.Album.PrimaryArtist = onlineSong.Artist;
                    }

                    // Can't change CloudSong to Song, so need to create a new song
                    var newSong = new Song(onlineSong)
                    {
                        Album  = new Album(onlineSong.Album),
                        Artist = new Artist(onlineSong.Artist)
                    };

                    // By setting it to just synced, the app knows it has to match an audio url to it
                    await _collectionService.AddSongAsync(newSong).ConfigureAwait(false);
                }
            }

            // commit the transaction
            _sqlService.Commit();
            if (reset)
            {
                await _dispatcherHelper.RunAsync(
                    () =>
                {
                    if (OnLargeSyncFinished != null)
                    {
                        OnLargeSyncFinished(null, EventArgs.Empty);
                    }

                    _collectionService.Songs.Reset();
                    _collectionService.Albums.Reset();
                    _collectionService.Artists.Reset();
                });
            }
        }