Example #1
0
        private static async Task AddVideo(VideoRepository videoRepo, IReadOnlyList <StorageFile> files, bool isCameraRoll)
        {
            try
            {
                foreach (StorageFile storageFile in files)
                {
                    MediaProperties videoProperties = null;
                    if (!isCameraRoll)
                    {
                        var media = Locator.VLCService.GetMediaFromPath(storageFile.Path);
                        videoProperties = Locator.VLCService.GetVideoProperties(media);
                        if (videoProperties == null)
                        {
                            TitleDecrapifier.tvShowEpisodeInfoFromString(storageFile.DisplayName);
                        }
                    }

                    bool isTvShow = !string.IsNullOrEmpty(videoProperties?.ShowTitle) && videoProperties?.Season >= 0 && videoProperties?.Episode >= 0;

                    // Check if we know the file:
                    //FIXME: We need to check if the files in DB still exist on disk
                    var mediaVM = await videoRepo.GetFromPath(storageFile.Path).ConfigureAwait(false);

                    if (mediaVM != null)
                    {
                        Debug.Assert(isTvShow == mediaVM.IsTvShow);
                        mediaVM.File = storageFile;
                        if (isTvShow)
                        {
                            await AddTvShow(videoProperties.ShowTitle, mediaVM);
                        }
                    }
                    else
                    {
                        // Analyse to see if it's a tv show
                        // if the file is from a tv show, we push it to this tvshow item

                        mediaVM = !isTvShow
                            ? new VideoItem()
                            : new VideoItem(videoProperties.Season, videoProperties.Episode);
                        await mediaVM.Initialize(storageFile);

                        mediaVM.IsCameraRoll = isCameraRoll;
                        if (string.IsNullOrEmpty(mediaVM.Name))
                        {
                            continue;
                        }
                        VideoItem searchVideo =
                            Locator.VideoLibraryVM.ViewedVideos.FirstOrDefault(x => x.Name == mediaVM.Name);
                        if (searchVideo != null)
                        {
                            mediaVM.TimeWatched = searchVideo.TimeWatched;
                        }

                        if (isTvShow)
                        {
                            await AddTvShow(videoProperties.ShowTitle, mediaVM);
                        }
                        await videoRepo.Insert(mediaVM);
                    }
                    // Get back to UI thread
                    await DispatchHelper.InvokeAsync(() =>
                    {
                        if (mediaVM.IsCameraRoll)
                        {
                            // TODO: Find a more efficient way to know if it's already in the list or not
                            if (Locator.VideoLibraryVM.CameraRoll.FirstOrDefault(x => x.Id == mediaVM.Id) == null)
                            {
                                Locator.VideoLibraryVM.CameraRoll.Add(mediaVM);
                            }
                        }
                        else if (!mediaVM.IsTvShow)
                        {
                            if (Locator.VideoLibraryVM.Videos.FirstOrDefault(x => x.Id == mediaVM.Id) == null)
                            {
                                Locator.VideoLibraryVM.Videos.Add(mediaVM);
                            }
                        }
                        if (Locator.VideoLibraryVM.ViewedVideos.Count < 6 &&
                            Locator.VideoLibraryVM.ViewedVideos.FirstOrDefault(
                                x => x.Path == mediaVM.Path && x.TimeWatched == TimeSpan.Zero) == null)
                        {
                            if (Locator.VideoLibraryVM.ViewedVideos.FirstOrDefault(x => x.Id == mediaVM.Id) == null)
                            {
                                Locator.VideoLibraryVM.ViewedVideos.Add(mediaVM);
                            }
                        }
                    });
                }
            }
            catch (Exception e)
            {
                ExceptionHelper.CreateMemorizedException("VideoLibraryManagement.AddVideo", e);
            }
        }