Пример #1
0
        public static void Install(Inv.Application application)
        {
            application.Title = "MegaDungeon";
            var surface = application.Window.NewSurface();
            var h       = application.Window.Height;
            var w       = application.Window.Width;
            var start   = DateTime.UtcNow;
            var ui      = new MegaDungeonUI(surface, 60, 40);
            var end     = DateTime.UtcNow;
            var ms      = (end - start).TotalMilliseconds;

            Console.WriteLine($"Initialized in {ms} milliseconds");

            surface.ArrangeEvent += () =>
            {
                Surface_ArrangeEvent(surface);
            };

            application.Window.Transition(surface);

            surface.ComposeEvent += () =>
            {
                Surface_ComposeEvent(ui);
            };

            surface.KeystrokeEvent += (keyStroke) =>
            {
                Surface_KeystrokeEvent(keyStroke, ui);
            };
            Console.WriteLine("Boot completed.");
        }
Пример #2
0
        public static void Install(Inv.Application application)
        {
            application.Title = "My Project";
            var surface = application.Window.NewSurface();
            var h       = application.Window.Height;
            var w       = application.Window.Width;

            InitializeSurface(surface);

            surface.ArrangeEvent += () =>
            {
                Surface_ArrangeEvent(surface);
            };

            application.Window.Transition(surface);
        }
