public static async Task LoadEbooksAsync()
        {
            if (Ebooks.Count != 0)
            {
                return;
            }

            await _ebooksAsyncLock.WaitAsync();

            try
            {
                var ebooks = await EbookService.LoadEbooksAsync();

                await UIThread.RunAsync(() =>
                {
                    foreach (var ebook in ebooks)
                    {
                        Ebooks.Add(ebook);
                    }
                });

                EbooksLoaded?.Invoke(null, new EventArgs());
            }
            finally
            {
                _ebooksAsyncLock.Release();
            }
        }
        public EbooksViewModel()
        {
            Refresh = new RelayCommand(async() =>
            {
                CanRefresh = false;
                try
                {
                    await EbookManager.RefreshEbooksAsync();
                }
                finally
                {
                    CanRefresh = true;
                }
            });

            if (!IsInDesignModeStatic)
            {
                _ = Task.Run(async() =>
                {
                    await EbookManager.LoadEbooksAsync();

                    if (EbookManager.Ebooks.Count == 0)
                    {
                        await EbookManager.RefreshEbooksAsync();
                    }

                    await UIThread.RunAsync(() =>
                    {
                        CanRefresh = true;
                    });
                });
            }
        }
        public static async Task RefreshEbooksAsync()
        {
            await _ebooksAsyncLock.WaitAsync();

            try
            {
                var credentials = CredentialsService.GetProviderCredentials();

                foreach (var credential in credentials)
                {
                    var provider = EbookProviderService.GetProvider(credential);

                    Ebook[] ebooks;

                    try
                    {
                        ebooks = await provider.GetEbooksAsync();
                    }
                    catch
                    {
                        await provider.AuthenticateAsync(credential);

                        ebooks = await provider.GetEbooksAsync();
                    }

                    await EbookStorage.SaveEbooksAsync(ebooks);

                    await UIThread.RunAsync(() =>
                    {
                        // TODO: Support for multiple providers of the same books
                        // remove
                        {
                            var oldEbooks = Ebooks.Where(eb => !ebooks.Select(ebb => ebb.Isbn).Contains(eb.Isbn)).ToArray();
                            foreach (var oldEbook in oldEbooks)
                            {
                                Ebooks.Remove(oldEbook);
                            }
                        }

                        // Add
                        foreach (var ebook in ebooks)
                        {
                            var existing = Ebooks.FirstOrDefault(eb => eb.Isbn == ebook.Isbn);

                            if (existing == null)
                            {
                                Ebooks.Add(ebook);
                            }
                        }
                    });
                }
            }
            finally
            {
                _ebooksAsyncLock.Release();
            }
        }
        public EbookViewModel(Ebook ebook)
        {
            _ebook     = ebook;
            Title      = ebook.Title;
            Isbn       = ebook.Isbn;
            IsPinnable = !EbookTile.Exists(_ebook.Isbn);

            Download = new RelayCommand(async() =>
            {
                IsDownloadable = false;
                IsDownloading  = true;

                Messenger.Default.Register <DownloadStatusMessage>(this, _ebook.Isbn, msg =>
                {
                    DownloadStatus = $"{msg.Done} av {msg.Total} sidor nerladdade";
                });

                await EbookManager.DownloadEbookAsync(_ebook);

                IsDownloading = false;
                IsDownloaded  = true;
                IsPinnable    = await Task.Run(() => !EbookTile.Exists(_ebook.Isbn));

                FrontPagePath = await PageStorage.GetPagePathAsync(_ebook, 1);

                _ = _watchForDirectoryRemoval();
            });

            Pin = new RelayCommand(async() =>
            {
                IsPinnable = !(await EbookTile.RequestCreateAsync(_ebook));
            });

            _ = Task.Run(async() =>
            {
                if (!await PageStorage.EbookExistsAsync(_ebook))
                {
                    if (NetworkInformation.GetInternetConnectionProfile() != null)
                    {
                        _ = UIThread.RunAsync(() => IsDownloadable = true);
                    }
                }
                else
                {
                    _ = UIThread.RunAsync(() => IsDownloaded = true);

                    try
                    {
                        _ = UIThread.RunAsync(async() => FrontPagePath = await PageStorage.GetPagePathAsync(_ebook, 1));
                    }
                    catch { }

                    _ = _watchForDirectoryRemoval();
                }
            });
        }
        public static async Task DownloadEbookAsync(Ebook ebook)
        {
            var credential = CredentialsService.GetProviderCredential(ebook.Provider);

            var provider = EbookProviderService.GetProvider(credential);

            try
            {
                try
                {
                    await provider.DownloadEbookAsync(ebook);
                }
                catch
                {
                    await provider.AuthenticateAsync(credential);

                    await provider.DownloadEbookAsync(ebook);
                }
            }
            catch (EbookException e)
            {
                ContentDialog downloadErrorDialog = new ContentDialog()
                {
                    Title                = "Kunde inte ladda ner e-bok",
                    Content              = $"E-boken {ebook.Title} (publicerad av {ebook.Publisher}) kunde inte laddas ner. Skapa gärna ett nytt Issue på GitHub om felet inträffar flera gånger.",
                    CloseButtonText      = "Stäng",
                    PrimaryButtonText    = "Öppna GitHub",
                    PrimaryButtonCommand = new RelayCommand(async() =>
                    {
                        var uriBing = new Uri(@"https://github.com/mikaeldui/MinaLaromedel/issues");

                        await Windows.System.Launcher.LaunchUriAsync(uriBing);
                    })
                };

                SentrySdk.CaptureException(e);

                await UIThread.RunAsync(async() => await downloadErrorDialog.ShowAsync());
            }
        }
        public bool TryEnterFullScreen()
        {
            var configuration = _appWindow.Presenter.GetConfiguration();

            if (_appWindow.Presenter.RequestPresentation(AppWindowPresentationKind.FullScreen))
            {
                FullScreenEntered?.Invoke(this, null);

                _ = Task.Run(async() =>
                {
                    await Task.Delay(500);
                    await UIThread.RunAsync(() =>
                    {
                        _appWindow.Changed += AppWindow_Changed;
                    });
                });

                return(true);
            }

            return(false);
        }