public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = inflater.Inflate(Resource.Layout.activity_recommendations, container, false);

            var list     = view.FindViewById <RecyclerView>(Resource.Id.list_recommendations);
            var progress = view.FindViewById <ProgressBar>(Resource.Id.progressBar_recommendations);

            adapter            = new RecommendationAdapter(new List <IBlock>(), this);
            adapter.ItemClick += AdapterOnItemClick;
            list.Clickable     = true;
            //progress.Visibility = ViewStates.Invisible;
            Handler handler         = new Handler(Looper.MainLooper);
            var     scroll_listener = new Listeners.OnScrollToBottomListener(() =>
            {
                if (!hasLoading)
                {
                    return;
                }

                if (recom_blocks?.Count > 0)
                {
                    Task.Run(() =>
                    {
                        System.Threading.Thread.Sleep(300);


                        var buffer = new List <IBlock>();
                        buffer.Add(recom_blocks.First());
                        adapter.AddBlocks(buffer);
                        handler.Post(new Runnable(() =>
                        {
                            adapter.NotifyItemRangeChanged(adapter.ItemCount - 1, 1);
                        }));
                    }).ContinueWith((t) =>
                    {
                        recom_blocks.Remove(recom_blocks.First());
                        progress.Visibility = ViewStates.Invisible;
                    });
                }
                else
                {
                    hasLoading = false;
                    return;
                }
            });

            List <IBlock> recs = new List <IBlock>();

            Task.Run(() =>
            {
                recs = Recommendations.NewSync().Blocks;
            }).ContinueWith((t) =>
            {
                recom_blocks.AddRange(recs);
                scroll_listener.InvokeCallback();
            });

            list.AddOnScrollListener(scroll_listener);
            list.SetAdapter(adapter);
            list.SetLayoutManager(new LinearLayoutManager(Application.Context, LinearLayoutManager.Vertical, false));
            return(view);
        }
Exemplo n.º 2
0
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = inflater.Inflate(Resource.Layout.activity_tracks, container, false);

            List <AudioFile> tracks = new List <AudioFile>();

            Tracks  = tracks;
            adapter = new TrackAdapter(tracks);

            List <PlaylistFile> plists = new List <PlaylistFile>();

            Playlists = plists;
            pAdapter  = new PlaylistAdapter(plists);

            var tracksView  = view.FindViewById <RecyclerView>(Resource.Id.list_tracks);
            var progressBar = view.FindViewById <ProgressBar>(Resource.Id.progressBar_tracks);

            var playlistsView        = view.FindViewById <RecyclerView>(Resource.Id.list_plists);
            var playlistsProgressBar = view.FindViewById <ProgressBar>(Resource.Id.progressBar_library_plists);

            Handler handler = new Handler(Looper.MainLooper);

            adapter.ItemClick  += AdapterOnItemClick;
            pAdapter.ItemClick += PlaylistOnItemClick;

            tracksView.SetAdapter(adapter);
            tracksView.SetLayoutManager(new LinearLayoutManager(Application.Context, LinearLayoutManager.Vertical, false));
            RegisterForContextMenu(tracksView);
            tracksView.Clickable = true;

            playlistsView.SetAdapter(pAdapter);
            playlistsView.SetLayoutManager(new LinearLayoutManager(Application.Context, LinearLayoutManager.Horizontal, false));
            playlistsView.Clickable = true;

            var scrollListener = new Listeners.OnScrollToBottomListener(() =>
            {
                //if (!HasLoading) return;
                var task = Task.Run(() =>
                {
                    handler.Post(new Runnable(() =>
                    {
                        progressBar.Visibility = ViewStates.Visible;
                    }));
                    try
                    {
                        tracks = MusicService.GetMusicLibrary(15, adapter.ItemCount);
                        var i  = 1 + 1; //Без этого нихуя не работает.
                        Fooxboy.MusicX.Core.Log.Debug(i.ToString());
                    }
                    catch (Exception e)
                    {
                        Toast.MakeText(Application.Context, $"Произошла ошибка: {e.ToString()}", ToastLength.Long).Show();
                    }
                });

                bool end = false;
                task.ContinueWith((t) =>
                {
                    while (tracks.Count == 0)
                    {
                        System.Threading.Thread.Sleep(300);
                    }

                    HasLoading = !(tracks.Count < 15);
                    handler.Post(new Runnable(() =>
                    {
                        var count = adapter.ItemCount;
                        adapter.AddItems(tracks);
                        TracksInLibrary.AddRange(tracks);
                        adapter.NotifyItemRangeChanged(count, tracks.Count);
                        progressBar.Visibility = ViewStates.Invisible;
                        end = true;
                    }));
                });
                var a = task.ConfigureAwait(false);
                while (!end)
                {
                    System.Threading.Thread.Sleep(300);
                }
            });

            tracksView.AddOnScrollListener(scrollListener);


            var plistsScrollListener = new Listeners.OnScrollToBottomListener(() =>
            {
                if (!HasLoading)
                {
                    return;
                }
                var task = Task.Run(() =>
                {
                    handler.Post(new Runnable(() =>
                    {
                        playlistsProgressBar.Visibility = ViewStates.Visible;
                    }));
                    try
                    {
                        //tracks = MusicService.GetMusicLibrary(15, adapter.ItemCount);
                        var vkplists = Core.VKontakte.Music.Library.PlaylistsSync(15, pAdapter.ItemCount);
                        plists       = PlaylistsService.CovertToPlaylistFiles(vkplists);
                        var i        = 1 + 1; //Без этого нихуя не работает.
                        Fooxboy.MusicX.Core.Log.Debug(i.ToString());
                    }
                    catch (Exception e)
                    {
                        Toast.MakeText(Application.Context, $"Произошла ошибка: {e.ToString()}", ToastLength.Long).Show();
                    }
                });

                bool end = false;
                task.ContinueWith((t) =>
                {
                    while (plists.Count == 0)
                    {
                        System.Threading.Thread.Sleep(300);
                    }

                    HasLoading = !(plists.Count < 15);
                    handler.Post(new Runnable(() =>
                    {
                        var count = pAdapter.ItemCount;
                        pAdapter.AddItems(plists);
                        PlaylistsInLibrary.AddRange(plists);
                        pAdapter.NotifyItemRangeChanged(count, plists.Count);
                        playlistsProgressBar.Visibility = ViewStates.Invisible;
                        end = true;
                    }));
                });
                var a = task.ConfigureAwait(false);
                while (!end)
                {
                    System.Threading.Thread.Sleep(300);
                }
            });

            playlistsView.AddOnScrollListener(plistsScrollListener);

            if (pAdapter.ItemCount == 0)
            {
                plistsScrollListener.InvokeCallback();
            }
            if (adapter.ItemCount == 0)
            {
                scrollListener.InvokeCallback();
            }

            return(view);
        }