public MainForm() { _ankiProvider = new AnkiProvider(FindOrCreateConfig()); _cambridgeProvider = new CambridgeProvider(CambridgeDataSet.British); _htmlFormatter = new HtmlFormatter(); _simpleTextFormatter = new SimpleTextFormatter(); _textFormatter = new TextFormatter(); _comparer = new OrdinalIgnoreCaseComparer(); InitializeComponent(); LoadDecks(); }
public void SetDependencies(AnkiProvider ankiProvider, CambridgeProvider cambridgeProvider, IWordInfoFormatter htmlFormatter, IWordInfoFormatter simpleTextFormatter, IWordInfoFormatter textFormatter, Comparer <string> comparer) { _ankiProvider = ankiProvider; _cambridgeProvider = cambridgeProvider; _htmlFormatter = htmlFormatter; _simpleTextFormatter = simpleTextFormatter; _textFormatter = textFormatter; _comparer = comparer; }
public string AsFormatted(IWordInfoFormatter formatter) { var entryBuilder = new StringBuilder(); if (!(formatter is HtmlFormatter)) { entryBuilder.Append(!string.IsNullOrWhiteSpace(InputWord) ? InputWord : Entries[0].ActualWord); entryBuilder.Append("\t"); } entryBuilder.AppendLine(formatter.Render(this)); return(entryBuilder.ToString()); }
public async Task <(bool Success, List <string> ErrorWords)> AddNotes(string deckName, string modelName, List <CambridgeWordInfo> words, IWordInfoFormatter formatter) { try { var notes = new List <dynamic>(); foreach (var word in words) { notes.Add(CreateNote(deckName, modelName, word.InputWord, word.AsFormatted(formatter))); } var postData = new { action = "addNotes", version = 6, @params = new { notes } }; var data = JsonConvert.SerializeObject(postData); var response = await _client.PostAsync(_client.BaseAddress, new StringContent(data)).ConfigureAwait(false); var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); dynamic deserialized = JsonConvert.DeserializeObject(content); if (deserialized.error != null) { return(Success : false, ErrorWords : null); } var errorWords = new List <string>(); var index = 0; foreach (JValue item in deserialized["result"]) { if (item.Value == null) { errorWords.Add(words[index].InputWord); } index++; } return(Success : true, ErrorWords : errorWords); } catch (Exception ex) { Debug.WriteLine($"AddNotes: {ex.Message}"); return(Success : false, ErrorWords : null); } }
public Task <bool> AddNote(string deckName, string modelName, CambridgeWordInfo wordInfo, IWordInfoFormatter formatter, bool checkIfExisting = false) { var front = wordInfo.InputWord; var back = wordInfo.AsFormatted(formatter); return(AddNote(deckName, modelName, front, back, checkIfExisting)); }