public async Task<QueryResult<LiveTvChannel>> GetInternalChannels(LiveTvChannelQuery query, CancellationToken cancellationToken) { var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(query.UserId); var channels = _channelIdList.Select(_libraryManager.GetItemById) .Where(i => i != null) .OfType<LiveTvChannel>(); if (user != null) { // Avoid implicitly captured closure var currentUser = user; channels = channels .Where(i => i.IsParentalAllowed(currentUser)) .OrderBy(i => { double number = 0; if (!string.IsNullOrEmpty(i.Number)) { double.TryParse(i.Number, out number); } return number; }); if (query.IsFavorite.HasValue) { var val = query.IsFavorite.Value; channels = channels .Where(i => _userDataManager.GetUserData(user.Id, i.GetUserDataKey()).IsFavorite == val); } if (query.IsLiked.HasValue) { var val = query.IsLiked.Value; channels = channels .Where(i => { var likes = _userDataManager.GetUserData(user.Id, i.GetUserDataKey()).Likes; return likes.HasValue && likes.Value == val; }); } if (query.IsDisliked.HasValue) { var val = query.IsDisliked.Value; channels = channels .Where(i => { var likes = _userDataManager.GetUserData(user.Id, i.GetUserDataKey()).Likes; return likes.HasValue && likes.Value != val; }); } } var enableFavoriteSorting = query.EnableFavoriteSorting; channels = channels.OrderBy(i => { if (enableFavoriteSorting) { var userData = _userDataManager.GetUserData(user.Id, i.GetUserDataKey()); if (userData.IsFavorite) { return 0; } if (userData.Likes.HasValue) { if (!userData.Likes.Value) { return 3; } return 1; } } return 2; }); var allChannels = channels.ToList(); IEnumerable<LiveTvChannel> allEnumerable = allChannels; if (query.StartIndex.HasValue) { allEnumerable = allEnumerable.Skip(query.StartIndex.Value); } if (query.Limit.HasValue) { allEnumerable = allEnumerable.Take(query.Limit.Value); } var result = new QueryResult<LiveTvChannel> { Items = allEnumerable.ToArray(), TotalRecordCount = allChannels.Count }; return result; }
public async Task<QueryResult<ChannelInfoDto>> GetChannels(LiveTvChannelQuery query, CancellationToken cancellationToken) { var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(query.UserId); var internalResult = await GetInternalChannels(query, cancellationToken).ConfigureAwait(false); var returnList = new List<ChannelInfoDto>(); foreach (var channel in internalResult.Items) { var currentProgram = await GetCurrentProgram(channel.ExternalId, cancellationToken).ConfigureAwait(false); returnList.Add(_tvDtoService.GetChannelInfoDto(channel, currentProgram, user)); } var result = new QueryResult<ChannelInfoDto> { Items = returnList.ToArray(), TotalRecordCount = internalResult.TotalRecordCount }; return result; }
public async Task<QueryResult<ChannelInfoDto>> GetChannels(LiveTvChannelQuery query, DtoOptions options, CancellationToken cancellationToken) { var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(query.UserId); var internalResult = await GetInternalChannels(query, cancellationToken).ConfigureAwait(false); var returnList = new List<ChannelInfoDto>(); var now = DateTime.UtcNow; var programs = query.AddCurrentProgram ? _libraryManager.QueryItems(new InternalItemsQuery { IncludeItemTypes = new[] { typeof(LiveTvProgram).Name }, MaxStartDate = now, MinEndDate = now, ChannelIds = internalResult.Items.Select(i => i.Id.ToString("N")).ToArray() }).Items.Cast<LiveTvProgram>().OrderBy(i => i.StartDate).ToList() : new List<LiveTvProgram>(); foreach (var channel in internalResult.Items) { var channelIdString = channel.Id.ToString("N"); var currentProgram = programs.FirstOrDefault(i => string.Equals(i.ChannelId, channelIdString, StringComparison.OrdinalIgnoreCase)); returnList.Add(_tvDtoService.GetChannelInfoDto(channel, options, currentProgram, user)); } var result = new QueryResult<ChannelInfoDto> { Items = returnList.ToArray(), TotalRecordCount = internalResult.TotalRecordCount }; return result; }
public async Task<QueryResult<LiveTvChannel>> GetInternalChannels(LiveTvChannelQuery query, CancellationToken cancellationToken) { var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(query.UserId); var topFolder = await GetInternalLiveTvFolder(cancellationToken).ConfigureAwait(false); var internalQuery = new InternalItemsQuery(user) { IsMovie = query.IsMovie, IsNews = query.IsNews, IsKids = query.IsKids, IsSports = query.IsSports, IsSeries = query.IsSeries, IncludeItemTypes = new[] { typeof(LiveTvChannel).Name }, SortOrder = query.SortOrder ?? SortOrder.Ascending, TopParentIds = new[] { topFolder.Id.ToString("N") }, IsFavorite = query.IsFavorite, IsLiked = query.IsLiked, StartIndex = query.StartIndex, Limit = query.Limit }; internalQuery.OrderBy.AddRange(query.SortBy.Select(i => new Tuple<string, SortOrder>(i, query.SortOrder ?? SortOrder.Ascending))); if (query.EnableFavoriteSorting) { internalQuery.OrderBy.Insert(0, new Tuple<string, SortOrder>(ItemSortBy.IsFavoriteOrLiked, SortOrder.Descending)); } if (!internalQuery.OrderBy.Any(i => string.Equals(i.Item1, ItemSortBy.SortName, StringComparison.OrdinalIgnoreCase))) { internalQuery.OrderBy.Add(new Tuple<string, SortOrder>(ItemSortBy.SortName, SortOrder.Ascending)); } var channelResult = _libraryManager.GetItemsResult(internalQuery); var result = new QueryResult<LiveTvChannel> { Items = channelResult.Items.Cast<LiveTvChannel>().ToArray(), TotalRecordCount = channelResult.TotalRecordCount }; return result; }
private async Task GetChannels(bool isRefresh) { if (!NavigationService.IsNetworkAvailable || (_channelsLoaded && !isRefresh)) { return; } try { SetProgressBar(AppResources.SysTrayGettingChannels); var query = new LiveTvChannelQuery { ChannelType = ChannelType.TV, UserId = AuthenticationService.Current.LoggedInUserId }; var items = await ApiClient.GetLiveTvChannelsAsync(query); if (items != null && !items.Items.IsNullOrEmpty()) { Channels = items.Items.ToList(); await GroupChannels(); } _channelsLoaded = true; } catch (HttpException ex) { Utils.HandleHttpException(ex, "GetChannels()", NavigationService, Log); } SetProgressBar(); }
public async Task<QueryResult<LiveTvChannel>> GetInternalChannels(LiveTvChannelQuery query, CancellationToken cancellationToken) { var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(query.UserId); var topFolder = await GetInternalLiveTvFolder(cancellationToken).ConfigureAwait(false); var channels = _libraryManager.GetItemList(new InternalItemsQuery { IncludeItemTypes = new[] { typeof(LiveTvChannel).Name }, SortBy = new[] { ItemSortBy.SortName }, TopParentIds = new[] { topFolder.Id.ToString("N") } }).Cast<LiveTvChannel>(); if (user != null) { // Avoid implicitly captured closure var currentUser = user; channels = channels .Where(i => i.IsVisible(currentUser)) .OrderBy(i => { double number = 0; if (!string.IsNullOrEmpty(i.Number)) { double.TryParse(i.Number, out number); } return number; }); if (query.IsFavorite.HasValue) { var val = query.IsFavorite.Value; channels = channels .Where(i => _userDataManager.GetUserData(user, i).IsFavorite == val); } if (query.IsLiked.HasValue) { var val = query.IsLiked.Value; channels = channels .Where(i => { var likes = _userDataManager.GetUserData(user, i).Likes; return likes.HasValue && likes.Value == val; }); } if (query.IsDisliked.HasValue) { var val = query.IsDisliked.Value; channels = channels .Where(i => { var likes = _userDataManager.GetUserData(user, i).Likes; return likes.HasValue && likes.Value != val; }); } } var enableFavoriteSorting = query.EnableFavoriteSorting; channels = channels.OrderBy(i => { if (enableFavoriteSorting) { var userData = _userDataManager.GetUserData(user, i); if (userData.IsFavorite) { return 0; } if (userData.Likes.HasValue) { if (!userData.Likes.Value) { return 3; } return 1; } } return 2; }); var allChannels = channels.ToList(); IEnumerable<LiveTvChannel> allEnumerable = allChannels; if (query.StartIndex.HasValue) { allEnumerable = allEnumerable.Skip(query.StartIndex.Value); } if (query.Limit.HasValue) { allEnumerable = allEnumerable.Take(query.Limit.Value); } var result = new QueryResult<LiveTvChannel> { Items = allEnumerable.ToArray(), TotalRecordCount = allChannels.Count }; return result; }
public async Task<QueryResult<ChannelInfoDto>> GetChannels(LiveTvChannelQuery query, CancellationToken cancellationToken) { var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(new Guid(query.UserId)); var channels = _channelIdList.Select(_libraryManager.GetItemById) .Where(i => i != null) .OfType<LiveTvChannel>(); if (user != null) { // Avoid implicitly captured closure var currentUser = user; channels = channels .Where(i => i.IsParentalAllowed(currentUser)) .OrderBy(i => { double number = 0; if (!string.IsNullOrEmpty(i.Number)) { double.TryParse(i.Number, out number); } return number; }); if (query.IsFavorite.HasValue) { var val = query.IsFavorite.Value; channels = channels .Where(i => _userDataManager.GetUserData(user.Id, i.GetUserDataKey()).IsFavorite == val); } if (query.IsLiked.HasValue) { var val = query.IsLiked.Value; channels = channels .Where(i => { var likes = _userDataManager.GetUserData(user.Id, i.GetUserDataKey()).Likes; return likes.HasValue && likes.Value == val; }); } if (query.IsDisliked.HasValue) { var val = query.IsDisliked.Value; channels = channels .Where(i => { var likes = _userDataManager.GetUserData(user.Id, i.GetUserDataKey()).Likes; return likes.HasValue && likes.Value != val; }); } } channels = channels.OrderBy(i => { double number = 0; if (!string.IsNullOrEmpty(i.Number)) { double.TryParse(i.Number, out number); } return number; }).ThenBy(i => i.Name); var allChannels = channels.ToList(); IEnumerable<LiveTvChannel> allEnumerable = allChannels; if (query.StartIndex.HasValue) { allEnumerable = allEnumerable.Skip(query.StartIndex.Value); } if (query.Limit.HasValue) { allEnumerable = allEnumerable.Take(query.Limit.Value); } var returnList = new List<ChannelInfoDto>(); foreach (var channel in allEnumerable) { var currentProgram = await GetCurrentProgram(channel.ExternalId, cancellationToken).ConfigureAwait(false); returnList.Add(_tvDtoService.GetChannelInfoDto(channel, currentProgram, user)); } var result = new QueryResult<ChannelInfoDto> { Items = returnList.ToArray(), TotalRecordCount = allChannels.Count }; return result; }