Пример #3
0
        public static void Install(Inv.Application application)
        {
            _application       = application;
            _application.Title = "FeedRead";

            // main surface
            var feedSurface = application.Window.NewSurface();

            feedSurface.Background.Colour = Colour.LightGray;

            // sub surface
            Surface articleSurface;

            // grap api key
            string apiKey = System.Text.Encoding.UTF8.GetString(Inv.Default.Resources.Text.Newsapi.GetBuffer());

            var priorityQueue = new InvDefault.SimplePriorityQueue <string, int>();

            // main listview
            var flow = feedSurface.NewFlow();

            _application.Window.Transition(feedSurface).Fade();
            feedSurface.Content = flow;

            // dumb caching
            var panelCache = new Dictionary <int, Panel>();
            var htmlCache  = new Dictionary <string, string>();

            var headerLabel = feedSurface.NewLabel();

            headerLabel.Text = "FeedRead";
            headerLabel.JustifyCenter();
            headerLabel.Alignment.TopStretch();
            headerLabel.Padding.Set(0, 12, 0, 4);
            headerLabel.Background.Colour = Colour.DodgerBlue;
            headerLabel.Font.Colour       = Colour.White;
            headerLabel.Font.Size         = 32;
            headerLabel.Font.Heavy();

            // section
            var section = flow.AddSection();

            section.SetHeader(headerLabel);

            IList <Article> items = new List <Article>();

            // pull-to-refresh made easy! except on windows
            flow.RefreshEvent += (FlowRefresh obj) =>
            {
                Debug.WriteLine($"Refresh listview");
                feedSurface.Window.RunTask((rThread) =>
                {
                    var feedObject = LoadArticles();
                    rThread.Post(() => {
                        headerLabel.Text = feedObject.source;
                        items            = feedObject.articles.ToList();
                        section.SetItemCount(items.Count);
                        foreach (var article in items)
                        {
                            priorityQueue.Enqueue($"cache: {article.url}", 20);
                            priorityQueue.Enqueue($"imageCache: {article.urlToImage}", 10);
                        }
                        obj.Complete();
                    });
                });
            };

            FeedObject LoadArticles()
            {
                var broker = application.Web.NewBroker("https://newsapi.org/v1/");

                return(broker.GetTextJsonObject <FeedObject>(@"articles?source=ars-technica&sortBy=top&apiKey=" + apiKey));
            }

            if (application.Device.IsWindows)
            {
                // I don't do windows
                // err, windows doesn't do flow.Refresh()
                var json = LoadArticles();
                items = json.articles.ToList();
                section.SetItemCount(items.Count);
            }
            else
            {
                flow.Refresh();
            }


            string fetchWebPage(string url)
            {
                var ampUri = @"https://mercury.postlight.com/amp?url=" + url;
                var uri3   = new Uri(ampUri);
                //Debug.WriteLine($"uri3: {ampUri}");
                var broker = _application.Web.NewBroker($"{uri3.Scheme}://{uri3.DnsSafeHost}");

                return("[cached]" + broker.GetPlainText(uri3.PathAndQuery));
            }

            void cacheImageToDisk(string url)
            {
                var cacheFileName = Helpers.GetMD5Hash(url) + ".jpg";
                var cacheFile     = _application.Directory.RootFolder.NewFile(cacheFileName);

                if (cacheFile.Exists())
                {
                    return;
                }
                using (var download = _application.Web.GetDownload(new Uri(url)))

                    using (var memoryStream = new MemoryStream((int)download.Length))
                    {
                        download.Stream.CopyTo(memoryStream);
                        memoryStream.Flush();
                        cacheFile.WriteAllBytes(memoryStream.ToArray());
                    }
            }

            // Background task queue
            feedSurface.Window.RunTask((WindowThread Thread) =>
            {
                while (true)
                {
                    if (priorityQueue.Count > 0)
                    {
                        string response = priorityQueue.Dequeue();
                        Debug.WriteLine($"{priorityQueue.Count}: {response}");

                        var split    = response.Split(new char[] { ':' }, 2);
                        var cmd      = split[0].Trim();
                        var cmdParam = split[1].Trim();
                        switch (cmd)
                        {
                        case "cache":
                            //Debug.WriteLine($"Cache: {cmdParam}");
                            if (htmlCache.ContainsKey(cmdParam))
                            {
                                break;
                            }
                            var html = fetchWebPage(cmdParam);
                            Thread.Post(() => htmlCache.Add(cmdParam, html));
                            break;

                        case "imageCache":
                            //Debug.WriteLine($"imageCache: {cmdParam}");
                            cacheImageToDisk(cmdParam);
                            break;

                        case "log":
                            Debug.WriteLine($"Log: {cmdParam}");
                            break;
                        }
                    }
                    else
                    {
                        Debug.WriteLine("sleep...");
                        Thread.Sleep(TimeSpan.FromMilliseconds(5000));
                    }
                }
            });

            feedSurface.LeaveEvent += () =>
            {
                // it's not you, it's me
                Debug.Write("mainSurface.LeaveEvent");
            };


            section.ItemQuery += i =>
            {
                // simple caching
                if (panelCache.ContainsKey(i))
                {
                    return(panelCache[i]);
                }

                var article = items[i];

                var cellPanel = new FeedItemPanel(_application, feedSurface, article);

                // cache panel
                panelCache[i] = cellPanel;

                cellPanel.SingleTapEvent += () =>
                {
                    articleSurface = _application.Window.NewSurface();
                    var browser = articleSurface.NewBrowser();

                    // get AMP optimized pages for mobile
                    var ampUri = @"https://mercury.postlight.com/amp?url=" + article.url;

                    // use cache
                    if (htmlCache.ContainsKey(article.url))
                    {
                        var html = htmlCache[article.url];
                        browser.LoadHtml(html);
                        articleSurface.Content = browser;
                    }
                    else
                    {
                        var uri2 = new Uri(ampUri);
                        Debug.WriteLine(uri2.AbsoluteUri);
                        browser.LoadUri(uri2);
                        articleSurface.Content = browser;
                        htmlCache.Add(article.url, browser.Html);
                    }


                    // aah, simple transition
                    _application.Window.Transition(articleSurface).CarouselNext();

                    articleSurface.GestureBackwardEvent += () =>
                    {
                        // and simple return
                        _application.Window.Transition(feedSurface).CarouselBack();
                    };
                };

                return(cellPanel);
            };
        }