/// <summary> /// Set the ui state according to the padoru entry /// </summary> /// <param name="entry">the padoru entry</param> void UiFromEntry(PadoruEntry entry) { txtImageUrl.Text = entry.ImageUrl; txtImagePath.Text = entry.ImageAbsolutePath; txtCharacterName.Text = entry.Name; chkCharacterFemale.Checked = entry.IsFemale; txtImageContributor.Text = entry.ImageContributor; txtImageCreator.Text = entry.ImageCreator; txtImageSource.Text = entry.ImageSource; txtSelectedMalName.Text = entry.MALName; txtSelectedMalId.Text = entry.MALId.HasValue ? entry.MALId.ToString() : ""; //parse entry mal id if (entry.MALId.HasValue) { loadedEntryMalId = entry.MALId.Value; } //save id editingId = entry.UID; //save parent collection EditingParentCollection = entry.ParentCollection; }
/// <summary> /// Test functionality of padoru client /// </summary> static async Task Padoru() { //init client PadoruClient client = new PadoruClient(); client.MaxCollectionAge = TimeSpan.FromSeconds(10);//for testing auto resync await client.LoadCollection(COLLECTION_URL); //force resync of collection await Task.Delay(15); //test get all entries IReadOnlyCollection <PadoruEntry> allEntries = await client.GetEntries(); client.MaxCollectionAge = TimeSpan.FromHours(1); //test get random entry (with resync) PadoruEntry randomEntry = await client.GetRandomEntry(); //test get specific entry IReadOnlyCollection <PadoruEntry> maleEntries = await client.GetEntriesWhere((p) => !p.IsFemale); //test image download Image rndImg; using (MemoryStream rndImgStream = new MemoryStream(await randomEntry.GetImageData())) { rndImg = Image.FromStream(rndImgStream); } //save image rndImg?.Save("./random.png"); }
/// <summary> /// Create a PadoruEntry from the current ui state /// </summary> /// <returns>the padoru entry</returns> PadoruEntry UiToEntry() { //create entry instance PadoruEntry entry = new PadoruEntry() { ImageUrl = txtImageUrl.Text, Name = txtCharacterName.Text, IsFemale = chkCharacterFemale.Checked, MALName = txtSelectedMalName.Text, MALId = null, ImageContributor = txtImageContributor.Text, ImageCreator = txtImageCreator.Text, ImageSource = txtImageSource.Text }; //parse and set mal id if (long.TryParse(txtSelectedMalId.Text, out long malId)) { entry.MALId = malId; } //set image path and parent collection if (EditingParentCollection != null) { entry.ParentCollection = EditingParentCollection; entry.SetImagePath(txtImagePath.Text); } if (!editingId.Equals(Guid.Empty)) { entry.UID = editingId; } return(entry); }
async void OnRemoveCurrentClick(object sender, EventArgs e) { //get current selection PadoruEntry toRemove = GetSelection(); if (toRemove == null) { return; } //ask if wants to remove element if (MessageBox.Show(this, $"Do you want to remove \"{toRemove.ToString()}\"?", "Remove Element", MessageBoxButtons.YesNo) != DialogResult.Yes) { return; } //remove element currentCollection.Entries.Remove(toRemove); SaveCollection(true); //re- populate selection panel await PopulateSelectionPanel(currentCollection.Entries); //update ui currentlySelectedEntryId = Guid.Empty; Refresh(); }
async void OnEditCurrentClick(object sender, EventArgs e) { //get current selection PadoruEntry toEdit = GetSelection(); if (toEdit == null) { return; } //show padoru editor PadoruEditor editor = new PadoruEditor { CurrentStateEntry = toEdit, CurrentManagerConfig = currentManagerConfig }; if (editor.ShowDialog() == DialogResult.Cancel) { return; } //get edited entry PadoruEntry editedEntry = editor.CurrentStateEntry; if (editedEntry == null) { return; } //write back saved entry int index = currentCollection.Entries.IndexOf(toEdit); currentCollection.Entries.Remove(toEdit); currentCollection.Entries.Insert(index, editedEntry); SaveCollection(true); //re- populate selection panel await PopulateSelectionPanel(currentCollection.Entries); //update ui Refresh(); }
/// <summary> /// Get the image for the given entry /// </summary> /// <param name="entry">the entry to get the image for</param> /// <returns>the loaded image</returns> async Task <Image> GetImageFromEntry(PadoruEntry entry) { if (forceRemoteImage) { return(await entry.GetImageDataRemote().ContinueWith((t) => { //load image from data, use fallback when load failed if (t.IsCompleted && !t.IsFaulted && !t.IsCanceled && t.Result != null) { using (MemoryStream entryImgDataStream = new MemoryStream(t.Result)) { return Image.FromStream(entryImgDataStream); } } //fallback to null return null; })); } else { return(await Task.Run(() => { //get image data for preview byte[] entryImgData = entry.GetImageDataLocal(); //load image from data, use fallback when load failed if (entryImgData != null) { using (MemoryStream entryImgDataStream = new MemoryStream(entryImgData)) { return Image.FromStream(entryImgDataStream); } } //fallback to null return null; })); } }
/// <summary> /// Get the currently selected entry /// </summary> /// <returns>the selected entry, or null if nothing is selected</returns> PadoruEntry GetSelection() { //check that selection and collection are ok if (currentCollection == null || currentlySelectedEntryId.Equals(Guid.Empty)) { return(null); } //remove current selection from collection PadoruEntry selectedEntry = null; foreach (PadoruEntry entry in currentCollection.Entries) { if (entry.UID.Equals(currentlySelectedEntryId)) { selectedEntry = entry; break; } } return(selectedEntry); }
async void OnAddNewClick(object sender, EventArgs e) { //check collection exists if (currentCollection == null) { MessageBox.Show(this, "No Collection is currently active!", "No Collection", MessageBoxButtons.OK); return; } //create Padoru Editor and let user create new entry PadoruEditor editor = new PadoruEditor { EditingParentCollection = currentCollection, CurrentManagerConfig = currentManagerConfig }; if (editor.ShowDialog() != DialogResult.OK) { return; } //Get new entry PadoruEntry newEntry = editor.CurrentStateEntry; //add new entry to list of entrys if (newEntry != null) { currentCollection.Entries.Add(newEntry); currentlySelectedEntryId = newEntry.UID; SaveCollection(true); } //re- populate selection panel await PopulateSelectionPanel(currentCollection.Entries); //update ui Refresh(); }
/// <summary> /// initialize the editor with the values from a existing entry /// </summary> /// <param name="entry">the entry whose values will be used</param> public PadoruEditor(PadoruEntry entry) : this() { CurrentStateEntry = entry; }