Exemplo n.º 1
0
        public virtual async Task <List <LabelInfo> > GetLabelsAsync(bool forceSync = false)
        {
            // From memory
            if (!forceSync && Labels != null && Labels.Count != 0)
            {
                return(Labels);
            }

            // From disk
            Labels = await MailStorage.GetLabelInfoAsync() ?? new List <LabelInfo>();

            if (!forceSync && Labels.Count != 0)
            {
                return(Labels);
            }

            if (Labels.Count == 0)
            {
                // Default label settings for a new account
                Labels.Add(new LabelInfo()
                {
                    Name = GConstants.Inbox, StoreMessages = true
                });
            }

            // Sync with server
            List <LabelInfo> final = new List <LabelInfo>();

            Mailbox[] serverMailboxes = await GmailImap.GetLabelsAsync();

            IEnumerable <LabelInfo> serverLabels = serverMailboxes.Select(box => new LabelInfo()
            {
                Name = box.Name
            });

            await SyncUtilities.CompareListsAsync <LabelInfo>(serverLabels, Labels,
                                                              label => label.Name,                                                         // Select key
                                                              async (serverLabel, clientLabel) => final.Add(clientLabel),                  // Match, client wins, it has client side settings.
                                                              async (serverLabel) => final.Add(serverLabel),                               // Server only, add.
                                                              (clientLabel) => MailStorage.DeleteLabelMessageListAsync(clientLabel.Name)); // Client side only, garbage collect.

            Labels = final;

            // Save back to storage
            await MailStorage.SaveLabelInfoAsync(Labels);

            return(Labels);
        }