Exemple #1
0
        public async void GetMegaContacts()
        {
            // User must be online to perform this operation
            if (!await IsUserOnlineAsync())
            {
                return;
            }

            // First cancel any other loading task that is busy
            CancelLoad();

            // Create the option to cancel
            CreateLoadCancelOption();

            await OnUiThreadAsync(() => this.ItemCollection.Clear());

            MUserList contactsList = this.MegaSdk.getContacts();

            await Task.Factory.StartNew(() =>
            {
                try
                {
                    for (int i = 0; i < contactsList.size(); i++)
                    {
                        // If the task has been cancelled, stop processing
                        if (LoadingCancelToken.IsCancellationRequested)
                        {
                            LoadingCancelToken.ThrowIfCancellationRequested();
                        }

                        // To avoid null values
                        if (contactsList.get(i) == null)
                        {
                            continue;
                        }

                        if (contactsList.get(i).getVisibility() != MUserVisibility.VISIBILITY_VISIBLE)
                        {
                            continue;
                        }

                        var megaContact = new ContactViewModel(contactsList.get(i), this);

                        OnUiThread(() => this.ItemCollection.Items.Add(megaContact));

                        megaContact.GetContactFirstname();
                        megaContact.GetContactLastname();
                        megaContact.GetContactAvatarColor();
                        megaContact.GetContactAvatar();
                    }
                }
                catch (OperationCanceledException)
                {
                    // Do nothing. Just exit this background process because a cancellation exception has been thrown
                }
            }, LoadingCancelToken, TaskCreationOptions.PreferFairness, TaskScheduler.Current);

            this.SortBy(this.CurrentOrder, this.ItemCollection.CurrentOrderDirection);
        }
        public void GetContactSharedFolders()
        {
            // User must be online to perform this operation
            if (!IsUserOnline())
            {
                return;
            }

            // First cancel any other loading task that is busy
            CancelLoad();

            // Create the option to cancel
            CreateLoadCancelOption();

            OnUiThread(() => InShares.ChildNodes.Clear());
            MNodeList inSharesList = MegaSdk.getInShares(MegaSdk.getContact(_selectedContact.Email));

            Task.Factory.StartNew(() =>
            {
                try
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        for (int i = 0; i < inSharesList.size(); i++)
                        {
                            // If the task has been cancelled, stop processing
                            if (LoadingCancelToken.IsCancellationRequested)
                            {
                                LoadingCancelToken.ThrowIfCancellationRequested();
                            }

                            // To avoid null values
                            if (inSharesList.get(i) == null)
                            {
                                continue;
                            }

                            var _inSharedFolder = NodeService.CreateNew(this.MegaSdk, this.AppInformation, inSharesList.get(i), ContainerType.InShares, InShares.ChildNodes);
                            _inSharedFolder.DefaultImagePathData = VisualResources.FolderTypePath_shared;
                            InShares.ChildNodes.Add(_inSharedFolder);
                        }

                        OnPropertyChanged("NumberOfInSharedFolders");
                        OnPropertyChanged("NumberOfInSharedFoldersText");
                    });
                }
                catch (OperationCanceledException)
                {
                    // Do nothing. Just exit this background process because a cancellation exception has been thrown
                }
            }, LoadingCancelToken, TaskCreationOptions.PreferFairness, TaskScheduler.Current);
        }
