private async Task CreateCustomStudy()
        {
            var deck = collection.Deck.Current();

            if (HandleNotCreatingDynamicDeckCases(deck))
            {
                return;
            }

            //Not in java and python ver, we do nothing if no cards are available
            if (studyOption != CustomStudyOption.CramMode)
            {
                var max = int.Parse(chosenLabelValue.Text);
                if (max == 0)
                {
                    return;
                }
            }

            customStudyFlyout.IsOpen = false;
            JsonObject dynamicDeck;
            long       deckId;
            var        currentCustomDeck = collection.Deck.GetDeckByName(DEFAULT_DYN_DECKNAME);

            if (currentCustomDeck != null)
            {
                deckId = (long)currentCustomDeck.GetNamedNumber("id");
                bool isDyn = collection.Deck.IsDyn(deckId);
                if (!isDyn)
                {
                    await UIHelper.ShowMessageDialog("Please rename the deck named \"" + DEFAULT_DYN_DECKNAME + "\" to another name first.");

                    return;
                }
                else
                {
                    collection.Sched.EmptyDyn(deckId);
                    dynamicDeck = currentCustomDeck;
                    collection.Deck.Select(deckId);
                }
            }
            else
            {
                deckId      = collection.Deck.NewDynamicDeck(DEFAULT_DYN_DECKNAME);
                dynamicDeck = collection.Deck.Get(deckId);
            }
            ProgressDialog dialog = new ProgressDialog();

            dialog.ProgressBarLabel = "Buidling custom study deck...";
            dialog.ShowInDeterminateStateNoStopAsync("Custom Study");

            CreateDynamicDeckConfigs(dynamicDeck, deck);

            var task = Task.Run(async() =>
            {
                var cards = collection.Sched.RebuildDyn();
                collection.SaveAndCommitAsync();

                await dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
                {
                    if (cards == null || cards.Count == 0)
                    {
                        await UIHelper.ShowMessageDialog(UIConst.WARN_CUSTOM_STUDY_NOCARDS_MATCH);
                    }

                    CustomStudyCreateEvent?.Invoke(studyOption, deckId);
                    dialog.Hide();
                });
            });
        }
Beispiel #2
0
        private async void OkButtonClickHandler(object sender, RoutedEventArgs e)
        {
            if (mediaZipFile == null)
            {
                await UIHelper.ShowMessageDialog("Please choose a *.apkg package or a backed up zip file.");

                mediaInsertFlyout.ShowAt(placeToShow);
                return;
            }

            var currentSelectedDeck = deckNameView.CurrentSelectedDeck();

            if (currentSelectedDeck == null)
            {
                await UIHelper.ShowMessageDialog("Please choose the deck you want to insert media files.");

                mediaInsertFlyout.ShowAt(placeToShow);
                return;
            }

            PrepareProgessDialog();
            ResetFlags();

            using (var stream = await mediaZipFile.OpenStreamForReadAsync())
                using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read))
                {
                    var total = archive.Entries.Count;
                    if (total == 0)
                    {
                        await UIHelper.ShowMessageDialog("No files found.");

                        mediaInsertFlyout.ShowAt(placeToShow);
                        return;
                    }

                    var deckList = await collection.Media.MapDeckIdToDeckIdFolder();

                    var deckFolder = deckList[currentSelectedDeck.Id];
                    int index      = 0;
                    var numToName  = CheckIfAnkiPackage(archive, deckFolder);

                    try
                    {
                        collection.Media.Database.BeginTransaction();
                        string entryName;
                        foreach (var entry in archive.Entries)
                        {
                            entryName = entry.Name;
                            if (numToName != null)
                            {
                                bool isSuccess = numToName.TryGetValue(entry.Name, out entryName);
                                if (!isSuccess)
                                {
                                    continue;
                                }
                            }

                            progressDialog.ProgressBarLabel = String.Format(PROGESS_LABEL, index, total);
                            index++;

                            var existFile = await deckFolder.TryGetItemAsync(entryName) as StorageFile;

                            if (existFile != null)
                            {
                                if (!isNotAksAgain)
                                {
                                    await AskUserConfirmationForReplacing(entryName);
                                }

                                if (isCancel)
                                {
                                    return;
                                }

                                if (!isReplace)
                                {
                                    continue;
                                }
                            }

                            if (entryName.StartsWith("_")) // static files in Anki
                            {
                                entry.ExtractToFile(collection.Media.MediaFolder.Path + "/" + entryName, true);
                            }
                            else
                            {
                                entry.ExtractToFile(deckFolder.Path + "/" + entryName, true);
                            }

                            collection.Media.MarkFileAddIntoDatabase(entryName, currentSelectedDeck.Id);
                        }

                        progressDialog.Hide();
                        await UIHelper.ShowMessageDialog("Finished inserting media files.");
                    }
                    finally
                    {
                        progressDialog.Hide();
                        collection.Media.Database.Commit();
                    }
                }
        }