コード例 #1
0
        public void Populate(List <object> items)
        {
            /*
             * This caching logic reduces average pagination speed from ~450 to ~350
             * (Tested on Android, Samsung Galaxy Tab S2)
             * Not as much as I'd hoped for, but it's something.
             *
             * Updating layout in itself takes 100ms.
             * A view's construction takes ~10ms, fetching it from cache takes ~1ms
             *
             * Using the same HubClient.PageSize (25) views each time, just changing the content,
             * not even updating layout, and then hiding some if it's the final page
             * would somewhat improve performance, but is rather unnecessary
             *
             * Currenty disabled cache, as it caused click event anomalies,
             * to re-enable, simply add items to cache after adding the views,
             * and when constructing a FileListPopupItem, check cache.Count > 0
             */

            Children.Clear();

            System.Threading.Tasks.Task.Run(delegate
            {
                foreach (object item in items)
                {
                    FileListPopupItem
                    view = new FileListPopupItem();

                    if (item is DriveFile)
                    {
                        view.DriveFile = (DriveFile)item;
                    }
                    else
                    {
                        view.GithubFile = (GithubFile)item;
                    }

                    view.Initialize();

                    view.Click -= OnItemClick;
                    view.Click += OnItemClick;

                    Device.BeginInvokeOnMainThread(delegate
                    {
                        AddSubview(view);
                    });
                }

                Device.BeginInvokeOnMainThread(delegate
                {
                    LayoutSubviews();
                });
            });
        }
コード例 #2
0
        async void OnItemClicked(object sender, EventArgs e)
        {
            FileListPopupItem item = (FileListPopupItem)sender;

            if (!item.IsEnabled)
            {
                Alert("This style is not publicly available and cannot be downloaded");
                return;
            }

            Device.BeginInvokeOnMainThread(delegate
            {
                if (item.GithubFile == null)
                {
                    // Do not Hide the popup when dealing with github,
                    // as a new page should load almost immediately
                    // and we need to show content there as well
                    ContentView.FileList.Hide();
                }

                ContentView.ShowLoading();
            });

            if (item.DriveFile != null)
            {
#if __ANDROID__
                /*
                 * Android uses a full-fledged Google Drive component.
                 * No need to manually handle clicks -> Goes straight to FileDownloadComplete()
                 */
#elif __IOS__
                DriveClientiOS.Instance.DownloadStyle(item.DriveFile.Id, item.DriveFile.Name);
#elif __UWP__
                Stream stream = await DriveClientUWP.Instance.DownloadStyle(item.DriveFile.Id, item.DriveFile.Name);

                if (stream == null)
                {
                    Device.BeginInvokeOnMainThread(delegate
                    {
                        ContentView.Popup.Show();
                        ContentView.HideLoading();
                        item.Disable();
                        Alert("This style is not publicly available and cannot be downloaded");
                    });
                }
                else
                {
                    OnFileDownloadComplete(null, new DownloadEventArgs {
                        Name = item.DriveFile.Name, Stream = stream
                    });
                }
#endif
            }
            else if (item.GithubFile != null)
            {
                /*
                 * Ignore file clicks
                 */
                if (item.GithubFile.IsDirectory)
                {
                    if (item.GithubFile.IsRepository)
                    {
                        GithubOwner = item.GithubFile.Owner;
                        GithubRepo  = item.GithubFile.Name;
                        GithubId    = item.GithubFile.Id;

                        ContentView.FileList.Pages.Hide();

                        ContentView.FileList.Branches.IsVisible = true;
                    }
                    else
                    {
                        ContentView.FileList.Branches.IsVisible = false;
                    }

                    GithubPath = item.GithubFile.Path;
                    await LoadGithubContents();

                    if (item.GithubFile.IsRepository)
                    {
                        var branches = await HubClient.Instance.GetBranches(GithubOwner, GithubRepo);

                        ContentView.FileList.Branches.Add(branches);

                        ContentView.FileList.Branches.Highlight(CurrentBranch);
                    }
                }
            }
        }