Search in sources :

Example 1 with EDITED

use of com.ichi2.anki.CardBrowser.Column.EDITED in project AnkiChinaAndroid by ankichinateam.

the class AbstractFlashcardViewer method updateCard.

private void updateCard(final String newContent) {
    Timber.d("updateCard()");
    mCacheContent = newContent;
    mUseTimerDynamicMS = 0;
    // Add CSS for font color and font size
    if (mCurrentCard == null) {
        processCardAction(cardWebView -> cardWebView.getSettings().setDefaultFontSize(calculateDynamicFontSize(newContent)));
    }
    if (sDisplayAnswer) {
        addAnswerSounds(newContent);
    } else {
        // reset sounds each time first side of card is displayed, which may happen repeatedly without ever
        // leaving the card (such as when edited)
        mSoundPlayer.resetSounds();
        mAnswerSoundsAdded = false;
        mSoundPlayer.addSounds(mBaseUrl, newContent, SoundSide.QUESTION);
        if (mUseTimer && !mAnswerSoundsAdded && getConfigForCurrentCard().optBoolean("autoplay", false)) {
            addAnswerSounds(mCurrentCard.a());
        }
    }
    String content = Sound.expandSounds(mBaseUrl, newContent);
    content = CardAppearance.fixBoldStyle(content);
    Timber.v("content card = \n %s", content);
    String style = mCardAppearance.getStyle();
    Timber.v("::style:: / %s", style);
    // CSS class for card-specific styling
    String cardClass = mCardAppearance.getCardClass(mCurrentCard.getOrd() + 1, Themes.getCurrentTheme(this));
    if (Template.textContainsMathjax(content)) {
        cardClass += " mathjax-needs-to-render";
    }
    if (isInNightMode()) {
        if (!mCardAppearance.hasUserDefinedNightMode(mCurrentCard)) {
            content = HtmlColors.invertColors(content);
        }
    }
    content = CardAppearance.convertSmpToHtmlEntity(content);
    SharedPreferences prefs = AnkiDroidApp.getSharedPrefs(this);
    String localViewSettingStr = prefs.getString(Consts.KEY_LOCAL_LAYOUT_CONFIG, "");
    boolean dark = false;
    if (AnkiDroidApp.getSharedPrefs(this).getBoolean("invertedColors", false)) {
        int theme = Integer.parseInt(prefs.getString("nightTheme", "0"));
        dark = theme == THEME_NIGHT_DARK || theme == THEME_NIGHT_BLACK;
    }
    if (mCurrentCSS.isEmpty() || mCurrentCSSModelID != mCurrentCard.model().getLong("id")) {
        mCurrentCSSModelID = mCurrentCard.model().getLong("id");
        Timber.i("find new model id css %s", mCurrentCSS);
        if (localViewSettingStr != null && !localViewSettingStr.isEmpty()) {
            try {
                JSONObject viewSetting = new JSONObject(localViewSettingStr);
                JSONObject currentModelSetting = viewSetting.getJSONObject(String.valueOf(mCurrentCSSModelID));
                if (currentModelSetting != null) {
                    mCurrentCSS = convertJson2Css(currentModelSetting, false);
                } else {
                    mCurrentCSS = "";
                }
            } catch (Exception e) {
                e.printStackTrace();
                mCurrentCSS = "";
            }
        }
    }
    Timber.i("now theme is dark:%s", dark);
    if (!dark) {
        if (!mCurrentCSS.isEmpty()) {
            Matcher bgMatcher = Pattern.compile("background-color:(.+?);").matcher(mCurrentCSS);
            Window window = getWindow();
            if (bgMatcher.find()) {
                String fld1 = bgMatcher.group(1).trim();
                if (shouldChangeToolbarBgLikeCss2()) {
                    findViewById(R.id.toolbar).setBackgroundColor(Color.parseColor(fld1));
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        window.setStatusBarColor(Color.parseColor(fld1));
                    }
                }
                mCardWebView.setBackgroundColor(Color.parseColor(fld1));
                findViewById(R.id.bottom_area_layout).setBackgroundColor(Color.parseColor(fld1));
            } else {
                if (shouldChangeToolbarBgLikeCss2()) {
                    int[] attrs = new int[] { R.attr.reviewStatusBarColor };
                    TypedArray ta = obtainStyledAttributes(attrs);
                    findViewById(R.id.toolbar).setBackground(ta.getDrawable(0));
                    ta.recycle();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        window.setStatusBarColor(Themes.getColorFromAttr(this, getStatusBarColorAttr()));
                    }
                }
                mCardWebView.setBackgroundColor(Color.WHITE);
                findViewById(R.id.bottom_area_layout).setBackgroundColor(Color.TRANSPARENT);
            }
        } else {
            if (shouldChangeToolbarBgLikeCss2()) {
                int[] attrs = new int[] { R.attr.reviewStatusBarColor };
                TypedArray ta = obtainStyledAttributes(attrs);
                findViewById(R.id.toolbar).setBackground(ta.getDrawable(0));
                ta.recycle();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    Window window = getWindow();
                    window.setStatusBarColor(Themes.getColorFromAttr(this, getStatusBarColorAttr()));
                }
            }
            mCardWebView.setBackgroundColor(Color.WHITE);
            findViewById(R.id.bottom_area_layout).setBackgroundColor(Color.TRANSPARENT);
        }
    }
    // if (mCurrentCSS.isEmpty()) {//保存的配置文件没有,则直接找默认的css
    // mCurrentCSS = mCurrentCard.css().replace("<style>", "").replace("</style>", "");
    // }
    mCardContent = mCardTemplate.replace("::content::", content).replace("::style::", style).replace("::class::", cardClass).replace("::style2::", mCurrentCSS).replace("::class2::", sDisplayAnswer ? "ck-back" : "ck-front");
    Timber.d("base url = %s", mBaseUrl);
    Timber.v("::content:: / %s", content);
    Timber.v("::style2:: / %s", mCurrentCSS);
    if (AnkiDroidApp.getSharedPrefs(this).getBoolean("html_javascript_debugging", false)) {
        try {
            try (FileOutputStream f = new FileOutputStream(new File(CollectionHelper.getCurrentAnkiDroidDirectory(this), "card.html"))) {
                f.write(mCardContent.getBytes());
            }
        } catch (IOException e) {
            Timber.d(e, "failed to save card");
        }
    }
    fillFlashcard();
    if (!mConfigurationChanged) {
        playSoundsVIP(false);
    }
}
Also used : Window(android.view.Window) SharedPreferences(android.content.SharedPreferences) Matcher(java.util.regex.Matcher) IOException(java.io.IOException) SuppressLint(android.annotation.SuppressLint) JSONException(com.ichi2.utils.JSONException) IOException(java.io.IOException) ActivityNotFoundException(android.content.ActivityNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONObject(com.ichi2.utils.JSONObject) TypedArray(android.content.res.TypedArray) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 2 with EDITED

use of com.ichi2.anki.CardBrowser.Column.EDITED in project Anki-Android by ankidroid.

the class CardTemplateEditor method showDiscardChangesDialog.

@VisibleForTesting
public MaterialDialog showDiscardChangesDialog() {
    MaterialDialog discardDialog = DiscardChangesDialog.getDefault(this).onPositive((dialog, which) -> {
        Timber.i("TemplateEditor:: OK button pressed to confirm discard changes");
        // Clear the edited model from any cache files, and clear it from this objects memory to discard changes
        TemporaryModel.clearTempModelFiles();
        mTempModel = null;
        finishWithAnimation(END);
    }).build();
    discardDialog.show();
    return discardDialog;
}
Also used : RequiresApi(androidx.annotation.RequiresApi) ActionMode(android.view.ActionMode) Bundle(android.os.Bundle) Deck(com.ichi2.libanki.Deck) NonNull(androidx.annotation.NonNull) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog) FunctionalInterfaces(com.ichi2.utils.FunctionalInterfaces) Decks(com.ichi2.libanki.Decks) Matcher(java.util.regex.Matcher) Fragment(androidx.fragment.app.Fragment) JSONException(com.ichi2.utils.JSONException) View(android.view.View) TargetApi(android.annotation.TargetApi) BottomNavigationView(com.google.android.material.bottomnavigation.BottomNavigationView) TabLayout(com.google.android.material.tabs.TabLayout) JSONObject(com.ichi2.utils.JSONObject) ViewGroup(android.view.ViewGroup) Timber(timber.log.Timber) InsertFieldDialog(com.ichi2.anki.dialogs.InsertFieldDialog) StringRes(androidx.annotation.StringRes) List(java.util.List) Nullable(androidx.annotation.Nullable) ActivityResultContracts(androidx.activity.result.contract.ActivityResultContracts) TaskListenerWithContext(com.ichi2.async.TaskListenerWithContext) Pattern(java.util.regex.Pattern) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) FixedEditText(com.ichi2.ui.FixedEditText) DiscardChangesDialog(com.ichi2.anki.dialogs.DiscardChangesDialog) Models(com.ichi2.libanki.Models) TextWatcher(android.text.TextWatcher) Context(android.content.Context) KeyEvent(android.view.KeyEvent) Direction(com.ichi2.anim.ActivityTransitionAnimation.Direction) Pair(android.util.Pair) Intent(android.content.Intent) ViewPager2(androidx.viewpager2.widget.ViewPager2) HashMap(java.util.HashMap) Collection(com.ichi2.libanki.Collection) StyledProgressDialog(com.ichi2.themes.StyledProgressDialog) Editable(android.text.Editable) FragmentActivity(androidx.fragment.app.FragmentActivity) MenuItem(android.view.MenuItem) FragmentStateAdapter(androidx.viewpager2.adapter.FragmentStateAdapter) JSONArray(com.ichi2.utils.JSONArray) NOT_FOUND_NOTE_TYPE(com.ichi2.libanki.Models.NOT_FOUND_NOTE_TYPE) MenuInflater(android.view.MenuInflater) Menu(android.view.Menu) Model(com.ichi2.libanki.Model) Build(android.os.Build) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) CheckResult(androidx.annotation.CheckResult) ActivityResultLauncher(androidx.activity.result.ActivityResultLauncher) LayoutInflater(android.view.LayoutInflater) FixedTextView(com.ichi2.ui.FixedTextView) SelectableDeck(com.ichi2.anki.dialogs.DeckSelectionDialog.SelectableDeck) VisibleForTesting(androidx.annotation.VisibleForTesting) InsertFieldDialogFactory(com.ichi2.anki.dialogs.InsertFieldDialogFactory) TabLayoutMediator(com.google.android.material.tabs.TabLayoutMediator) Resources(android.content.res.Resources) DeckSelectionDialog(com.ichi2.anki.dialogs.DeckSelectionDialog) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) VisibleForTesting(androidx.annotation.VisibleForTesting)

