<commit_before>﻿using System;
using MusicPlayer.Api;
using MediaPlayer;
using Foundation;
using System.Linq;
using System.Threading.Tasks;
using MusicPlayer.Models;
using MusicPlayer.Managers;
using System.Collections.Generic;
using MusicPlayer.Data;

namespace MusicPlayer.Api.iPodApi
{
	public class iPodProvider : MusicProvider
	{

		public override MediaProviderCapabilities[] Capabilities {
			get {
				return new[]{ MediaProviderCapabilities.None};
			}
		}
		public override ServiceType ServiceType  => ServiceType.iPod;

		public override string Id  => "iPod";
		public override bool RequiresAuthentication => false;

		public iPodProvider() : base(null)
		{
		}
		public override string Email
		{
			get
			{
				return  null;
			}
		}

		#region implemented abstract members of MusicProvider
		public bool Disabled => !Settings.IncludeIpod;
		protected override async System.Threading.Tasks.Task<bool> Sync()
		{
			if(Disabled)
				return true;
			return await Task.Run(async ()=>{
				try{
					await Database.Main.ExecuteAsync("update Track set Deleted = 1 where ServiceId = ?", Id);
					var mediaQuery = MPMediaQuery.SongsQuery;
					var predicate = MPMediaPropertyPredicate.PredicateWithValue(NSNumber.FromBoolean(false),MPMediaItem.IsCloudItemProperty);
					mediaQuery.AddFilterPredicate(predicate);
					var items = mediaQuery.Items.Where(x=> x.AssetURL != null && !string.IsNullOrEmpty(x.AssetURL.AbsoluteString)).Select(x => new FullTrackData(x.Title,x.Artist,x.AlbumArtist,x.AlbumTitle,x.Genre) {
						Id = x.PersistentID.ToString(),
						AlbumServerId = x.AlbumPersistentID.ToString(),
						Disc = x.DiscNumber,
						Duration = x.PlaybackDuration,
						FileExtension = "mp3",
						MediaType = MediaType.Audio,
						Priority = 1,
						ServiceId = Id,
						ServiceType = ServiceType,
						Track = x.AlbumTrackNumber,
		//				Year = x.ReleaseDate
					});
					await items.BatchForeach(100, (batch) => MusicProvider.ProcessTracks(batch.ToList()));
					await FinalizeProcessing(Id);
					return true;
				}
				catch(Exception ex)
				{
					LogManager.Shared.Report(ex);
					return false;
				}<commit_msg>[iOS] Handle the case when `mediaQuery.Items` is null (#6)<commit_after>﻿using System;
using MusicPlayer.Api;
using MediaPlayer;
using Foundation;
using System.Linq;
using System.Threading.Tasks;
using MusicPlayer.Models;
using MusicPlayer.Managers;
using System.Collections.Generic;
using MusicPlayer.Data;

namespace MusicPlayer.Api.iPodApi
{
	public class iPodProvider : MusicProvider
	{

		public override MediaProviderCapabilities[] Capabilities {
			get {
				return new[]{ MediaProviderCapabilities.None};
			}
		}
		public override ServiceType ServiceType  => ServiceType.iPod;

		public override string Id  => "iPod";
		public override bool RequiresAuthentication => false;

		public iPodProvider() : base(null)
		{
		}
		public override string Email
		{
			get
			{
				return  null;
			}
		}

		#region implemented abstract members of MusicProvider
		public bool Disabled => !Settings.IncludeIpod;
		protected override async System.Threading.Tasks.Task<bool> Sync()
		{
			if(Disabled)
				return true;
			return await Task.Run(async ()=>{
				try{
					await Database.Main.ExecuteAsync("update Track set Deleted = 1 where ServiceId = ?", Id);
					var mediaQuery = MPMediaQuery.SongsQuery;
					var predicate = MPMediaPropertyPredicate.PredicateWithValue(NSNumber.FromBoolean(false),MPMediaItem.IsCloudItemProperty);
					mediaQuery.AddFilterPredicate(predicate);
					if (mediaQuery.Items == null)
						return true;

					var items = mediaQuery.Items.Where(x=> x.AssetURL != null && !string.IsNullOrEmpty(x.AssetURL.AbsoluteString)).Select(x => new FullTrackData(x.Title,x.Artist,x.AlbumArtist,x.AlbumTitle,x.Genre) {
						Id = x.PersistentID.ToString(),
						AlbumServerId = x.AlbumPersistentID.ToString(),
						Disc = x.DiscNumber,
						Duration = x.PlaybackDuration,
						FileExtension = "mp3",
						MediaType = MediaType.Audio,
						Priority = 1,
						ServiceId = Id,
						ServiceType = ServiceType,
						Track = x.AlbumTrackNumber,
		//				Year = x.ReleaseDate
					});
					await items.BatchForeach(100, (batch) => MusicProvider.ProcessTracks(batch.ToList()));
					await FinalizeProcessing(Id);
					return true;
				}
				catch(Exception ex)
				{
					LogManager.Shared.Report(ex);
					return false;
				}
{'fraction_non_alphanumeric': 0.10461397898583828, 'fraction_numerical': 0.0029693924166285975, 'mean_word_length': 3.6934619506966775, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5908426', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1296, 'n_words': 293}