Example #1
0
File: Sync.cs Project: aata/szotar
        // TODO Sync tags
        private async Task SyncSet(IQuizletService api, WordList list, CancellationToken cancel)
        {
            Debug.Assert(list.SyncID != null, "list.SyncID != null");
            Debug.Assert(list.ID != null, "list.ID != null");

            if (list.SyncNeeded)
            {
                try {
                    var info = await api.FetchSetInfo(list.SyncID.Value, cancel);

                    // Find updates from Quizlet
                    var missingLocally = (from qTerm in info.Terms
                                          where list.All(e => !SameTerm(e.Phrase, e.Translation, qTerm.Term, qTerm.Definition))
                                          select new TranslationPair(qTerm.Term, qTerm.Definition)).ToList();

                    var missingRemotely = (from lTerm in list
                                           where info.Terms.All(t => !SameTerm(lTerm.Phrase, lTerm.Translation, t.Term, t.Definition))
                                           select new TranslationPair(lTerm.Phrase, lTerm.Translation)).ToList();

                    var modifiedLocally  = list.SyncNeeded;
                    var modifiedRemotely = info.Modified > list.SyncDate || !list.SyncDate.HasValue;

                    if (modifiedLocally && modifiedRemotely)
                    {
                        // Merge conflict!
                    }
                    else if (modifiedRemotely)
                    {
                        // Apply deletions from Quizlet
                        for (int i = 0; i < missingRemotely.Count; i++)
                        {
                            var qd = missingRemotely[i];
                            list.Remove(e => SameTerm(e.Phrase, e.Translation, qd.Phrase, qd.Translation));
                        }

                        // Apply insertions from Quizlet
                        foreach (var qi in missingLocally)
                        {
                            list.Add(new WordListEntry(list, qi.Phrase, qi.Translation));
                        }
                    }
                    else
                    {
                        // Just push a full update of the entries to Quizlet
                        var set = GetSetModel(list);

                        // Update properties that aren't stored locally
                        await api.UpdateSet(set, cancel);
                    }
                } catch (Exception e) {
                    OnItemError(list, e);
                    return;
                }
            }

            OnItemFinished(list);
        }
Example #2
0
        public void ParagraphByChars_Does_Not_End_With_Space_Dot()
        {
            RandomNumber.ResetSeed(42);
            var maxWordLength = 0;

            _ = WordList.All(x => { maxWordLength = Math.Max(maxWordLength, x.Length); return(true); });
            _ = SupplementalWordList.All(x => { maxWordLength = Math.Max(maxWordLength, x.Length); return(true); });
            const int startValue = 50;
            var       charCount  = startValue;

            while (charCount++ <= startValue + maxWordLength)
            {
                var paragraph = Lorem.ParagraphByChars(43);
                Assert.IsFalse(paragraph.EndsWith(" ."));
            }
        }
Example #3
0
		// TODO Sync tags
		private async Task SyncSet(IQuizletService api, WordList list, CancellationToken cancel) {
			Debug.Assert(list.SyncID != null, "list.SyncID != null");
			Debug.Assert(list.ID != null, "list.ID != null");

			if (list.SyncNeeded) {
				try {
					var info = await api.FetchSetInfo(list.SyncID.Value, cancel);
					
					// Find updates from Quizlet
					var missingLocally = (from qTerm in info.Terms
										  where list.All(e => !SameTerm(e.Phrase, e.Translation, qTerm.Term, qTerm.Definition))
										  select new TranslationPair(qTerm.Term, qTerm.Definition)).ToList();

					var missingRemotely = (from lTerm in list
										   where info.Terms.All(t => !SameTerm(lTerm.Phrase, lTerm.Translation, t.Term, t.Definition))
										   select new TranslationPair(lTerm.Phrase, lTerm.Translation)).ToList();

					var modifiedLocally = list.SyncNeeded;
					var modifiedRemotely = info.Modified > list.SyncDate || !list.SyncDate.HasValue;
						
					if (modifiedLocally && modifiedRemotely) {
						// Merge conflict!
					} else if (modifiedRemotely) {
						// Apply deletions from Quizlet
						for (int i = 0; i < missingRemotely.Count; i++) {
							var qd = missingRemotely[i];
							list.Remove(e => SameTerm(e.Phrase, e.Translation, qd.Phrase, qd.Translation));
						}

						// Apply insertions from Quizlet
						foreach (var qi in missingLocally)
							list.Add(new WordListEntry(list, qi.Phrase, qi.Translation));
					} else {
						// Just push a full update of the entries to Quizlet
						var set = GetSetModel(list);
						
						// Update properties that aren't stored locally
						await api.UpdateSet(set, cancel);
					}
				} catch (Exception e) {
					OnItemError(list, e);
					return;
				}
			}

			OnItemFinished(list);
		}