Ejemplo n.º 1
0
        private void LoadDeckSerializer()
        {
            const int totalSteps = 4;

            ResetProgressBar(totalSteps);

            // Set messages
            mLoadResults.Text = "Loading...";
            mInfoGrid.Rows.Clear();
            IncrementBar();

            // Attempt to download sheet into our temp location
            // If download fails, default to LKG
            string fileToUse;

            try
            {
                using (var client = new WebClient())
                {
                    client.DownloadFile(kGoogleSheetSourceURL, kTempBookFileName);
                }
                fileToUse = kTempBookFileName;
            }
            catch (Exception)
            {
                fileToUse = kLKGBookFileName;
            }
            IncrementBar();

            // We can't do anything if we both failed to download and had no available Last Known Good
            if (!File.Exists(fileToUse))
            {
                mLoadResults.Text = "Failed download with no available LKG";
                return;
            }

            // Load serializer, this will throw on fail
            mDeckSerializer = new DeckSerializer(fileToUse, "みんなの日本語");
            mDeckSerializer.LoadDecks();
            IncrementBar();

            // Report invalid decks
            List <Deck> invalidDecks  = mDeckSerializer.GetInvalidDecks();
            bool        hasErrors     = false;
            bool        hasEmptyDecks = false;

            if (invalidDecks.Count > 0)
            {
                foreach (Deck deck in invalidDecks)
                {
                    List <string> errors = deck.GetErrors();
                    if (errors.Count > 0)
                    {
                        hasErrors = true;
                        foreach (string error in errors)
                        {
                            AddToInfoGrid(deck, error, true);
                        }
                    }
                    else if (deck.GetCardCount() == 0)
                    {
                        hasEmptyDecks = true;
                        AddToInfoGrid(deck, "Empty deck");
                    }
                    else
                    {
                        hasErrors = true;
                        AddToInfoGrid(deck, "Marked as invalid, but unable to identify why", true);
                    }
                }
            }

            if (hasErrors)
            {
                mLoadResults.Text = "Errors present; fully valid decks still loaded.";
            }
            else
            {
                mLoadResults.Text = (hasEmptyDecks ? "Success, but with empty decks" : "Success!");

                // Serializer loaded successfully (we didn't throw an exception during it)
                // If we used a file other than LKG, swap
                if (fileToUse != kLKGBookFileName)
                {
                    if (File.Exists(kLKGBookFileName))
                    {
                        File.Delete(kLKGBookFileName);
                    }
                    File.Move(fileToUse, kLKGBookFileName);
                }
            }
            IncrementBar();

            // Update our interface with the new values
            mBookAndChapterNumbers = mDeckSerializer.GetBookAndChapterNumbers();
            mTrackBarMin.Maximum   = mBookAndChapterNumbers.Count - 1;
            mTrackBarMax.Maximum   = mTrackBarMin.Maximum;
            mTrackBarMax.Value     = mTrackBarMin.Maximum;
            UpdateAggregateDisplay();
        }