コード例 #1
0
ファイル: MessageColumn.cs プロジェクト: mpvyard/Twice
        protected override async Task OnLoad(AsyncLoadContext context)
        {
            Dictionary <ulong, DirectMessage> userMap = new Dictionary <ulong, DirectMessage>();
            var cachedMessages = await Cache.GetMessages();

            foreach (var c in cachedMessages)
            {
                var userId = c.Sender == Context.UserId
                                        ? c.Recipient
                                        : c.Sender;
                var msg = JsonConvert.DeserializeObject <DirectMessage>(c.Data);

                DirectMessage existing;
                if (userMap.TryGetValue(userId, out existing))
                {
                    if (existing.CreatedAt < msg.CreatedAt)
                    {
                        userMap[userId] = msg;
                    }
                }
                else
                {
                    userMap.Add(userId, msg);
                }
            }

            var messages = await Context.Twitter.Messages.IncomingMessages(200, MaxId);

            foreach (var msg in messages)
            {
                var           userId = msg.SenderID;
                DirectMessage existing;
                if (userMap.TryGetValue(userId, out existing))
                {
                    if (existing.CreatedAt < msg.CreatedAt)
                    {
                        userMap[userId] = msg;
                    }
                }
                else
                {
                    userMap.Add(userId, msg);
                }
            }

            var toCache = new List <MessageCacheEntry>();

            toCache.AddRange(messages.Select(m => new MessageCacheEntry(m)));

            messages = await Context.Twitter.Messages.OutgoingMessages(200, MaxId);

            foreach (var msg in messages)
            {
                var           userId = msg.RecipientID;
                DirectMessage existing;
                if (userMap.TryGetValue(userId, out existing))
                {
                    if (existing.CreatedAt < msg.CreatedAt)
                    {
                        userMap[userId] = msg;
                    }
                }
                else
                {
                    userMap.Add(userId, msg);
                }
            }

            toCache.AddRange(messages.Select(m => new MessageCacheEntry(m)));
            await Cache.AddMessages(toCache);

            var list = new List <MessageViewModel>();

            foreach (var s in userMap.Values.OrderByDescending(m => m.CreatedAt))
            {
                var vm = await CreateViewModel(s);

                vm.WasRead = true;

                list.Add(vm);
            }

            await AddItems(list);
        }