Exemple #3
0
        protected async void GetOutgoingSharedItems()
        {
            // User must be online to perform this operation
            if (!await IsUserOnlineAsync())
            {
                return;
            }

            // First cancel any other loading task that is busy
            CancelLoad();

            // Create the option to cancel
            CreateLoadCancelOption();

            // Process is started so we can set the empty content template to loading already
            SetEmptyContent(true);

            await OnUiThreadAsync(() => this.ItemCollection.Clear());

            MShareList outSharedItems = SdkService.MegaSdk.getOutShares();

            await Task.Factory.StartNew(() =>
            {
                try
                {
                    ulong lastFolderHandle     = 0;
                    var outSharedItemsListSize = outSharedItems.size();
                    for (int i = 0; i < outSharedItemsListSize; i++)
                    {
                        // If the task has been cancelled, stop processing
                        if (LoadingCancelToken.IsCancellationRequested)
                        {
                            LoadingCancelToken.ThrowIfCancellationRequested();
                        }

                        var item = outSharedItems.get(i);

                        // To avoid null values and repeated values (folders shared with more than one user)
                        if (item == null || lastFolderHandle == item.getNodeHandle())
                        {
                            continue;
                        }

                        lastFolderHandle = item.getNodeHandle();

                        var node = NodeService.CreateNewSharedFolder(SdkService.MegaSdk, App.AppInformation,
                                                                     SdkService.MegaSdk.getNodeByHandle(item.getNodeHandle()), this);

                        // If node creation failed for some reason, continue with the rest and leave this one
                        if (node == null)
                        {
                            continue;
                        }

                        OnUiThread(() => this.ItemCollection.Items.Add(node));
                    }
                }
                catch (OperationCanceledException)
                {
                    // Do nothing. Just exit this background process because a cancellation exception has been thrown
                }
            }, LoadingCancelToken, TaskCreationOptions.PreferFairness, TaskScheduler.Current);

            this.SortBy(this.CurrentOrder, this.ItemCollection.CurrentOrderDirection);
            OnItemCollectionChanged(this, EventArgs.Empty);
            SetEmptyContent(false);
        }
Exemple #4
0
        /// <summary>
        /// Gets the outgoing shared folders and fills the list.
        /// </summary>
        public void GetOutgoingSharedFolders()
        {
            // User must be online to perform this operation
            if (!IsUserOnline())
            {
                return;
            }

            // First cancel any other loading task that is busy
            CancelLoad();

            // Create the option to cancel
            CreateLoadCancelOption();

            OutShares.SetProgressIndication(true);

            // Process is started so we can set the empty content template to loading already
            OutShares.SetEmptyContentTemplate(true);

            MShareList outSharesList = MegaSdk.getOutShares();

            if (outSharesList == null)
            {
                OnUiThread(() =>
                {
                    new CustomMessageDialog(
                        AppMessages.AM_LoadFailed_Title,
                        AppMessages.AM_LoadOutSharesFailed,
                        App.AppInformation,
                        MessageDialogButtons.Ok).ShowDialog();

                    OutShares.SetEmptyContentTemplate(false);
                });

                return;
            }

            OnUiThread(() => OutShares.ChildNodes.Clear());

            Task.Factory.StartNew(() =>
            {
                try
                {
                    ulong lastFolderHandle = 0;
                    int outSharesListSize  = outSharesList.size();
                    for (int i = 0; i < outSharesListSize; i++)
                    {
                        // If the task has been cancelled, stop processing
                        if (LoadingCancelToken.IsCancellationRequested)
                        {
                            LoadingCancelToken.ThrowIfCancellationRequested();
                        }

                        MShare outSharedFolder = outSharesList.get(i);

                        // To avoid null values and public links
                        if ((outSharedFolder != null) && MegaSdk.isOutShare(MegaSdk.getNodeByHandle(outSharedFolder.getNodeHandle())))
                        {
                            // To avoid repeated values, folders shared with more than one user
                            if (lastFolderHandle != outSharedFolder.getNodeHandle())
                            {
                                lastFolderHandle = outSharedFolder.getNodeHandle();

                                var _outSharedFolder = NodeService.CreateNew(this.MegaSdk, this.AppInformation,
                                                                             MegaSdk.getNodeByHandle(lastFolderHandle), ContainerType.OutShares, OutShares.ChildNodes);

                                if (_outSharedFolder != null)
                                {
                                    _outSharedFolder.DefaultImagePathData = VisualResources.FolderTypePath_shared;
                                    OnUiThread(() => OutShares.ChildNodes.Add(_outSharedFolder));
                                }
                            }
                        }
                    }

                    OnUiThread(() =>
                    {
                        OnPropertyChanged("HasOutSharedFolders");
                        OnPropertyChanged("NumberOfOutSharedFolders");
                        OnPropertyChanged("NumberOfOutSharedFoldersText");
                    });
                }
                catch (OperationCanceledException)
                {
                    // Do nothing. Just exit this background process because a cancellation exception has been thrown
                }
                finally
                {
                    // Show the user that processing the childnodes is done
                    OutShares.SetProgressIndication(false);

                    // Set empty content to folder instead of loading view
                    OutShares.SetEmptyContentTemplate(false);
                }
            }, LoadingCancelToken, TaskCreationOptions.PreferFairness, TaskScheduler.Current);
        }
