private IncrementalObservableCollection <T> CreateLastIncrementalCollection <T>( Func <PageResponse <T> > getPageResponse, Action <PageResponse <T> > setPageResponse, Func <int, Task <PageResponse <T> > > searchFunc) where T : new() { var collection = new IncrementalObservableCollection <T> { HasMoreItemsFunc = () => { var page = getPageResponse(); if (page != null) { return(page.Page < page.TotalPages); } return(false); } }; collection.LoadMoreItemsFunc = count => { Func <Task <LoadMoreItemsResult> > taskFunc = async() => { try { IsLoading = true; var pageResp = getPageResponse(); var resp = await searchFunc(pageResp.Page + 1); foreach (var item in resp.Content) { collection.Add(item); } IsLoading = false; setPageResponse(resp); return(new LoadMoreItemsResult { Count = (uint)resp.Content.Count }); } catch { IsLoading = false; setPageResponse(null); CurtainPrompt.ShowError("GenericLoadingMoreError".FromLanguageResource()); return(new LoadMoreItemsResult { Count = 0 }); } }; var loadMorePostsTask = taskFunc(); return(loadMorePostsTask.AsAsyncOperation()); }; return(collection); }
public static IncrementalObservableCollection <WebSong> ToWebSong(this IncrementalObservableCollection <LastTrack> tracks) { IncrementalObservableCollection <WebSong> _songs = new IncrementalObservableCollection <WebSong>(); foreach (var track in tracks) { _songs.Add(WebSongConverter.CreateSong(track)); } return(_songs); }
private IncrementalObservableCollection <T> CreateIncrementalCollection <T>( Func <PageResponse <T> > getPageResponse, Action <PageResponse <T> > setPageResponse, Func <int, Task <PageResponse <T> > > searchFunc) where T : new() { var collection = new IncrementalObservableCollection <T> { HasMoreItemsFunc = () => getPageResponse().Page < getPageResponse().TotalPages }; collection.LoadMoreItemsFunc = count => { Func <Task <LoadMoreItemsResult> > taskFunc = async() => { try { IsLoading = true; var resp = await searchFunc(getPageResponse().Page + 1); foreach (var item in resp.Content) { collection.Add(item); } IsLoading = false; setPageResponse(resp); return(new LoadMoreItemsResult { Count = (uint)resp.Content.Count }); } catch { IsLoading = false; setPageResponse(null); CurtainToast.ShowError("Problem loading more items."); return(new LoadMoreItemsResult { Count = 0 }); } }; var loadMorePostsTask = taskFunc(); return(loadMorePostsTask.AsAsyncOperation()); }; return(collection); }
public IncrementalObservableCollection <Base> CreateLastIncrementalCollection <T>( string type, Func <PageResponse <T> > getPageResponse, Action <PageResponse <T> > setPageResponse, Func <int, Task <PageResponse <T> > > searchFunc) where T : new() { var collection = new IncrementalObservableCollection <Base> { HasMoreItemsFunc = () => { var page = getPageResponse(); if (page != null) { return(page.Page < page.TotalPages); } return(false); } }; collection.LoadMoreItemsFunc = count => { Func <Task <LoadMoreItemsResult> > taskFunc = async() => { try { if (Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Mobile") { if (type.Equals("songs") && Tracks != null && Tracks.Count >= 70) { IsLoading = false; setPageResponse(null); return(new LoadMoreItemsResult { Count = 0 }); } if (type.Equals("albums") && Albums != null && Albums.Count >= 20) { IsLoading = false; setPageResponse(null); return(new LoadMoreItemsResult { Count = 0 }); } } else { if (type.Equals("songs") && Tracks != null && Tracks.Count >= 120) { IsLoading = false; setPageResponse(null); return(new LoadMoreItemsResult { Count = 0 }); } if (type.Equals("albums") && Albums != null && Albums.Count >= 40) { IsLoading = false; setPageResponse(null); return(new LoadMoreItemsResult { Count = 0 }); } } if (type.Equals("artists") && Artists != null && Artists.Count >= 1) { IsLoading = false; setPageResponse(null); return(new LoadMoreItemsResult { Count = 0 }); } IsLoading = true; var itemsAdded = 0; var pageResp = getPageResponse(); var resp = await searchFunc(pageResp.Page + 1); if (resp != null) { foreach (var item in resp.Content) { if (item is LastAlbum) { collection.Add(Data.Model.WebSongConverter.CreateAlbum(item as LastAlbum)); } else if (item is LastTrack) { collection.Add(Data.Model.WebSongConverter.CreateSong(item as LastTrack)); } } setPageResponse(resp); itemsAdded = resp.Content.Count; } else { setPageResponse(null); } IsLoading = false; return(new LoadMoreItemsResult { Count = (uint)itemsAdded }); } catch { IsLoading = false; setPageResponse(null); ToastManager.ShowError("Error in loading more results."); return(new LoadMoreItemsResult { Count = 0 }); } }; var loadMorePostsTask = taskFunc(); return(loadMorePostsTask.AsAsyncOperation()); }; return(collection); }
private IncrementalObservableCollection <Base> CreateIncrementalCollection <T>( string type, Func <DeezerPageResponse <T> > getPageResponse, Action <DeezerPageResponse <T> > setPageResponse, Func <int, Task <DeezerPageResponse <T> > > searchFunc) where T : new() { var collection = new IncrementalObservableCollection <Base> { HasMoreItemsFunc = () => { var page = getPageResponse(); if (page != null) { return(!string.IsNullOrEmpty(page.next)); } return(false); } }; collection.LoadMoreItemsFunc = count => { Func <Task <LoadMoreItemsResult> > taskFunc = async() => { try { if (Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Mobile") { if (type.Equals("songs") && Tracks != null && Tracks.Count >= 70) { IsLoading = false; setPageResponse(null); return(new LoadMoreItemsResult { Count = 0 }); } if (type.Equals("albums") && Albums != null && Albums.Count >= 20) { IsLoading = false; setPageResponse(null); return(new LoadMoreItemsResult { Count = 0 }); } } else { if (type.Equals("songs") && Tracks != null && Tracks.Count >= 120) { IsLoading = false; setPageResponse(null); return(new LoadMoreItemsResult { Count = 0 }); } if (type.Equals("albums") && Albums != null && Albums.Count >= 40) { IsLoading = false; setPageResponse(null); return(new LoadMoreItemsResult { Count = 0 }); } } if (type.Equals("artists") && Artists != null && Artists.Count >= 1) { IsLoading = false; setPageResponse(null); return(new LoadMoreItemsResult { Count = 0 }); } IsLoading = true; var itemsAdded = 0; var pageResp = getPageResponse(); var next = pageResp.next.Split('?')[1]?.Split('='); int page = int.Parse(next[next.Length - 1]); var resp = await searchFunc(page); if (resp != null) { foreach (var item in resp.data) { if (item is DeezerAlbum) { collection.Add(Data.Model.WebSongConverter.CreateDeezerAlbum(item as DeezerAlbum)); } else if (item is DeezerSong) { collection.Add(Data.Model.WebSongConverter.CreateDeezerSong(item as DeezerSong)); } } setPageResponse(resp); itemsAdded = resp.data.Count; } else { setPageResponse(null); } IsLoading = false; return(new LoadMoreItemsResult { Count = (uint)itemsAdded }); } catch { IsLoading = false; setPageResponse(null); ToastManager.ShowError("Error in loading more results."); return(new LoadMoreItemsResult { Count = 0 }); } }; var loadMorePostsTask = taskFunc(); return(loadMorePostsTask.AsAsyncOperation()); }; return(collection); }
public async Task SearchAsync(string term) { if (string.IsNullOrEmpty(term)) { ToastManager.ShowError("Please enter something."); return; } SetMessage(Core.StringMessage.LoadingPleaseWait); if (!App.Locator.Network.IsActive) { SetMessage(Core.StringMessage.NoInternetConnection); return; } foundInViez = false; IsLoading = true; try { ClearSearchViewModelData(); var tasks = new List <Task> { Task.Run( async() => { _tracksResponse = await _service.SearchTracksAsync(term, limit: 20); if (_tracksResponse != null) { await DispatcherHelper.RunAsync( () => { int indexToAdd = -1; Tracks = CreateLastIncrementalCollection( "songs", () => _tracksResponse, tracks => _tracksResponse = tracks, async i => await _service.SearchTracksAsync(term, page: i, limit: 20)); foreach (var addedOTrack in _tracksResponse.Content) { indexToAdd++; Tracks.Add(Data.Model.WebSongConverter.CreateSong(addedOTrack)); if (indexToAdd == 2 || indexToAdd == 10 || indexToAdd == 22 || indexToAdd == 34 || indexToAdd == 49 || indexToAdd == 63 || indexToAdd == 78 || indexToAdd == 88 || indexToAdd == 99) { Tracks.Insert(indexToAdd, new ListAdvert()); } } }); } }), Task.Run( async() => { _albumsResponse = await _service.SearchAlbumsAsync(term, limit: 20); if (_albumsResponse != null) { await DispatcherHelper.RunAsync( () => { int indexToAdd = -1; Albums = CreateLastIncrementalCollection( "albums", () => _albumsResponse, albums => _albumsResponse = albums, async i => await _service.SearchAlbumsAsync(term, page: i, limit: 20)); foreach (var addedOAlbum in _albumsResponse?.Content) { if (!string.IsNullOrEmpty(addedOAlbum.Mbid) || !string.IsNullOrEmpty(addedOAlbum.Id)) { indexToAdd++; Albums.Add(Data.Model.WebSongConverter.CreateAlbum(addedOAlbum)); if (indexToAdd == 2 || indexToAdd == 10 || indexToAdd == 22 || indexToAdd == 34 || indexToAdd == 49 || indexToAdd == 63 || indexToAdd == 78 || indexToAdd == 88 || indexToAdd == 99) { Albums.Insert(indexToAdd, new Advert()); } } } }); } }), Task.Run( async() => { _artistsResponse = await _service.SearchArtistsAsync(term, limit: 1); if (_artistsResponse != null && _artistsResponse.Content.Count > 0) { await DispatcherHelper.RunAsync(() => Artists = new List <LastArtist>(_artistsResponse.Content)); } }), Task.Run( async() => { try { await Task.Factory.StartNew(async() => { await DispatcherHelper.RunAsync(async() => { if (App.Locator.Setting.VideoOnOff) { VideoMessage = Core.StringMessage.LoadingPleaseWait; OnlineVideos = await _misc.GetSearchedVideos(20, term); if (OnlineVideos != null && OnlineVideos.CountOrDefault() > 0) { VideoMessage = Core.StringMessage.NoMessge; } else { VideoMessage = Core.StringMessage.EmptyMessage; } } else { VideoMessage = Core.StringMessage.NoMessge; } }); }); } catch { VideoMessage = Core.StringMessage.SomethinWentWrong; } }) }; await Task.WhenAll(tasks); } catch { SetMessage(Core.StringMessage.SomethinWentWrong); } if (Tracks == null || Tracks.Count < 1) { await FindOnViez(term); } else { TrackMessage = (Tracks != null && Tracks.Count > 0) ? Core.StringMessage.NoMessge : Core.StringMessage.EmptyMessage; } if ((Albums != null && Albums.Count > 0) && (Artists != null && Artists.Count > 0)) { ArtistHeader = true; } else { ArtistHeader = false; } AlbumArtistMessage = ((Albums != null && Albums.Count > 0) || (Artists != null && Artists.Count > 0)) ? Core.StringMessage.NoMessge : Core.StringMessage.EmptyMessage; IsLoading = false; }
private IncrementalObservableCollection <T> CreateIncrementalCollection <T>( Func <Paging <T> > getPageResponse, Action <Paging <T> > setPageResponse, Func <int, Task <Paging <T> > > searchFunc) where T : new() { var collection = new IncrementalObservableCollection <T> { HasMoreItemsFunc = () => { var page = getPageResponse(); if (page != null) { return(!string.IsNullOrEmpty(page.Next)); } return(false); } }; collection.LoadMoreItemsFunc = count => { Func <Task <LoadMoreItemsResult> > taskFunc = async() => { try { IsLoading = true; var itemsAdded = 0; var pageResp = getPageResponse(); var resp = await searchFunc(pageResp.Offset + pageResp.Limit); if (resp != null) { foreach (var item in resp.Items) { collection.Add(item); } setPageResponse(resp); itemsAdded = resp.Items.Count; } else { setPageResponse(null); } IsLoading = false; return(new LoadMoreItemsResult { Count = (uint)itemsAdded }); } catch { IsLoading = false; setPageResponse(null); CurtainPrompt.ShowError("GenericLoadingMoreError".FromLanguageResource()); return(new LoadMoreItemsResult { Count = 0 }); } }; var loadMorePostsTask = taskFunc(); return(loadMorePostsTask.AsAsyncOperation()); }; return(collection); }
public static IDisposable ConnectTo <T, TU, TV>(IncrementalObservableCollection <T> collection, TU connectTarget, Expression <Func <TU, ObservableCollection <TV> > > expr, Func <TV, T> converter) where T : class where TU : class, ISupportIncrementalLoading { collection.SupportIncrementalLoading = connectTarget; var obsCollection = expr.Compile().Invoke(connectTarget); var disposable = new CompositeDisposable { obsCollection.ObserveAddChanged().ObserveOnUIDispatcher().Subscribe(w => collection.Add(converter(w))), obsCollection.ObserveRemoveChanged().Subscribe(w => collection.Remove(converter(w))), obsCollection.ObserveResetChanged().Subscribe(w => collection.Clear()) }; return(disposable); }