Ejemplo n.º 1
0
        void OnDownloadImageCompletedForDownloadPage(object sender, OpenReadCompletedEventArgs e)
        {
            DownloadImageState state = (DownloadImageState)e.UserState;

            if (!_downloadPageProgress.ContainsKey(state.Context))
            {
                return;
            }

            _downloadPageProgress[state.Context]--;

            if (e.Error == null && e.Result != null)
            {
                try
                {
                    Storage.SaveStreamToFile(e.Result, (string)state.CachePath);

                    if (_downloadPageProgress[state.Context] == 0)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(new GetPageAsyncCompletedTrigger(TriggerGetPageAsyncCompleted), state.Page);
                    }
                }
                catch
                {
                    _downloadPageProgress.Remove(state.Context);
                    PageModel pageModel = new PageModel();
                    pageModel.Images = new EntitySet<ImageModel>();
                    Deployment.Current.Dispatcher.BeginInvoke(new GetPageAsyncCompletedTrigger(TriggerGetPageAsyncCompleted), pageModel);
                }
            }
            else
            {
                _downloadPageProgress.Remove(state.Context);
                PageModel pageModel = new PageModel();
                pageModel.Images = new EntitySet<ImageModel>();
                Deployment.Current.Dispatcher.BeginInvoke(new GetPageAsyncCompletedTrigger(TriggerGetPageAsyncCompleted), pageModel);
            }
        }
Ejemplo n.º 2
0
 private void TriggerGetPageAsyncCompleted(PageModel pageModel)
 {
     if (Events != null)
     {
         Events.InvokeGetPageAsyncCompleted(this, pageModel);
     }
 }
Ejemplo n.º 3
0
        public void LoadPageAsync(PageModel page, List<string> cachePaths, bool forceRefresh)
        {
            _currentlyViewingPage = page.Page;
            try
            {
                _downloadPageContext++;

                uint numDownloadPending = 0;
                IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
                for (int i = 0; i < cachePaths.Count; i++)
                {
                    bool found = false;

                    string cachePath = cachePaths[i];
                    found = store.FileExists(cachePath);

                    if (forceRefresh || !found)
                    {
                        if (forceRefresh)
                        {
                            store.DeleteFile(cachePath);
                        }

                        WebClient webClient = new WebClient();
                        webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(OnDownloadImageCompletedForDownloadPage);
                        webClient.OpenReadAsync(new Uri(page.Images[i].Url), new DownloadImageState(cachePath, page, _downloadPageContext));

                        numDownloadPending++;
                    }
                }

                if (numDownloadPending > 0)
                {
                    _downloadPageProgress.Add(_downloadPageContext, numDownloadPending);
                }
                else
                {
                    TriggerGetPageAsyncCompleted(page);
                }
            }
            catch
            {
                PageModel pageModel = new PageModel();
                pageModel.Images = new EntitySet<ImageModel>();
                TriggerGetPageAsyncCompleted(pageModel);
            }
        }
Ejemplo n.º 4
0
 public DownloadImageState(string cachePath, PageModel page, uint context)
 {
     this.CachePath = cachePath;
     this.Page = page;
     this.Context = context;
 }
Ejemplo n.º 5
0
 public void InvokeGetPageAsyncCompleted(AppData appData, PageModel pageModel)
 {
     GetPageAsyncCompleted.Invoke(appData, pageModel);
 }
Ejemplo n.º 6
0
        void OnGetPageAsyncCompleted(object sender, PageModel pageModel)
        {
            if (pageModel != null && pageModel.Images.Count >= 1)
            {
                if (App.AppData.Manga != null)
                {
                    MangaModel manga = App.AppData.Manga;

                    string content = string.Empty;

                    if (pageModel.Images.Count == 1)
                    {
                        string cachePath = GetCachePath(manga, pageModel, 0);
                        content = "<html><head><meta name=\"Viewport\" content=\"width=800; height=800; user-scaleable=yes;\"/></head><body><img src=\"" + cachePath + "\"/></body></html>";
                    }
                    else
                    {
                        StringBuilder bodyBuilder = new StringBuilder();
                        StringBuilder styleBuilder = new StringBuilder();
                        for (int i = 0; i < pageModel.Images.Count; i++)
                        {
                            ImageModel imageModel = pageModel.Images[i];
                            styleBuilder.Append("#p" + i + "{position:absolute;z-index:" + imageModel.ZIndex + ";width:" + imageModel.Width + "px;height:" + imageModel.Height + "px;top:" + imageModel.Top + "px;left:" + imageModel.Left + "px}\n");
                            bodyBuilder.Append("<div id=\"p" + i + "\"><img src=\"" + GetCachePath(manga, pageModel, i) + "\"/></div>");
                        }

                        content = "<html><head><meta name=\"Viewport\" content=\"width=800; height=800; user-scaleable=yes;\"/><style type=\"text/css\">" + styleBuilder.ToString() + "</style></head><body><div>" + bodyBuilder.ToString() + "</div></body></html>";
                    }

                    IsolatedStorageFileStream stream = null;
                    try
                    {
                        byte[] buffer = Encoding.UTF8.GetBytes(content);
                        IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
                        stream = store.CreateFile("index.html");
                        stream.Write(buffer, 0, buffer.Length);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Couldn't show content");
                    }
                    finally
                    {
                        if (stream != null)
                        {
                            stream.Close();
                        }
                    }

                    _currentViewingPage = pageModel.Page - 1;

                    bool oldNextAllowed = NextAllowed;
                    NextAllowed = (_currentViewingPage < manga.Pages.Count - 1);
                    if (oldNextAllowed != NextAllowed)
                    {
                        NotifyPropertyChanged("NextAllowed");
                    }

                    bool oldPreviousAllowed = PreviousAllowed;
                    PreviousAllowed = (_currentViewingPage > 0);
                    if (oldPreviousAllowed != PreviousAllowed)
                    {
                        NotifyPropertyChanged("PreviousAllowed");
                    }
                }
                else
                {
                    // Couldn't display content
                }
                DisplayContent = "index.html";
                NotifyPropertyChanged("DisplayContent");
            }
            else
            {
                MessageBox.Show("Failed to retrieve images. Please try refresh again.");
            }
            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;
            SetLoadingStatus(false);
        }
Ejemplo n.º 7
0
 private string GetCachePath(MangaModel mangaModel, PageModel pageModel, int count)
 {
     return mangaModel.SeriesId + "_" + mangaModel.MangaId + "_" + pageModel.Page + "_" + count + ".img";
 }