Exemple #5
0
        /// <summary>
        /// Gets the incoming shared folders and fills the list.
        /// </summary>
        public void GetIncomingSharedFolders()
        {
            // User must be online to perform this operation
            if (!IsUserOnline())
            {
                return;
            }

            // First cancel any other loading task that is busy
            CancelLoad();

            // Create the option to cancel
            CreateLoadCancelOption();

            InShares.SetProgressIndication(true);

            // Process is started so we can set the empty content template to loading already
            InShares.SetEmptyContentTemplate(true);

            MNodeList inSharesList = MegaSdk.getInShares();

            if (inSharesList == null)
            {
                OnUiThread(() =>
                {
                    new CustomMessageDialog(
                        AppMessages.AM_LoadFailed_Title,
                        AppMessages.AM_LoadInSharesFailed,
                        App.AppInformation,
                        MessageDialogButtons.Ok).ShowDialog();

                    InShares.SetEmptyContentTemplate(false);
                });

                return;
            }

            OnUiThread(() => InShares.ChildNodes.Clear());

            Task.Factory.StartNew(() =>
            {
                try
                {
                    int inSharesListSize = inSharesList.size();
                    for (int i = 0; i < inSharesListSize; i++)
                    {
                        // If the task has been cancelled, stop processing
                        if (LoadingCancelToken.IsCancellationRequested)
                        {
                            LoadingCancelToken.ThrowIfCancellationRequested();
                        }

                        MNode inSharedFolder = inSharesList.get(i);
                        if (inSharedFolder != null)
                        {
                            var _inSharedFolder = NodeService.CreateNew(this.MegaSdk, this.AppInformation,
                                                                        inSharedFolder, ContainerType.InShares, InShares.ChildNodes);

                            if (_inSharedFolder != null)
                            {
                                _inSharedFolder.DefaultImagePathData = VisualResources.FolderTypePath_shared;
                                OnUiThread(() => InShares.ChildNodes.Add(_inSharedFolder));
                            }
                        }
                    }

                    OnUiThread(() =>
                    {
                        OnPropertyChanged("HasInSharedFolders");
                        OnPropertyChanged("NumberOfInSharedFolders");
                        OnPropertyChanged("NumberOfInSharedFoldersText");
                    });
                }
                catch (OperationCanceledException)
                {
                    // Do nothing. Just exit this background process because a cancellation exception has been thrown
                }
                finally
                {
                    // Show the user that processing the childnodes is done
                    InShares.SetProgressIndication(false);

                    // Set empty content to folder instead of loading view
                    InShares.SetEmptyContentTemplate(false);
                }
            }, LoadingCancelToken, TaskCreationOptions.PreferFairness, TaskScheduler.Current);
        }