Example 3 with EDITED

use of com.ichi2.anki.CardBrowser.Column.EDITED in project AnkiChinaAndroid by ankichinateam.

the class CardTemplateEditor method showDiscardChangesDialog.

@VisibleForTesting
public MaterialDialog showDiscardChangesDialog() {
    MaterialDialog discardDialog = DiscardChangesDialog.getDefault(this).onPositive((dialog, which) -> {
        Timber.i("TemplateEditor:: OK button pressed to confirm discard changes");
        // Clear the edited model from any cache files, and clear it from this objects memory to discard changes
        TemporaryModel.clearTempModelFiles();
        mTempModel = null;
        finishWithAnimation(ActivityTransitionAnimation.RIGHT);
    }).build();
    discardDialog.show();
    return discardDialog;
}
Also used : Bundle(android.os.Bundle) Deck(com.ichi2.libanki.Deck) NonNull(androidx.annotation.NonNull) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog) FunctionalInterfaces(com.ichi2.utils.FunctionalInterfaces) Decks(com.ichi2.libanki.Decks) Matcher(java.util.regex.Matcher) Fragment(androidx.fragment.app.Fragment) JSONException(com.ichi2.utils.JSONException) View(android.view.View) TaskData(com.ichi2.async.TaskData) TabLayout(com.google.android.material.tabs.TabLayout) JSONObject(com.ichi2.utils.JSONObject) ViewGroup(android.view.ViewGroup) Timber(timber.log.Timber) StringRes(androidx.annotation.StringRes) List(java.util.List) Nullable(androidx.annotation.Nullable) Consts(com.ichi2.libanki.Consts) TaskListenerWithContext(com.ichi2.async.TaskListenerWithContext) Pattern(java.util.regex.Pattern) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) DiscardChangesDialog(com.ichi2.anki.dialogs.DiscardChangesDialog) Models(com.ichi2.libanki.Models) TextWatcher(android.text.TextWatcher) Context(android.content.Context) TaskListener(com.ichi2.async.TaskListener) Intent(android.content.Intent) ViewPager2(androidx.viewpager2.widget.ViewPager2) Collection(com.ichi2.libanki.Collection) StyledProgressDialog(com.ichi2.themes.StyledProgressDialog) Editable(android.text.Editable) FragmentActivity(androidx.fragment.app.FragmentActivity) MenuItem(android.view.MenuItem) FragmentStateAdapter(androidx.viewpager2.adapter.FragmentStateAdapter) JSONArray(com.ichi2.utils.JSONArray) MenuInflater(android.view.MenuInflater) Menu(android.view.Menu) Model(com.ichi2.libanki.Model) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) CheckResult(androidx.annotation.CheckResult) LayoutInflater(android.view.LayoutInflater) SelectableDeck(com.ichi2.anki.dialogs.DeckSelectionDialog.SelectableDeck) VisibleForTesting(androidx.annotation.VisibleForTesting) ActivityTransitionAnimation(com.ichi2.anim.ActivityTransitionAnimation) EditText(android.widget.EditText) TabLayoutMediator(com.google.android.material.tabs.TabLayoutMediator) Resources(android.content.res.Resources) DeckSelectionDialog(com.ichi2.anki.dialogs.DeckSelectionDialog) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) VisibleForTesting(androidx.annotation.VisibleForTesting)

Aggregations

Context (android.content.Context)2 Intent (android.content.Intent)2 Resources (android.content.res.Resources)2 Bundle (android.os.Bundle)2 Editable (android.text.Editable)2 TextWatcher (android.text.TextWatcher)2 LayoutInflater (android.view.LayoutInflater)2 Menu (android.view.Menu)2 MenuInflater (android.view.MenuInflater)2 MenuItem (android.view.MenuItem)2 View (android.view.View)2 ViewGroup (android.view.ViewGroup)2 CheckResult (androidx.annotation.CheckResult)2 NonNull (androidx.annotation.NonNull)2 Nullable (androidx.annotation.Nullable)2 StringRes (androidx.annotation.StringRes)2 VisibleForTesting (androidx.annotation.VisibleForTesting)2 Fragment (androidx.fragment.app.Fragment)2 FragmentActivity (androidx.fragment.app.FragmentActivity)2 FragmentStateAdapter (androidx.viewpager2.adapter.FragmentStateAdapter)2