コード例 #1
0
        public void FriendsToCache(List <UserBriefResponse> newFriends)
        {
            var friendCache = Utils.Utils.getOwnPath().Combine("friends.cache.json");

            Logger.Log("Loading cached friends from", friendCache.FullName.Quote());
            var newFriendsBackup = new FriendsBackup(me, newFriends);

            if (friendCache.Exists)
            {
                try {
                    var cachedFriends = JsonConvert.DeserializeObject <FriendsBackup>(File.ReadAllText(friendCache.FullName));
                    Logger.Log("Loaded", cachedFriends.Friends.Count, "cached friends from", friendCache.Name.Quote());
                    CompareFriendCaches(cachedFriends, newFriendsBackup);
                } catch (JsonException ex) {
                    MessageBox.Show($"An error was thrown while reading cached friends!\nClick okay to discard current cache and write a new one\n\n{ex.Message}\n\n{ex.StackTrace}", "Error while loading cached friends!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            Logger.Log("Saving", newFriendsBackup.Friends.Count, "new friends to", friendCache.Name.Quote());
            // File.WriteAllLines(friendCache.FullName, newFriends);
            File.WriteAllText(friendCache.FullName, JsonConvert.SerializeObject(newFriendsBackup, Formatting.Indented));
        }
コード例 #2
0
        private void CompareFriendCaches(FriendsBackup oldFriends, FriendsBackup newFriends)
        {
            if (oldFriends != newFriends)
            {
                var sb = new StringBuilder($"Last Backup: {oldFriends.TimeStamp} ({(DateTime.Now - oldFriends.TimeStamp).StripMilliseconds()} ago)\n\n");
                if (oldFriends.Account.id != newFriends.Account.id)
                {
                    var diffAccResult = MessageBox.Show($"Last Friends Backup was created with account\n\n{oldFriends.Account.displayName.Quote()}\n\nbut you're currently logged in as\n\n{newFriends.Account.displayName.Quote()}\n\nAre you sure you still want to compare them?", "Friend backup conflict detected!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    if (diffAccResult != DialogResult.OK)
                    {
                        return;
                    }
                }
                // var addedFriends = new List<FriendsBackup.Friend>();
                var removedFriends = new List <FriendsBackup.Friend>();
                // var changedNames = new List<FriendsBackup.Friend>();
                var changed = false;
                foreach (var oldFriend in oldFriends.Friends)
                {
                    var oldInNew = newFriends.Friends.FirstOrDefault(c => c.id == oldFriend.id);
                    if (oldInNew is null)
                    {
                        removedFriends.Add(oldFriend); changed = true;
                    }
                    else
                    {
                        if (oldInNew.displayName != oldFriend.displayName)
                        {
                            sb.AppendLine($"{oldFriend.displayName.Quote()} changed their name to {oldInNew.displayName.Quote()}!"); changed = true;
                        }
                    }
                }
                if (changed)
                {
                    var title = "Friend List Changed";
                    if (removedFriends.Count > 0)
                    {
                        Logger.Debug("Removed Friends:", removedFriends.ToJson());
                        foreach (var removedFriend in removedFriends)
                        {
                            sb.AppendLine($"{removedFriend.displayName.Quote()} was removed!");
                        }
                        sb.AppendLine("\n\nPress Yes to re-add removed friends to your list");
                        var reAddResult = MessageBox.Show(sb.ToString(), title, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                        if (reAddResult == DialogResult.Yes)
                        {
                            AddFriendsAsync(removedFriends.Select(c => c.id).ToList());
                        }
                    }
                    else
                    {
                        MessageBox.Show(sb.ToString(), title, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }

                /*foreach (var oldFriend in oldFriends.Friends)
                 * {
                 *  // var change = FriendsBackup.Friend.Changed.Everything;
                 *  foreach (var newFriend in newFriends.Friends)
                 *  {
                 *      /*switch (oldFriend.Difference(newFriend))
                 *      {
                 *          case FriendsBackup.Friend.Changed.Name:
                 *              change = FriendsBackup.Friend.Changed.Name; break;
                 *          case FriendsBackup.Friend.Changed.Nothing:
                 *              change = FriendsBackup.Friend.Changed.Nothing; break;
                 *          default:
                 *              break;
                 *      }
                 *      if (change != FriendsBackup.Friend.Changed.Everything) break;
                 *
                 *  }
                 *  switch (change)
                 *  {
                 *      case FriendsBackup.Friend.Changed.Name:
                 *          changedNames.Add(oldFriend); break;
                 *      case FriendsBackup.Friend.Changed.Everything:
                 *          removedFriends.Add(oldFriend); break;
                 *      default:
                 *          break;
                 *  }
                 * }*/
                /*foreach (var newFriend in newFriends.Friends) {
                 *  if (!newFriends.Friends.Any(x => x.id == newFriend.id)) {
                 *      addedFriends.Add(newFriend);
                 *  }
                 * }*/
            }
        }