Exemple #6
0
        public void GetMegaContacts()
        {
            // User must be online to perform this operation
            if (!IsUserOnline())
            {
                return;
            }

            // First cancel any other loading task that is busy
            CancelLoad();

            // Create the option to cancel
            CreateLoadCancelOption();

            SetEmptyContentTemplate(true);

            OnUiThread(() => MegaContactsList.Clear());
            MUserList contactsList = this.MegaSdk.getContacts();

            Task.Factory.StartNew(() =>
            {
                try
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        for (int i = 0; i < contactsList.size(); i++)
                        {
                            // If the task has been cancelled, stop processing
                            if (LoadingCancelToken.IsCancellationRequested)
                            {
                                LoadingCancelToken.ThrowIfCancellationRequested();
                            }

                            // To avoid null values
                            if (contactsList.get(i) == null)
                            {
                                continue;
                            }

                            if ((contactsList.get(i).getVisibility() == MUserVisibility.VISIBILITY_VISIBLE))
                            {
                                var _megaContact = new Contact()
                                {
                                    Handle      = contactsList.get(i).getHandle(),
                                    Email       = contactsList.get(i).getEmail(),
                                    Timestamp   = contactsList.get(i).getTimestamp(),
                                    Visibility  = contactsList.get(i).getVisibility(),
                                    AvatarColor = UiService.GetColorFromHex(SdkService.MegaSdk.getUserAvatarColor(contactsList.get(i)))
                                };

                                MegaContactsList.Add(_megaContact);

                                MegaSdk.getUserAttribute(contactsList.get(i), (int)MUserAttrType.USER_ATTR_FIRSTNAME,
                                                         new GetContactDataRequestListener(_megaContact));
                                MegaSdk.getUserAttribute(contactsList.get(i), (int)MUserAttrType.USER_ATTR_LASTNAME,
                                                         new GetContactDataRequestListener(_megaContact));
                                MegaSdk.getUserAvatar(contactsList.get(i), _megaContact.AvatarPath,
                                                      new GetContactAvatarRequestListener(_megaContact));
                            }
                        }

                        SetEmptyContentTemplate(false);
                    });
                }
                catch (OperationCanceledException)
                {
                    // Do nothing. Just exit this background process because a cancellation exception has been thrown
                }
            }, LoadingCancelToken, TaskCreationOptions.PreferFairness, TaskScheduler.Current);
        }
        private void CreateChildren(MNodeList childList, int listSize)
        {
            // Set the parameters for the performance for the different view types of a folder
            int viewportItemCount, backgroundItemCount;

            InitializePerformanceParameters(out viewportItemCount, out backgroundItemCount);

            // We will not add nodes one by one in the dispatcher but in groups
            List <IMegaNode> helperList;

            try { helperList = new List <IMegaNode>(1024); }
            catch (ArgumentOutOfRangeException) { helperList = new List <IMegaNode>(); }

            for (int i = 0; i < listSize; i++)
            {
                // If the task has been cancelled, stop processing
                if (LoadingCancelToken.IsCancellationRequested)
                {
                    LoadingCancelToken.ThrowIfCancellationRequested();
                }

                // To avoid pass null values to CreateNew
                if (childList.get(i) == null)
                {
                    continue;
                }

                var node = NodeService.CreateNew(this.MegaSdk, this.AppInformation, childList.get(i), this.Type, ChildNodes);

                // If node creation failed for some reason, continue with the rest and leave this one
                if (node == null)
                {
                    continue;
                }

                // If the user is moving nodes, check if the node had been selected to move
                // and establish the corresponding display mode
                if (CurrentDisplayMode == DriveDisplayMode.CopyOrMoveItem)
                {
                    // Check if it is the only focused node
                    if ((FocusedNode != null) && (node.OriginalMNode.getBase64Handle() == FocusedNode.OriginalMNode.getBase64Handle()))
                    {
                        node.DisplayMode = NodeDisplayMode.SelectedForCopyOrMove;
                        FocusedNode      = node;
                    }

                    // Check if it is one of the multiple selected nodes
                    IsSelectedNode(node);
                }

                helperList.Add(node);

                // First add the viewport items to show some data to the user will still loading
                if (i == viewportItemCount)
                {
                    var waitHandleViewportNodes = new AutoResetEvent(false);
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        // If the task has been cancelled, stop processing
                        foreach (var megaNode in helperList.TakeWhile(megaNode => !LoadingCancelToken.IsCancellationRequested))
                        {
                            ChildNodes.Add(megaNode);
                        }
                        waitHandleViewportNodes.Set();
                    });
                    waitHandleViewportNodes.WaitOne();

                    helperList.Clear();
                    continue;
                }

                if (helperList.Count != backgroundItemCount || i <= viewportItemCount)
                {
                    continue;
                }

                // Add the rest of the items in the background to the list
                var waitHandleBackgroundNodes = new AutoResetEvent(false);
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    // If the task has been cancelled, stop processing
                    foreach (var megaNode in helperList.TakeWhile(megaNode => !LoadingCancelToken.IsCancellationRequested))
                    {
                        ChildNodes.Add(megaNode);
                    }
                    waitHandleBackgroundNodes.Set();
                });
                waitHandleBackgroundNodes.WaitOne();

                helperList.Clear();
            }

            // Add any nodes that are left over
            var waitHandleRestNodes = new AutoResetEvent(false);

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                // Show the user that processing the childnodes is done
                SetProgressIndication(false);

                // Set empty content to folder instead of loading view
                SetEmptyContentTemplate(false);

                // If the task has been cancelled, stop processing
                foreach (var megaNode in helperList.TakeWhile(megaNode => !LoadingCancelToken.IsCancellationRequested))
                {
                    ChildNodes.Add(megaNode);
                }
                waitHandleRestNodes.Set();
            });
            waitHandleRestNodes.WaitOne();

            OnUiThread(() => OnPropertyChanged("HasChildNodesBinding"));
        }