<commit_before>﻿using System;
using System.Linq;
using Android.Runtime;
using Android.Support.V4.Media;
using Com.Google.Android.Exoplayer2.Ext.Mediasession;
using Com.Google.Android.Exoplayer2.Source;
using MediaManager.Library;
using MediaManager.Platforms.Android.Media;

namespace MediaManager.Platforms.Android.Queue
{
    public class QueueDataAdapter : Java.Lang.Object, TimelineQueueEditor.IQueueDataAdapter
    {
        protected MediaManagerImplementation MediaManager => CrossMediaManager.Android;
        protected ConcatenatingMediaSource _mediaSource;

        public QueueDataAdapter(ConcatenatingMediaSource mediaSource)
        {
            _mediaSource = mediaSource;
            MediaManager.Queue.MediaItems.CollectionChanged += MediaQueue_CollectionChanged;
        }

        protected QueueDataAdapter(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer)
        {
        }

        public void Add(int index, MediaDescriptionCompat description)
        {
            MediaManager.Queue.Insert(index, description.ToMediaItem());
        }

        public MediaDescriptionCompat GetMediaDescription(int index)
        {
            return MediaManager.Queue.ElementAtOrDefault(index)?.ToMediaDescription();
        }

        public void Move(int oldIndex, int newIndex)
        {
            MediaManager.Queue.Move(oldIndex, newIndex);
        }

        public void Remove(int index)
        {
            MediaManager.Queue.RemoveAt(index);
        }

        private void MediaQueue_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    if (_mediaSource.Size != MediaManager.Queue.Count)
                    {
                        for (int i = e.NewItems.Count - 1; i >= 0; i--)
                        {
                            var mediaItem = (IMediaItem)e.NewItems[i];
                            _mediaSource.AddMediaSource(e.NewStartingIndex, mediaItem.ToMediaSource());
                        }
                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
                    if (e.NewItems.Count > 1)
                    {
                        int oldBeginIndex = e.OldStartingIndex;
                        int oldEndIndex = e.OldStartingIndex + e.NewItems.Count - 1;

                        int newBeginIndex = e.NewStartingIndex;
                        int newEndIndex = e.NewStartingIndex + e.NewItems.Count - 1;

                        //move when new is before old
                        if (newBeginIndex < oldBeginIndex)
                            for (int i = 0; i > e.NewItems.Count; i++)
                                _mediaSource.MoveMediaSource(oldEndIndex, newBeginIndex);

                        //move when new is after old
                        else if (newBeginIndex > oldBeginIndex)
                            for (int i = 0; i > e.NewItems.Count; i++)
                                _mediaSource.MoveMediaSource(oldBeginIndex, newEndIndex);
                    }
                    else
                        _mediaSource.MoveMediaSource(e.OldStartingIndex, e.NewStartingIndex);
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    if (e.NewItems.Count > 1)
                    {
                        for (int i = 0; i > e.NewItems.Count; i++)
                            _mediaSource.RemoveMediaSource(e.OldStartingIndex);
                    }
                    else
                        _mediaSource.RemoveMediaSource(e.OldStartingIndex);
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
                    if (e.NewItems.Count > 1)
                    {
                        for (int i = 0; i > e.NewItems.Count; i++)
                            _mediaSource.RemoveMediaSource(e.OldStartingIndex);
                    }<commit_msg>Fix nullreference exception (e.NewItems is null in case of NotifyCollectionChangedAction.Removed).<commit_after>﻿using System;
using System.Linq;
using Android.Runtime;
using Android.Support.V4.Media;
using Com.Google.Android.Exoplayer2.Ext.Mediasession;
using Com.Google.Android.Exoplayer2.Source;
using MediaManager.Library;
using MediaManager.Platforms.Android.Media;

namespace MediaManager.Platforms.Android.Queue
{
    public class QueueDataAdapter : Java.Lang.Object, TimelineQueueEditor.IQueueDataAdapter
    {
        protected MediaManagerImplementation MediaManager => CrossMediaManager.Android;
        protected ConcatenatingMediaSource _mediaSource;

        public QueueDataAdapter(ConcatenatingMediaSource mediaSource)
        {
            _mediaSource = mediaSource;
            MediaManager.Queue.MediaItems.CollectionChanged += MediaQueue_CollectionChanged;
        }

        protected QueueDataAdapter(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer)
        {
        }

        public void Add(int index, MediaDescriptionCompat description)
        {
            MediaManager.Queue.Insert(index, description.ToMediaItem());
        }

        public MediaDescriptionCompat GetMediaDescription(int index)
        {
            return MediaManager.Queue.ElementAtOrDefault(index)?.ToMediaDescription();
        }

        public void Move(int oldIndex, int newIndex)
        {
            MediaManager.Queue.Move(oldIndex, newIndex);
        }

        public void Remove(int index)
        {
            MediaManager.Queue.RemoveAt(index);
        }

        private void MediaQueue_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    if (_mediaSource.Size != MediaManager.Queue.Count)
                    {
                        for (int i = e.NewItems.Count - 1; i >= 0; i--)
                        {
                            var mediaItem = (IMediaItem)e.NewItems[i];
                            _mediaSource.AddMediaSource(e.NewStartingIndex, mediaItem.ToMediaSource());
                        }
                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
                    if (e.NewItems.Count > 1)
                    {
                        int oldBeginIndex = e.OldStartingIndex;
                        int oldEndIndex = e.OldStartingIndex + e.NewItems.Count - 1;

                        int newBeginIndex = e.NewStartingIndex;
                        int newEndIndex = e.NewStartingIndex + e.NewItems.Count - 1;

                        //move when new is before old
                        if (newBeginIndex < oldBeginIndex)
                            for (int i = 0; i > e.NewItems.Count; i++)
                                _mediaSource.MoveMediaSource(oldEndIndex, newBeginIndex);

                        //move when new is after old
                        else if (newBeginIndex > oldBeginIndex)
                            for (int i = 0; i > e.NewItems.Count; i++)
                                _mediaSource.MoveMediaSource(oldBeginIndex, newEndIndex);
                    }
                    else
                        _mediaSource.MoveMediaSource(e.OldStartingIndex, e.NewStartingIndex);
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    if (e.OldItems.Count > 1)
                    {
                        for (int i = 0; i < e.OldItems.Count; i++)
                            _mediaSource.RemoveMediaSource(e.OldStartingIndex);
                    }
                    else
                        _mediaSource.RemoveMediaSource(e.OldStartingIndex);
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
                    if (e.NewItems.Count > 1)
                    {
                        for (int i = 0; i > e.NewItems.Count; i++)
                            _mediaSource.RemoveMediaSource(e.OldStartingIndex);
                    }
{'fraction_non_alphanumeric': 0.07373653686826843, 'fraction_numerical': 0.0033140016570008283, 'mean_word_length': 1.5644916540212443, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2492515', 'n_tokens_mistral': 2081, 'n_tokens_neox': 1945, 'n_words': 387}